Skip to content

A platform for community discussion. Free, open, simple.

DEV: Added the ability to use users' names in group mention notifications and mentions shown in emails (#32848)

Juan David Martínez Cubillose147d2afe668
diff --git a/app/assets/javascripts/discourse/app/lib/notification-types/group-mentioned.js b/app/assets/javascripts/discourse/app/lib/notification-types/group-mentioned.js
index 6256eda6869..eaa32796795 100644
--- a/app/assets/javascripts/discourse/app/lib/notification-types/group-mentioned.js
+++ b/app/assets/javascripts/discourse/app/lib/notification-types/group-mentioned.js
@@ -1,8 +1,19 @@
+import { service } from "@ember/service";
import NotificationTypeBase from "discourse/lib/notification-types/base";
export default class extends NotificationTypeBase {
+ @service siteSettings;
+
get label() {
- return `${this.username} @${this.notification.data.group_name}`;
+ let name;
+
+ if (this.siteSettings.prioritize_full_name_in_ux) {
+ name = this.notification.acting_user_name || this.username;
+ } else {
+ name = this.username;
+ }
+
+ return `${name} @${this.notification.data.group_name}`;
}
get labelClasses() {
diff --git a/app/assets/javascripts/discourse/tests/unit/lib/notification-types/group-mentioned-test.js b/app/assets/javascripts/discourse/tests/unit/lib/notification-types/group-mentioned-test.js
index 8f4c64ec27c..d4e56876163 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/notification-types/group-mentioned-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/notification-types/group-mentioned-test.js
@@ -1,3 +1,4 @@
+import Service from "@ember/service";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import { deepMerge } from "discourse/lib/object";
@@ -19,12 +20,15 @@ function getNotification(overrides = {}) {
topic_id: 449,
fancy_title: "This is fancy title <a>!",
slug: "this-is-fancy-title",
+ acting_user_name: "Osama Obama",
data: {
topic_title: "this is title before it becomes fancy <a>!",
original_post_id: 112,
original_post_type: 1,
original_username: "kolary",
+ original_name: "Osama Obama",
display_username: "osama",
+ display_name: "Osama Obama",
group_id: 333,
group_name: "hikers",
},
@@ -37,17 +41,48 @@ function getNotification(overrides = {}) {
module("Unit | Notification Types | group-mentioned", function (hooks) {
setupTest(hooks);
+ hooks.beforeEach(function () {
+ this.owner.register(
+ "service:site-settings",
+ class extends Service {
+ prioritize_full_name_in_ux = false;
+ }
+ );
+
+ this.siteSettings = this.owner.lookup("service:site-settings");
+ });
+
test("label", function (assert) {
const notification = getNotification();
+
const director = createRenderDirector(
notification,
"group_mentioned",
this.siteSettings
);
+
assert.strictEqual(
director.label,
"osama @hikers",
- "contains the user who mentioned and the mentioned group"
+ "contains the user's username who mentioned and the mentioned group"
+ );
+ });
+
+ test("label uses the user's name when prioritize_username_in_ux is false", function (assert) {
+ this.siteSettings.prioritize_full_name_in_ux = true;
+
+ const notification = getNotification();
+
+ const director = createRenderDirector(
+ notification,
+ "group_mentioned",
+ this.siteSettings
+ );
+
+ assert.strictEqual(
+ director.label,
+ "Osama Obama @hikers",
+ "contains the user's name who mentioned and the mentioned group"
);
});
});
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index 0d8aa320a08..49c1b0ac2b4 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -393,6 +393,11 @@ class UserNotifications < ActionMailer::Base
opts[:use_site_subject] = true
opts[:show_category_in_subject] = true
opts[:show_tags_in_subject] = true
+
+ if !SiteSetting.prioritize_username_in_ux
+ opts[:post].cooked = replace_username_with_name_from_post(opts[:post], user)
+ end
+
notification_email(user, opts)
end
@@ -706,6 +711,7 @@ class UserNotifications < ActionMailer::Base
(SiteSetting.max_emails_per_day_per_user - 1)
in_reply_to_post = post.reply_to_post if user.user_option.email_in_reply_to
+
if SiteSetting.private_email?
message = I18n.t("system_messages.contents_hidden")
else
@@ -872,4 +878,19 @@ class UserNotifications < ActionMailer::Base
.count
.tap { Discourse.redis.setex(key, 1.day, _1) }
end
+
+ def replace_username_with_name_from_post(post, user)
+ alnum = '\p{Alphabetic}\p{Mark}\p{Decimal_Number}'
+ mention_regex =
+ /
+ @(
+ [#{alnum}_]
+ [#{alnum}._-]{0,58}
+ [#{alnum}]
+ ) |
+ @([#{alnum}_])
+ /ux
+
+ post.cooked.gsub!(mention_regex) { |match| user.name ? "@#{user.name}" : match }
+ end
end
diff --git a/spec/fabricators/notification_fabricator.rb b/spec/fabricators/notification_fabricator.rb
index 51d34032e48..d7bf4a43eac 100644
--- a/spec/fabricators/notification_fabricator.rb
+++ b/spec/fabricators/notification_fabricator.rb
@@ -92,6 +92,24 @@ Fabricator(:mentioned_notification, from: :notification) do
end
end
+Fabricator(:group_mentioned_notification, from: :notification) do
+ transient :group
+ notification_type Notification.types[:group_mentioned]
+ data do |attrs|
+ {
+ topic_title: attrs[:topic].title,
+ original_post_id: attrs[:post].id,
+ original_post_type: attrs[:post].post_type,
+ original_username: attrs[:post].user.username,
+ revision_number: nil,
+ display_username: attrs[:post].user.username,
+ display_name: attrs[:user].name,
+ group_id: attrs[:group].id,
+ group_name: attrs[:group].name,
+ }.to_json
+ end
+end
+
Fabricator(:watching_first_post_notification, from: :notification) do
notification_type Notification.types[:watching_first_post]
data do |attrs|
diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb
index 7d072ea6a89..30f84045cb2 100644
--- a/spec/mailers/user_notifications_spec.rb
+++ b/spec/mailers/user_notifications_spec.rb
@@ -682,6 +682,97 @@ RSpec.describe UserNotifications do
end
end
+ describe ".user_mentioned" do
+ let(:response_by_user) { Fabricate(:user, name: "John Doe") }
+ let(:category) { Fabricate(:category, name: "India") }
+
+ let(:topic) { Fabricate(:topic, category: category, title: "Super cool topic") }
+
+ let(:user) { Fabricate(:user) }
+ let(:post) { Fabricate(:post, topic: topic, raw: "This is My super duper cool topic") }
+ let(:response) do
+ Fabricate(
+ :basic_reply,
+ topic: post.topic,
+ user: response_by_user,
+ raw: "@#{user.username} response to post",
+ )
+ end
+ let(:notification) { Fabricate(:mentioned_notification, user: user, post: response) }
+
+ context "when prioritize_username_in_ux is false" do
+ before { SiteSetting.prioritize_username_in_ux = false }
+ context "when user has name" do
+ it "generates a correct email" do
+ mail =
+ UserNotifications.user_mentioned(
+ user,
+ post: response,
+ notification_type: notification.notification_type,
+ notification_data_hash: notification.data_hash,
+ )
+
+ # from should include full user name
+ expect(mail[:from].display_names).to eql(["John Doe via Discourse"])
+
+ # subject should include category name
+ expect(mail.subject).to match(/India/)
+
+ mail_html = mail.html_part.body.to_s
+
+ expect(mail_html.scan(%r{@#{user.name}</a> response to post}).count).to eq(1)
+
+ expect(mail_html.scan(/Visit Topic/).count).to eq(1)
+
+ expect(mail_html.scan(/to respond/).count).to eq(1)
+
+ # 1 unsubscribe
+ expect(mail_html.scan(/To unsubscribe/).count).to eq(1)
+
+ # side effect, topic user is updated with post number
+ tu = TopicUser.get(post.topic_id, user)
+ expect(tu.last_emailed_post_number).to eq(response.post_number)
+ end
+ end
+
+ context "when user doesn't have a name" do
+ it "generates a correct email" do
+ user.name = nil
+ user.save
+
+ mail =
+ UserNotifications.user_mentioned(
+ user,
+ post: response,
+ notification_type: notification.notification_type,
+ notification_data_hash: notification.data_hash,
+ )
+
+ # from should include full user name
+ expect(mail[:from].display_names).to eql(["John Doe via Discourse"])
+
+ # subject should include category name
+ expect(mail.subject).to match(/India/)
+
+ mail_html = mail.html_part.body.to_s
+
+ expect(mail_html.scan(%r{@#{user.username}</a> response to post}).count).to eq(1)
+
+ expect(mail_html.scan(/Visit Topic/).count).to eq(1)
+
+ expect(mail_html.scan(/to respond/).count).to eq(1)
+
+ # 1 unsubscribe
+ expect(mail_html.scan(/To unsubscribe/).count).to eq(1)
+
+ # side effect, topic user is updated with post number
+ tu = TopicUser.get(post.topic_id, user)
+ expect(tu.last_emailed_post_number).to eq(response.post_number)
+ end
+ end
+ end
+ end
+
describe ".user_posted" do
let(:response_by_user) { Fabricate(:user, name: "John Doe", username: "john") }
let(:topic) { Fabricate(:topic, title: "Super cool topic") }
diff --git a/spec/system/page_objects/pages/user_notifications.rb b/spec/system/page_objects/pages/user_notifications.rb
index 574541432bc..faead7ec839 100644
--- a/spec/system/page_objects/pages/user_notifications.rb
+++ b/spec/system/page_objects/pages/user_notifications.rb
@@ -12,6 +12,10 @@ module PageObjects
PageObjects::Components::SelectKit.new(".notifications-filter")
end
+ def find_notification(notification)
+ find(".notification a[href='#{notification.url}']")
+ end
+
def set_filter_value(value)
filter_dropdown.select_row_by_value(value)
end
diff --git a/spec/system/user_page/user_notifications_spec.rb b/spec/system/user_page/user_notifications_spec.rb
index 3fff7dfaa1a..8ea44f95fb4 100644
--- a/spec/system/user_page/user_notifications_spec.rb
+++ b/spec/system/user_page/user_notifications_spec.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
describe "User notifications", type: :system do
- fab!(:user)
+ fab!(:user) { Fabricate(:user, name: "Awesome Name") }
let(:user_notifications_page) { PageObjects::Pages::UserNotifications.new }
let(:user_page) { PageObjects::Pages::User.new }
@@ -51,4 +51,59 @@ describe "User notifications", type: :system do
expect(user_notifications_page).to have_notification_count_of(1)
end
end
+
+ describe "user group notifications" do
+ fab!(:group) { Fabricate(:group, name: "Awesome_Group") }
+ fab!(:topic) { Fabricate(:topic, title: "Group Mention Notification test") }
+ fab!(:post) do
+ Fabricate(
+ :post,
+ raw: "@#{group.name} this is a post to create a group mention notification",
+ user: user,
+ topic: topic,
+ )
+ end
+ fab!(:user2) { Fabricate(:user) }
+ fab!(:group_mention_notification) do
+ Fabricate(:group_mentioned_notification, post: post, user: user2, group: group)
+ end
+
+ before { group.add(user2) }
+
+ context "when prioritize_username_in_ux is false" do
+ before do
+ SiteSetting.prioritize_full_name_in_ux = true
+ sign_in(user2)
+ end
+
+ it "shows the user name in the notification" do
+ user_notifications_page.visit(user2)
+
+ expect(user_notifications_page).to have_notification(group_mention_notification)
+
+ notification = user_notifications_page.find_notification(group_mention_notification)
+
+ expect(notification).to have_content(group.name)
+ expect(notification).to have_content(user.name)
+ end
+
+ context "when user doesn't have a name" do
+ before do
+ user.name = nil
+ user.save
+ end
+
+ it "shows the username in the notification instead" do
+ user_notifications_page.visit(user2)
+
+ expect(user_notifications_page).to have_notification(group_mention_notification)
+
+ notification = user_notifications_page.find_notification(group_mention_notification)
+
+ expect(notification).to have_content(group.name)
+ expect(notification).to have_content(user.username)
+ end
+ end
+ end
+ end
end