FIX: Old notifications didn't link to correct post after moving post

This commit is contained in:
Gerhard Schlager
2019-07-20 21:36:18 +02:00
parent 5fc5a7f5ae
commit 1235105c03
3 changed files with 84 additions and 1 deletions

View File

@@ -108,6 +108,8 @@ class PostMover
end
def create_first_post(post)
old_post_attributes = post_attributes(post)
new_post = PostCreator.create(
post.user,
raw: post.raw,
@@ -123,6 +125,7 @@ class PostMover
move_incoming_emails(post, new_post)
move_email_logs(post, new_post)
move_notifications(old_post_attributes, new_post)
PostAction.copy(post, new_post)
new_post.update_column(:reply_count, @reply_count[1] || 0)
@@ -149,11 +152,12 @@ class PostMover
update[:reply_to_user_id] = nil
end
old_post_attributes = post_attributes(post)
post.attributes = update
post.save(validate: false)
move_incoming_emails(post, post)
move_email_logs(post, post)
move_notifications(old_post_attributes, post)
DiscourseEvent.trigger(:post_moved, post, original_topic.id)
@@ -175,6 +179,31 @@ class PostMover
.update_all(post_id: new_post.id)
end
def move_notifications(old_post_attributes, new_post)
params = {
old_topic_id: old_post_attributes[:topic_id],
old_post_number: old_post_attributes[:post_number],
new_topic_id: new_post.topic_id,
new_post_number: new_post.post_number,
new_topic_title: new_post.topic.title
}
DB.exec(<<~SQL, params)
UPDATE notifications
SET topic_id = :new_topic_id,
post_number = :new_post_number,
data = (data :: JSONB ||
jsonb_strip_nulls(
jsonb_build_object(
'topic_title', CASE WHEN data :: JSONB ->> 'topic_title' IS NULL
THEN NULL
ELSE :new_topic_title END
)
)) :: JSON
WHERE topic_id = :old_topic_id AND post_number = :old_post_number
SQL
end
def update_statistics
destination_topic.update_statistics
original_topic.update_statistics
@@ -276,4 +305,11 @@ class PostMover
end
destination_topic.save!
end
def post_attributes(post)
{
topic_id: post.topic_id,
post_number: post.post_number
}
end
end