Skip to content

Commit

Permalink
FIX: When moving posts between threads, ensure only relevant timings …
Browse files Browse the repository at this point in the history
…are moved (discourse#30176)

Co-authored-by: Martin Brennan <[email protected]>
  • Loading branch information
pento and martin-brennan authored Dec 10, 2024
1 parent eeb01ea commit 19c3f7d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/models/post_mover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,9 @@ def delete_invalid_post_timings
end

def move_post_timings
DB.exec <<~SQL
params = { post_ids: @post_ids_after_move }

DB.exec(<<~SQL, params)
UPDATE post_timings pt
SET topic_id = mp.new_topic_id,
post_number = mp.new_post_number
Expand All @@ -513,6 +515,7 @@ def move_post_timings
AND pt.post_number = mp.old_post_number
AND mp.old_post_id = mp.new_post_id
AND mp.old_topic_id <> mp.new_topic_id
AND mp.new_post_id IN (:post_ids)
SQL
end

Expand Down
41 changes: 41 additions & 0 deletions spec/models/post_mover_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,47 @@ def create_topic_user(user, topic, opts = {})
expect(TopicUser.find_by(topic: destination_topic, user: user).liked).to eq(true)
end

it "allows moving posts from multiple topics into one existing topic" do
dest_topic = Fabricate(:topic, user: user, created_at: 5.hours.ago)
Fabricate(:post, topic: dest_topic, created_at: 5.hours.ago)
create_post_timing(dest_topic.first_post, user, 500)

source_1_topic = Fabricate(:topic, user: user, created_at: 4.hours.ago)
Fabricate(:post, topic: source_1_topic, user: user, created_at: 4.hours.ago)
create_post_timing(source_1_topic.first_post, user, 500)
source_1_post =
Fabricate(:post, topic: source_1_topic, user: user, created_at: 3.hours.ago)
create_post_timing(source_1_topic.posts.second, user, 500)

source_2_topic = Fabricate(:topic, user: user, created_at: 2.hours.ago)
Fabricate(:post, topic: source_2_topic, user: user, created_at: 2.hours.ago)
create_post_timing(source_2_topic.first_post, user, 500)
source_2_post =
Fabricate(:post, topic: source_2_topic, user: user, created_at: 1.hours.ago)
create_post_timing(source_2_topic.posts.second, user, 500)

moved_to =
source_2_topic.move_posts(
user,
[source_2_post.id],
destination_topic_id: dest_topic.id,
chronological_order: true,
)

expect(moved_to).to be_present
dest_topic.reload

moved_to_too =
source_1_topic.move_posts(
user,
[source_1_post.id],
destination_topic_id: dest_topic.id,
chronological_order: true,
)

expect(moved_to_too).to be_present
end

context "with read state and other stats per user" do
def create_topic_user(user, topic, opts = {})
notification_level = opts.delete(:notification_level) || :regular
Expand Down

0 comments on commit 19c3f7d

Please sign in to comment.