FIX: correctly uses private_email site setting in chat (#24528)

Chat will now check for the state of `SiteSetting.private_email` when sending the summary, when enabled, the mail will not display user information, channel information other than the ID and no message information, only the count of messages.
This commit is contained in:
Joffrey JAFFEUX 2023-11-23 15:54:22 +01:00 committed by GitHub
parent 6ac5f34ad7
commit 57584c38c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 143 additions and 34 deletions

View File

@ -26,9 +26,17 @@
<tbody>
<tr>
<td colspan="100%">
<h5 style="margin:0.5em 0 0.5em 0;font-size:0.9em;"><%= chat_channel.title(@user) %></h5>
<h5 style="margin:0.5em 0 0.5em 0;font-size:0.9em;">
<%- if SiteSetting.private_email %>
<%= I18n.t("system_messages.private_channel_title", id: chat_channel.id) %>
<%- else %>
<%= chat_channel.title(@user) %>
<%- end %>
</h5>
</td>
</tr>
<%- unless SiteSetting.private_email %>
<%- messages.take(2).each do |chat_message| %>
<%- sender = chat_message.user %>
<%- sender_name = @display_usernames ? sender.username : sender.name %>
@ -50,14 +58,20 @@
</td>
</tr>
<%- end %>
<%- end %>
<tr>
<td colspan="100%" style="padding:<%= rtl? ? '2em 2em 0 0' : '2em 0 0 2em' %>">
<a class="more-messages-link" href="<%= messages.first.full_url %>">
<%- if SiteSetting.private_email %>
<%= I18n.t("user_notifications.chat_summary.view_messages", count: messages.size)%>
<%- else %>
<%- if other_messages_count <= 0 %>
<%= I18n.t("user_notifications.chat_summary.view_messages", count: messages.size)%>
<%- else %>
<%= I18n.t("user_notifications.chat_summary.view_more", count: other_messages_count)%>
<%- end %>
<%- end %>
</a>
</td>
</tr>

View File

@ -30,6 +30,7 @@ en:
direct_message_enabled_groups_invalid: "You must specify at least one group for this setting. If you do not want anyone except staff to send direct messages, choose the staff group."
chat_upload_not_allowed_secure_uploads: "Chat uploads are not allowed when secure uploads site setting is enabled."
system_messages:
private_channel_title: "Channel %{id}"
chat_channel_archive_complete:
title: "Chat Channel Archive Complete"
subject_template: "Chat channel archive completed successfully"
@ -233,6 +234,7 @@ en:
other: "You have new chat messages"
from: "%{site_name}"
subject:
private_message: "[%{email_prefix}] New message"
direct_message_from_1: "[%{email_prefix}] New message from %{username}"
direct_message_from_2: "[%{email_prefix}] New message from %{username1} and %{username2}"
direct_message_from_more:

View File

@ -53,6 +53,15 @@ module Chat
end
def summary_subject(user, grouped_messages)
if SiteSetting.private_email
return(
I18n.t(
"user_notifications.chat_summary.subject.private_message",
email_prefix: @email_prefix,
)
)
end
all_channels = grouped_messages.keys
grouped_channels = all_channels.partition { |c| !c.direct_message_channel? }
channels = grouped_channels.first

View File

@ -33,6 +33,22 @@ describe UserNotifications do
end
describe "email subject" do
context "when private_email setting is enabled" do
before { SiteSetting.private_email = true }
it "has a generic subject" do
Fabricate(:chat_message, user: sender, chat_channel: channel)
email = described_class.chat_summary(user, {})
expect(email.subject).to eq(
I18n.t(
"user_notifications.chat_summary.subject.private_message",
email_prefix: SiteSetting.email_prefix.presence || SiteSetting.title,
),
)
end
end
it "includes the sender username in the subject" do
expected_subject =
I18n.t(
@ -245,6 +261,21 @@ describe UserNotifications do
)
end
context "when private_email setting is enabled" do
before { SiteSetting.private_email = true }
it "has a generic subject" do
email = described_class.chat_summary(user, {})
expect(email.subject).to eq(
I18n.t(
"user_notifications.chat_summary.subject.private_message",
email_prefix: SiteSetting.email_prefix.presence || SiteSetting.title,
),
)
end
end
it "includes the sender username in the subject" do
expected_subject =
I18n.t(
@ -500,6 +531,31 @@ describe UserNotifications do
end
describe "mail contents" do
context "when private_email setting is enabled" do
before { SiteSetting.private_email = true }
it "has a generic channel title name" do
email = described_class.chat_summary(user, {})
expect(email.html_part.body.to_s).to include(
I18n.t("system_messages.private_channel_title", id: channel.id),
)
end
it "doesnt include message content" do
email = described_class.chat_summary(user, {})
expect(email.html_part.body.to_s).to_not include(chat_message.cooked_for_excerpt)
end
it "doesnt include user info" do
email = described_class.chat_summary(user, {})
expect(email.html_part.body.to_s).to_not include(chat_message.user.small_avatar_url)
expect(email.html_part.body.to_s).to_not include(chat_message.user.username)
end
end
it "returns an email when the user has unread mentions" do
email = described_class.chat_summary(user, {})
@ -581,7 +637,8 @@ describe UserNotifications do
expect(user_avatar.attribute("alt").value).to eq(sender.username)
end
it "includes a view more link when there are more than two mentions" do
context "when there are more than two mentions" do
it "includes a view more link " do
2.times do
msg = Fabricate(:chat_message, user: sender, chat_channel: channel)
notification = Fabricate(:notification)
@ -592,12 +649,39 @@ describe UserNotifications do
more_messages_channel_link =
Nokogiri::HTML5.fragment(email.html_part.body.to_s).css(".more-messages-link")
expect(more_messages_channel_link.attribute("href").value).to eq(chat_message.full_url)
expect(more_messages_channel_link.attribute("href").value).to eq(
chat_message.full_url,
)
expect(more_messages_channel_link.text).to include(
I18n.t("user_notifications.chat_summary.view_more", count: 1),
)
end
context "when private_email setting is enabled" do
before { SiteSetting.private_email = true }
it "has only a link to view all messages" do
2.times do
msg = Fabricate(:chat_message, user: sender, chat_channel: channel)
notification = Fabricate(:notification)
Fabricate(
:chat_mention,
user: user,
chat_message: msg,
notification: notification,
)
end
email = described_class.chat_summary(user, {})
more_messages_channel_link =
Nokogiri::HTML5.fragment(email.html_part.body.to_s).css(".more-messages-link")
expect(more_messages_channel_link.text).to include(
I18n.t("user_notifications.chat_summary.view_messages", count: 3),
)
end
end
end
it "doesn't repeat mentions we already sent" do
user_membership.update!(
last_read_message_id: chat_message.id - 1,