Remove context_notifications when unpublishing all posts (#18287)

* Remove context_notifications when unpublishing all posts

* Fixed typo

Co-authored-by: Mac Siri <krairit.siri@gmail.com>

* Fixed typo

Co-authored-by: Mac Siri <krairit.siri@gmail.com>

Co-authored-by: Mac Siri <krairit.siri@gmail.com>
This commit is contained in:
Anna Buianova 2022-08-10 09:03:52 +03:00 committed by GitHub
parent cfc0406250
commit 44a401643c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 11 deletions

View file

@ -22,6 +22,9 @@ module Moderator
Notification.remove_all_by_action_without_delay(notifiable_ids: article.id,
notifiable_type: "Article",
action: "Published")
ContextNotification.delete_by(context_id: article.id, context_type: "Article", action: "Published")
return unless article.comments.exists?
Notification.remove_all(notifiable_ids: article.comments.ids, notifiable_type: "Comment")

View file

@ -2,17 +2,37 @@ require "rails_helper"
RSpec.describe Moderator::UnpublishAllArticlesWorker, type: :worker do
include_examples "#enqueues_on_correct_queue", "medium_priority", 1
it "unpublishes all articles" do
user = create(:user)
create_list(:article, 3, user: user)
expect { described_class.new.perform(user.id) }.to change { user.articles.published.size }.from(3).to(0)
end
it "applies proper frontmatter", :aggregate_failures do
user = create(:user)
create_list(:article, 3, user: user)
described_class.new.perform(user.id)
expect(Article.last.body_markdown).to include("published: false")
expect(Article.last.body_markdown).not_to include("published: true")
context "when unpublishing" do
let!(:user) { create(:user) }
let!(:articles) { create_list(:article, 3, user: user) }
it "unpublishes all articles" do
expect { described_class.new.perform(user.id) }.to change { user.articles.published.size }.from(3).to(0)
end
it "applies proper frontmatter", :aggregate_failures do
described_class.new.perform(user.id)
expect(Article.last.body_markdown).to include("published: false")
expect(Article.last.body_markdown).not_to include("published: true")
end
it "destroys the pre-existing notifications" do
allow(Notification).to receive(:remove_all_by_action_without_delay).and_call_original
described_class.new.perform(user.id)
articles.map(&:id).each do |id|
attrs = { notifiable_ids: id, notifiable_type: "Article", action: "Published" }
expect(Notification).to have_received(:remove_all_by_action_without_delay).with(attrs)
end
end
it "destroys the pre-existing context notifications" do
articles.each do |article|
create(:context_notification, context: article, action: "Published")
end
expect do
described_class.new.perform(user.id)
end.to change(ContextNotification, :count).by(-3)
end
end
end