From 3e8e861103a147fbfb545cf831e04b61d0b28657 Mon Sep 17 00:00:00 2001 From: marstall Date: Fri, 20 Dec 2024 11:43:13 -0500 Subject: [PATCH] DEV: return full name in /notifications.json (#30335) * wip: return full name in /notifications.json * DEV: test for full name * DEV: add test for enable_names=true * DEV: add notification6, cleanup * DEV: fix tests --- app/models/notification.rb | 6 +++++- spec/models/notification_spec.rb | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/models/notification.rb b/app/models/notification.rb index 356691b8ff7..15b45662eb7 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -371,13 +371,17 @@ class Notification < ActiveRecord::Base ( notification.data_hash[:username] || notification.data_hash[:display_username] || notification.data_hash[:mentioned_by_username] || - notification.data_hash[:invited_by_username] + notification.data_hash[:invited_by_username] || + notification.data_hash[:original_username] )&.downcase end users = User.where(username_lower: usernames.uniq).index_by(&:username_lower) notifications.each do |notification| notification.acting_user = users[notification.acting_username] + notification.data_hash[ + :original_name + ] = notification.acting_user&.name if SiteSetting.enable_names end notifications diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index cbd6708e2a4..06fe1837f9d 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -809,10 +809,14 @@ RSpec.describe Notification do end describe ".populate_acting_user" do + SiteSetting.enable_names = true + fab!(:user1) { Fabricate(:user) } fab!(:user2) { Fabricate(:user) } fab!(:user3) { Fabricate(:user) } fab!(:user4) { Fabricate(:user) } + fab!(:user5) { Fabricate(:user) } + fab!(:user6) { Fabricate(:user) } fab!(:notification1) do Fabricate(:notification, user: user, data: { username: user1.username }.to_json) end @@ -825,16 +829,32 @@ RSpec.describe Notification do fab!(:notification4) do Fabricate(:notification, user: user, data: { invited_by_username: user4.username }.to_json) end + fab!(:notification5) do + Fabricate(:notification, user: user, data: { original_username: user5.username }.to_json) + end + fab!(:notification6) do + Fabricate(:notification, user: user, data: { original_username: user6.username }.to_json) + end it "Sets the acting_user correctly for each notification" do Notification.populate_acting_user( - [notification1, notification2, notification3, notification4], + [notification1, notification2, notification3, notification4, notification5], ) - expect(notification1.acting_user).to eq(user1) expect(notification2.acting_user).to eq(user2) expect(notification3.acting_user).to eq(user3) expect(notification4.acting_user).to eq(user4) + expect(notification5.acting_user).to eq(user5) + expect(notification5.data_hash[:original_name]).to eq user5.name + end + + context "with SiteSettings.enable_names=false" do + it "doesn't set the :original_name property" do + SiteSetting.enable_names = false + Notification.populate_acting_user([notification6]) + expect(notification6.data_hash[:original_name]).to be_nil + SiteSetting.enable_names = true + end end end end