2019-05-03 06:17:27 +08: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 14:34:54 +08:00
|
|
|
class DraftsController < ApplicationController
|
|
|
|
requires_login
|
|
|
|
|
|
|
|
skip_before_action :check_xhr, :preload_json
|
|
|
|
|
2023-07-28 19:53:46 +08:00
|
|
|
INDEX_LIMIT = 50
|
|
|
|
|
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 14:34:54 +08:00
|
|
|
def index
|
|
|
|
params.permit(:offset)
|
|
|
|
|
2023-07-28 19:53:46 +08:00
|
|
|
stream =
|
|
|
|
Draft.stream(
|
|
|
|
user: current_user,
|
|
|
|
offset: params[:offset],
|
|
|
|
limit: fetch_limit_from_params(default: nil, max: INDEX_LIMIT),
|
|
|
|
)
|
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 14:34:54 +08:00
|
|
|
|
2024-04-10 22:35:42 +08:00
|
|
|
response = { drafts: serialize_data(stream, DraftSerializer) }
|
|
|
|
|
|
|
|
if guardian.can_lazy_load_categories?
|
|
|
|
category_ids = stream.map { |draft| draft.topic&.category_id }.compact.uniq
|
|
|
|
categories = Category.secured(guardian).with_parents(category_ids)
|
|
|
|
response[:categories] = serialize_data(categories, CategoryBadgeSerializer)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: response
|
2021-09-14 20:18:01 +08: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 14:34:54 +08:00
|
|
|
|
2023-01-25 19:50:21 +08:00
|
|
|
if params[:data].size > SiteSetting.max_draft_length
|
|
|
|
raise Discourse::InvalidParameters.new(:data)
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
data = JSON.parse(params[:data])
|
|
|
|
rescue JSON::ParserError
|
|
|
|
raise Discourse::InvalidParameters.new(:data)
|
|
|
|
end
|
|
|
|
|
2023-07-28 19:04:18 +08:00
|
|
|
if reached_max_drafts_per_user?(params)
|
|
|
|
render_json_error I18n.t("draft.too_many_drafts.title"),
|
|
|
|
status: 403,
|
|
|
|
extras: {
|
|
|
|
description:
|
|
|
|
I18n.t(
|
|
|
|
"draft.too_many_drafts.description",
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-09-14 20:18:01 +08: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)
|
|
|
|
|
2024-12-03 17:12:04 +08:00
|
|
|
# check for conflicts when editing a post
|
|
|
|
if data.present? && data["postId"].present? && data["action"].to_s.start_with?("edit")
|
|
|
|
original_text = data["original_text"] || data["originalText"]
|
|
|
|
original_title = data["original_title"]
|
|
|
|
original_tags = data["original_tags"]
|
|
|
|
|
|
|
|
if original_text.present?
|
|
|
|
if post = Post.find_by(id: data["postId"])
|
|
|
|
conflict = original_text != post.raw
|
|
|
|
|
|
|
|
if post.post_number == 1
|
|
|
|
conflict ||= original_title.present? && original_title != post.topic.title
|
2024-12-10 01:50:57 +08:00
|
|
|
|
|
|
|
# Since the topic might have hidden tags the current editor can't see,
|
|
|
|
# we need to check for conflicts even though there might not be any visible tags in the editor
|
|
|
|
if !conflict
|
|
|
|
original_tags = (original_tags || []).map(&:downcase).to_set
|
|
|
|
current_tags = post.topic.tags.pluck(:name).to_set
|
|
|
|
hidden_tags = DiscourseTagging.hidden_tag_names(@guardian).to_set
|
|
|
|
conflict = original_tags != (current_tags - hidden_tags)
|
|
|
|
end
|
2024-12-03 17:12:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if conflict
|
|
|
|
conflict_user = BasicUserSerializer.new(post.last_editor, root: false)
|
|
|
|
json.merge!(conflict_user:)
|
|
|
|
end
|
2021-09-14 20:18:01 +08:00
|
|
|
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 14:34:54 +08:00
|
|
|
end
|
|
|
|
|
2021-09-14 20:18:01 +08:00
|
|
|
def destroy
|
2023-04-20 03:41:45 +08:00
|
|
|
user =
|
|
|
|
if is_api?
|
|
|
|
if @guardian.is_admin?
|
|
|
|
fetch_user_from_params
|
|
|
|
else
|
|
|
|
raise Discourse::InvalidAccess
|
|
|
|
end
|
|
|
|
else
|
|
|
|
current_user
|
|
|
|
end
|
|
|
|
|
2021-09-14 20:18:01 +08:00
|
|
|
begin
|
2023-04-20 03:41:45 +08:00
|
|
|
Draft.clear(user, params[:id], params[:sequence].to_i)
|
2023-04-20 23:26:11 +08:00
|
|
|
rescue Draft::OutOfSequence
|
|
|
|
# nothing really we can do here, if try clearing a draft that is not ours, just skip it.
|
|
|
|
# rendering an error causes issues in the composer
|
2023-04-20 03:41:45 +08:00
|
|
|
rescue StandardError => e
|
|
|
|
return render json: failed_json.merge(errors: e), status: 401
|
2021-09-14 20:18:01 +08:00
|
|
|
end
|
2023-04-20 03:41:45 +08:00
|
|
|
|
2021-09-14 20:18:01 +08:00
|
|
|
render json: success_json
|
|
|
|
end
|
2023-07-28 19:04:18 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def reached_max_drafts_per_user?(params)
|
|
|
|
user_id = current_user.id
|
|
|
|
|
|
|
|
Draft.where(user_id: user_id).count >= SiteSetting.max_drafts_per_user &&
|
|
|
|
!Draft.exists?(user_id: user_id, draft_key: params[:draft_key])
|
|
|
|
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 14:34:54 +08:00
|
|
|
end
|