Class: PostRevisor
- Inherits:
-
Object
- Object
- PostRevisor
- Defined in:
- lib/post_revisor.rb
Defined Under Namespace
Classes: TopicChanges
Constant Summary collapse
- POST_TRACKED_FIELDS =
%w[raw cooked edit_reason user_id wiki post_type]
- USER_ACTIONS_TO_REMOVE =
[UserAction::REPLY, UserAction::RESPONSE]
Instance Attribute Summary collapse
-
#category_changed ⇒ Object
readonly
Returns the value of attribute category_changed.
-
#post_revision ⇒ Object
readonly
Returns the value of attribute post_revision.
Class Method Summary collapse
- .create_small_action_for_category_change(topic:, user:, old_category:, new_category:) ⇒ Object
- .create_small_action_for_tag_changes(topic:, user:, added_tags:, removed_tags:) ⇒ Object
- .tag_list_to_raw(tag_list) ⇒ Object
- .tags_changed_raw(added:, removed:) ⇒ Object
- .track_and_revise(topic_changes, field, attribute) ⇒ Object
- .track_topic_field(field, &block) ⇒ Object
- .tracked_topic_fields ⇒ Object
Instance Method Summary collapse
- #advance_draft_sequence ⇒ Object
- #alert_users ⇒ Object
- #bump_topic ⇒ Object
- #bypass_bump? ⇒ Boolean
- #bypass_rate_limiter? ⇒ Boolean
- #cached_original_cooked ⇒ Object
- #cached_original_raw ⇒ Object
- #cleanup_whitespaces(raw) ⇒ Object
- #create_or_update_revision ⇒ Object
- #create_revision ⇒ Object
- #diff_size(before, after) ⇒ Object
- #edit_reason_specified? ⇒ Boolean
- #edited_by_another_user? ⇒ Boolean
- #editing_a_flagged_and_hidden_post? ⇒ Boolean
- #flagged? ⇒ Boolean
- #force_new_version? ⇒ Boolean
- #grace_period_edit? ⇒ Boolean
- #grant_badge ⇒ Object
- #guardian ⇒ Object
-
#initialize(post, topic = post.topic) ⇒ PostRevisor
constructor
A new instance of PostRevisor.
- #is_last_post? ⇒ Boolean
- #only_hidden_tags_changed? ⇒ Boolean
- #original_cooked=(val) ⇒ Object
- #original_cooked_key ⇒ Object
- #original_raw ⇒ Object
- #original_raw=(val) ⇒ Object
- #original_raw_key ⇒ Object
- #owner_changed? ⇒ Boolean
- #perform_edit ⇒ Object
- #plugin_callbacks ⇒ Object
- #post_changed? ⇒ Boolean
- #post_changes ⇒ Object
- #post_process_post ⇒ Object
- #publish_changes ⇒ Object
- #raw_changed? ⇒ Boolean
- #remove_flags_and_unhide_post ⇒ Object
- #reviewable_content_changed? ⇒ Boolean
- #revise ⇒ Object
-
#revise!(editor, fields, opts = {}) ⇒ Object
AVAILABLE OPTIONS: - revised_at: changes the date of the revision - force_new_version: bypass grace period edit window - bypass_rate_limiter: - bypass_bump: do not bump the topic, even if last post - skip_validations: ask ActiveRecord to skip validations - skip_revision: do not create a new PostRevision record - skip_staff_log: skip creating an entry in the staff action log.
- #revise_and_create_new_version ⇒ Object
- #revise_post ⇒ Object
- #revise_topic ⇒ Object
- #self_edit? ⇒ Boolean
- #should_create_new_version? ⇒ Boolean
- #should_revise? ⇒ Boolean
- #successfully_saved_post_and_topic ⇒ Object
- #topic_changed? ⇒ Boolean
- #topic_diff ⇒ Object
- #topic_title_changed? ⇒ Boolean
- #update_category_description ⇒ Object
- #update_post ⇒ Object
- #update_revision ⇒ Object
- #update_topic ⇒ Object
- #update_topic_excerpt ⇒ Object
Constructor Details
#initialize(post, topic = post.topic) ⇒ PostRevisor
Returns a new instance of PostRevisor.
47 48 49 50 51 52 53 |
# File 'lib/post_revisor.rb', line 47 def initialize(post, topic = post.topic) @post = post @topic = topic # Make sure we have only one Topic instance post.topic = topic end |
Instance Attribute Details
#category_changed ⇒ Object (readonly)
Returns the value of attribute category_changed.
45 46 47 |
# File 'lib/post_revisor.rb', line 45 def category_changed @category_changed end |
#post_revision ⇒ Object (readonly)
Returns the value of attribute post_revision.
45 46 47 |
# File 'lib/post_revisor.rb', line 45 def post_revision @post_revision end |
Class Method Details
.create_small_action_for_category_change(topic:, user:, old_category:, new_category:) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/post_revisor.rb', line 156 def self.create_small_action_for_category_change(topic:, user:, old_category:, new_category:) if !old_category || !new_category || !SiteSetting.create_post_for_category_and_tag_changes || SiteSetting.whispers_allowed_groups.blank? return end topic.add_moderator_post( user, I18n.t( "topic_category_changed", from: "##{old_category.slug_ref}", to: "##{new_category.slug_ref}", ), post_type: Post.types[:whisper], action_code: "category_changed", ) end |
.create_small_action_for_tag_changes(topic:, user:, added_tags:, removed_tags:) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/post_revisor.rb', line 174 def self.create_small_action_for_tag_changes(topic:, user:, added_tags:, removed_tags:) if !SiteSetting.create_post_for_category_and_tag_changes || SiteSetting.whispers_allowed_groups.blank? return end topic.add_moderator_post( user, (added: , removed: ), post_type: Post.types[:whisper], action_code: "tags_changed", custom_fields: { tags_added: , tags_removed: , }, ) end |
.tag_list_to_raw(tag_list) ⇒ Object
206 207 208 |
# File 'lib/post_revisor.rb', line 206 def self.tag_list_to_raw(tag_list) tag_list.sort.map { |tag_name| "##{tag_name}" }.join(", ") end |
.tags_changed_raw(added:, removed:) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/post_revisor.rb', line 192 def self.(added:, removed:) if removed.present? && added.present? I18n.t( "topic_tag_changed.added_and_removed", added: tag_list_to_raw(added), removed: tag_list_to_raw(removed), ) elsif added.present? I18n.t("topic_tag_changed.added", added: tag_list_to_raw(added)) elsif removed.present? I18n.t("topic_tag_changed.removed", removed: tag_list_to_raw(removed)) end end |
.track_and_revise(topic_changes, field, attribute) ⇒ Object
69 70 71 72 |
# File 'lib/post_revisor.rb', line 69 def self.track_and_revise(topic_changes, field, attribute) topic_changes.record_change(field, topic_changes.topic.public_send(field), attribute) topic_changes.topic.public_send("#{field}=", attribute) end |
.track_topic_field(field, &block) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/post_revisor.rb', line 60 def self.track_topic_field(field, &block) tracked_topic_fields[field] = block # Define it in the serializer unless it already has been defined if PostRevisionSerializer.instance_methods(false).exclude?("#{field}_changes".to_sym) PostRevisionSerializer.add_compared_field(field) end end |
.tracked_topic_fields ⇒ Object
55 56 57 58 |
# File 'lib/post_revisor.rb', line 55 def self.tracked_topic_fields @@tracked_topic_fields ||= {} @@tracked_topic_fields end |
Instance Method Details
#advance_draft_sequence ⇒ Object
713 714 715 |
# File 'lib/post_revisor.rb', line 713 def advance_draft_sequence @post.advance_draft_sequence end |
#alert_users ⇒ Object
723 724 725 726 |
# File 'lib/post_revisor.rb', line 723 def alert_users return if @editor.id == Discourse::SYSTEM_USER_ID Jobs.enqueue(:post_alert, post_id: @post.id) end |
#bump_topic ⇒ Object
652 653 654 655 656 657 658 |
# File 'lib/post_revisor.rb', line 652 def bump_topic return if bypass_bump? || !is_last_post? @topic.update_column(:bumped_at, Time.now) TopicTrackingState.publish_muted(@topic) TopicTrackingState.publish_unmuted(@topic) TopicTrackingState.publish_latest(@topic) end |
#bypass_bump? ⇒ Boolean
660 661 662 663 |
# File 'lib/post_revisor.rb', line 660 def bypass_bump? !@post_successfully_saved || @topic_changes.errored? || @opts[:bypass_bump] == true || @post.whisper? || end |
#bypass_rate_limiter? ⇒ Boolean
648 649 650 |
# File 'lib/post_revisor.rb', line 648 def bypass_rate_limiter? @opts[:bypass_rate_limiter] == true end |
#cached_original_cooked ⇒ Object
397 398 399 |
# File 'lib/post_revisor.rb', line 397 def cached_original_cooked @cached_original_cooked ||= Discourse.redis.get(original_cooked_key) end |
#cached_original_raw ⇒ Object
393 394 395 |
# File 'lib/post_revisor.rb', line 393 def cached_original_raw @cached_original_raw ||= Discourse.redis.get(original_raw_key) end |
#cleanup_whitespaces(raw) ⇒ Object
336 337 338 |
# File 'lib/post_revisor.rb', line 336 def cleanup_whitespaces(raw) raw.present? ? TextCleaner.normalize_whitespaces(raw).rstrip : "" end |
#create_or_update_revision ⇒ Object
578 579 580 581 582 583 |
# File 'lib/post_revisor.rb', line 578 def create_or_update_revision return if @skip_revision # don't create an empty revision if something failed return unless successfully_saved_post_and_topic @version_changed ? create_revision : update_revision end |
#create_revision ⇒ Object
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/post_revisor.rb', line 585 def create_revision modifications = post_changes.merge(@topic_changes.diff) modifications["raw"][0] = cached_original_raw || modifications["raw"][0] if modifications["raw"] if modifications["cooked"] modifications["cooked"][0] = cached_original_cooked || modifications["cooked"][0] end @post_revision = PostRevision.create!( user_id: @post.last_editor_id, post_id: @post.id, number: @post.version, modifications: modifications, hidden: , ) end |
#diff_size(before, after) ⇒ Object
415 416 417 418 419 420 |
# File 'lib/post_revisor.rb', line 415 def diff_size(before, after) @diff_size ||= begin ONPDiff.new(before, after).short_diff.sum { |str, type| type == :common ? 0 : str.size } end end |
#edit_reason_specified? ⇒ Boolean
373 374 375 |
# File 'lib/post_revisor.rb', line 373 def edit_reason_specified? @fields[:edit_reason].present? && @fields[:edit_reason] != @post.edit_reason end |
#edited_by_another_user? ⇒ Boolean
381 382 383 |
# File 'lib/post_revisor.rb', line 381 def edited_by_another_user? @post.last_editor_id != @editor.id end |
#editing_a_flagged_and_hidden_post? ⇒ Boolean
559 560 561 562 |
# File 'lib/post_revisor.rb', line 559 def editing_a_flagged_and_hidden_post? self_edit? && @post.hidden && @post.hidden_reason_id == Post.hidden_reasons[:flag_threshold_reached] end |
#flagged? ⇒ Boolean
377 378 379 |
# File 'lib/post_revisor.rb', line 377 def flagged? @post.is_flagged? end |
#force_new_version? ⇒ Boolean
444 445 446 |
# File 'lib/post_revisor.rb', line 444 def force_new_version? @opts[:force_new_version] == true end |
#grace_period_edit? ⇒ Boolean
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/post_revisor.rb', line 422 def grace_period_edit? return false if (@revised_at - @last_version_at) > SiteSetting.editing_grace_period.to_i if new_raw = @fields[:raw] max_diff = SiteSetting.editing_grace_period_max_diff.to_i if @editor.staff? || (@editor.trust_level > 1) max_diff = SiteSetting.editing_grace_period_max_diff_high_trust.to_i end if (original_raw.size - new_raw.size).abs > max_diff || diff_size(original_raw, new_raw) > max_diff return false end end true end |
#grant_badge ⇒ Object
741 742 743 |
# File 'lib/post_revisor.rb', line 741 def grant_badge BadgeGranter.queue_badge_grant(Badge::Trigger::PostRevision, post: @post) end |
#guardian ⇒ Object
749 750 751 |
# File 'lib/post_revisor.rb', line 749 def guardian @guardian ||= Guardian.new(@editor) end |
#is_last_post? ⇒ Boolean
678 679 680 |
# File 'lib/post_revisor.rb', line 678 def is_last_post? !Post.where(topic_id: @topic.id).where("post_number > ?", @post.post_number).exists? end |
#only_hidden_tags_changed? ⇒ Boolean
665 666 667 668 669 670 671 672 673 674 675 676 |
# File 'lib/post_revisor.rb', line 665 def return false if (hidden_tag_names = DiscourseTagging.hidden_tag_names).blank? modifications = post_changes.merge(@topic_changes.diff) if modifications.keys.size == 1 && ( = modifications["tags"]).present? a, b = [0] || [], [1] || [] = ((a + b) - (a & b)).map(&:presence).compact return true if ( - hidden_tag_names).empty? end false end |
#original_cooked=(val) ⇒ Object
410 411 412 413 |
# File 'lib/post_revisor.rb', line 410 def original_cooked=(val) @cached_original_cooked = val Discourse.redis.setex(original_cooked_key, SiteSetting.editing_grace_period + 1, val) end |
#original_cooked_key ⇒ Object
389 390 391 |
# File 'lib/post_revisor.rb', line 389 def original_cooked_key "original_cooked_#{(@last_version_at.to_f * 1000).to_i}#{@post.id}" end |
#original_raw ⇒ Object
401 402 403 |
# File 'lib/post_revisor.rb', line 401 def original_raw cached_original_raw || @post.raw end |
#original_raw=(val) ⇒ Object
405 406 407 408 |
# File 'lib/post_revisor.rb', line 405 def original_raw=(val) @cached_original_raw = val Discourse.redis.setex(original_raw_key, SiteSetting.editing_grace_period + 1, val) end |
#original_raw_key ⇒ Object
385 386 387 |
# File 'lib/post_revisor.rb', line 385 def original_raw_key "original_raw_#{(@last_version_at.to_f * 1000).to_i}#{@post.id}" end |
#owner_changed? ⇒ Boolean
440 441 442 |
# File 'lib/post_revisor.rb', line 440 def owner_changed? @fields.has_key?(:user_id) && @fields[:user_id] != @post.user_id end |
#perform_edit ⇒ Object
643 644 645 646 |
# File 'lib/post_revisor.rb', line 643 def perform_edit return if bypass_rate_limiter? EditRateLimiter.new(@editor).performed! end |
#plugin_callbacks ⇒ Object
682 683 684 685 |
# File 'lib/post_revisor.rb', line 682 def plugin_callbacks DiscourseEvent.trigger(:before_edit_post, @post, @fields) DiscourseEvent.trigger(:validate_post, @post) end |
#post_changed? ⇒ Boolean
344 345 346 347 348 349 |
# File 'lib/post_revisor.rb', line 344 def post_changed? POST_TRACKED_FIELDS.each do |field| return true if @fields.has_key?(field) && @fields[field] != @post.public_send(field) end false end |
#post_changes ⇒ Object
635 636 637 |
# File 'lib/post_revisor.rb', line 635 def post_changes @post.previous_changes.slice(*POST_TRACKED_FIELDS) end |
#post_process_post ⇒ Object
717 718 719 720 721 |
# File 'lib/post_revisor.rb', line 717 def post_process_post @post.invalidate_oneboxes = true @post.trigger_post_process DiscourseEvent.trigger(:post_edited, @post, self.topic_changed?, self) end |
#publish_changes ⇒ Object
728 729 730 731 732 733 734 735 736 737 738 739 |
# File 'lib/post_revisor.rb', line 728 def publish_changes = if !@topic_changes.diff.empty? && !@topic_changes.errored? { reload_topic: true } else {} end DiscourseEvent.trigger(:before_post_publish_changes, post_changes, @topic_changes, ) @post.publish_change_to_clients!(:revised, ) end |
#raw_changed? ⇒ Boolean
753 754 755 |
# File 'lib/post_revisor.rb', line 753 def raw_changed? @fields.has_key?(:raw) && @fields[:raw] != cached_original_raw && @post_successfully_saved end |
#remove_flags_and_unhide_post ⇒ Object
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
# File 'lib/post_revisor.rb', line 542 def remove_flags_and_unhide_post return if @opts[:deleting_post] return unless editing_a_flagged_and_hidden_post? flaggers = [] @post .post_actions .where(post_action_type_id: PostActionType..values) .each do |action| flaggers << action.user if action.user action.remove_act!(Discourse.system_user) end @post.unhide! PostActionNotifier.after_post_unhide(@post, flaggers) end |
#reviewable_content_changed? ⇒ Boolean
762 763 764 |
# File 'lib/post_revisor.rb', line 762 def reviewable_content_changed? raw_changed? || topic_title_changed? end |
#revise ⇒ Object
459 460 461 462 463 464 |
# File 'lib/post_revisor.rb', line 459 def revise update_post update_topic if topic_changed? create_or_update_revision remove_flags_and_unhide_post end |
#revise!(editor, fields, opts = {}) ⇒ Object
AVAILABLE OPTIONS:
-
revised_at: changes the date of the revision
-
force_new_version: bypass grace period edit window
-
bypass_rate_limiter:
-
bypass_bump: do not bump the topic, even if last post
-
skip_validations: ask ActiveRecord to skip validations
-
skip_revision: do not create a new PostRevision record
-
skip_staff_log: skip creating an entry in the staff action log
218 219 220 221 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 254 255 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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/post_revisor.rb', line 218 def revise!(editor, fields, opts = {}) @editor = editor @fields = fields.with_indifferent_access @opts = opts @topic_changes = TopicChanges.new(@topic, editor) # some normalization @fields[:raw] = cleanup_whitespaces(@fields[:raw]) if @fields.has_key?(:raw) @fields[:user_id] = @fields[:user_id].to_i if @fields.has_key?(:user_id) @fields[:category_id] = @fields[:category_id].to_i if @fields.has_key?(:category_id) # always reset edit_reason unless provided, do not set to nil else # previous reasons are lost @fields.delete(:edit_reason) if @fields[:edit_reason].blank? Post.plugin_permitted_update_params.each do |field, val| val[:handler].call(@post, @fields[field]) if @fields.key?(field) && val[:plugin].enabled? end if !should_revise? # the draft sequence is advanced here to handle the edge case where a # user opens the composer to edit a post and makes some changes (which # saves a draft), but then un-does the changes and clicks save. In this # case, should_revise? returns false because nothing has really changed # in the post, but we want to get rid of the draft so we advance the # sequence. advance_draft_sequence if !opts[:keep_existing_draft] return false end @post.acting_user = @editor @topic.acting_user = @editor @revised_at = @opts[:revised_at] || Time.now @last_version_at = @post.last_version_at || Time.now if guardian.affected_by_slow_mode?(@topic) && !grace_period_edit? && SiteSetting.slow_mode_prevents_editing @post.errors.add(:base, I18n.t("cannot_edit_on_slow_mode")) return false end @version_changed = false @post_successfully_saved = true @validate_post = true @validate_post = @opts[:validate_post] if @opts.has_key?(:validate_post) @validate_post = !@opts[:skip_validations] if @opts.has_key?(:skip_validations) @validate_topic = true @validate_topic = @opts[:validate_topic] if @opts.has_key?(:validate_topic) @validate_topic = !@opts[:skip_validations] if @opts.has_key?(:skip_validations) @skip_revision = false @skip_revision = @opts[:skip_revision] if @opts.has_key?(:skip_revision) @post.incoming_email&.update(imap_sync: true) if @post.incoming_email&.imap_uid old_raw = @post.raw Post.transaction do revise_post yield if block_given? # TODO: these callbacks are being called in a transaction # it is kind of odd, because the callback is called "before_edit" # but the post is already edited at this point # Trouble is that much of the logic of should I edit? is deeper # down so yanking this in front of the transaction will lead to # false positive. plugin_callbacks revise_topic advance_draft_sequence if !opts[:keep_existing_draft] end # bail out if the post or topic failed to save return false if !successfully_saved_post_and_topic # Lock the post by default if the appropriate setting is true if ( SiteSetting.staff_edit_locks_post? && !@post.wiki? && @fields.has_key?("raw") && @editor.staff? && @editor != Discourse.system_user && !@post.user&.staff? ) PostLocker.new(@post, @editor).lock end # We log staff/group moderator edits to posts if ( ( @editor.staff? || ( @post.is_category_description? && guardian.can_edit_category_description?(@post.topic.category) ) ) && @editor.id != @post.user_id && @fields.has_key?("raw") && !@opts[:skip_staff_log] ) StaffActionLogger.new(@editor).log_post_edit(@post, old_raw: old_raw) end # WARNING: do not pull this into the transaction # it can fire events in sidekiq before the post is done saving # leading to corrupt state QuotedPost.extract_from(@post) TopicLink.extract_from(@post) Topic.reset_highest(@topic.id) post_process_post alert_users publish_changes grant_badge ReviewablePost.queue_for_review_if_possible(@post, @editor) if should_create_new_version? successfully_saved_post_and_topic end |
#revise_and_create_new_version ⇒ Object
448 449 450 451 452 453 454 455 456 457 |
# File 'lib/post_revisor.rb', line 448 def revise_and_create_new_version @version_changed = true @post.version += 1 @post.public_version += 1 @post.last_version_at = @revised_at revise perform_edit bump_topic end |
#revise_post ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/post_revisor.rb', line 355 def revise_post if should_create_new_version? revise_and_create_new_version else unless cached_original_raw self.original_raw = @post.raw self.original_cooked = @post.cooked end revise end end |
#revise_topic ⇒ Object
687 688 689 690 691 692 |
# File 'lib/post_revisor.rb', line 687 def revise_topic return unless @post.is_first_post? update_topic_excerpt update_category_description end |
#self_edit? ⇒ Boolean
538 539 540 |
# File 'lib/post_revisor.rb', line 538 def self_edit? @editor == @post.user end |
#should_create_new_version? ⇒ Boolean
367 368 369 370 371 |
# File 'lib/post_revisor.rb', line 367 def should_create_new_version? return false if @skip_revision edited_by_another_user? || flagged? || !grace_period_edit? || owner_changed? || force_new_version? || edit_reason_specified? end |
#should_revise? ⇒ Boolean
340 341 342 |
# File 'lib/post_revisor.rb', line 340 def should_revise? post_changed? || topic_changed? end |
#successfully_saved_post_and_topic ⇒ Object
745 746 747 |
# File 'lib/post_revisor.rb', line 745 def successfully_saved_post_and_topic @post_successfully_saved && !@topic_changes.errored? end |
#topic_changed? ⇒ Boolean
351 352 353 |
# File 'lib/post_revisor.rb', line 351 def topic_changed? PostRevisor.tracked_topic_fields.keys.any? { |f| @fields.has_key?(f) } end |
#topic_diff ⇒ Object
639 640 641 |
# File 'lib/post_revisor.rb', line 639 def topic_diff @topic_changes.diff end |
#topic_title_changed? ⇒ Boolean
757 758 759 760 |
# File 'lib/post_revisor.rb', line 757 def topic_title_changed? topic_changed? && @fields.has_key?(:title) && topic_diff.has_key?(:title) && !@topic_changes.errored? end |
#update_category_description ⇒ Object
698 699 700 701 702 703 704 705 706 707 708 709 710 711 |
# File 'lib/post_revisor.rb', line 698 def update_category_description return unless category = Category.find_by(topic_id: @topic.id) doc = Nokogiri::HTML5.fragment(@post.cooked) doc.css("img").remove if html = doc.css("p").first&.inner_html&.strip new_description = html unless html.starts_with?(Category.post_template[0..50]) category.update_column(:description, new_description) @category_changed = category else @post.errors.add(:base, I18n.t("category.errors.description_incomplete")) end end |
#update_post ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/post_revisor.rb', line 468 def update_post if @fields.has_key?("user_id") && @fields["user_id"] != @post.user_id && @post.user_id != nil prev_owner = User.find(@post.user_id) new_owner = User.find(@fields["user_id"]) UserAction .where(target_post_id: @post.id) .where(user_id: prev_owner.id) .where(action_type: USER_ACTIONS_TO_REMOVE) .update_all(user_id: new_owner.id) if @post.post_number == 1 UserAction .where(target_topic_id: @post.topic_id) .where(user_id: prev_owner.id) .where(action_type: UserAction::NEW_TOPIC) .update_all(user_id: new_owner.id) end end POST_TRACKED_FIELDS.each do |field| @post.public_send("#{field}=", @fields[field]) if @fields.has_key?(field) end @post.edit_reason = @fields[:edit_reason] if should_create_new_version? @post.last_editor_id = @editor.id @post.word_count = @fields[:raw].scan(/[[:word:]]+/).size if @fields.has_key?(:raw) @post.self_edits += 1 if self_edit? @post.extract_quoted_post_numbers @post_successfully_saved = @post.save(validate: @validate_post) @post.link_post_uploads @post.save_reply_relationships @editor.increment_post_edits_count if @post_successfully_saved # post owner changed if prev_owner && new_owner && prev_owner != new_owner likes = UserAction .where(target_post_id: @post.id) .where(user_id: prev_owner.id) .where(action_type: UserAction::WAS_LIKED) .update_all(user_id: new_owner.id) = @topic. prev_owner_user_stat = prev_owner.user_stat unless UserStatCountUpdater.decrement!(@post, user_stat: prev_owner_user_stat) if !@post.trashed? prev_owner_user_stat.likes_received -= likes end if @post.created_at == prev_owner.user_stat.first_post_created_at prev_owner_user_stat.update!( first_post_created_at: prev_owner.posts.order("created_at ASC").first.try(:created_at), ) end new_owner_user_stat = new_owner.user_stat unless UserStatCountUpdater.increment!(@post, user_stat: new_owner_user_stat) if !@post.trashed? new_owner_user_stat.likes_received += likes end new_owner_user_stat.save! end end |
#update_revision ⇒ Object
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 |
# File 'lib/post_revisor.rb', line 604 def update_revision return unless revision = PostRevision.find_by(post_id: @post.id, number: @post.version) revision.user_id = @post.last_editor_id modifications = post_changes.merge(@topic_changes.diff) modifications.each_key do |field| if revision.modifications.has_key?(field) old_value = revision.modifications[field][0] new_value = modifications[field][1] if old_value.to_s != new_value.to_s revision.modifications[field] = [old_value, new_value] else revision.modifications.delete(field) end else revision.modifications[field] = modifications[field] end end # should probably do this before saving the post! if revision.modifications.empty? revision.destroy @post.last_editor_id = PostRevision.where(post_id: @post.id).order(number: :desc).pick(:user_id) || @post.user_id @post.version -= 1 @post.public_version -= 1 @post.save(validate: @validate_post) else revision.save end end |
#update_topic ⇒ Object
564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/post_revisor.rb', line 564 def update_topic Topic.transaction do PostRevisor.tracked_topic_fields.each do |f, cb| if !@topic_changes.errored? && @fields.has_key?(f) cb.call(@topic_changes, @fields[f], @fields) end end unless @topic_changes.errored? @topic_changes.check_result(@topic.save(validate: @validate_topic)) end end end |
#update_topic_excerpt ⇒ Object
694 695 696 |
# File 'lib/post_revisor.rb', line 694 def update_topic_excerpt @topic.update_excerpt(@post.excerpt_for_topic) end |