mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Make sure topic_user.bookmarked is synced in more places (#13383)
When we call Bookmark.cleanup! we want to make sure that topic_user.bookmarked is updated for topics linked to the bookmarks that were deleted. Also when PostDestroyer calls destroy and recover. We have a job for this already -- SyncTopicUserBookmarked -- so we just utilize that.
This commit is contained in:
@@ -241,6 +241,13 @@ describe PostDestroyer do
|
||||
expect(UserAction.where(target_topic_id: post.topic_id, action_type: UserAction::NEW_TOPIC).count).to eq(1)
|
||||
expect(UserAction.where(target_topic_id: post.topic_id, action_type: UserAction::REPLY).count).to eq(1)
|
||||
end
|
||||
|
||||
it "runs the SyncTopicUserBookmarked for the topic that the post is in so topic_users.bookmarked is correct" do
|
||||
PostDestroyer.new(@user, @reply).destroy
|
||||
expect_enqueued_with(job: :sync_topic_user_bookmarked, args: { topic_id: @reply.topic_id }) do
|
||||
PostDestroyer.new(@user, @reply.reload).recover
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "recovered by admin" do
|
||||
@@ -465,6 +472,12 @@ describe PostDestroyer do
|
||||
expect(post.raw).to eq(I18n.t('js.post.deleted_by_author_simple'))
|
||||
end
|
||||
|
||||
it "runs the SyncTopicUserBookmarked for the topic that the post is in so topic_users.bookmarked is correct" do
|
||||
post2 = create_post
|
||||
PostDestroyer.new(post2.user, post2).destroy
|
||||
expect_job_enqueued(job: :sync_topic_user_bookmarked, args: { topic_id: post2.topic_id })
|
||||
end
|
||||
|
||||
context "as a moderator" do
|
||||
it "deletes the post" do
|
||||
author = post.user
|
||||
|
||||
@@ -27,6 +27,24 @@ RSpec.describe Jobs::SyncTopicUserBookmarked do
|
||||
expect(tu5.reload.bookmarked).to eq(false)
|
||||
end
|
||||
|
||||
it "does not consider topic as bookmarked if the bookmarked post is deleted" do
|
||||
topic = Fabricate(:topic)
|
||||
post1 = Fabricate(:post, topic: topic)
|
||||
|
||||
tu1 = Fabricate(:topic_user, topic: topic, bookmarked: false)
|
||||
tu2 = Fabricate(:topic_user, topic: topic, bookmarked: true)
|
||||
|
||||
Fabricate(:bookmark, user: tu1.user, topic: topic, post: post1)
|
||||
Fabricate(:bookmark, user: tu2.user, topic: topic, post: post1)
|
||||
|
||||
post1.trash!
|
||||
|
||||
subject.execute(topic_id: topic.id)
|
||||
|
||||
expect(tu1.reload.bookmarked).to eq(false)
|
||||
expect(tu2.reload.bookmarked).to eq(false)
|
||||
end
|
||||
|
||||
it "works when no topic id is provided (runs for all topics)" do
|
||||
topic = Fabricate(:topic)
|
||||
Fabricate(:post, topic: topic)
|
||||
|
||||
@@ -15,6 +15,26 @@ describe Bookmark do
|
||||
expect(Bookmark.find_by(id: bookmark2.id)).to eq(bookmark2)
|
||||
end
|
||||
|
||||
it "runs a SyncTopicUserBookmarked job for all deleted bookmark unique topics to make sure topic_user.bookmarked is in sync" do
|
||||
post = Fabricate(:post)
|
||||
post2 = Fabricate(:post)
|
||||
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
|
||||
bookmark2 = Fabricate(:bookmark, post: Fabricate(:post, topic: post.topic))
|
||||
bookmark3 = Fabricate(:bookmark, post: post2, topic: post2.topic)
|
||||
bookmark4 = Fabricate(:bookmark, post: post2, topic: post2.topic)
|
||||
post.trash!
|
||||
post.update(deleted_at: 4.days.ago)
|
||||
post2.trash!
|
||||
post2.update(deleted_at: 4.days.ago)
|
||||
Bookmark.cleanup!
|
||||
expect(Bookmark.find_by(id: bookmark.id)).to eq(nil)
|
||||
expect(Bookmark.find_by(id: bookmark2.id)).to eq(bookmark2)
|
||||
expect(Bookmark.find_by(id: bookmark3.id)).to eq(nil)
|
||||
expect(Bookmark.find_by(id: bookmark4.id)).to eq(nil)
|
||||
expect_job_enqueued(job: :sync_topic_user_bookmarked, args: { topic_id: post.topic_id })
|
||||
expect_job_enqueued(job: :sync_topic_user_bookmarked, args: { topic_id: post2.topic_id })
|
||||
end
|
||||
|
||||
it "deletes bookmarks attached to a deleted topic which has been deleted for > 3 days" do
|
||||
post = Fabricate(:post)
|
||||
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
|
||||
|
||||
Reference in New Issue
Block a user