2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream
* improve drafts stream display in user profile
* truncate excerpts in drafts list, better handling for resume draft action
* improve draft stream SQL query, add rspec tests
* if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username)
* cleanup
* linting fixes
* apply prettier styling to modified files
* add client tests for drafts, includes a fixture for drafts.json
* improvements to code following review
* refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix
* added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed
* prettier, eslint fixes
* use "username_lower" from users table, added error handling for rejected promises
* adds guardian spec for can_see_drafts, adds improvements following code review
* move DraftsController spec to its own file
* fix failing drafts qunit test, use getOwner instead of deprecated this.container
* limit test fixture for draft.json testing to new_topic request only
2018-08-01 02:34:54 -04:00
|
|
|
class DraftsController < ApplicationController
|
|
|
|
requires_login
|
|
|
|
|
|
|
|
skip_before_action :check_xhr, :preload_json
|
|
|
|
|
|
|
|
def index
|
|
|
|
params.permit(:offset)
|
|
|
|
params.permit(:limit)
|
|
|
|
|
|
|
|
stream = Draft.stream(user: current_user, offset: params[:offset], limit: params[:limit])
|
|
|
|
|
2021-12-08 12:46:54 -05:00
|
|
|
render json: { drafts: stream ? serialize_data(stream, DraftSerializer) : [] }
|
2021-09-14 08:18:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
raise Discourse::NotFound.new if params[:id].blank?
|
|
|
|
|
|
|
|
seq = params[:sequence] || DraftSequence.current(current_user, params[:id])
|
|
|
|
render json: { draft: Draft.get(current_user, params[:id], seq), draft_sequence: seq }
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
raise Discourse::NotFound.new if params[:draft_key].blank?
|
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream
* improve drafts stream display in user profile
* truncate excerpts in drafts list, better handling for resume draft action
* improve draft stream SQL query, add rspec tests
* if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username)
* cleanup
* linting fixes
* apply prettier styling to modified files
* add client tests for drafts, includes a fixture for drafts.json
* improvements to code following review
* refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix
* added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed
* prettier, eslint fixes
* use "username_lower" from users table, added error handling for rejected promises
* adds guardian spec for can_see_drafts, adds improvements following code review
* move DraftsController spec to its own file
* fix failing drafts qunit test, use getOwner instead of deprecated this.container
* limit test fixture for draft.json testing to new_topic request only
2018-08-01 02:34:54 -04:00
|
|
|
|
2021-09-14 08:18:01 -04:00
|
|
|
sequence =
|
|
|
|
begin
|
|
|
|
Draft.set(
|
|
|
|
current_user,
|
|
|
|
params[:draft_key],
|
|
|
|
params[:sequence].to_i,
|
|
|
|
params[:data],
|
|
|
|
params[:owner],
|
|
|
|
force_save: params[:force_save],
|
|
|
|
)
|
|
|
|
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)
|
|
|
|
|
|
|
|
begin
|
|
|
|
data = JSON.parse(params[:data])
|
|
|
|
rescue JSON::ParserError
|
|
|
|
raise Discourse::InvalidParameters.new(:data)
|
|
|
|
end
|
|
|
|
|
|
|
|
if data.present?
|
|
|
|
# this is a bit of a kludge we need to remove (all the parsing) too many special cases here
|
|
|
|
# we need to catch action edit and action editSharedDraft
|
|
|
|
if data["postId"].present? && data["originalText"].present? &&
|
|
|
|
data["action"].to_s.start_with?("edit")
|
|
|
|
post = Post.find_by(id: data["postId"])
|
|
|
|
if post && post.raw != data["originalText"]
|
|
|
|
conflict_user = BasicUserSerializer.new(post.last_editor, root: false)
|
|
|
|
render json: json.merge(conflict_user: conflict_user)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: json
|
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream
* improve drafts stream display in user profile
* truncate excerpts in drafts list, better handling for resume draft action
* improve draft stream SQL query, add rspec tests
* if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username)
* cleanup
* linting fixes
* apply prettier styling to modified files
* add client tests for drafts, includes a fixture for drafts.json
* improvements to code following review
* refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix
* added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed
* prettier, eslint fixes
* use "username_lower" from users table, added error handling for rejected promises
* adds guardian spec for can_see_drafts, adds improvements following code review
* move DraftsController spec to its own file
* fix failing drafts qunit test, use getOwner instead of deprecated this.container
* limit test fixture for draft.json testing to new_topic request only
2018-08-01 02:34:54 -04:00
|
|
|
end
|
|
|
|
|
2021-09-14 08:18:01 -04:00
|
|
|
def destroy
|
|
|
|
begin
|
|
|
|
Draft.clear(current_user, params[:id], 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
|
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream
* improve drafts stream display in user profile
* truncate excerpts in drafts list, better handling for resume draft action
* improve draft stream SQL query, add rspec tests
* if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username)
* cleanup
* linting fixes
* apply prettier styling to modified files
* add client tests for drafts, includes a fixture for drafts.json
* improvements to code following review
* refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix
* added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed
* prettier, eslint fixes
* use "username_lower" from users table, added error handling for rejected promises
* adds guardian spec for can_see_drafts, adds improvements following code review
* move DraftsController spec to its own file
* fix failing drafts qunit test, use getOwner instead of deprecated this.container
* limit test fixture for draft.json testing to new_topic request only
2018-08-01 02:34:54 -04:00
|
|
|
end
|