From 21f595ea2c178bda4b8a99515caee1990e8bcebd Mon Sep 17 00:00:00 2001 From: Jess Lee Date: Tue, 21 May 2019 15:06:04 -0400 Subject: [PATCH] Update Slack Notifications (#2870) [ci skip] --- app/observers/application_observer.rb | 2 +- app/observers/article_observer.rb | 18 ++++++++++-------- app/observers/comment_observer.rb | 2 +- spec/observers/article_observer_spec.rb | 5 ++--- spec/observers/comment_observer_spec.rb | 2 +- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/observers/application_observer.rb b/app/observers/application_observer.rb index 4d44d2726..81f1e35a6 100644 --- a/app/observers/application_observer.rb +++ b/app/observers/application_observer.rb @@ -2,7 +2,7 @@ class ApplicationObserver < ActiveRecord::Observer def warned_user_ping(activity) return unless activity.user.warned == true - SlackBot.delay.ping "@#{activity.user.username} just posted.\nThey've been warned since #{activity.user.roles.where(name: 'warned')[0].updated_at.strftime('%d %B %Y')}\nhttps://dev.to#{activity.path}", + SlackBot.delay.ping "Activity: https://dev.to/#{activity.path}\nManage @#{activity.user.username}: https://dev.to/internal/users/#{activity.user.id}", channel: "warned-user-activity", username: "sloan_watch_bot", icon_emoji: ":sloan:" diff --git a/app/observers/article_observer.rb b/app/observers/article_observer.rb index f1bac64e1..77de1f870 100644 --- a/app/observers/article_observer.rb +++ b/app/observers/article_observer.rb @@ -2,15 +2,17 @@ class ArticleObserver < ApplicationObserver def after_save(article) return if Rails.env.development? - if article.published && article.published_at > 30.seconds.ago - SlackBot.delay.ping "New Article Published: #{article.title}\nhttps://dev.to#{article.path}", - channel: "activity", - username: "article_bot", - icon_emoji: ":writing_hand:" - - end - warned_user_ping(article) + ping_new_article(article) rescue StandardError => e Rails.logger.error(e) end + + def ping_new_article(article) + return unless article.published && article.published_at > 30.seconds.ago + + SlackBot.delay.ping "New Article Published: #{article.title}\nhttps://dev.to#{article.path}", + channel: "activity", + username: "article_bot", + icon_emoji: ":writing_hand:" + end end diff --git a/app/observers/comment_observer.rb b/app/observers/comment_observer.rb index a0a373a03..bdb96c6e0 100644 --- a/app/observers/comment_observer.rb +++ b/app/observers/comment_observer.rb @@ -1,5 +1,5 @@ class CommentObserver < ApplicationObserver - def after_save(comment) + def after_create(comment) return if Rails.env.development? warned_user_ping(comment) diff --git a/spec/observers/article_observer_spec.rb b/spec/observers/article_observer_spec.rb index 23b2f27d5..6a97c401b 100644 --- a/spec/observers/article_observer_spec.rb +++ b/spec/observers/article_observer_spec.rb @@ -7,13 +7,12 @@ RSpec.describe ArticleObserver, type: :observer do allow(SlackBot).to receive(:ping).and_return(true) end - it "pings slack if user with warned role creates an article" do - user.add_role :warned + it "pings slack #activity if new article is created" do Article.observers.enable :article_observer do run_background_jobs_immediately do create(:article, user_id: user.id) end end - expect(SlackBot).to have_received(:ping).twice + expect(SlackBot).to have_received(:ping).once end end diff --git a/spec/observers/comment_observer_spec.rb b/spec/observers/comment_observer_spec.rb index 1ae14f58f..cecbc856a 100644 --- a/spec/observers/comment_observer_spec.rb +++ b/spec/observers/comment_observer_spec.rb @@ -17,6 +17,6 @@ RSpec.describe CommentObserver, type: :observer do end end end - expect(SlackBot).to have_received(:ping).twice + expect(SlackBot).to have_received(:ping).once end end