Fixed sending slack notifications for published articles (#18272)
This commit is contained in:
parent
3403dc9bfd
commit
a00468633f
4 changed files with 12 additions and 52 deletions
|
|
@ -180,7 +180,6 @@ class Article < ApplicationRecord
|
|||
before_save :fetch_video_duration
|
||||
before_save :set_caches
|
||||
before_create :create_password
|
||||
after_update :notify_slack_channel_about_publication, if: -> { published && saved_change_to_published? }
|
||||
before_destroy :before_destroy_actions, prepend: true
|
||||
|
||||
after_save :create_conditional_autovomits
|
||||
|
|
@ -946,10 +945,6 @@ class Article < ApplicationRecord
|
|||
collection.touch if collection && previous_changes.present?
|
||||
end
|
||||
|
||||
def notify_slack_channel_about_publication
|
||||
Slack::Messengers::ArticlePublished.call(article: self)
|
||||
end
|
||||
|
||||
def enrich_image_attributes
|
||||
return unless saved_change_to_attribute?(:processed_html)
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ module Slack
|
|||
end
|
||||
|
||||
def call
|
||||
return unless article.published && article.published_at > 30.seconds.ago
|
||||
return unless article.published && article.published_at > 10.minutes.ago
|
||||
|
||||
message = I18n.t(
|
||||
"services.slack.messengers.article_published.body",
|
||||
|
|
|
|||
|
|
@ -1165,51 +1165,6 @@ RSpec.describe Article, type: :model do
|
|||
end
|
||||
end
|
||||
|
||||
describe "slack messages" do
|
||||
before do
|
||||
# making sure there are no other enqueued jobs from other tests
|
||||
sidekiq_perform_enqueued_jobs(only: Slack::Messengers::Worker)
|
||||
end
|
||||
|
||||
# slack messages will be queued in a publish worker
|
||||
it "doesn't queue a slack message to be sent for a new published article" do
|
||||
sidekiq_assert_no_enqueued_jobs(only: Slack::Messengers::Worker) do
|
||||
create(:article, user: user, published: true, published_at: Time.current)
|
||||
end
|
||||
end
|
||||
|
||||
it "does not queue a message for a new article published more than 30 seconds ago" do
|
||||
Timecop.freeze(Time.current) do
|
||||
sidekiq_assert_no_enqueued_jobs(only: Slack::Messengers::Worker) do
|
||||
create(:article, published: true, published_at: 31.seconds.ago)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "does not queue a message for a draft article" do
|
||||
sidekiq_assert_no_enqueued_jobs(only: Slack::Messengers::Worker) do
|
||||
markdown = "---\ntitle: Title\npublished: false\ndescription:\ntags: heytag\n---\n\nHey this is the article"
|
||||
create(:article, body_markdown: markdown, published: false)
|
||||
end
|
||||
end
|
||||
|
||||
it "doesn't queue a message for an article that remains published" do
|
||||
sidekiq_assert_no_enqueued_jobs(only: Slack::Messengers::Worker) do
|
||||
markdown = "---\ntitle: Title\npublished: true\ndescription:\ntags: heytag\n---\n\nHey this is the article"
|
||||
article.update(body_markdown: markdown, published: true)
|
||||
end
|
||||
end
|
||||
|
||||
it "queues a message for a draft article that gets published" do
|
||||
Timecop.freeze(Time.current) do
|
||||
sidekiq_assert_enqueued_with(job: Slack::Messengers::Worker) do
|
||||
article.update_columns(published: false)
|
||||
article.update(published: true, published_at: Time.current)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "enrich image attributes" do
|
||||
it "enqueues Articles::EnrichImageAttributesWorker if the HTML has changed" do
|
||||
sidekiq_assert_enqueued_with(job: Articles::EnrichImageAttributesWorker, args: [article.id]) do
|
||||
|
|
|
|||
|
|
@ -22,7 +22,17 @@ RSpec.describe Slack::Messengers::ArticlePublished, type: :service do
|
|||
sidekiq_assert_no_enqueued_jobs(only: Slack::Messengers::Worker) do
|
||||
article = build(:article).tap do |art|
|
||||
art.published = true
|
||||
art.published_at = 1.minute.ago
|
||||
art.published_at = 15.minutes.ago
|
||||
end
|
||||
described_class.call(article: article)
|
||||
end
|
||||
end
|
||||
|
||||
it "messages slack for an article that was published a few minutes ago" do
|
||||
sidekiq_assert_enqueued_jobs(1, only: Slack::Messengers::Worker) do
|
||||
article = build(:article).tap do |art|
|
||||
art.published = true
|
||||
art.published_at = 5.minutes.ago
|
||||
end
|
||||
described_class.call(article: article)
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue