Mobile mention notifications (#15780)

This commit is contained in:
Josh Puetz 2021-12-16 11:06:54 -06:00 committed by GitHub
parent 3fd03b5ed6
commit 731849a068
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 83 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -61,7 +61,11 @@
</header>
<div class="crayons-field crayons-field--checkbox">
<%= 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" %>
</div>
<div class="crayons-field crayons-field--checkbox">
<%= 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" %>
</div>
</div>

View file

@ -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

View file

@ -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

View file

@ -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}!

View file

@ -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}!

View file

@ -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

View file

@ -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

View file

@ -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 }

View file

@ -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