mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 21:23:59 +08:00
e1ae32103d
This is extracted from #22390. This patch aims to ease the transition to the new message creation service. (in progress in #22390) Indeed, the new service patch is breaking some specs from `discourse-ai` and `discourse-templates` because these plugins are using either `Chat::MessageCreator` or the `chat_message` fabricator. This patch addresses theses issues by normalizing how we create a chat message in specs. To do so, the preferred way is to use `Fabricate(:chat_message)` with a new `:use_service` option allowing to call the service under the hood. While this patch will obviously call `Chat::MessageCreator`, the new service patch will now be able to simply change the call to `Chat::CreateMessage` without breaking any specs from other plugins. Another thing this patch does is to not create chat messages using the service for specs that aren’t system ones, thus speeding the execution time a bit in the process.
175 lines
5.5 KiB
Ruby
175 lines
5.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "rails_helper"
|
|
|
|
RSpec.describe Chat::ParsedMentions do
|
|
fab!(:channel_member_1) { Fabricate(:user) }
|
|
fab!(:channel_member_2) { Fabricate(:user) }
|
|
fab!(:channel_member_3) { Fabricate(:user) }
|
|
fab!(:not_a_channel_member) { Fabricate(:user) }
|
|
fab!(:chat_channel) { Fabricate(:chat_channel) }
|
|
|
|
before do
|
|
chat_channel.add(channel_member_1)
|
|
chat_channel.add(channel_member_2)
|
|
chat_channel.add(channel_member_3)
|
|
end
|
|
|
|
describe "#global_mentions" do
|
|
it "returns all members of the channel" do
|
|
message = create_message("mentioning @all")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.global_mentions.pluck(:username)
|
|
|
|
expect(result).to contain_exactly(
|
|
channel_member_1.username,
|
|
channel_member_2.username,
|
|
channel_member_3.username,
|
|
)
|
|
end
|
|
|
|
it "doesn't include users that were also mentioned directly" do
|
|
message = create_message("mentioning @all and @#{channel_member_1.username}")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.global_mentions.pluck(:username)
|
|
|
|
expect(result).to contain_exactly(channel_member_2.username, channel_member_3.username)
|
|
end
|
|
|
|
it "returns an empty list if there are no global mentions" do
|
|
message = create_message("not mentioning anybody")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.global_mentions.pluck(:username)
|
|
|
|
expect(result).to be_empty
|
|
end
|
|
end
|
|
|
|
describe "#here_mentions" do
|
|
before do
|
|
freeze_time
|
|
channel_member_1.update(last_seen_at: 2.minutes.ago)
|
|
channel_member_2.update(last_seen_at: 2.minutes.ago)
|
|
channel_member_3.update(last_seen_at: 5.minutes.ago)
|
|
end
|
|
|
|
it "returns all members of the channel who were online in the last 5 minutes" do
|
|
message = create_message("mentioning @here")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.here_mentions.pluck(:username)
|
|
|
|
expect(result).to contain_exactly(channel_member_1.username, channel_member_2.username)
|
|
end
|
|
|
|
it "doesn't include users that were also mentioned directly" do
|
|
message = create_message("mentioning @here and @#{channel_member_1.username}")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.here_mentions.pluck(:username)
|
|
|
|
expect(result).to contain_exactly(channel_member_2.username)
|
|
end
|
|
|
|
it "returns an empty list if there are no here mentions" do
|
|
message = create_message("not mentioning anybody")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.here_mentions.pluck(:username)
|
|
|
|
expect(result).to be_empty
|
|
end
|
|
end
|
|
|
|
describe "#direct_mentions" do
|
|
it "returns users who were mentioned directly" do
|
|
message =
|
|
create_message("mentioning @#{channel_member_1.username} and @#{channel_member_2.username}")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.direct_mentions.pluck(:username)
|
|
|
|
expect(result).to contain_exactly(channel_member_1.username, channel_member_2.username)
|
|
end
|
|
|
|
it "returns a user when self-mentioning" do
|
|
message = create_message("Hey @#{channel_member_1.username}", user: channel_member_1)
|
|
|
|
mentions = described_class.new(message)
|
|
|
|
result = mentions.direct_mentions.pluck(:username)
|
|
expect(result).to contain_exactly(channel_member_1.username)
|
|
end
|
|
|
|
it "returns a mentioned user even if he's not a member of the channel" do
|
|
message = create_message("mentioning @#{not_a_channel_member.username}")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.direct_mentions.pluck(:username)
|
|
|
|
expect(result).to contain_exactly(not_a_channel_member.username)
|
|
end
|
|
|
|
it "returns an empty list if no one was mentioned directly" do
|
|
message = create_message("not mentioning anybody")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.direct_mentions.pluck(:username)
|
|
|
|
expect(result).to be_empty
|
|
end
|
|
end
|
|
|
|
describe "#group_mentions" do
|
|
fab!(:group1) { Fabricate(:group, mentionable_level: Group::ALIAS_LEVELS[:everyone]) }
|
|
fab!(:group_member_1) { Fabricate(:user, group_ids: [group1.id]) }
|
|
fab!(:group_member_2) { Fabricate(:user, group_ids: [group1.id]) }
|
|
fab!(:group_member_3) { Fabricate(:user, group_ids: [group1.id]) }
|
|
|
|
before do
|
|
chat_channel.add(group_member_1)
|
|
chat_channel.add(group_member_2)
|
|
end
|
|
|
|
it "returns members of a mentioned group even if some of them is not members of the channel" do
|
|
message = create_message("mentioning @#{group1.name}")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.group_mentions.pluck(:username)
|
|
|
|
expect(result).to contain_exactly(
|
|
group_member_1.username,
|
|
group_member_2.username,
|
|
group_member_3.username,
|
|
)
|
|
end
|
|
|
|
it "returns an empty list if no group was mentioned" do
|
|
message = create_message("not mentioning anyone")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.group_mentions.pluck(:username)
|
|
|
|
expect(result).to be_empty
|
|
end
|
|
|
|
it "returns an empty list when mentioning an unmentionable group" do
|
|
group1.mentionable_level = Group::ALIAS_LEVELS[:nobody]
|
|
group1.save!
|
|
message = create_message("mentioning @#{group1.name}")
|
|
|
|
mentions = described_class.new(message)
|
|
result = mentions.group_mentions.pluck(:username)
|
|
|
|
expect(result).to be_empty
|
|
end
|
|
end
|
|
|
|
def create_message(text, **extra_args)
|
|
Fabricate(:chat_message, chat_channel: chat_channel, message: text, **extra_args)
|
|
end
|
|
end
|