From 27ac0010866d1d5a4794e89f44d32a15c7114dd3 Mon Sep 17 00:00:00 2001 From: lightalloy Date: Tue, 12 Mar 2019 14:09:59 +0300 Subject: [PATCH 1/2] Enable djs to confirm follow spec fail --- spec/requests/follows_create_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/requests/follows_create_spec.rb b/spec/requests/follows_create_spec.rb index 38f60610cfcb..2700b74bb509 100644 --- a/spec/requests/follows_create_spec.rb +++ b/spec/requests/follows_create_spec.rb @@ -13,7 +13,9 @@ let(:tag) { create(:tag) } before do - post "/follows", params: { followable_type: "Tag", followable_id: tag.id } + run_background_jobs_immediately do + post "/follows", params: { followable_type: "Tag", followable_id: tag.id } + end end it "follows" do From ce5786c5d39958fc6b109bdd9160f45336bcbce3 Mon Sep 17 00:00:00 2001 From: lightalloy Date: Wed, 13 Mar 2019 13:31:00 +0300 Subject: [PATCH 2/2] Don't send new follower notification when followable is a tag --- app/controllers/follows_controller.rb | 5 +++-- app/models/follow.rb | 4 ++++ app/models/notification.rb | 2 ++ spec/models/notification_spec.rb | 19 +++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/controllers/follows_controller.rb b/app/controllers/follows_controller.rb index f033df827ab6..ee7dc05f8015 100644 --- a/app/controllers/follows_controller.rb +++ b/app/controllers/follows_controller.rb @@ -24,13 +24,14 @@ def create User.find(params[:followable_id]) end add_param_context(:followable_type, :followable_id, :verb) + need_notification = Follow.need_new_follower_notification_for?(followable.class.name) @result = if params[:verb] == "unfollow" follow = current_user.stop_following(followable) - Notification.send_new_follower_notification_without_delay(follow, true) + Notification.send_new_follower_notification_without_delay(follow, true) if need_notification "unfollowed" else follow = current_user.follow(followable) - Notification.send_new_follower_notification(follow) + Notification.send_new_follower_notification(follow) if need_notification "followed" end current_user.touch diff --git a/app/models/follow.rb b/app/models/follow.rb index edf37ef0af6c..5c2a39e1334c 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -26,6 +26,10 @@ class Follow < ApplicationRecord validates :followable_id, uniqueness: { scope: %i[followable_type follower_id] } + def self.need_new_follower_notification_for?(followable_type) + %w[User Organization].include?(followable_type) + end + private def touch_follower diff --git a/app/models/notification.rb b/app/models/notification.rb index c17e08ee37d0..03790fdc5cda 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -12,6 +12,8 @@ class Notification < ApplicationRecord class << self def send_new_follower_notification(follow, is_read = false) + return unless Follow.need_new_follower_notification_for?(follow.followable_type) + recent_follows = Follow.where(followable_type: follow.followable_type, followable_id: follow.followable_id).where("created_at > ?", 24.hours.ago).order("created_at DESC") notification_params = { action: "Follow" } diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index 95bde4ef24e4..75b565d52f5c 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -8,6 +8,25 @@ let(:article) { create(:article, user_id: user.id, page_views_count: 4000, positive_reactions_count: 70) } let(:follow_instance) { user.follow(user2) } + describe "when trying to #send_new_follower_notification after following a tag" do + let(:tag) { create(:tag) } + let(:tag_follow) { user.follow(tag) } + + it "runs fine" do + run_background_jobs_immediately do + Notification.send_new_follower_notification(tag_follow) + end + end + + it "doesn't create a notification" do + run_background_jobs_immediately do + expect do + Notification.send_new_follower_notification(tag_follow) + end.not_to change(Notification, :count) + end + end + end + describe "#send_new_follower_notification" do before do run_background_jobs_immediately do