2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
class ReviewableQueuedPost < Reviewable
|
|
|
|
after_create do
|
|
|
|
# Backwards compatibility, new code should listen for `reviewable_created`
|
|
|
|
DiscourseEvent.trigger(:queued_post_created, self)
|
|
|
|
end
|
|
|
|
|
2022-06-08 19:24:30 -04:00
|
|
|
after_save do
|
2023-11-19 18:50:09 -05:00
|
|
|
if saved_change_to_payload? && self.status.to_sym == :pending &&
|
2022-06-08 19:24:30 -04:00
|
|
|
self.payload&.[]("raw").present?
|
|
|
|
upload_ids = Upload.extract_upload_ids(self.payload["raw"])
|
|
|
|
UploadReference.ensure_exist!(upload_ids: upload_ids, target: self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-26 12:16:00 -04:00
|
|
|
after_commit :compute_user_stats, only: %i[create update]
|
|
|
|
|
2023-10-12 21:28:31 -04:00
|
|
|
def self.additional_args(params)
|
|
|
|
return {} if params[:revise_reason].blank?
|
|
|
|
|
|
|
|
{
|
|
|
|
revise_reason: params[:revise_reason],
|
|
|
|
revise_feedback: params[:revise_feedback],
|
|
|
|
revise_custom_reason: params[:revise_custom_reason],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-07-18 07:50:31 -04:00
|
|
|
def updatable_reviewable_scores
|
|
|
|
# Approvals are possible for already rejected queued posts. We need the
|
|
|
|
# scores to be updated when this happens.
|
|
|
|
reviewable_scores.pending.or(reviewable_scores.disagreed)
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def build_actions(actions, guardian, args)
|
2019-04-17 17:12:32 -04:00
|
|
|
unless approved?
|
2019-06-26 12:20:38 -04:00
|
|
|
if topic&.closed?
|
|
|
|
actions.add(:approve_post_closed) do |a|
|
|
|
|
a.icon = "check"
|
|
|
|
a.label = "reviewables.actions.approve_post.title"
|
|
|
|
a.confirm_message = "reviewables.actions.approve_post.confirm_closed"
|
|
|
|
end
|
|
|
|
else
|
2023-07-18 07:50:31 -04:00
|
|
|
if target_created_by.present?
|
|
|
|
actions.add(:approve_post) do |a|
|
|
|
|
a.icon = "check"
|
|
|
|
a.label = "reviewables.actions.approve_post.title"
|
|
|
|
end
|
2019-06-26 12:20:38 -04:00
|
|
|
end
|
2019-04-23 12:18:39 -04:00
|
|
|
end
|
2019-04-17 17:12:32 -04:00
|
|
|
end
|
2019-04-23 12:18:39 -04:00
|
|
|
|
2023-02-06 13:55:52 -05:00
|
|
|
if pending?
|
2023-08-26 22:05:05 -04:00
|
|
|
if guardian.can_delete_user?(target_created_by)
|
|
|
|
reject_bundle =
|
|
|
|
actions.add_bundle("#{id}-reject", label: "reviewables.actions.reject_post.title")
|
|
|
|
|
|
|
|
actions.add(:reject_post, bundle: reject_bundle) do |a|
|
2024-09-13 11:50:52 -04:00
|
|
|
a.icon = "xmark"
|
2023-08-26 22:05:05 -04:00
|
|
|
a.label = "reviewables.actions.discard_post.title"
|
|
|
|
a.button_class = "reject-post"
|
|
|
|
end
|
|
|
|
delete_user_actions(actions, reject_bundle)
|
|
|
|
else
|
|
|
|
actions.add(:reject_post) do |a|
|
2024-09-13 11:50:52 -04:00
|
|
|
a.icon = "xmark"
|
2023-08-26 22:05:05 -04:00
|
|
|
a.label = "reviewables.actions.reject_post.title"
|
|
|
|
end
|
2019-04-23 12:18:39 -04:00
|
|
|
end
|
2023-10-12 21:28:31 -04:00
|
|
|
|
|
|
|
actions.add(:revise_and_reject_post) do |a|
|
|
|
|
a.label = "reviewables.actions.revise_and_reject_post.title"
|
|
|
|
end
|
2019-04-17 17:12:32 -04:00
|
|
|
end
|
2019-04-12 09:55:27 -04:00
|
|
|
|
2024-09-30 10:35:16 -04:00
|
|
|
actions.add(:delete) do |a|
|
|
|
|
a.label = "reviewables.actions.delete_single.title"
|
|
|
|
end if guardian.can_delete?(self)
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_editable_fields(fields, guardian, args)
|
2023-02-06 13:55:52 -05:00
|
|
|
if pending?
|
|
|
|
# We can edit category / title if it's a new topic
|
|
|
|
if topic_id.blank?
|
|
|
|
# Only staff can edit category for now, since in theory a category group reviewer could
|
|
|
|
# post in a category they don't have access to.
|
|
|
|
fields.add("category_id", :category) if guardian.is_staff?
|
|
|
|
|
|
|
|
fields.add("payload.title", :text)
|
|
|
|
fields.add("payload.tags", :tags)
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2023-02-06 13:55:52 -05:00
|
|
|
fields.add("payload.raw", :editor)
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_options
|
|
|
|
result = payload.symbolize_keys
|
|
|
|
result[:cooking_options].symbolize_keys! if result[:cooking_options]
|
|
|
|
result[:topic_id] = topic_id if topic_id
|
|
|
|
result[:category] = category_id if category_id
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2019-04-23 12:18:39 -04:00
|
|
|
def perform_approve_post(performed_by, args)
|
2019-01-03 12:03:01 -05:00
|
|
|
created_post = nil
|
2023-01-05 09:01:37 -05:00
|
|
|
opts =
|
|
|
|
create_options.merge(
|
2019-01-03 12:03:01 -05:00
|
|
|
skip_validations: true,
|
|
|
|
skip_jobs: true,
|
|
|
|
skip_events: true,
|
|
|
|
skip_guardian: true,
|
2024-07-30 22:45:00 -04:00
|
|
|
reviewed_queued_post: true,
|
2023-01-05 09:01:37 -05:00
|
|
|
)
|
|
|
|
opts.merge!(guardian: Guardian.new(performed_by)) if performed_by.staff?
|
|
|
|
|
2023-07-18 07:50:31 -04:00
|
|
|
creator = PostCreator.new(target_created_by, opts)
|
2019-01-03 12:03:01 -05:00
|
|
|
created_post = creator.create
|
|
|
|
|
|
|
|
unless created_post && creator.errors.blank?
|
|
|
|
return create_result(:failure) { |r| r.errors = creator.errors }
|
|
|
|
end
|
|
|
|
|
2021-05-26 14:43:18 -04:00
|
|
|
self.target = created_post
|
|
|
|
self.topic_id = created_post.topic_id if topic_id.nil?
|
2019-01-03 12:03:01 -05:00
|
|
|
save
|
|
|
|
|
2023-07-18 07:50:31 -04:00
|
|
|
UserSilencer.unsilence(target_created_by, performed_by) if target_created_by.silenced?
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
StaffActionLogger.new(performed_by).log_post_approved(created_post) if performed_by.staff?
|
|
|
|
|
|
|
|
# Backwards compatibility, new code should listen for `reviewable_transitioned_to`
|
|
|
|
DiscourseEvent.trigger(:approved_post, self, created_post)
|
|
|
|
|
2019-04-15 16:19:32 -04:00
|
|
|
Notification.create!(
|
|
|
|
notification_type: Notification.types[:post_approved],
|
2023-07-18 07:50:31 -04:00
|
|
|
user_id: target_created_by.id,
|
2021-04-12 11:08:23 -04:00
|
|
|
data: { post_url: created_post.url }.to_json,
|
2019-04-15 16:19:32 -04:00
|
|
|
topic_id: created_post.topic_id,
|
|
|
|
post_number: created_post.post_number,
|
|
|
|
)
|
|
|
|
|
2019-04-23 17:29:46 -04:00
|
|
|
create_result(:success, :approved) do |result|
|
|
|
|
result.created_post = created_post
|
|
|
|
|
|
|
|
# Do sidekiq work outside of the transaction
|
|
|
|
result.after_commit = -> do
|
|
|
|
creator.enqueue_jobs
|
|
|
|
creator.trigger_after_events
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2019-04-23 17:29:46 -04:00
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
2019-06-26 12:20:38 -04:00
|
|
|
def perform_approve_post_closed(performed_by, args)
|
|
|
|
perform_approve_post(performed_by, args)
|
|
|
|
end
|
|
|
|
|
2019-04-23 12:18:39 -04:00
|
|
|
def perform_reject_post(performed_by, args)
|
2019-01-03 12:03:01 -05:00
|
|
|
# Backwards compatibility, new code should listen for `reviewable_transitioned_to`
|
|
|
|
DiscourseEvent.trigger(:rejected_post, self)
|
|
|
|
|
|
|
|
StaffActionLogger.new(performed_by).log_post_rejected(self, DateTime.now) if performed_by.staff?
|
|
|
|
|
|
|
|
create_result(:success, :rejected)
|
2023-10-12 21:28:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_revise_and_reject_post(performed_by, args)
|
|
|
|
pm_translation_args = {
|
2023-10-26 23:05:41 -04:00
|
|
|
topic_title: self.topic&.title || self.payload["title"],
|
|
|
|
topic_url: self.topic&.url,
|
2023-10-12 21:28:31 -04:00
|
|
|
reason: args[:revise_custom_reason].presence || args[:revise_reason],
|
|
|
|
feedback: args[:revise_feedback],
|
|
|
|
original_post: self.payload["raw"],
|
|
|
|
site_name: SiteSetting.title,
|
|
|
|
}
|
FIX: Use site contact user + group for Revise... review action (#29271)
Followup 9762e6575841889d50275cb6026e4482d8091857
When we added the Revise... option for posts/new topics
in the review queue, which sends a PM to the user, we used
`SystemMessage.create_from_system_user`, which always sends
the PM from the system user. However, this makes it so if the
user replies to the PM, which they are encouraged to do,
no one will see it unless they actively monitor the system inbox.
This commit changes it so `SystemMessage.create` is used,
which uses the `site_contact_username` and `site_contact_group`
site settings as participants in the sent PM. Then, when the
user replies, it will send to that inbox instead.
If `site_contact_username` is blank, the system user is used.
2024-10-27 19:40:10 -04:00
|
|
|
SystemMessage.create(
|
2023-10-12 21:28:31 -04:00
|
|
|
self.target_created_by,
|
2023-10-26 23:05:41 -04:00
|
|
|
(
|
|
|
|
if self.topic.blank?
|
|
|
|
:reviewable_queued_post_revise_and_reject_new_topic
|
|
|
|
else
|
|
|
|
:reviewable_queued_post_revise_and_reject
|
|
|
|
end
|
|
|
|
),
|
2023-10-12 21:28:31 -04:00
|
|
|
pm_translation_args,
|
|
|
|
)
|
|
|
|
StaffActionLogger.new(performed_by).log_post_rejected(self, DateTime.now) if performed_by.staff?
|
|
|
|
create_result(:success, :rejected)
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
2019-04-12 09:55:27 -04:00
|
|
|
def perform_delete(performed_by, args)
|
|
|
|
create_result(:success, :deleted)
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def perform_delete_user(performed_by, args)
|
2021-06-15 11:35:45 -04:00
|
|
|
delete_user(performed_by, delete_opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_delete_user_block(performed_by, args)
|
|
|
|
delete_options = delete_opts
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
delete_options.merge!(block_email: true, block_ip: true) if Rails.env.production?
|
|
|
|
|
2021-06-15 11:35:45 -04:00
|
|
|
delete_user(performed_by, delete_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def delete_user(performed_by, delete_options)
|
2023-07-18 07:50:31 -04:00
|
|
|
reviewable_ids = Reviewable.where(created_by: target_created_by).pluck(:id)
|
|
|
|
|
|
|
|
UserDestroyer.new(performed_by).destroy(target_created_by, delete_options)
|
|
|
|
update_column(:target_created_by_id, nil)
|
|
|
|
|
|
|
|
create_result(:success, :rejected) { |r| r.remove_reviewable_ids += reviewable_ids }
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
2021-06-15 11:35:45 -04:00
|
|
|
def delete_opts
|
|
|
|
{
|
|
|
|
context: I18n.t("reviewables.actions.delete_user.reason"),
|
|
|
|
delete_posts: true,
|
|
|
|
block_urls: true,
|
|
|
|
delete_as_spammer: true,
|
|
|
|
}
|
|
|
|
end
|
2021-08-26 12:16:00 -04:00
|
|
|
|
|
|
|
def compute_user_stats
|
|
|
|
return unless status_changed_from_or_to_pending?
|
2023-07-18 07:50:31 -04:00
|
|
|
target_created_by&.user_stat&.update_pending_posts
|
2021-08-26 12:16:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def status_changed_from_or_to_pending?
|
|
|
|
saved_change_to_id?(from: nil) && pending? || saved_change_to_status?(from: "pending")
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: reviewables
|
|
|
|
#
|
2019-05-02 18:34:12 -04:00
|
|
|
# id :bigint not null, primary key
|
2019-01-03 12:03:01 -05:00
|
|
|
# type :string not null
|
2021-12-08 12:12:24 -05:00
|
|
|
# status :integer default("pending"), not null
|
2019-01-03 12:03:01 -05:00
|
|
|
# created_by_id :integer not null
|
|
|
|
# reviewable_by_moderator :boolean default(FALSE), not null
|
|
|
|
# category_id :integer
|
|
|
|
# topic_id :integer
|
|
|
|
# score :float default(0.0), not null
|
|
|
|
# potential_spam :boolean default(FALSE), not null
|
|
|
|
# target_id :integer
|
|
|
|
# target_type :string
|
|
|
|
# target_created_by_id :integer
|
|
|
|
# payload :json
|
|
|
|
# version :integer default(0), not null
|
|
|
|
# latest_score :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2021-07-05 18:14:15 -04:00
|
|
|
# force_review :boolean default(FALSE), not null
|
|
|
|
# reject_reason :text
|
2019-01-03 12:03:01 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2022-07-28 04:16:33 -04:00
|
|
|
# idx_reviewables_score_desc_created_at_desc (score,created_at)
|
2019-05-02 18:34:12 -04:00
|
|
|
# index_reviewables_on_reviewable_by_group_id (reviewable_by_group_id)
|
2019-04-12 09:55:27 -04:00
|
|
|
# index_reviewables_on_status_and_created_at (status,created_at)
|
|
|
|
# index_reviewables_on_status_and_score (status,score)
|
|
|
|
# index_reviewables_on_status_and_type (status,type)
|
2020-09-01 04:00:36 -04:00
|
|
|
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
|
2019-04-12 09:55:27 -04:00
|
|
|
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
|
|
|
|
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
|
2019-01-03 12:03:01 -05:00
|
|
|
#
|