diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb
index e914dcc75..b03998d7d 100644
--- a/app/policies/user_policy.rb
+++ b/app/policies/user_policy.rb
@@ -1,5 +1,6 @@
class UserPolicy < ApplicationPolicy
PERMITTED_ATTRIBUTES = %i[
+ reaction_notifications
available_for
behance_url
bg_color_hex
diff --git a/app/views/users/_notifications.html.erb b/app/views/users/_notifications.html.erb
index 710805b36..db86f655d 100644
--- a/app/views/users/_notifications.html.erb
+++ b/app/views/users/_notifications.html.erb
@@ -68,13 +68,17 @@
<%= f.check_box :welcome_notifications, class: "crayons-checkbox" %>
<%= f.label :welcome_notifications, "Send me occasional tips on how to enhance my #{community_name} experience", class: "crayons-field__label" %>
+
+ <%= f.check_box :reaction_notifications, class: "crayons-checkbox" %>
+ <%= f.label :reaction_notifications, "Send notifications when someone reacts to my comments or posts", class: "crayons-field__label" %>
+
<% if current_user.trusted %>
diff --git a/app/workers/notifications/new_reaction_worker.rb b/app/workers/notifications/new_reaction_worker.rb
index 127855467..809efa99f 100644
--- a/app/workers/notifications/new_reaction_worker.rb
+++ b/app/workers/notifications/new_reaction_worker.rb
@@ -13,7 +13,14 @@ module Notifications
return unless %w[User Organization].include?(receiver_klass)
receiver = receiver_klass.constantize.find_by(id: receiver_data.fetch(:id))
- Notifications::Reactions::Send.call(reaction_data, receiver) if receiver
+ Notifications::Reactions::Send.call(reaction_data, receiver) if should_send?(receiver, receiver_klass)
+ end
+
+ def should_send?(receiver, receiver_klass)
+ return false unless receiver
+ return true if receiver_klass == "Organization"
+
+ receiver.reaction_notifications
end
end
end
diff --git a/db/migrate/20200813042118_add_reaction_notifications_to_users.rb b/db/migrate/20200813042118_add_reaction_notifications_to_users.rb
new file mode 100644
index 000000000..dde6ba9c0
--- /dev/null
+++ b/db/migrate/20200813042118_add_reaction_notifications_to_users.rb
@@ -0,0 +1,5 @@
+class AddReactionNotificationsToUsers < ActiveRecord::Migration[6.0]
+ def change
+ add_column :users, :reaction_notifications, :boolean, default: true
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 8d4f15b04..b64056f8a 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -1276,6 +1276,7 @@ ActiveRecord::Schema.define(version: 2020_08_20_093752) do
t.string "profile_image"
t.datetime "profile_updated_at", default: "2017-01-01 05:00:00"
t.integer "rating_votes_count", default: 0, null: false
+ t.boolean "reaction_notifications", default: true
t.integer "reactions_count", default: 0, null: false
t.boolean "registered", default: true
t.datetime "registered_at"
diff --git a/spec/requests/user/user_settings_spec.rb b/spec/requests/user/user_settings_spec.rb
index 9b15ad271..9aef0e967 100644
--- a/spec/requests/user/user_settings_spec.rb
+++ b/spec/requests/user/user_settings_spec.rb
@@ -180,6 +180,11 @@ RSpec.describe "UserSettings", type: :request do
expect(user.reload.profile_updated_at).to be > 2.minutes.ago
end
+ it "disables reaction notifications" do
+ put "/users/#{user.id}", params: { user: { tab: "notifications", reaction_notifications: 0 } }
+ expect(user.reload.reaction_notifications).to be(false)
+ end
+
it "enables community-success notifications" do
put "/users/#{user.id}", params: { user: { tab: "notifications", mod_roundrobin_notifications: 1 } }
expect(user.reload.mod_roundrobin_notifications).to be(true)
diff --git a/spec/workers/notifications/new_reaction_worker_spec.rb b/spec/workers/notifications/new_reaction_worker_spec.rb
index 115203a90..c591aa22c 100644
--- a/spec/workers/notifications/new_reaction_worker_spec.rb
+++ b/spec/workers/notifications/new_reaction_worker_spec.rb
@@ -3,7 +3,11 @@ require "rails_helper"
RSpec.describe Notifications::NewReactionWorker, type: :worker do
let(:reaction_data) { { reactable_type: "Comment", reactable_id: 1, reactable_user_id: 2 } }
let(:org) { create(:organization) }
- let(:receiver_data) { { klass: "Organization", id: org.id } }
+ let(:user) { create(:user) }
+ let(:user_disabled) { create(:user, reaction_notifications: false) }
+ let(:receiver_data_org) { { klass: "Organization", id: org.id } }
+ let(:receiver_data_user) { { klass: "User", id: user.id } }
+ let(:receiver_data_user_disabled) { { klass: "User", id: user_disabled.id } }
let(:worker) { subject }
include_examples "#enqueues_on_correct_queue", "medium_priority", [{}, {}]
@@ -13,11 +17,21 @@ RSpec.describe Notifications::NewReactionWorker, type: :worker do
before { allow(reaction_service).to receive(:call) }
- it "calls the service" do
- worker.perform(reaction_data, receiver_data)
+ it "calls the service if receiver is an organization" do
+ worker.perform(reaction_data, receiver_data_org)
allow(reaction_service).to receive(:call).with(reaction_data, org).once
end
+ it "calls the service if receiver is a user" do
+ worker.perform(reaction_data, receiver_data_user)
+ allow(reaction_service).to receive(:call).with(reaction_data, user).once
+ end
+
+ it "doesn't call if reaction notifications are turned off for user" do
+ worker.perform(reaction_data, receiver_data_user_disabled)
+ expect(reaction_service).not_to have_received(:call)
+ end
+
it "doesn't call if is a receiver is of a wrong class" do
worker.perform(reaction_data, klass: "Tag", id: 10)
expect(reaction_service).not_to have_received(:call)