2023-02-16 23:55:18 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
|
2023-05-05 19:47:07 +08:00
|
|
|
RSpec.describe Chat::ParsedMentions do
|
2023-02-16 23:55:18 +08:00
|
|
|
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")
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-02-16 23:55:18 +08:00
|
|
|
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}")
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-02-16 23:55:18 +08:00
|
|
|
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")
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-02-16 23:55:18 +08:00
|
|
|
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")
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-02-16 23:55:18 +08:00
|
|
|
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}")
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-02-16 23:55:18 +08:00
|
|
|
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")
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-02-16 23:55:18 +08:00
|
|
|
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}")
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-02-16 23:55:18 +08:00
|
|
|
result = mentions.direct_mentions.pluck(:username)
|
|
|
|
|
|
|
|
expect(result).to contain_exactly(channel_member_1.username, channel_member_2.username)
|
|
|
|
end
|
|
|
|
|
2023-05-11 23:30:26 +08:00
|
|
|
it "returns a user when self-mentioning" do
|
2023-08-24 21:22:51 +08:00
|
|
|
message = create_message("Hey @#{channel_member_1.username}", user: channel_member_1)
|
2023-05-11 23:30:26 +08:00
|
|
|
|
|
|
|
mentions = described_class.new(message)
|
|
|
|
|
|
|
|
result = mentions.direct_mentions.pluck(:username)
|
|
|
|
expect(result).to contain_exactly(channel_member_1.username)
|
|
|
|
end
|
|
|
|
|
2023-02-16 23:55:18 +08:00
|
|
|
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}")
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-02-16 23:55:18 +08:00
|
|
|
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")
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-02-16 23:55:18 +08:00
|
|
|
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}")
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-02-16 23:55:18 +08:00
|
|
|
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
|
2023-03-07 23:07:11 +08:00
|
|
|
message = create_message("not mentioning anyone")
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-03-07 23:07:11 +08:00
|
|
|
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}")
|
2023-02-16 23:55:18 +08:00
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
mentions = described_class.new(message)
|
2023-02-16 23:55:18 +08:00
|
|
|
result = mentions.group_mentions.pluck(:username)
|
|
|
|
|
|
|
|
expect(result).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-24 21:22:51 +08:00
|
|
|
def create_message(text, **extra_args)
|
|
|
|
Fabricate(:chat_message, chat_channel: chat_channel, message: text, **extra_args)
|
2023-02-16 23:55:18 +08:00
|
|
|
end
|
|
|
|
end
|