mirror of
https://github.com/discourse/discourse.git
synced 2025-05-28 01:56:58 +08:00
FIX: under some conditions draft would say it was saving when not
This is a major change to draft internals. Previously there were quite a few cases where the draft system would say "draft saved", when in fact we just skipped saving. This commit ensures the draft system deals with draft ownership handover in a predictable way. For example: - Window 1 editing draft - Window 2 editing same draft at the same time Previously we would allow window 1 and 2 to just fight on the same draft each window overwriting the same draft over an over. This commit introduces an ownership concept where either window 1 or 2 win and user is prompted on the loser window to reload screen to correct the issue This also corrects edge cases where a user could have multiple browser windows open and posts in 1 window, later to post in the second window. Previously drafts would break in the second window, this corrects it.
This commit is contained in:
@ -11,7 +11,41 @@ class DraftController < ApplicationController
|
||||
end
|
||||
|
||||
def update
|
||||
Draft.set(current_user, params[:draft_key], params[:sequence].to_i, params[:data])
|
||||
sequence =
|
||||
begin
|
||||
Draft.set(
|
||||
current_user,
|
||||
params[:draft_key],
|
||||
params[:sequence].to_i,
|
||||
params[:data],
|
||||
params[:owner]
|
||||
)
|
||||
rescue Draft::OutOfSequence
|
||||
|
||||
begin
|
||||
if !Draft.exists?(user_id: current_user.id, draft_key: params[:draft_key])
|
||||
Draft.set(
|
||||
current_user,
|
||||
params[:draft_key],
|
||||
DraftSequence.current(current_user, params[:draft_key]),
|
||||
params[:data],
|
||||
params[:owner]
|
||||
)
|
||||
else
|
||||
raise Draft::OutOfSequence
|
||||
end
|
||||
|
||||
rescue Draft::OutOfSequence
|
||||
render_json_error I18n.t('draft.sequence_conflict_error.title'),
|
||||
status: 409,
|
||||
extras: {
|
||||
description: I18n.t('draft.sequence_conflict_error.description')
|
||||
}
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
json = success_json.merge(draft_sequence: sequence)
|
||||
|
||||
if data = JSON::parse(params[:data])
|
||||
# this is a bit of a kludge we need to remove (all the parsing) too many special cases here
|
||||
@ -20,16 +54,21 @@ class DraftController < ApplicationController
|
||||
post = Post.find_by(id: data["postId"])
|
||||
if post && post.raw != data["originalText"]
|
||||
conflict_user = BasicUserSerializer.new(post.last_editor, root: false)
|
||||
return render json: success_json.merge(conflict_user: conflict_user)
|
||||
render json: json.merge(conflict_user: conflict_user)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
render json: success_json
|
||||
render json: json
|
||||
end
|
||||
|
||||
def destroy
|
||||
Draft.clear(current_user, params[:draft_key], params[:sequence].to_i)
|
||||
begin
|
||||
Draft.clear(current_user, params[:draft_key], params[:sequence].to_i)
|
||||
rescue Draft::OutOfSequence
|
||||
# nothing really we can do here, if try clearing a draft that is not ours, just skip it.
|
||||
end
|
||||
render json: success_json
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user