2018-06-19 02:13:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class Draft < ActiveRecord::Base
|
2023-01-09 07:20:10 -05:00
|
|
|
NEW_TOPIC ||= "new_topic"
|
|
|
|
NEW_PRIVATE_MESSAGE ||= "new_private_message"
|
|
|
|
EXISTING_TOPIC ||= "topic_"
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2020-03-23 07:02:24 -04:00
|
|
|
belongs_to :user
|
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
after_commit :update_draft_count, on: %i[create destroy]
|
2021-07-27 07:05:33 -04:00
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
class OutOfSequence < StandardError
|
|
|
|
end
|
2019-10-31 02:15:41 -04:00
|
|
|
|
2020-09-29 12:59:03 -04:00
|
|
|
def self.set(user, key, sequence, data, owner = nil, force_save: false)
|
2020-05-25 20:07:00 -04:00
|
|
|
return 0 if !User.human_user_id?(user.id)
|
2020-09-29 12:59:03 -04:00
|
|
|
force_save = force_save.to_s == "true"
|
2020-05-25 20:07:00 -04:00
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
if SiteSetting.backup_drafts_to_pm_length > 0 &&
|
|
|
|
SiteSetting.backup_drafts_to_pm_length < data.length
|
2019-10-17 01:56:40 -04:00
|
|
|
backup_draft(user, key, sequence, data)
|
|
|
|
end
|
|
|
|
|
2019-10-31 02:15:41 -04: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
|
2023-01-09 07:20:10 -05:00
|
|
|
raise Draft::OutOfSequence if !force_save && (current_sequence != sequence)
|
2018-11-14 11:47:59 -05:00
|
|
|
|
2020-09-29 12:59:03 -04:00
|
|
|
sequence = current_sequence if force_save
|
2020-05-12 02:55:24 -04:00
|
|
|
sequence += 1
|
|
|
|
|
|
|
|
# we need to keep upping our sequence on every save
|
|
|
|
# if we do not do that there are bad race conditions
|
2023-01-09 07:20:10 -05:00
|
|
|
DraftSequence.upsert(
|
|
|
|
{ sequence: sequence, draft_key: key, user_id: user.id },
|
|
|
|
unique_by: %i[user_id draft_key],
|
2020-05-12 02:55:24 -04:00
|
|
|
)
|
2019-10-31 02:15:41 -04:00
|
|
|
|
|
|
|
DB.exec(<<~SQL, id: draft_id, sequence: sequence, data: data, owner: owner || current_owner)
|
2018-11-14 11:47:59 -05:00
|
|
|
UPDATE drafts
|
|
|
|
SET sequence = :sequence
|
|
|
|
, data = :data
|
|
|
|
, revisions = revisions + 1
|
2019-10-31 02:15:41 -04:00
|
|
|
, owner = :owner
|
2020-03-16 18:17:20 -04:00
|
|
|
, updated_at = CURRENT_TIMESTAMP
|
2018-11-14 11:47:59 -05:00
|
|
|
WHERE id = :id
|
|
|
|
SQL
|
2019-10-31 02:15:41 -04:00
|
|
|
elsif sequence != current_sequence
|
|
|
|
raise Draft::OutOfSequence
|
2013-02-05 14:16:51 -05:00
|
|
|
else
|
2023-01-09 07:20:10 -05:00
|
|
|
opts = { user_id: user.id, draft_key: key, data: data, sequence: sequence, owner: owner }
|
2020-03-16 18:17:20 -04:00
|
|
|
|
2022-06-08 19:24:30 -04:00
|
|
|
draft_id = DB.query_single(<<~SQL, opts).first
|
2020-03-16 18:17:20 -04:00
|
|
|
INSERT INTO drafts (user_id, draft_key, data, sequence, owner, created_at, updated_at)
|
|
|
|
VALUES (:user_id, :draft_key, :data, :sequence, :owner, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
|
|
|
ON CONFLICT (user_id, draft_key) DO
|
|
|
|
UPDATE
|
|
|
|
SET
|
|
|
|
sequence = :sequence,
|
|
|
|
data = :data,
|
|
|
|
revisions = drafts.revisions + 1,
|
|
|
|
owner = :owner,
|
|
|
|
updated_at = CURRENT_TIMESTAMP
|
2022-06-08 19:24:30 -04:00
|
|
|
RETURNING id
|
2020-03-16 18:17:20 -04:00
|
|
|
SQL
|
2021-07-27 07:05:33 -04:00
|
|
|
|
|
|
|
UserStat.update_draft_count(user.id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2018-06-19 02:13:14 -04:00
|
|
|
|
2022-06-08 19:24:30 -04:00
|
|
|
UploadReference.ensure_exist!(
|
|
|
|
upload_ids: Upload.extract_upload_ids(data),
|
2023-01-09 07:20:10 -05:00
|
|
|
target_type: "Draft",
|
|
|
|
target_id: draft_id,
|
2022-06-08 19:24:30 -04:00
|
|
|
)
|
|
|
|
|
2019-10-31 02:15:41 -04:00
|
|
|
sequence
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.get(user, key, sequence)
|
2020-05-25 20:07:00 -04:00
|
|
|
return if !user || !user.id || !User.human_user_id?(user.id)
|
2019-10-31 02:15:41 -04:00
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
opts = { user_id: user.id, draft_key: key, sequence: sequence }
|
2019-10-31 02:15:41 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
raise Draft::OutOfSequence if sequence != current_sequence
|
2019-10-31 02:15:41 -04:00
|
|
|
|
|
|
|
data if current_sequence == draft_sequence
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2021-03-19 09:19:15 -04:00
|
|
|
def self.has_topic_draft(user)
|
|
|
|
return if !user || !user.id || !User.human_user_id?(user.id)
|
|
|
|
|
|
|
|
Draft.where(user_id: user.id, draft_key: NEW_TOPIC).present?
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.clear(user, key, sequence)
|
2020-05-25 20:07:00 -04:00
|
|
|
return if !user || !user.id || !User.human_user_id?(user.id)
|
|
|
|
|
2019-10-31 02:15:41 -04:00
|
|
|
current_sequence = DraftSequence.current(user, key)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2019-10-31 02:15:41 -04:00
|
|
|
# bad caller is a reason to complain
|
2023-01-09 07:20:10 -05:00
|
|
|
raise Draft::OutOfSequence if sequence != current_sequence
|
2019-10-31 02:15:41 -04: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 14:16:51 -05:00
|
|
|
end
|
2015-06-01 23:45:47 -04:00
|
|
|
|
2020-03-23 07:02:24 -04:00
|
|
|
def display_user
|
|
|
|
post&.user || topic&.user || user
|
|
|
|
end
|
|
|
|
|
|
|
|
def parsed_data
|
2021-09-14 08:18:01 -04:00
|
|
|
begin
|
|
|
|
JSON.parse(data)
|
|
|
|
rescue JSON::ParserError
|
|
|
|
{}
|
|
|
|
end
|
2020-03-23 07:02:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def topic_id
|
2023-01-09 07:20:10 -05:00
|
|
|
draft_key.gsub(EXISTING_TOPIC, "").to_i if draft_key.starts_with?(EXISTING_TOPIC)
|
2020-03-23 07:02:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def topic_preloaded?
|
|
|
|
!!defined?(@topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic
|
2023-01-09 07:20:10 -05:00
|
|
|
topic_preloaded? ?
|
|
|
|
@topic :
|
|
|
|
@topic = Draft.allowed_draft_topics_for_user(user).find_by(id: topic_id)
|
2020-03-23 07:02:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def preload_topic(topic)
|
|
|
|
@topic = topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_id
|
|
|
|
parsed_data["postId"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_preloaded?
|
|
|
|
!!defined?(@post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post
|
|
|
|
post_preloaded? ? @post : @post = Draft.allowed_draft_posts_for_user(user).find_by(id: post_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def preload_post(post)
|
|
|
|
@post = post
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.preload_data(drafts, user)
|
|
|
|
topic_ids = drafts.map(&:topic_id)
|
|
|
|
post_ids = drafts.map(&:post_id)
|
|
|
|
|
|
|
|
topics = self.allowed_draft_topics_for_user(user).where(id: topic_ids)
|
|
|
|
posts = self.allowed_draft_posts_for_user(user).where(id: post_ids)
|
|
|
|
|
|
|
|
drafts.each do |draft|
|
|
|
|
draft.preload_topic(topics.detect { |t| t.id == draft.topic_id })
|
|
|
|
draft.preload_post(posts.detect { |p| p.id == draft.post_id })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.allowed_draft_topics_for_user(user)
|
|
|
|
topics = Topic.listable_topics.secured(Guardian.new(user))
|
|
|
|
pms = Topic.private_messages_for_user(user)
|
|
|
|
topics.or(pms)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.allowed_draft_posts_for_user(user)
|
|
|
|
# .secured handles whispers, merge handles topic/pm visibility
|
|
|
|
Post.secured(Guardian.new(user)).joins(:topic).merge(self.allowed_draft_topics_for_user(user))
|
|
|
|
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
|
|
|
def self.stream(opts = nil)
|
|
|
|
opts ||= {}
|
|
|
|
|
|
|
|
user_id = opts[:user].id
|
|
|
|
offset = (opts[:offset] || 0).to_i
|
|
|
|
limit = (opts[:limit] || 30).to_i
|
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
stream = Draft.where(user_id: user_id).order(updated_at: :desc).offset(offset).limit(limit)
|
2020-03-23 07:02:24 -04:00
|
|
|
|
|
|
|
# Preload posts and topics to avoid N+1 queries
|
|
|
|
Draft.preload_data(stream, opts[:user])
|
|
|
|
|
|
|
|
stream
|
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
|
|
|
|
|
2015-06-01 23:45:47 -04:00
|
|
|
def self.cleanup!
|
2018-11-14 11:47:59 -05: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 04:52:41 -04: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
|
2021-07-29 10:06:11 -04:00
|
|
|
|
|
|
|
UserStat.update_draft_count
|
2015-06-01 23:45:47 -04:00
|
|
|
end
|
|
|
|
|
2019-10-17 01:56:40 -04: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 06:32:27 -04:00
|
|
|
post_id = BackupDraftPost.where(user_id: user.id, key: key).pluck_first(:post_id)
|
2019-10-17 01:56:40 -04:00
|
|
|
post = Post.where(id: post_id).first if post_id
|
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
BackupDraftPost.where(user_id: user.id, key: key).delete_all if post_id && !post
|
2019-10-17 01:56:40 -04:00
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
indented_reply = reply.split("\n").map! { |l| " #{l}" }
|
2019-10-17 01:56:40 -04:00
|
|
|
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
|
2023-01-09 07:20:10 -05:00
|
|
|
post =
|
|
|
|
PostCreator.new(
|
|
|
|
user,
|
|
|
|
raw: draft_body,
|
|
|
|
skip_jobs: true,
|
|
|
|
skip_validations: true,
|
|
|
|
topic_id: topic.id,
|
|
|
|
).create
|
2019-10-17 01:56:40 -04:00
|
|
|
BackupDraftPost.create!(user_id: user.id, key: key, post_id: post.id)
|
|
|
|
end
|
2019-10-17 02:41:28 -04:00
|
|
|
elsif post.last_version_at > 5.minutes.ago
|
2019-10-17 01:56:40 -04:00
|
|
|
# bypass all validations here to maximize speed
|
|
|
|
post.update_columns(
|
|
|
|
raw: draft_body,
|
|
|
|
cooked: PrettyText.cook(draft_body),
|
2023-01-09 07:20:10 -05:00
|
|
|
updated_at: Time.zone.now,
|
2019-10-17 01:56:40 -04:00
|
|
|
)
|
|
|
|
else
|
|
|
|
revisor = PostRevisor.new(post, post.topic)
|
2023-01-09 07:20:10 -05:00
|
|
|
revisor.revise!(
|
|
|
|
user,
|
|
|
|
{ raw: draft_body },
|
2019-10-17 01:56:40 -04:00
|
|
|
bypass_bump: true,
|
|
|
|
skip_validations: true,
|
|
|
|
skip_staff_log: true,
|
2023-01-09 07:20:10 -05:00
|
|
|
bypass_rate_limiter: true,
|
2019-10-17 01:56:40 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
Discourse.warn_exception(e, message: "Failed to backup draft")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ensure_draft_topic!(user)
|
2019-10-21 06:32:27 -04:00
|
|
|
topic_id = BackupDraftTopic.where(user_id: user.id).pluck_first(:topic_id)
|
2019-10-17 01:56:40 -04:00
|
|
|
topic = Topic.find_by(id: topic_id) if topic_id
|
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
BackupDraftTopic.where(user_id: user.id).delete_all if topic_id && !topic
|
2019-10-17 01:56:40 -04:00
|
|
|
|
|
|
|
if !topic
|
|
|
|
Topic.transaction do
|
2023-01-09 07:20:10 -05:00
|
|
|
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,
|
|
|
|
)
|
2019-10-17 01:56:40 -04:00
|
|
|
topic = creator.create.topic
|
|
|
|
BackupDraftTopic.create!(topic_id: topic.id, user_id: user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
topic
|
|
|
|
end
|
|
|
|
|
2021-07-27 07:05:33 -04:00
|
|
|
def update_draft_count
|
|
|
|
UserStat.update_draft_count(self.user_id)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: drafts
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
2019-01-11 14:29:56 -05:00
|
|
|
# draft_key :string not null
|
2013-05-23 22:48:32 -04:00
|
|
|
# data :text not null
|
2014-08-27 01:19:25 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2020-05-12 02:55:24 -04:00
|
|
|
# sequence :bigint default(0), not null
|
2015-09-17 20:41:10 -04:00
|
|
|
# revisions :integer default(1), not null
|
2019-10-31 20:21:57 -04:00
|
|
|
# owner :string
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2019-11-07 19:44:02 -05:00
|
|
|
# index_drafts_on_user_id_and_draft_key (user_id,draft_key) UNIQUE
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|