discourse/plugins/chat/spec/jobs/regular/chat_notify_watching_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

313 lines
10 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
RSpec.describe Jobs::ChatNotifyWatching do
fab!(:user1) { Fabricate(:user) }
fab!(:user2) { Fabricate(:user) }
fab!(:user3) { Fabricate(:user) }
fab!(:group) { Fabricate(:group) }
let(:except_user_ids) { [] }
before do
SiteSetting.chat_enabled = true
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
end
def run_job
described_class.new.execute(chat_message_id: message.id, except_user_ids: except_user_ids)
end
def notification_messages_for(user)
MessageBus
.track_publish { run_job }
.filter { |m| m.channel == "/chat/notification-alert/#{user.id}" }
end
context "for a category channel" do
fab!(:channel) { Fabricate(:category_channel) }
fab!(:membership1) do
Fabricate(:user_chat_channel_membership, user: user1, chat_channel: channel)
end
fab!(:membership2) do
Fabricate(:user_chat_channel_membership, user: user2, chat_channel: channel)
end
fab!(:membership3) do
Fabricate(:user_chat_channel_membership, user: user3, chat_channel: channel)
end
fab!(:message) do
Fabricate(:chat_message, chat_channel: channel, user: user1, message: "this is a new message")
end
before do
membership2.update!(
desktop_notification_level: UserChatChannelMembership::NOTIFICATION_LEVELS[:always],
)
end
it "sends a desktop notification" do
messages = notification_messages_for(user2)
expect(messages.first.data).to include(
{
username: user1.username,
notification_type: Notification.types[:chat_message],
post_url: channel.relative_url,
translated_title:
I18n.t(
"discourse_push_notifications.popup.new_chat_message",
{ username: user1.username, channel: channel.title(user2) },
),
tag: Chat::ChatNotifier.push_notification_tag(:message, channel.id),
excerpt: message.message,
},
)
end
context "when the channel is muted via membership preferences" do
before { membership2.update!(muted: true) }
it "does not send a desktop or mobile notification" do
PostAlerter.expects(:push_notification).never
messages = notification_messages_for(user2)
expect(messages).to be_empty
end
end
context "when mobile_notification_level is always and desktop_notification_level is none" do
before do
membership2.update!(
desktop_notification_level: UserChatChannelMembership::NOTIFICATION_LEVELS[:never],
mobile_notification_level: UserChatChannelMembership::NOTIFICATION_LEVELS[:always],
)
end
it "sends a mobile notification" do
PostAlerter.expects(:push_notification).with(
user2,
has_entries(
{
username: user1.username,
notification_type: Notification.types[:chat_message],
post_url: channel.relative_url,
translated_title:
I18n.t(
"discourse_push_notifications.popup.new_chat_message",
{ username: user1.username, channel: channel.title(user2) },
),
tag: Chat::ChatNotifier.push_notification_tag(:message, channel.id),
excerpt: message.message,
},
),
)
messages = notification_messages_for(user2)
expect(messages.length).to be_zero
end
context "when the channel is muted via membership preferences" do
before { membership2.update!(muted: true) }
it "does not send a desktop or mobile notification" do
PostAlerter.expects(:push_notification).never
messages = notification_messages_for(user2)
expect(messages).to be_empty
end
end
end
context "when the target user cannot chat" do
before { SiteSetting.chat_allowed_groups = group.id }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
context "when the target user cannot see the chat channel" do
before { channel.update!(chatable: Fabricate(:private_category, group: group)) }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
context "when the target user has seen the message already" do
before { membership2.update!(last_read_message_id: message.id) }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
context "when the target user is online via presence channel" do
before { PresenceChannel.any_instance.expects(:user_ids).returns([user2.id]) }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
context "when the target user is suspended" do
before { user2.update!(suspended_till: 1.year.from_now) }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
context "when the target user is inside the except_user_ids array" do
let(:except_user_ids) { [user2.id] }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
end
context "for a direct message channel" do
DEV: start glimmer-ification and optimisations of chat plugin (#19531) Note this is a very large PR, and some of it could have been splited, but keeping it one chunk made it to merge conflicts and to revert if necessary. Actual new code logic is also not that much, as most of the changes are removing js tests, adding system specs or moving things around. To make it possible this commit is doing the following changes: - converting (and adding new) existing js acceptances tests into system tests. This change was necessary to ensure as little regressions as possible while changing paradigm - moving away from store. Using glimmer and tracked properties requires to have class objects everywhere and as a result works well with models. However store/adapters are suffering from many bugs and limitations. As a workaround the `chat-api` and `chat-channels-manager` are an answer to this problem by encapsulating backend calls and frontend storage logic; while still using js models. - dropping `appEvents` as much as possible. Using tracked properties and a better local storage of channel models, allows to be much more reactive and doesn’t require arbitrary manual updates everywhere in the app. - while working on replacing store, the existing work of a chat api (backend) has been continued to support more cases. - removing code from the `chat` service to separate concerns, `chat-subscriptions-manager` and `chat-channels-manager`, being the largest examples of where the code has been rewritten/moved. Future wok: - improve behavior when closing/deleting a channel, it's already slightly buggy on live, it's rare enough that it's not a big issue, but should be improved - improve page objects used in chat - move more endpoints to the API - finish temporarily skipped tests - extract more code from the `chat` service - use glimmer for `chat-messages` - separate concerns in `chat-live-pane` - eventually add js tests for `chat-api`, `chat-channels-manager` and `chat-subscriptions-manager`, they are indirectly heavy tested through system tests but it would be nice to at least test the public API <!-- NOTE: All pull requests should have tests (rspec in Ruby, qunit in JavaScript). If your code does not include test coverage, please include an explanation of why it was omitted. -->
2022-12-21 07:21:02 -05:00
fab!(:channel) do
Fabricate(:direct_message_channel, users: [user1, user2, user3], with_membership: false)
end
fab!(:membership1) do
Fabricate(:user_chat_channel_membership, user: user1, chat_channel: channel)
end
fab!(:membership2) do
Fabricate(:user_chat_channel_membership, user: user2, chat_channel: channel)
end
fab!(:membership3) do
Fabricate(:user_chat_channel_membership, user: user3, chat_channel: channel)
end
fab!(:message) { Fabricate(:chat_message, chat_channel: channel, user: user1) }
before do
membership2.update!(
desktop_notification_level: UserChatChannelMembership::NOTIFICATION_LEVELS[:always],
)
end
it "sends a desktop notification" do
messages = notification_messages_for(user2)
expect(messages.first.data).to include(
{
username: user1.username,
notification_type: Notification.types[:chat_message],
post_url: channel.relative_url,
translated_title:
I18n.t(
"discourse_push_notifications.popup.new_direct_chat_message",
{ username: user1.username, channel: channel.title(user2) },
),
tag: Chat::ChatNotifier.push_notification_tag(:message, channel.id),
excerpt: message.message,
},
)
end
context "when the channel is muted via membership preferences" do
before { membership2.update!(muted: true) }
it "does not send a desktop or mobile notification" do
PostAlerter.expects(:push_notification).never
messages = notification_messages_for(user2)
expect(messages).to be_empty
end
end
context "when mobile_notification_level is always and desktop_notification_level is none" do
before do
membership2.update!(
desktop_notification_level: UserChatChannelMembership::NOTIFICATION_LEVELS[:never],
mobile_notification_level: UserChatChannelMembership::NOTIFICATION_LEVELS[:always],
)
end
it "sends a mobile notification" do
PostAlerter.expects(:push_notification).with(
user2,
has_entries(
{
username: user1.username,
notification_type: Notification.types[:chat_message],
post_url: channel.relative_url,
translated_title:
I18n.t(
"discourse_push_notifications.popup.new_direct_chat_message",
{ username: user1.username, channel: channel.title(user2) },
),
tag: Chat::ChatNotifier.push_notification_tag(:message, channel.id),
excerpt: message.message,
},
),
)
messages = notification_messages_for(user2)
expect(messages.length).to be_zero
end
context "when the channel is muted via membership preferences" do
before { membership2.update!(muted: true) }
it "does not send a desktop or mobile notification" do
PostAlerter.expects(:push_notification).never
messages = notification_messages_for(user2)
expect(messages).to be_empty
end
end
end
context "when the target user cannot chat" do
before { SiteSetting.chat_allowed_groups = group.id }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
context "when the target user cannot see the chat channel" do
before { membership2.destroy! }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
context "when the target user has seen the message already" do
before { membership2.update!(last_read_message_id: message.id) }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
context "when the target user is online via presence channel" do
before { PresenceChannel.any_instance.expects(:user_ids).returns([user2.id]) }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
context "when the target user is suspended" do
before { user2.update!(suspended_till: 1.year.from_now) }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
context "when the target user is inside the except_user_ids array" do
let(:except_user_ids) { [user2.id] }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
context "when the target user is preventing communication from the message creator" do
before { UserCommScreener.any_instance.expects(:allowing_actor_communication).returns([]) }
it "does not send a desktop notification" do
expect(notification_messages_for(user2).count).to be_zero
end
end
end
end