Class: NewPostManager
- Inherits:
-
Object
- Object
- NewPostManager
- Defined in:
- lib/new_post_manager.rb
Overview
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.
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
- .add_handler(priority = 0, &block) ⇒ Object
- .add_plugin_payload_attribute(attribute) ⇒ Object
- .auto_silence?(manager) ⇒ Boolean
- .clear_handlers! ⇒ Object
- .default_handler(manager) ⇒ Object
- .exempt_user?(user) ⇒ Boolean
- .handlers ⇒ Object
- .is_fast_typer?(manager) ⇒ Boolean
- .is_first_post?(manager) ⇒ Boolean
- .matches_auto_silence_regex?(manager) ⇒ Boolean
- .plugin_payload_attributes ⇒ Object
- .post_needs_approval?(manager) ⇒ Boolean
- .post_needs_approval_in_its_category?(manager) ⇒ Boolean
- .queue_enabled? ⇒ Boolean
- .sorted_handlers ⇒ Object
Instance Method Summary collapse
-
#enqueue(reason = nil) ⇒ Object
Enqueue this post.
-
#initialize(user, args) ⇒ NewPostManager
constructor
A new instance of NewPostManager.
- #perform ⇒ Object
- #perform_create_post ⇒ Object
Constructor Details
#initialize(user, args) ⇒ NewPostManager
Returns a new instance of NewPostManager.
217 218 219 220 |
# File 'lib/new_post_manager.rb', line 217 def initialize(user, args) @user = user @args = args.delete_if { |_, v| v.nil? } end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
9 10 11 |
# File 'lib/new_post_manager.rb', line 9 def args @args end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
9 10 11 |
# File 'lib/new_post_manager.rb', line 9 def user @user end |
Class Method Details
.add_handler(priority = 0, &block) ⇒ Object
31 32 33 34 |
# File 'lib/new_post_manager.rb', line 31 def self.add_handler(priority = 0, &block) sorted_handlers << { priority: priority, proc: block } @sorted_handlers.sort_by! { |h| -h[:priority] } end |
.add_plugin_payload_attribute(attribute) ⇒ Object
23 24 25 |
# File 'lib/new_post_manager.rb', line 23 def self.add_plugin_payload_attribute(attribute) plugin_payload_attributes << attribute end |
.auto_silence?(manager) ⇒ Boolean
52 53 54 55 |
# File 'lib/new_post_manager.rb', line 52 def self.auto_silence?(manager) is_first_post?(manager) && WordWatcher.new("#{manager.args[:title]} #{manager.args[:raw]}").should_silence? end |
.clear_handlers! ⇒ Object
27 28 29 |
# File 'lib/new_post_manager.rb', line 27 def self.clear_handlers! @sorted_handlers = [] end |
.default_handler(manager) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/new_post_manager.rb', line 143 def self.default_handler(manager) reason = post_needs_approval?(manager) return if reason == :skip validator = PostValidator.new 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) result.errors.add(:base, post.errors[:raw]) 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) result = NewPostResult.new(:created_post, false) result.errors.add(:base, I18n.t(:topic_not_found)) return result end elsif manager.args[:category] category = Category.find_by(id: manager.args[:category]) unless manager.user.guardian.can_create_topic_on_category?(category) result = NewPostResult.new(:created_post, false) result.errors.add(:base, I18n.t("js.errors.reasons.forbidden")) return result end end result = manager.enqueue(reason) I18n.with_locale(SiteSetting.default_locale) do if is_fast_typer?(manager) UserSilencer.silence( manager.user, Discourse.system_user, keep_posts: true, reason: I18n.t("user.new_user_typed_too_fast"), ) elsif auto_silence?(manager) || matches_auto_silence_regex?(manager) UserSilencer.silence( manager.user, Discourse.system_user, keep_posts: true, reason: I18n.t("user.content_matches_auto_silence_regex"), ) elsif reason == :email_spam && is_first_post?(manager) UserSilencer.silence( manager.user, Discourse.system_user, keep_posts: true, reason: I18n.t("user.email_in_spam_header"), ) end end result end |
.exempt_user?(user) ⇒ Boolean
75 76 77 |
# File 'lib/new_post_manager.rb', line 75 def self.exempt_user?(user) user.staff? end |
.handlers ⇒ Object
15 16 17 |
# File 'lib/new_post_manager.rb', line 15 def self.handlers sorted_handlers.map { |h| h[:proc] } end |
.is_fast_typer?(manager) ⇒ Boolean
43 44 45 46 47 48 49 50 |
# File 'lib/new_post_manager.rb', line 43 def self.is_fast_typer?(manager) args = manager.args is_first_post?(manager) && 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 end |
.is_first_post?(manager) ⇒ Boolean
36 37 38 39 40 41 |
# File 'lib/new_post_manager.rb', line 36 def self.is_first_post?(manager) user = manager.user args = manager.args !!(args[:first_post_checks] && user.post_count == 0 && user.topic_count == 0) end |
.matches_auto_silence_regex?(manager) ⇒ Boolean
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/new_post_manager.rb', line 57 def self.matches_auto_silence_regex?(manager) args = manager.args pattern = SiteSetting.auto_silence_first_post_regex return false if pattern.blank? 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 |
.plugin_payload_attributes ⇒ Object
19 20 21 |
# File 'lib/new_post_manager.rb', line 19 def self.plugin_payload_attributes @payload_attributes ||= [] end |
.post_needs_approval?(manager) ⇒ Boolean
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/new_post_manager.rb', line 79 def self.post_needs_approval?(manager) user = manager.user return :email_auth_res_enqueue if manager.args[:email_auth_res_action] == :enqueue return :skip if exempt_user?(user) return :email_spam if manager.args[:email_spam] if ( user.trust_level <= TrustLevel.levels[:basic] && (user.post_count + user.topic_count) < SiteSetting.approve_post_count ) return :post_count end if !user.staged? && !user.in_any_groups?(SiteSetting.approve_unless_allowed_groups_map) return :group end if ( manager.args[:title].present? && !user.staged? && !user.in_any_groups?(SiteSetting.approve_new_topics_unless_allowed_groups_map) ) return :new_topics_unless_allowed_groups end if WordWatcher.new("#{manager.args[:title]} #{manager.args[:raw]}").requires_approval? return :watched_word end return :fast_typer if is_fast_typer?(manager) return :auto_silence_regex if auto_silence?(manager) || matches_auto_silence_regex?(manager) return :staged if SiteSetting.approve_unless_staged? && user.staged? return :category if post_needs_approval_in_its_category?(manager) if ( manager.args[:image_sizes].present? && !user.in_any_groups?(SiteSetting.skip_review_media_groups_map) ) return :contains_media end :skip end |
.post_needs_approval_in_its_category?(manager) ⇒ Boolean
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/new_post_manager.rb', line 128 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 topic = Topic.find(manager.args[:topic_id]) cat.require_reply_approval? && !manager.user.guardian.can_review_topic?(topic) elsif manager.args[:category].present? cat = Category.find(manager.args[:category]) cat.require_topic_approval? && !manager.user.guardian.is_category_group_moderator?(cat) else false end end |
.queue_enabled? ⇒ Boolean
204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/new_post_manager.rb', line 204 def self.queue_enabled? SiteSetting.approve_post_count > 0 || !( SiteSetting.approve_unless_allowed_groups_map.include?(Group::AUTO_GROUPS[:trust_level_0]) ) || !( 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 end |
.sorted_handlers ⇒ Object
11 12 13 |
# File 'lib/new_post_manager.rb', line 11 def self.sorted_handlers @sorted_handlers ||= clear_handlers! end |
Instance Method Details
#enqueue(reason = nil) ⇒ Object
Enqueue this post
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/new_post_manager.rb', line 256 def enqueue(reason = nil) result = NewPostResult.new(:enqueued) payload = { raw: @args[:raw], tags: @args[:tags] } %w[typing_duration_msecs composer_open_duration_msecs reply_to_post_number].each do |a| payload[a] = @args[a].to_i if @args[a] end self.class.plugin_payload_attributes.each { |a| payload[a] = @args[a] if @args[a].present? } payload[:via_email] = true if !!@args[:via_email] payload[:raw_email] = @args[:raw_email] if @args[:raw_email].present? reviewable = ReviewableQueuedPost.new( created_by: Discourse.system_user, payload: payload, topic_id: @args[:topic_id], reviewable_by_moderator: true, target_created_by: @user, ) reviewable.payload["title"] = @args[:title] if @args[:title].present? reviewable.category_id = args[:category] if args[:category].present? reviewable.created_new! = reviewable. creator = ( if @args[:topic_id] PostCreator.new(@user, ) else TopicCreator.new(@user, Guardian.new(@user), ) end ) errors = Set.new creator.valid? creator.errors..each { |msg| errors << msg } errors = creator.errors..uniq if errors.blank? if reviewable.save reviewable.add_score( Discourse.system_user, ReviewableScore.types[:needs_approval], reason: reason, force_review: true, ) else reviewable.errors..each { |msg| errors << msg } end end result.reviewable = reviewable result.reason = reason if reason result.check_errors(errors) result.pending_count = ReviewableQueuedPost.where(target_created_by: @user).pending.count result end |
#perform ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/new_post_manager.rb', line 222 def perform if !self.class.exempt_user?(@user) && matches = WordWatcher.new("#{@args[:title]} #{@args[:raw]}").should_block?.presence result = NewPostResult.new(:created_post, false) if matches.size == 1 key = "contains_blocked_word" translation_args = { word: CGI.escapeHTML(matches[0]) } else key = "contains_blocked_words" translation_args = { words: CGI.escapeHTML(matches.join(", ")) } end result.errors.add(:base, I18n.t(key, translation_args)) return result end # Perform handlers until one returns a result NewPostManager.handlers.any? do |handler| result = handler.call(self) return result if result end # We never queue private messages if @args[:archetype] == Archetype. || ( args[:topic_id] && Topic.where(id: args[:topic_id], archetype: Archetype.).exists? ) return perform_create_post end NewPostManager.default_handler(self) || perform_create_post end |
#perform_create_post ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/new_post_manager.rb', line 315 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 |