2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-31 11:58:56 -05:00
|
|
|
# Determines what actions should be taken with new posts.
|
|
|
|
#
|
|
|
|
# The default action is to create the post, but this can be extended
|
|
|
|
# with `NewPostManager.add_handler` to take other approaches depending
|
|
|
|
# on the user or input.
|
|
|
|
class NewPostManager
|
|
|
|
attr_reader :user, :args
|
|
|
|
|
2015-04-28 12:53:05 -05:00
|
|
|
def self.sorted_handlers
|
|
|
|
@sorted_handlers ||= clear_handlers!
|
|
|
|
end
|
|
|
|
|
2015-03-31 11:58:56 -05:00
|
|
|
def self.handlers
|
2017-07-27 20:20:09 -05:00
|
|
|
sorted_handlers.map { |h| h[:proc] }
|
2015-03-31 11:58:56 -05:00
|
|
|
end
|
|
|
|
|
2019-08-14 13:02:59 -05:00
|
|
|
def self.plugin_payload_attributes
|
|
|
|
@payload_attributes ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_plugin_payload_attribute(attribute)
|
|
|
|
plugin_payload_attributes << attribute
|
|
|
|
end
|
|
|
|
|
2015-04-28 12:53:05 -05:00
|
|
|
def self.clear_handlers!
|
2019-05-25 08:53:03 -05:00
|
|
|
@sorted_handlers = []
|
2015-04-28 12:53:05 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.add_handler(priority = 0, &block)
|
2015-04-28 12:53:05 -05:00
|
|
|
sorted_handlers << { priority: priority, proc: block }
|
2017-07-27 20:20:09 -05:00
|
|
|
@sorted_handlers.sort_by! { |h| -h[:priority] }
|
2015-03-31 11:58:56 -05:00
|
|
|
end
|
|
|
|
|
2015-08-04 22:08:21 -05:00
|
|
|
def self.is_first_post?(manager)
|
2015-08-03 19:55:59 -05:00
|
|
|
user = manager.user
|
|
|
|
args = manager.args
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
!!(args[:first_post_checks] && user.post_count == 0 && user.topic_count == 0)
|
2015-08-04 22:08:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_fast_typer?(manager)
|
|
|
|
args = manager.args
|
|
|
|
|
|
|
|
is_first_post?(manager) &&
|
2023-01-09 06:10:19 -06:00
|
|
|
args[:typing_duration_msecs].to_i < SiteSetting.min_first_post_typing_time &&
|
|
|
|
SiteSetting.auto_silence_fast_typers_on_first_post &&
|
|
|
|
manager.user.trust_level <= SiteSetting.auto_silence_fast_typers_max_trust_level
|
2015-08-03 21:06:07 -05:00
|
|
|
end
|
|
|
|
|
2021-05-27 11:19:58 -05:00
|
|
|
def self.auto_silence?(manager)
|
2023-01-09 06:10:19 -06:00
|
|
|
is_first_post?(manager) &&
|
|
|
|
WordWatcher.new("#{manager.args[:title]} #{manager.args[:raw]}").should_silence?
|
2021-05-27 11:19:58 -05:00
|
|
|
end
|
|
|
|
|
2021-02-26 07:07:04 -06:00
|
|
|
def self.matches_auto_silence_regex?(manager)
|
|
|
|
args = manager.args
|
|
|
|
|
|
|
|
pattern = SiteSetting.auto_silence_first_post_regex
|
|
|
|
|
2024-05-27 05:27:13 -05:00
|
|
|
return false if pattern.blank?
|
2021-02-26 07:07:04 -06:00
|
|
|
return false unless is_first_post?(manager)
|
|
|
|
|
|
|
|
begin
|
|
|
|
regex = Regexp.new(pattern, Regexp::IGNORECASE)
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn "Invalid regex in auto_silence_first_post_regex #{e}"
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
"#{args[:title]} #{args[:raw]}" =~ regex
|
|
|
|
end
|
|
|
|
|
2017-06-28 15:56:44 -05:00
|
|
|
def self.exempt_user?(user)
|
2018-04-06 04:41:25 -05:00
|
|
|
user.staff?
|
2017-06-28 15:56:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.post_needs_approval?(manager)
|
2015-08-03 21:06:07 -05:00
|
|
|
user = manager.user
|
|
|
|
|
2020-01-21 10:12:00 -06:00
|
|
|
return :email_auth_res_enqueue if manager.args[:email_auth_res_action] == :enqueue
|
|
|
|
|
2019-04-10 16:42:49 -05:00
|
|
|
return :skip if exempt_user?(user)
|
|
|
|
|
2020-01-21 10:12:00 -06:00
|
|
|
return :email_spam if manager.args[:email_spam]
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
if (
|
|
|
|
user.trust_level <= TrustLevel.levels[:basic] &&
|
|
|
|
(user.post_count + user.topic_count) < SiteSetting.approve_post_count
|
|
|
|
)
|
|
|
|
return :post_count
|
|
|
|
end
|
2019-04-10 16:42:49 -05:00
|
|
|
|
2023-11-28 08:34:02 -06:00
|
|
|
if !user.staged? && !user.in_any_groups?(SiteSetting.approve_unless_allowed_groups_map)
|
2023-11-21 12:31:42 -06:00
|
|
|
return :group
|
|
|
|
end
|
2019-04-10 16:42:49 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
if (
|
2023-11-28 08:34:02 -06:00
|
|
|
manager.args[:title].present? && !user.staged? &&
|
2023-11-22 11:44:59 -06:00
|
|
|
!user.in_any_groups?(SiteSetting.approve_new_topics_unless_allowed_groups_map)
|
2023-01-09 06:10:19 -06:00
|
|
|
)
|
2023-11-22 11:44:59 -06:00
|
|
|
return :new_topics_unless_allowed_groups
|
2023-01-09 06:10:19 -06:00
|
|
|
end
|
2019-04-10 16:42:49 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
if WordWatcher.new("#{manager.args[:title]} #{manager.args[:raw]}").requires_approval?
|
|
|
|
return :watched_word
|
|
|
|
end
|
2020-09-09 12:36:22 -05:00
|
|
|
|
2019-04-10 16:42:49 -05:00
|
|
|
return :fast_typer if is_fast_typer?(manager)
|
|
|
|
|
2021-05-27 11:19:58 -05:00
|
|
|
return :auto_silence_regex if auto_silence?(manager) || matches_auto_silence_regex?(manager)
|
2021-02-26 07:07:04 -06:00
|
|
|
|
2019-04-10 16:42:49 -05:00
|
|
|
return :staged if SiteSetting.approve_unless_staged? && user.staged?
|
|
|
|
|
|
|
|
return :category if post_needs_approval_in_its_category?(manager)
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
if (
|
|
|
|
manager.args[:image_sizes].present? &&
|
2024-01-10 21:43:01 -06:00
|
|
|
!user.in_any_groups?(SiteSetting.skip_review_media_groups_map)
|
2023-01-09 06:10:19 -06:00
|
|
|
)
|
|
|
|
return :contains_media
|
|
|
|
end
|
2020-09-18 10:45:09 -05:00
|
|
|
|
2019-04-10 16:42:49 -05:00
|
|
|
:skip
|
2018-07-12 21:51:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.post_needs_approval_in_its_category?(manager)
|
|
|
|
if manager.args[:topic_id].present?
|
|
|
|
cat = Category.joins(:topics).find_by(topics: { id: manager.args[:topic_id] })
|
|
|
|
return false unless cat
|
2021-02-18 12:00:06 -06:00
|
|
|
|
|
|
|
topic = Topic.find(manager.args[:topic_id])
|
|
|
|
cat.require_reply_approval? && !manager.user.guardian.can_review_topic?(topic)
|
2018-07-12 21:51:08 -05:00
|
|
|
elsif manager.args[:category].present?
|
2021-02-18 12:00:06 -06:00
|
|
|
cat = Category.find(manager.args[:category])
|
|
|
|
cat.require_topic_approval? && !manager.user.guardian.is_category_group_moderator?(cat)
|
2018-07-12 21:51:08 -05:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2015-04-28 15:14:36 -05:00
|
|
|
end
|
|
|
|
|
2015-04-15 11:12:20 -05:00
|
|
|
def self.default_handler(manager)
|
2019-04-10 16:42:49 -05:00
|
|
|
reason = post_needs_approval?(manager)
|
|
|
|
return if reason == :skip
|
|
|
|
|
2019-10-01 23:01:53 -05:00
|
|
|
validator = PostValidator.new
|
2019-04-10 16:42:49 -05:00
|
|
|
post = Post.new(raw: manager.args[:raw])
|
|
|
|
post.user = manager.user
|
|
|
|
validator.validate(post)
|
|
|
|
|
|
|
|
if post.errors[:raw].present?
|
|
|
|
result = NewPostResult.new(:created_post, false)
|
2019-04-30 01:58:18 -05:00
|
|
|
result.errors.add(:base, post.errors[:raw])
|
2019-04-10 16:42:49 -05:00
|
|
|
return result
|
|
|
|
elsif manager.args[:topic_id]
|
|
|
|
topic = Topic.unscoped.where(id: manager.args[:topic_id]).first
|
|
|
|
|
|
|
|
unless manager.user.guardian.can_create_post_on_topic?(topic)
|
2016-09-12 11:26:49 -05:00
|
|
|
result = NewPostResult.new(:created_post, false)
|
2019-04-30 01:58:18 -05:00
|
|
|
result.errors.add(:base, I18n.t(:topic_not_found))
|
2016-09-12 11:26:49 -05:00
|
|
|
return result
|
2016-09-09 11:15:56 -05:00
|
|
|
end
|
2019-04-10 16:42:49 -05:00
|
|
|
elsif manager.args[:category]
|
|
|
|
category = Category.find_by(id: manager.args[:category])
|
2016-09-09 11:15:56 -05:00
|
|
|
|
2019-04-10 16:42:49 -05:00
|
|
|
unless manager.user.guardian.can_create_topic_on_category?(category)
|
|
|
|
result = NewPostResult.new(:created_post, false)
|
2019-04-30 01:58:18 -05:00
|
|
|
result.errors.add(:base, I18n.t("js.errors.reasons.forbidden"))
|
2019-04-10 16:42:49 -05:00
|
|
|
return result
|
2016-03-18 06:16:37 -05:00
|
|
|
end
|
2019-04-10 16:42:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
result = manager.enqueue(reason)
|
2015-08-03 21:06:07 -05:00
|
|
|
|
2020-08-10 12:51:54 -05:00
|
|
|
I18n.with_locale(SiteSetting.default_locale) do
|
|
|
|
if is_fast_typer?(manager)
|
2023-01-09 06:10:19 -06:00
|
|
|
UserSilencer.silence(
|
|
|
|
manager.user,
|
|
|
|
Discourse.system_user,
|
|
|
|
keep_posts: true,
|
|
|
|
reason: I18n.t("user.new_user_typed_too_fast"),
|
|
|
|
)
|
2021-05-27 11:19:58 -05:00
|
|
|
elsif auto_silence?(manager) || matches_auto_silence_regex?(manager)
|
2023-01-09 06:10:19 -06:00
|
|
|
UserSilencer.silence(
|
|
|
|
manager.user,
|
|
|
|
Discourse.system_user,
|
|
|
|
keep_posts: true,
|
|
|
|
reason: I18n.t("user.content_matches_auto_silence_regex"),
|
|
|
|
)
|
2020-08-10 12:51:54 -05:00
|
|
|
elsif reason == :email_spam && is_first_post?(manager)
|
2023-01-09 06:10:19 -06:00
|
|
|
UserSilencer.silence(
|
|
|
|
manager.user,
|
|
|
|
Discourse.system_user,
|
|
|
|
keep_posts: true,
|
|
|
|
reason: I18n.t("user.email_in_spam_header"),
|
|
|
|
)
|
2020-08-10 12:51:54 -05:00
|
|
|
end
|
2015-08-03 21:06:07 -05:00
|
|
|
end
|
2019-04-10 16:42:49 -05:00
|
|
|
|
|
|
|
result
|
2015-04-15 11:12:20 -05:00
|
|
|
end
|
|
|
|
|
2015-04-21 12:59:57 -05:00
|
|
|
def self.queue_enabled?
|
2023-11-21 12:31:42 -06:00
|
|
|
SiteSetting.approve_post_count > 0 ||
|
|
|
|
!(
|
|
|
|
SiteSetting.approve_unless_allowed_groups_map.include?(Group::AUTO_GROUPS[:trust_level_0])
|
2023-11-22 11:44:59 -06:00
|
|
|
) ||
|
|
|
|
!(
|
|
|
|
SiteSetting.approve_new_topics_unless_allowed_groups_map.include?(
|
|
|
|
Group::AUTO_GROUPS[:trust_level_0],
|
|
|
|
)
|
|
|
|
) || SiteSetting.approve_unless_staged ||
|
|
|
|
WordWatcher.words_for_action_exist?(:require_approval) || handlers.size > 1
|
2015-04-21 12:59:57 -05:00
|
|
|
end
|
|
|
|
|
2015-03-31 11:58:56 -05:00
|
|
|
def initialize(user, args)
|
|
|
|
@user = user
|
2017-07-27 20:20:09 -05:00
|
|
|
@args = args.delete_if { |_, v| v.nil? }
|
2015-03-31 11:58:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
2023-01-09 06:10:19 -06:00
|
|
|
if !self.class.exempt_user?(@user) &&
|
|
|
|
matches = WordWatcher.new("#{@args[:title]} #{@args[:raw]}").should_block?.presence
|
2017-06-28 15:56:44 -05:00
|
|
|
result = NewPostResult.new(:created_post, false)
|
2019-07-22 06:59:56 -05:00
|
|
|
if matches.size == 1
|
2023-01-09 06:10:19 -06:00
|
|
|
key = "contains_blocked_word"
|
2021-09-24 03:55:15 -05:00
|
|
|
translation_args = { word: CGI.escapeHTML(matches[0]) }
|
2019-07-22 06:59:56 -05:00
|
|
|
else
|
2023-01-09 06:10:19 -06:00
|
|
|
key = "contains_blocked_words"
|
|
|
|
translation_args = { words: CGI.escapeHTML(matches.join(", ")) }
|
2019-07-22 06:59:56 -05:00
|
|
|
end
|
|
|
|
result.errors.add(:base, I18n.t(key, translation_args))
|
2017-06-28 15:56:44 -05:00
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2015-03-31 11:58:56 -05:00
|
|
|
# Perform handlers until one returns a result
|
2019-05-25 08:53:03 -05:00
|
|
|
NewPostManager.handlers.any? do |handler|
|
2015-03-31 11:58:56 -05:00
|
|
|
result = handler.call(self)
|
|
|
|
return result if result
|
|
|
|
end
|
|
|
|
|
2019-05-25 08:53:03 -05:00
|
|
|
# We never queue private messages
|
2023-01-09 06:10:19 -06:00
|
|
|
if @args[:archetype] == Archetype.private_message ||
|
|
|
|
(
|
|
|
|
args[:topic_id] &&
|
|
|
|
Topic.where(id: args[:topic_id], archetype: Archetype.private_message).exists?
|
|
|
|
)
|
|
|
|
return perform_create_post
|
|
|
|
end
|
2019-05-25 08:53:03 -05:00
|
|
|
|
|
|
|
NewPostManager.default_handler(self) || perform_create_post
|
2015-03-31 11:58:56 -05:00
|
|
|
end
|
|
|
|
|
2019-01-03 11:03:01 -06:00
|
|
|
# Enqueue this post
|
|
|
|
def enqueue(reason = nil)
|
2015-03-31 11:58:56 -05:00
|
|
|
result = NewPostResult.new(:enqueued)
|
2023-01-09 06:10:19 -06:00
|
|
|
payload = { raw: @args[:raw], tags: @args[:tags] }
|
|
|
|
%w[typing_duration_msecs composer_open_duration_msecs reply_to_post_number].each do |a|
|
2019-06-05 11:12:13 -05:00
|
|
|
payload[a] = @args[a].to_i if @args[a]
|
|
|
|
end
|
2019-08-14 13:02:59 -05:00
|
|
|
|
|
|
|
self.class.plugin_payload_attributes.each { |a| payload[a] = @args[a] if @args[a].present? }
|
|
|
|
|
2019-07-19 10:56:14 -05:00
|
|
|
payload[:via_email] = true if !!@args[:via_email]
|
|
|
|
payload[:raw_email] = @args[:raw_email] if @args[:raw_email].present?
|
2015-03-31 11:58:56 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
reviewable =
|
|
|
|
ReviewableQueuedPost.new(
|
2023-07-18 06:50:31 -05:00
|
|
|
created_by: Discourse.system_user,
|
2023-01-09 06:10:19 -06:00
|
|
|
payload: payload,
|
|
|
|
topic_id: @args[:topic_id],
|
|
|
|
reviewable_by_moderator: true,
|
2023-07-18 06:50:31 -05:00
|
|
|
target_created_by: @user,
|
2023-01-09 06:10:19 -06:00
|
|
|
)
|
|
|
|
reviewable.payload["title"] = @args[:title] if @args[:title].present?
|
2019-04-02 10:11:43 -05:00
|
|
|
reviewable.category_id = args[:category] if args[:category].present?
|
2019-05-01 13:40:38 -05:00
|
|
|
reviewable.created_new!
|
2019-01-03 11:03:01 -06:00
|
|
|
|
|
|
|
create_options = reviewable.create_options
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
creator =
|
|
|
|
(
|
|
|
|
if @args[:topic_id]
|
|
|
|
PostCreator.new(@user, create_options)
|
|
|
|
else
|
|
|
|
TopicCreator.new(@user, Guardian.new(@user), create_options)
|
|
|
|
end
|
|
|
|
)
|
2019-01-03 11:03:01 -06:00
|
|
|
|
|
|
|
errors = Set.new
|
|
|
|
creator.valid?
|
|
|
|
creator.errors.full_messages.each { |msg| errors << msg }
|
|
|
|
errors = creator.errors.full_messages.uniq
|
|
|
|
if errors.blank?
|
|
|
|
if reviewable.save
|
|
|
|
reviewable.add_score(
|
|
|
|
Discourse.system_user,
|
|
|
|
ReviewableScore.types[:needs_approval],
|
2019-04-11 10:11:35 -05:00
|
|
|
reason: reason,
|
2023-01-09 06:10:19 -06:00
|
|
|
force_review: true,
|
2019-01-03 11:03:01 -06:00
|
|
|
)
|
|
|
|
else
|
|
|
|
reviewable.errors.full_messages.each { |msg| errors << msg }
|
|
|
|
end
|
|
|
|
end
|
2015-04-10 16:00:50 -05:00
|
|
|
|
2019-01-03 11:03:01 -06:00
|
|
|
result.reviewable = reviewable
|
2015-04-28 14:06:27 -05:00
|
|
|
result.reason = reason if reason
|
2019-01-03 11:03:01 -06:00
|
|
|
result.check_errors(errors)
|
2023-07-18 06:50:31 -05:00
|
|
|
result.pending_count = ReviewableQueuedPost.where(target_created_by: @user).pending.count
|
2015-03-31 11:58:56 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_create_post
|
|
|
|
result = NewPostResult.new(:create_post)
|
|
|
|
creator = PostCreator.new(@user, @args)
|
|
|
|
post = creator.create
|
|
|
|
result.check_errors_from(creator)
|
|
|
|
|
|
|
|
if result.success?
|
|
|
|
result.post = post
|
|
|
|
else
|
|
|
|
@user.flag_linked_posts_as_spam if creator.spam?
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|