# frozen_string_literal: true describe UserNotifications do fab!(:user) { Fabricate(:user, last_seen_at: 1.hour.ago) } fab!(:other) { Fabricate(:user) } fab!(:another) { Fabricate(:user) } fab!(:someone) { Fabricate(:user) } fab!(:group) { Fabricate(:group, users: [user, other]) } fab!(:followed_channel) { Fabricate(:category_channel) } fab!(:followed_channel_2) { Fabricate(:category_channel) } fab!(:followed_channel_3) { Fabricate(:category_channel) } fab!(:non_followed_channel) { Fabricate(:category_channel) } fab!(:muted_channel) { Fabricate(:category_channel) } fab!(:unseen_channel) { Fabricate(:category_channel) } fab!(:private_channel) { Fabricate(:private_category_channel, group:) } fab!(:direct_message) { Fabricate(:direct_message_channel, users: [user, other]) } fab!(:direct_message_2) { Fabricate(:direct_message_channel, users: [user, another]) } fab!(:direct_message_3) { Fabricate(:direct_message_channel, users: [user, someone]) } fab!(:group_message) { Fabricate(:direct_message_channel, users: [user, other, another]) } fab!(:site_name) { SiteSetting.email_prefix.presence || SiteSetting.title } before do SiteSetting.chat_enabled = true SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone] end def create_message(chat_channel, message, mention_klass = nil) chat_message = Fabricate(:chat_message, user: other, chat_channel:, message:) if mention_klass notification_type = Notification.types[:chat_mention] Fabricate( :chat_mention_notification, notification: Fabricate(:notification, user:, notification_type:), chat_mention: mention_klass.find_by(chat_message:), ) end chat_message end def no_chat_summary_email email = described_class.chat_summary(user, {}) expect(email.to).to be_blank end def chat_summary_email email = described_class.chat_summary(user, {}) expect(email.to).to contain_exactly(user.email) email end def chat_summary_with_subject(type, opts = {}) expect(chat_summary_email.subject).to eq( I18n.t("user_notifications.chat_summary.subject.#{type}", { site_name:, **opts }), ) end describe "in a followed channel" do before { followed_channel.add(user) } describe "user is mentioned" do let!(:chat_mention) do create_message(followed_channel, "hello @#{user.username}", Chat::UserMention) end it "sends a chat summary email" do chat_summary_with_subject(:chat_channel_1, channel: followed_channel.name, count: 1) end it "pluralizes the subject" do create_message(followed_channel, "how are you?") chat_summary_with_subject(:chat_channel_1, channel: followed_channel.name, count: 2) end it "sends a chat summary email with correct body" do html = chat_summary_email.html_part.body.to_s expect(html).to include(followed_channel.title(user)) expect(html).to include(chat_mention.full_url) expect(html).to include(PrettyText.format_for_email(chat_mention.cooked_for_excerpt)) expect(html).to include(chat_mention.user.small_avatar_url) expect(html).to include(chat_mention.user.username) expect(html).to include( I18n.l(UserOption.user_tzinfo(user.id).to_local(chat_mention.created_at), format: :long), ) expect(html).to include(I18n.t("user_notifications.chat_summary.view_messages", count: 1)) end it "sends a chat summary email with view more link" do create_message(followed_channel, "how are you...") create_message(followed_channel, "doing...") create_message(followed_channel, "today?") html = chat_summary_email.html_part.body.to_s expect(html).to include(I18n.t("user_notifications.chat_summary.view_more", count: 2)) end describe "SiteSetting.prioritize_username_in_ux is disabled" do before { SiteSetting.prioritize_username_in_ux = false } it "sends a chat summary email with the username instead of the name" do html = chat_summary_email.html_part.body.to_s expect(html).to include(chat_mention.user.name) expect(html).not_to include(chat_mention.user.username) end end describe "when using subfolder" do before { set_subfolder "/community" } it "sends a chat summary email with the correct URL" do html = chat_summary_email.html_part.body.to_s expect(html).to include <<~HTML.strip