diff --git a/app/controllers/users/notification_settings_controller.rb b/app/controllers/users/notification_settings_controller.rb
index 6cf2c216e..603be42fa 100644
--- a/app/controllers/users/notification_settings_controller.rb
+++ b/app/controllers/users/notification_settings_controller.rb
@@ -15,6 +15,7 @@ module Users
email_tag_mod_newsletter
email_unread_notifications
mobile_comment_notifications
+ mobile_mention_notifications
mod_roundrobin_notifications
reaction_notifications
welcome_notifications].freeze
diff --git a/app/services/notifications/new_mention/send.rb b/app/services/notifications/new_mention/send.rb
index 2965c50dc..4790633dc 100644
--- a/app/services/notifications/new_mention/send.rb
+++ b/app/services/notifications/new_mention/send.rb
@@ -1,6 +1,8 @@
module Notifications
module NewMention
class Send
+ include ActionView::Helpers::TextHelper
+
delegate :user_data, to: Notifications
delegate :comment_data, to: Notifications
delegate :article_data, to: Notifications
@@ -14,6 +16,8 @@ module Notifications
end
def call
+ return if mention.mentionable.score.negative?
+
Notification.create(
user_id: mention.user_id,
notifiable_id: mention.id,
@@ -21,6 +25,32 @@ module Notifications
action: nil,
json_data: json_data,
)
+
+ # Send PNs using Rpush - respecting users' notificaton delivery settings
+ return unless Users::NotificationSetting.find_by(user_id: mention.user_id)&.mobile_mention_notifications?
+
+ target = mention.user_id
+ message_key, mentionable_title =
+ if mention.mentionable.is_a?(Article)
+ ["views.notifications.mention.article_mobile", mention.mentionable.title.strip]
+ else
+ ["views.notifications.mention.comment_mobile", mention.mentionable.commentable.title.strip]
+ end
+
+ PushNotifications::Send.call(
+ user_ids: [target],
+ title: "🗣️ New Mention",
+ body: "#{I18n.t(
+ message_key,
+ user: mention.user.username,
+ title: mentionable_title, # For an article this should be title, for comment should be article's title
+ )}:\n" \
+ "#{strip_tags(mention.mentionable.processed_html).strip}",
+ payload: {
+ url: URL.url(mention.mentionable.path),
+ type: "new mention"
+ },
+ )
end
private
diff --git a/app/views/users/_notifications.html.erb b/app/views/users/_notifications.html.erb
index 3eeab59b8..be27fad19 100644
--- a/app/views/users/_notifications.html.erb
+++ b/app/views/users/_notifications.html.erb
@@ -61,7 +61,11 @@
<%= f.check_box :mobile_comment_notifications, class: "crayons-checkbox" %>
- <%= f.label :mobile_comment_notifications, "Notify me when someone replies to me in a comment thread", class: "crayons-field__label" %>
+ <%= f.label :mobile_comment_notifications, t("helpers.label.users_notification_setting.mobile_comment_notifications"), class: "crayons-field__label" %>
+
+
+ <%= f.check_box :mobile_mention_notifications, class: "crayons-checkbox" %>
+ <%= f.label :mobile_mention_notifications, t("helpers.label.users_notification_setting.mobile_mention_notifications"), class: "crayons-field__label" %>
diff --git a/config/locales/helpers/en.yml b/config/locales/helpers/en.yml
index 09cc40bc1..5e9202e63 100644
--- a/config/locales/helpers/en.yml
+++ b/config/locales/helpers/en.yml
@@ -63,7 +63,8 @@ en:
email_badge_notifications: Send me an email when I receive a badge
email_connect_messages: Send me an email when I receive a direct message (while inactive)
email_unread_notifications: Send me occasional reminders that I have unread notifications on %{community}
- mobile_comment_notifications: Notify me when someone replies to me in a comment thread
+ mobile_mention_notifications: Send me a push notification when someone mentions me
+ mobile_comment_notifications: Send me a push notification when someone replies to me in a comment thread
welcome_notifications: Send me occasional tips on how to enhance my %{community} experience
reaction_notifications: Send notifications when someone reacts to my comments or posts
mod_roundrobin_notifications: Send me occasional community-success mod notifications
diff --git a/config/locales/helpers/fr.yml b/config/locales/helpers/fr.yml
index b38fc239c..eb4d141d7 100644
--- a/config/locales/helpers/fr.yml
+++ b/config/locales/helpers/fr.yml
@@ -63,7 +63,8 @@ fr:
email_badge_notifications: Send me an email when I receive a badge
email_connect_messages: Send me an email when I receive a direct message (while inactive)
email_unread_notifications: Send me occasional reminders that I have unread notifications on %{community}
- mobile_comment_notifications: Notify me when someone replies to me in a comment thread
+ mobile_mention_notifications: Send me a push notification when someone mentions me
+ mobile_comment_notifications: Send me a push notification when someone replies to me in a comment thread
welcome_notifications: Send me occasional tips on how to enhance my %{community} experience
reaction_notifications: Send notifications when someone reacts to my comments or posts
mod_roundrobin_notifications: Send me occasional community-success mod notifications
diff --git a/config/locales/views/notifications/en.yml b/config/locales/views/notifications/en.yml
index 033e2fc83..fdfe880c9 100644
--- a/config/locales/views/notifications/en.yml
+++ b/config/locales/views/notifications/en.yml
@@ -43,6 +43,8 @@ en:
mention:
article_html: "%{user} mentioned you in a post%{if_org}"
comment_html: "%{user} mentioned you in a comment"
+ article_mobile: "%{user} mentioned you in the post %{title}"
+ comment_mobile: "%{user} mentioned you in a comment on %{title}"
if_org_html: " under %{org}"
milestone:
heading_html: Your post %{title} passed %{type}!
diff --git a/config/locales/views/notifications/fr.yml b/config/locales/views/notifications/fr.yml
index e155a3d8f..8794d49fa 100644
--- a/config/locales/views/notifications/fr.yml
+++ b/config/locales/views/notifications/fr.yml
@@ -43,6 +43,8 @@ fr:
mention:
article_html: "%{user} mentioned you in a post%{if_org}"
comment_html: "%{user} mentioned you in a comment"
+ article_mobile: "%{user} mentioned you in the post %{title}"
+ comment_mobile: "%{user} mentioned you in a comment on %{title}"
if_org_html: " under %{org}"
milestone:
heading_html: Your post %{title} passed %{type}!
diff --git a/db/migrate/20211209225729_add_mobile_mention_notification_to_user_notification_settings.rb b/db/migrate/20211209225729_add_mobile_mention_notification_to_user_notification_settings.rb
new file mode 100644
index 000000000..e3935b963
--- /dev/null
+++ b/db/migrate/20211209225729_add_mobile_mention_notification_to_user_notification_settings.rb
@@ -0,0 +1,5 @@
+class AddMobileMentionNotificationToUserNotificationSettings < ActiveRecord::Migration[6.1]
+ def change
+ add_column :users_notification_settings, :mobile_mention_notifications, :boolean, default: true, null: false
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index d0a69f4b5..90ae35fca 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 2021_11_04_161101) do
+ActiveRecord::Schema.define(version: 2021_12_09_225729) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
@@ -1346,6 +1346,7 @@ ActiveRecord::Schema.define(version: 2021_11_04_161101) do
t.boolean "email_tag_mod_newsletter", default: false, null: false
t.boolean "email_unread_notifications", default: true, null: false
t.boolean "mobile_comment_notifications", default: true, null: false
+ t.boolean "mobile_mention_notifications", default: true, null: false
t.boolean "mod_roundrobin_notifications", default: true, null: false
t.boolean "reaction_notifications", default: true, null: false
t.datetime "updated_at", precision: 6, null: false
diff --git a/spec/factories/users_notification_settings.rb b/spec/factories/users_notification_settings.rb
index 7e17d7dae..99438b989 100644
--- a/spec/factories/users_notification_settings.rb
+++ b/spec/factories/users_notification_settings.rb
@@ -11,6 +11,7 @@ FactoryBot.define do
email_tag_mod_newsletter { false }
email_unread_notifications { true }
mobile_comment_notifications { true }
+ mobile_mention_notifications { true }
mod_roundrobin_notifications { true }
reaction_notifications { true }
welcome_notifications { true }
diff --git a/spec/services/notifications/new_mention/send_spec.rb b/spec/services/notifications/new_mention/send_spec.rb
index e083729d6..686998832 100644
--- a/spec/services/notifications/new_mention/send_spec.rb
+++ b/spec/services/notifications/new_mention/send_spec.rb
@@ -26,12 +26,39 @@ end
RSpec.describe Notifications::NewMention::Send, type: :service do
let(:user) { create(:user) }
let!(:article) { create(:article) }
+ let!(:mention) { create(:mention, mentionable: article, user: user) }
- it_behaves_like "mentionable" do
- let(:mentionable) { create(:comment, commentable: article) }
+ it "creates users notifications" do
+ expect do
+ described_class.call(mention)
+ end.to change(Notification, :count).by(1)
end
- it_behaves_like "mentionable" do
- let(:mentionable) { article }
+ it "creates a correct user notification" do
+ described_class.call(mention)
+
+ notification = Notification.last
+
+ expect(notification.notifiable_type).to eq("Mention")
+ expect(notification.notifiable_id).to eq(mention.id)
+ expect(notification.user_id).to eq(mention.user.id)
+ expect(notification.action).to be_nil
+ expect(notification.json_data["user"]["id"]).to eq(article.user.id)
+ expect(notification.json_data["user"]["username"]).to eq(article.user.username)
+ end
+
+ it "does not send if the article has negative score already" do
+ prior_notification_size = Notification.all.size
+ article.update_column(:score, -1)
+ described_class.call(mention)
+ expect(Notification.all.size).to eq prior_notification_size
+ end
+
+ it "doesn't create a mobile notification if the mobile receive notification is false" do
+ user.notification_setting.update(mobile_mention_notifications: false)
+ allow(PushNotifications::Send).to receive(:call)
+
+ described_class.call(mention)
+ expect(PushNotifications::Send).not_to have_received(:call)
end
end