Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't send new follower notification when followable is a tag #2041 #2050

Merged
merged 2 commits into from Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/controllers/follows_controller.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/follow.rb
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/models/notification.rb
Expand Up @@ -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" }
Expand Down
19 changes: 19 additions & 0 deletions spec/models/notification_spec.rb
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion spec/requests/follows_create_spec.rb
Expand Up @@ -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
Expand Down