DEV: Add user_id and post_user_id to MovedPost records (#30130)

Follow-up from this commit - 9b8af0ea9f

Adds helpful data into MovedPost records for later lookup. ALSO fixes notifications for freeze_original to point to the newly created post, not the moved post.
This commit is contained in:
Mark VanLandingham
2024-12-05 17:10:32 -06:00
committed by GitHub
parent 13793a3d8e
commit 71bec686a2
4 changed files with 84 additions and 13 deletions

View File

@@ -6,6 +6,12 @@ class MovedPost < ActiveRecord::Base
belongs_to :new_topic, class_name: "Topic", foreign_key: :new_topic_id
belongs_to :new_post, class_name: "Post", foreign_key: :new_post_id
# The author of the moved post
belongs_to :posting_user, class_name: "User", foreign_key: :post_user_id
# The user who moved the post
belongs_to :user
end
# == Schema Information
@@ -23,12 +29,16 @@ end
# created_new_topic :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
# old_topic_title :string
# post_user_id :integer
# user_id :integer
#
# Indexes
#
# index_moved_posts_on_new_post_id (new_post_id)
# index_moved_posts_on_new_topic_id (new_topic_id)
# index_moved_posts_on_old_post_id (old_post_id)
# index_moved_posts_on_old_post_number (old_post_number)
# index_moved_posts_on_old_topic_id (old_topic_id)
# index_moved_posts_on_new_post_id (new_post_id)
# index_moved_posts_on_new_topic_id (new_topic_id)
# index_moved_posts_on_new_topic_id_and_post_user_id (new_topic_id,post_user_id)
# index_moved_posts_on_old_post_id (old_post_id)
# index_moved_posts_on_old_post_number (old_post_number)
# index_moved_posts_on_old_topic_id (old_topic_id)
#

View File

@@ -11,8 +11,12 @@ class PostMover
# freeze_original: :boolean - if true, the original topic will be frozen but not deleted and posts will be "copied" to topic
def initialize(original_topic, user, post_ids, move_to_pm: false, options: {})
@original_topic = original_topic
@original_topic_title = original_topic.title
@user = user
@post_ids = post_ids
# For now we store a copy of post_ids. If `freeze_original` is present, we will have new post_ids.
# When we create the new posts, we will pluck out post_ids out of this and replace with updated ids.
@post_ids_after_move = post_ids
@move_to_pm = move_to_pm
@options = options
end
@@ -307,6 +311,11 @@ class PostMover
moved_post.disable_rate_limits! if @options[:freeze_original]
moved_post.save(validate: false)
if moved_post.id != post.id
@post_ids_after_move =
@post_ids_after_move.map { |post_id| post_id == post.id ? moved_post.id : post_id }
end
DiscourseEvent.trigger(:post_moved, moved_post, original_topic.id)
# Move any links from the post to the new topic
@@ -337,6 +346,7 @@ class PostMover
old_topic_id: post.topic_id,
old_post_id: post.id,
old_post_number: post.post_number,
post_user_id: post.user_id,
new_topic_id: destination_topic.id,
new_post_number: new_post_number,
new_topic_title: destination_topic.title,
@@ -347,10 +357,12 @@ class PostMover
metadata[:new_post_id] = new_post.id
metadata[:now] = Time.zone.now
metadata[:created_new_topic] = @creating_new_topic
metadata[:old_topic_title] = @original_topic_title
metadata[:user_id] = @user.id
DB.exec(<<~SQL, metadata)
INSERT INTO moved_posts(old_topic_id, old_post_id, old_post_number, new_topic_id, new_topic_title, new_post_id, new_post_number, created_new_topic, created_at, updated_at)
VALUES (:old_topic_id, :old_post_id, :old_post_number, :new_topic_id, :new_topic_title, :new_post_id, :new_post_number, :created_new_topic, :now, :now)
INSERT INTO moved_posts(old_topic_id, old_topic_title, old_post_id, old_post_number, post_user_id, user_id, new_topic_id, new_topic_title, new_post_id, new_post_number, created_new_topic, created_at, updated_at)
VALUES (:old_topic_id, :old_topic_title, :old_post_id, :old_post_number, :post_user_id, :user_id, :new_topic_id, :new_topic_title, :new_post_id, :new_post_number, :created_new_topic, :now, :now)
SQL
end
@@ -703,7 +715,7 @@ class PostMover
def enqueue_jobs(topic)
@post_creator.enqueue_jobs if @post_creator
Jobs.enqueue(:notify_moved_posts, post_ids: post_ids, moved_by_id: user.id)
Jobs.enqueue(:notify_moved_posts, post_ids: @post_ids_after_move, moved_by_id: user.id)
Jobs.enqueue(:delete_inaccessible_notifications, topic_id: topic.id)
end