2018-06-19 01:13:14 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
class Draft < ActiveRecord::Base
|
2018-11-14 10:47:59 -06:00
|
|
|
NEW_TOPIC ||= 'new_topic'
|
|
|
|
NEW_PRIVATE_MESSAGE ||= 'new_private_message'
|
|
|
|
EXISTING_TOPIC ||= 'topic_'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2019-10-31 01:15:41 -05:00
|
|
|
class OutOfSequence < StandardError; end
|
|
|
|
|
2020-01-01 18:37:52 -06:00
|
|
|
def self.set(user, key, sequence, data, owner = nil, retry_not_unique: true)
|
2019-10-17 00:56:40 -05:00
|
|
|
if SiteSetting.backup_drafts_to_pm_length > 0 && SiteSetting.backup_drafts_to_pm_length < data.length
|
|
|
|
backup_draft(user, key, sequence, data)
|
|
|
|
end
|
|
|
|
|
2019-10-31 01:15:41 -05:00
|
|
|
# this is called a lot so we should micro optimize here
|
|
|
|
draft_id, current_owner, current_sequence = DB.query_single(<<~SQL, user_id: user.id, key: key)
|
|
|
|
WITH draft AS (
|
|
|
|
SELECT id, owner FROM drafts
|
|
|
|
WHERE
|
|
|
|
user_id = :user_id AND
|
|
|
|
draft_key = :key
|
|
|
|
),
|
|
|
|
draft_sequence AS (
|
|
|
|
SELECT sequence
|
|
|
|
FROM draft_sequences
|
|
|
|
WHERE
|
|
|
|
user_id = :user_id AND
|
|
|
|
draft_key = :key
|
|
|
|
)
|
|
|
|
|
|
|
|
SELECT
|
|
|
|
(SELECT id FROM draft),
|
|
|
|
(SELECT owner FROM draft),
|
|
|
|
(SELECT sequence FROM draft_sequence)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
current_sequence ||= 0
|
|
|
|
|
|
|
|
if draft_id
|
|
|
|
if current_sequence != sequence
|
|
|
|
raise Draft::OutOfSequence
|
|
|
|
end
|
2018-11-14 10:47:59 -06:00
|
|
|
|
2019-10-31 01:15:41 -05:00
|
|
|
if owner && current_owner && current_owner != owner
|
|
|
|
sequence += 1
|
|
|
|
|
|
|
|
DraftSequence.upsert({
|
|
|
|
sequence: sequence,
|
|
|
|
draft_key: key,
|
|
|
|
user_id: user.id,
|
|
|
|
},
|
|
|
|
unique_by: [:user_id, :draft_key]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
DB.exec(<<~SQL, id: draft_id, sequence: sequence, data: data, owner: owner || current_owner)
|
2018-11-14 10:47:59 -06:00
|
|
|
UPDATE drafts
|
|
|
|
SET sequence = :sequence
|
|
|
|
, data = :data
|
|
|
|
, revisions = revisions + 1
|
2019-10-31 01:15:41 -05:00
|
|
|
, owner = :owner
|
2018-11-14 10:47:59 -06:00
|
|
|
WHERE id = :id
|
|
|
|
SQL
|
2019-10-31 01:15:41 -05:00
|
|
|
|
|
|
|
elsif sequence != current_sequence
|
|
|
|
raise Draft::OutOfSequence
|
2013-02-05 13:16:51 -06:00
|
|
|
else
|
2020-01-01 18:37:52 -06:00
|
|
|
begin
|
|
|
|
Draft.create!(
|
|
|
|
user_id: user.id,
|
|
|
|
draft_key: key,
|
|
|
|
data: data,
|
|
|
|
sequence: sequence,
|
|
|
|
owner: owner
|
|
|
|
)
|
|
|
|
rescue ActiveRecord::RecordNotUnique => e
|
|
|
|
# we need this to be fast and with minimal locking, in some cases we can have a race condition
|
|
|
|
# around 2 controller actions calling for draft creation at the exact same time
|
|
|
|
# to avoid complex locking and a distributed mutex, since this is so rare, simply add a single retry
|
|
|
|
if retry_not_unique
|
2020-03-11 10:12:28 -05:00
|
|
|
set(user, key, sequence, data, owner, retry_not_unique: false)
|
2020-01-01 18:37:52 -06:00
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2018-06-19 01:13:14 -05:00
|
|
|
|
2019-10-31 01:15:41 -05:00
|
|
|
sequence
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.get(user, key, sequence)
|
2019-10-31 01:15:41 -05:00
|
|
|
|
|
|
|
opts = {
|
|
|
|
user_id: user.id,
|
|
|
|
draft_key: key,
|
|
|
|
sequence: sequence
|
|
|
|
}
|
|
|
|
|
|
|
|
current_sequence, data, draft_sequence = DB.query_single(<<~SQL, opts)
|
|
|
|
WITH draft AS (
|
|
|
|
SELECT data, sequence
|
|
|
|
FROM drafts
|
|
|
|
WHERE draft_key = :draft_key AND user_id = :user_id
|
|
|
|
),
|
|
|
|
draft_sequence AS (
|
|
|
|
SELECT sequence
|
|
|
|
FROM draft_sequences
|
|
|
|
WHERE draft_key = :draft_key AND user_id = :user_id
|
|
|
|
)
|
|
|
|
SELECT
|
|
|
|
( SELECT sequence FROM draft_sequence) ,
|
|
|
|
( SELECT data FROM draft ),
|
|
|
|
( SELECT sequence FROM draft )
|
|
|
|
SQL
|
|
|
|
|
|
|
|
current_sequence ||= 0
|
|
|
|
|
|
|
|
if sequence != current_sequence
|
|
|
|
raise Draft::OutOfSequence
|
|
|
|
end
|
|
|
|
|
|
|
|
data if current_sequence == draft_sequence
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear(user, key, sequence)
|
2019-10-31 01:15:41 -05:00
|
|
|
current_sequence = DraftSequence.current(user, key)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2019-10-31 01:15:41 -05:00
|
|
|
# bad caller is a reason to complain
|
|
|
|
if sequence != current_sequence
|
|
|
|
raise Draft::OutOfSequence
|
2014-03-25 17:56:21 -05:00
|
|
|
end
|
2019-10-31 01:15:41 -05:00
|
|
|
|
|
|
|
# corrupt data is not a reason not to leave data
|
|
|
|
Draft.where(user_id: user.id, draft_key: key).destroy_all
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2015-06-01 22:45:47 -05:00
|
|
|
|
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 01:34:54 -05:00
|
|
|
def self.stream(opts = nil)
|
|
|
|
opts ||= {}
|
|
|
|
|
|
|
|
user_id = opts[:user].id
|
|
|
|
offset = (opts[:offset] || 0).to_i
|
|
|
|
limit = (opts[:limit] || 30).to_i
|
|
|
|
|
|
|
|
# JOIN of topics table based on manipulating draft_key seems imperfect
|
|
|
|
builder = DB.build <<~SQL
|
|
|
|
SELECT
|
|
|
|
d.*, t.title, t.id topic_id, t.archetype,
|
|
|
|
t.category_id, t.closed topic_closed, t.archived topic_archived,
|
|
|
|
pu.username, pu.name, pu.id user_id, pu.uploaded_avatar_id, pu.username_lower,
|
|
|
|
du.username draft_username, NULL as raw, NULL as cooked, NULL as post_number
|
|
|
|
FROM drafts d
|
2018-08-01 16:41:27 -05:00
|
|
|
LEFT JOIN LATERAL json_extract_path_text (d.data::json, 'postId') postId ON TRUE
|
|
|
|
LEFT JOIN posts p ON postId :: BIGINT = p.id
|
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 01:34:54 -05:00
|
|
|
LEFT JOIN topics t ON
|
|
|
|
CASE
|
|
|
|
WHEN d.draft_key LIKE '%' || '#{EXISTING_TOPIC}' || '%'
|
|
|
|
THEN CAST(replace(d.draft_key, '#{EXISTING_TOPIC}', '') AS INT)
|
|
|
|
ELSE 0
|
|
|
|
END = t.id
|
2018-08-01 16:41:27 -05:00
|
|
|
JOIN users pu on pu.id = COALESCE(p.user_id, t.user_id, d.user_id)
|
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 01:34:54 -05:00
|
|
|
JOIN users du on du.id = #{user_id}
|
|
|
|
/*where*/
|
|
|
|
/*order_by*/
|
|
|
|
/*offset*/
|
|
|
|
/*limit*/
|
|
|
|
SQL
|
|
|
|
|
|
|
|
builder
|
|
|
|
.where('d.user_id = :user_id', user_id: user_id.to_i)
|
|
|
|
.order_by('d.updated_at desc')
|
|
|
|
.offset(offset)
|
|
|
|
.limit(limit)
|
|
|
|
.query
|
|
|
|
end
|
|
|
|
|
2015-06-01 22:45:47 -05:00
|
|
|
def self.cleanup!
|
2018-11-14 10:47:59 -06:00
|
|
|
DB.exec(<<~SQL)
|
|
|
|
DELETE FROM drafts
|
|
|
|
WHERE sequence < (
|
|
|
|
SELECT MAX(s.sequence)
|
|
|
|
FROM draft_sequences s
|
|
|
|
WHERE s.draft_key = drafts.draft_key
|
|
|
|
AND s.user_id = drafts.user_id
|
|
|
|
)
|
|
|
|
SQL
|
2015-06-03 03:52:41 -05:00
|
|
|
|
|
|
|
# remove old drafts
|
|
|
|
delete_drafts_older_than_n_days = SiteSetting.delete_drafts_older_than_n_days.days.ago
|
|
|
|
Draft.where("updated_at < ?", delete_drafts_older_than_n_days).destroy_all
|
2015-06-01 22:45:47 -05:00
|
|
|
end
|
|
|
|
|
2019-10-17 00:56:40 -05:00
|
|
|
def self.backup_draft(user, key, sequence, data)
|
|
|
|
reply = JSON.parse(data)["reply"] || ""
|
|
|
|
return if reply.length < SiteSetting.backup_drafts_to_pm_length
|
|
|
|
|
2019-10-21 05:32:27 -05:00
|
|
|
post_id = BackupDraftPost.where(user_id: user.id, key: key).pluck_first(:post_id)
|
2019-10-17 00:56:40 -05:00
|
|
|
post = Post.where(id: post_id).first if post_id
|
|
|
|
|
|
|
|
if post_id && !post
|
|
|
|
BackupDraftPost.where(user_id: user.id, key: key).delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
indented_reply = reply.split("\n").map! do |l|
|
|
|
|
" #{l}"
|
|
|
|
end
|
|
|
|
draft_body = <<~MD
|
|
|
|
#{indented_reply.join("\n")}
|
|
|
|
|
|
|
|
```text
|
|
|
|
seq: #{sequence}
|
|
|
|
key: #{key}
|
|
|
|
```
|
|
|
|
MD
|
|
|
|
|
|
|
|
return if post && post.raw == draft_body
|
|
|
|
|
|
|
|
if !post
|
|
|
|
topic = ensure_draft_topic!(user)
|
|
|
|
Post.transaction do
|
|
|
|
post = PostCreator.new(
|
|
|
|
user,
|
|
|
|
raw: draft_body,
|
|
|
|
skip_jobs: true,
|
|
|
|
skip_validations: true,
|
|
|
|
topic_id: topic.id,
|
|
|
|
).create
|
|
|
|
BackupDraftPost.create!(user_id: user.id, key: key, post_id: post.id)
|
|
|
|
end
|
2019-10-17 01:41:28 -05:00
|
|
|
elsif post.last_version_at > 5.minutes.ago
|
2019-10-17 00:56:40 -05:00
|
|
|
# bypass all validations here to maximize speed
|
|
|
|
post.update_columns(
|
|
|
|
raw: draft_body,
|
|
|
|
cooked: PrettyText.cook(draft_body),
|
|
|
|
updated_at: Time.zone.now
|
|
|
|
)
|
|
|
|
else
|
|
|
|
revisor = PostRevisor.new(post, post.topic)
|
|
|
|
revisor.revise!(user, { raw: draft_body },
|
|
|
|
bypass_bump: true,
|
|
|
|
skip_validations: true,
|
|
|
|
skip_staff_log: true,
|
|
|
|
bypass_rate_limiter: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue => e
|
|
|
|
Discourse.warn_exception(e, message: "Failed to backup draft")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ensure_draft_topic!(user)
|
2019-10-21 05:32:27 -05:00
|
|
|
topic_id = BackupDraftTopic.where(user_id: user.id).pluck_first(:topic_id)
|
2019-10-17 00:56:40 -05:00
|
|
|
topic = Topic.find_by(id: topic_id) if topic_id
|
|
|
|
|
|
|
|
if topic_id && !topic
|
|
|
|
BackupDraftTopic.where(user_id: user.id).delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
if !topic
|
|
|
|
Topic.transaction do
|
|
|
|
creator = PostCreator.new(
|
|
|
|
user,
|
|
|
|
title: I18n.t("draft_backup.pm_title"),
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
raw: I18n.t("draft_backup.pm_body"),
|
|
|
|
skip_jobs: true,
|
|
|
|
skip_validations: true,
|
|
|
|
target_usernames: user.username
|
|
|
|
)
|
|
|
|
topic = creator.create.topic
|
|
|
|
BackupDraftTopic.create!(topic_id: topic.id, user_id: user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
topic
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-05-23 21:48:32 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: drafts
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
2019-01-11 13:29:56 -06:00
|
|
|
# draft_key :string not null
|
2013-05-23 21:48:32 -05:00
|
|
|
# data :text not null
|
2014-08-27 00:19:25 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-05-23 21:48:32 -05:00
|
|
|
# sequence :integer default(0), not null
|
2015-09-17 19:41:10 -05:00
|
|
|
# revisions :integer default(1), not null
|
2019-10-31 19:21:57 -05:00
|
|
|
# owner :string
|
2013-05-23 21:48:32 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2019-11-07 18:44:02 -06:00
|
|
|
# index_drafts_on_user_id_and_draft_key (user_id,draft_key) UNIQUE
|
2013-05-23 21:48:32 -05:00
|
|
|
#
|