2015-04-01 00:58:56 +08:00
|
|
|
require_dependency 'post_creator'
|
|
|
|
require_dependency 'new_post_result'
|
|
|
|
require_dependency 'post_enqueuer'
|
|
|
|
|
|
|
|
# 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-29 01:53:05 +08:00
|
|
|
def self.sorted_handlers
|
|
|
|
@sorted_handlers ||= clear_handlers!
|
|
|
|
end
|
|
|
|
|
2015-04-01 00:58:56 +08:00
|
|
|
def self.handlers
|
2015-04-29 01:53:05 +08:00
|
|
|
sorted_handlers.map {|h| h[:proc]}
|
2015-04-01 00:58:56 +08:00
|
|
|
end
|
|
|
|
|
2015-04-29 01:53:05 +08:00
|
|
|
def self.clear_handlers!
|
|
|
|
@sorted_handlers = [{ priority: 0, proc: method(:default_handler) }]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_handler(priority=0, &block)
|
|
|
|
sorted_handlers << { priority: priority, proc: block }
|
|
|
|
@sorted_handlers.sort_by! {|h| -h[:priority]}
|
2015-04-01 00:58:56 +08:00
|
|
|
end
|
|
|
|
|
2015-08-05 11:08:21 +08:00
|
|
|
def self.is_first_post?(manager)
|
2015-08-04 08:55:59 +08:00
|
|
|
user = manager.user
|
|
|
|
args = manager.args
|
|
|
|
|
2015-08-06 08:07:07 +08:00
|
|
|
!!(
|
|
|
|
args[:first_post_checks] &&
|
|
|
|
user.post_count == 0
|
|
|
|
)
|
2015-08-05 11:08:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_fast_typer?(manager)
|
|
|
|
args = manager.args
|
|
|
|
|
|
|
|
is_first_post?(manager) &&
|
2015-08-06 08:07:07 +08:00
|
|
|
args[:typing_duration_msecs].to_i < SiteSetting.min_first_post_typing_time &&
|
|
|
|
SiteSetting.auto_block_fast_typers_on_first_post &&
|
|
|
|
manager.user.trust_level <= SiteSetting.auto_block_fast_typers_max_trust_level
|
2015-08-04 10:06:07 +08:00
|
|
|
end
|
|
|
|
|
2015-08-05 11:08:21 +08:00
|
|
|
def self.matches_auto_block_regex?(manager)
|
|
|
|
args = manager.args
|
|
|
|
|
|
|
|
pattern = SiteSetting.auto_block_first_post_regex
|
|
|
|
|
|
|
|
return false unless pattern.present?
|
|
|
|
return false unless is_first_post?(manager)
|
|
|
|
|
|
|
|
begin
|
|
|
|
regex = Regexp.new(pattern, Regexp::IGNORECASE)
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn "Invalid regex in auto_block_first_post_regex #{e}"
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
"#{args[:title]} #{args[:raw]}" =~ regex
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-08-04 10:06:07 +08:00
|
|
|
def self.user_needs_approval?(manager)
|
|
|
|
user = manager.user
|
|
|
|
|
2015-11-24 23:58:26 +08:00
|
|
|
return false if user.staff? || user.staged
|
2015-06-04 23:35:36 +08:00
|
|
|
|
2016-03-03 03:54:03 +08:00
|
|
|
(user.trust_level <= TrustLevel.levels[:basic] && user.post_count < SiteSetting.approve_post_count) ||
|
2015-08-04 08:55:59 +08:00
|
|
|
(user.trust_level < SiteSetting.approve_unless_trust_level.to_i) ||
|
2016-09-20 02:07:15 +08:00
|
|
|
(manager.args[:title].present? && user.trust_level < SiteSetting.approve_new_topics_unless_trust_level.to_i) ||
|
2015-08-05 11:08:21 +08:00
|
|
|
is_fast_typer?(manager) ||
|
|
|
|
matches_auto_block_regex?(manager)
|
2015-04-29 04:14:36 +08:00
|
|
|
end
|
|
|
|
|
2015-04-16 00:12:20 +08:00
|
|
|
def self.default_handler(manager)
|
2015-08-04 10:06:07 +08:00
|
|
|
if user_needs_approval?(manager)
|
|
|
|
|
2016-09-13 00:26:49 +08:00
|
|
|
validator = Validators::PostValidator.new
|
|
|
|
post = Post.new(raw: manager.args[:raw])
|
2016-09-17 01:15:01 +08:00
|
|
|
post.user = manager.user
|
2016-09-13 00:26:49 +08:00
|
|
|
validator.validate(post)
|
|
|
|
if post.errors[:raw].present?
|
|
|
|
result = NewPostResult.new(:created_post, false)
|
|
|
|
result.errors[:base] = post.errors[:raw]
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2016-09-10 00:15:56 +08:00
|
|
|
# Can the user create the post in the first place?
|
|
|
|
if manager.args[:topic_id]
|
|
|
|
topic = Topic.unscoped.where(id: manager.args[:topic_id]).first
|
|
|
|
|
|
|
|
unless manager.user.guardian.can_create_post_on_topic?(topic)
|
|
|
|
result = NewPostResult.new(:created_post, false)
|
|
|
|
result.errors[:base] << I18n.t(:topic_not_found)
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-04 10:06:07 +08:00
|
|
|
result = manager.enqueue('default')
|
|
|
|
|
2017-03-11 04:45:48 +08:00
|
|
|
if is_fast_typer?(manager)
|
|
|
|
UserBlocker.block(manager.user, Discourse.system_user, keep_posts: true, reason: I18n.t("user.new_user_typed_too_fast"))
|
|
|
|
elsif matches_auto_block_regex?(manager)
|
|
|
|
UserBlocker.block(manager.user, Discourse.system_user, keep_posts: true, reason: I18n.t("user.content_matches_auto_block_regex"))
|
2016-03-18 19:16:37 +08:00
|
|
|
end
|
2015-08-04 10:06:07 +08:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
2015-04-16 00:12:20 +08:00
|
|
|
end
|
|
|
|
|
2015-04-22 01:59:57 +08:00
|
|
|
def self.queue_enabled?
|
|
|
|
SiteSetting.approve_post_count > 0 ||
|
|
|
|
SiteSetting.approve_unless_trust_level.to_i > 0 ||
|
2016-09-23 02:51:36 +08:00
|
|
|
SiteSetting.approve_new_topics_unless_trust_level.to_i > 0 ||
|
2015-04-22 01:59:57 +08:00
|
|
|
handlers.size > 1
|
|
|
|
end
|
|
|
|
|
2015-04-01 00:58:56 +08:00
|
|
|
def initialize(user, args)
|
|
|
|
@user = user
|
2015-04-11 05:00:50 +08:00
|
|
|
@args = args.delete_if {|_, v| v.nil?}
|
2015-04-01 00:58:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
2015-05-04 23:07:46 +08:00
|
|
|
# We never queue private messages
|
|
|
|
return perform_create_post if @args[:archetype] == Archetype.private_message
|
2015-05-14 00:08:39 +08:00
|
|
|
if args[:topic_id] && Topic.where(id: args[:topic_id], archetype: Archetype.private_message).exists?
|
|
|
|
return perform_create_post
|
|
|
|
end
|
2015-05-04 23:07:46 +08:00
|
|
|
|
2015-04-01 00:58:56 +08:00
|
|
|
# Perform handlers until one returns a result
|
|
|
|
handled = NewPostManager.handlers.any? do |handler|
|
|
|
|
result = handler.call(self)
|
|
|
|
return result if result
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
perform_create_post unless handled
|
|
|
|
end
|
|
|
|
|
|
|
|
# Enqueue this post in a queue
|
2015-04-29 03:06:27 +08:00
|
|
|
def enqueue(queue, reason=nil)
|
2015-04-01 00:58:56 +08:00
|
|
|
result = NewPostResult.new(:enqueued)
|
|
|
|
enqueuer = PostEnqueuer.new(@user, queue)
|
|
|
|
|
2015-04-11 05:00:50 +08:00
|
|
|
queued_args = {post_options: @args.dup}
|
|
|
|
queued_args[:raw] = queued_args[:post_options].delete(:raw)
|
|
|
|
queued_args[:topic_id] = queued_args[:post_options].delete(:topic_id)
|
2015-04-11 04:29:13 +08:00
|
|
|
|
2015-04-11 05:00:50 +08:00
|
|
|
post = enqueuer.enqueue(queued_args)
|
|
|
|
|
|
|
|
QueuedPost.broadcast_new! if post && post.errors.empty?
|
|
|
|
|
|
|
|
result.queued_post = post
|
2015-04-29 03:06:27 +08:00
|
|
|
result.reason = reason if reason
|
2015-04-01 00:58:56 +08:00
|
|
|
result.check_errors_from(enqueuer)
|
2015-04-25 03:44:59 +08:00
|
|
|
result.pending_count = QueuedPost.new_posts.where(user_id: @user.id).count
|
2015-04-01 00:58:56 +08: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
|