Class: PostValidator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- PostValidator
- Defined in:
- lib/validators/post_validator.rb
Instance Method Summary collapse
- #force_edit_last_validator(post) ⇒ Object
- #max_attachments_validator(post) ⇒ Object
- #max_embedded_media_validator(post) ⇒ Object
- #max_links_validator(post) ⇒ Object
- #max_mention_validator(post) ⇒ Object
- #max_posts_validator(post) ⇒ Object
- #post_body_validator(post) ⇒ Object
- #presence(post) ⇒ Object
- #stripped_length(post) ⇒ Object
- #unique_post_validator(post) ⇒ Object
- #validate(record) ⇒ Object
Instance Method Details
#force_edit_last_validator(post) ⇒ Object
142 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 |
# File 'lib/validators/post_validator.rb', line 142 def force_edit_last_validator(post) return if post.id return if (post) return if post.acting_user&.staff? return if SiteSetting.max_consecutive_replies == 0 topic = post.topic return if topic&.ordered_posts&.first&.user == post.user guardian = Guardian.new(post.acting_user) return if guardian.is_category_group_moderator?(post.topic&.category) last_posts_count = DB.query_single( <<~SQL, SELECT COUNT(*) FROM ( SELECT user_id FROM posts WHERE deleted_at IS NULL AND NOT hidden AND topic_id = :topic_id ORDER BY post_number DESC LIMIT :max_replies ) c WHERE c.user_id = :user_id SQL topic_id: post.topic_id, user_id: post.acting_user.id, max_replies: SiteSetting.max_consecutive_replies, ).first return if last_posts_count < SiteSetting.max_consecutive_replies if guardian.can_edit?(topic.ordered_posts.last) post.errors.add( :base, I18n.t(:max_consecutive_replies, count: SiteSetting.max_consecutive_replies), ) end end |
#max_attachments_validator(post) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/validators/post_validator.rb', line 117 def (post) return if acting_user_is_trusted?(post) || (post) add_error_if_count_exceeded( post, :no_attachments_allowed, :too_many_attachments, post., SiteSetting., ) end |
#max_embedded_media_validator(post) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/validators/post_validator.rb', line 94 def (post) return if post.acting_user.nil? return if post.acting_user.staff? if !post.acting_user.in_any_groups?(SiteSetting.) add_error_if_count_exceeded( post, :no_embedded_media_allowed_group, :no_embedded_media_allowed_group, post., 0, ) elsif post.acting_user.trust_level == TrustLevel[0] add_error_if_count_exceeded( post, :no_embedded_media_allowed, :too_many_embedded_media, post., SiteSetting., ) end end |
#max_links_validator(post) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/validators/post_validator.rb', line 129 def max_links_validator(post) if (post.link_count == 0 && !post.has_oneboxes?) || (post) return newuser_links_validator(post) end guardian = Guardian.new(post.acting_user) if post.linked_hosts.keys.all? { |h| guardian.can_post_link?(host: h) } return newuser_links_validator(post) end post.errors.add(:base, I18n.t(:links_require_trust)) end |
#max_mention_validator(post) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/validators/post_validator.rb', line 72 def max_mention_validator(post) return if post.acting_user&.staff? if acting_user_is_trusted?(post) || (post) add_error_if_count_exceeded( post, :no_mentions_allowed, :too_many_mentions, post.raw_mentions.size, SiteSetting.max_mentions_per_post, ) else add_error_if_count_exceeded( post, :no_mentions_allowed_newuser, :too_many_mentions_newuser, post.raw_mentions.size, SiteSetting.newuser_max_mentions_per_post, ) end end |
#max_posts_validator(post) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/validators/post_validator.rb', line 54 def max_posts_validator(post) if post.new_record? && post.acting_user&.posted_too_much_in_topic?(post.topic_id) post.errors.add( :base, I18n.t(:too_many_replies, count: SiteSetting.newuser_max_replies_per_topic), ) end end |
#post_body_validator(post) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/validators/post_validator.rb', line 30 def post_body_validator(post) return if [:skip_post_body] || post.topic&.pm_with_non_human_user? stripped_length(post) raw_quality(post) WatchedWordsValidator.new(attributes: [:raw]).validate(post) if !post.acting_user&.staged end |
#presence(post) ⇒ Object
25 26 27 28 |
# File 'lib/validators/post_validator.rb', line 25 def presence(post) post.errors.add(:topic_id, :blank, **) if ![:skip_topic] && post.topic_id.blank? post.errors.add(:user_id, :blank, **) if post.new_record? && post.user_id.blank? end |
#stripped_length(post) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/validators/post_validator.rb', line 37 def stripped_length(post) range = if (post) SiteSetting. elsif post.is_first_post? || (post.topic.present? && post.topic.posts_count == 0) if post.topic&.featured_link.present? (0..SiteSetting.max_post_length) else SiteSetting.first_post_length end else SiteSetting.post_length end StrippedLengthValidator.validate(post, :raw, post.raw, range) end |
#unique_post_validator(post) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/validators/post_validator.rb', line 63 def unique_post_validator(post) return if SiteSetting.unique_posts_mins == 0 return if post.skip_unique_check return if post.acting_user&.staff? return if post.raw.blank? post.errors.add(:raw, I18n.t(:just_posted_that)) if post.matches_recent_post? end |
#validate(record) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/validators/post_validator.rb', line 4 def validate(record) presence(record) return if record.acting_user&.staged? return if record.acting_user&.admin? && Discourse.static_doc_topic_ids.include?(record.topic_id) post_body_validator(record) max_posts_validator(record) unique_post_validator(record) # These validators might cook the post or do SQL queries. # So we only run them if the post is otherwise valid. if record.errors.empty? max_mention_validator(record) (record) (record) max_links_validator(record) force_edit_last_validator(record) end end |