FEATURE: Add user status to inline mentions in chat (#20564)

This PR adds status to mentions in chat and makes those mentions receive live updates.

There are known unfinished part in this implementation: when posting a message, status on mentions on that message appears immediately, but only if a user used autocomplete when typing the message. If user copy and paste a message with mentions into chat composer, those mentions won't have user status on them.

PRs with fixes for both problems are following soon.

Preparations for this PR that were made previously include:
- DEV: correct a relationship – a chat message may have several mentions 0dcfd7ddeccd438fed97c15827214a3ddd944838
- DEV: extract the logic for extracting and expanding mentions from ChatNotifier 75b81b6854087842b53b4c9559ef5836d9516269
- DEV: Always create chat mention records fa543cda06594b5bebe0ab35e48e613540f9d3dc
- DEV: better split create_notification! and send_notifications logic e292c45924bb0b0a79497e5f494afcb8af2e1efc
- DEV: more tests for mentions when updating chat messages e7292e1682bb910635af26879c31a17b8843b738
- DEV: extract updating status on mentions into a lib function e49d338c21885189970e51a60ac79ab8c1fc397e
- DEV: Create and update chat message mentions earlier 35a414bb38b2ab990ff920f8c3adf2f877249f12
- DEV: Create a chat_mention record when self mentioning 2703f2311aefd87a7774316b50f5339626ea5b48
- DEV: When deleting a chat message, do not delete mention records f4fde4e49b03d5e8f01d02e2354c553c82f3402d
This commit is contained in:
Andrei Prigorshnev
2023-05-24 16:55:20 +04:00
committed by GitHub
parent 436b68a581
commit d4a5b79592
16 changed files with 845 additions and 9 deletions

View File

@ -136,6 +136,19 @@ describe Chat::MessageCreator do
expect(events.map { _1[:event_name] }).to include(:chat_message_created)
end
it "publishes created message to message bus" do
content = "a test chat message"
messages =
MessageBus.track_publish("/chat/#{public_chat_channel.id}") do
described_class.create(chat_channel: public_chat_channel, user: user1, content: content)
end
expect(messages.count).to be(1)
message = messages[0].data
expect(message["chat_message"]["message"]).to eq(content)
expect(message["chat_message"]["user"]["id"]).to eq(user1.id)
end
context "with mentions" do
it "creates mentions and mention notifications for public chat" do
message =
@ -405,6 +418,52 @@ describe Chat::MessageCreator do
mention = user2.chat_mentions.where(chat_message: message).first
expect(mention.notification).to be_nil
end
it "adds mentioned user and their status to the message bus message" do
SiteSetting.enable_user_status = true
status = { description: "dentist", emoji: "tooth" }
user2.set_status!(status[:description], status[:emoji])
messages =
MessageBus.track_publish("/chat/#{public_chat_channel.id}") do
described_class.create(
chat_channel: public_chat_channel,
user: user1,
content: "Hey @#{user2.username}",
)
end
expect(messages.count).to be(1)
message = messages[0].data
expect(message["chat_message"]["mentioned_users"].count).to be(1)
mentioned_user = message["chat_message"]["mentioned_users"][0]
expect(mentioned_user["id"]).to eq(user2.id)
expect(mentioned_user["username"]).to eq(user2.username)
expect(mentioned_user["status"]).to be_present
expect(mentioned_user["status"].slice(:description, :emoji)).to eq(status)
end
it "doesn't add mentioned user's status to the message bus message when status is disabled" do
SiteSetting.enable_user_status = false
user2.set_status!("dentist", "tooth")
messages =
MessageBus.track_publish("/chat/#{public_chat_channel.id}") do
described_class.create(
chat_channel: public_chat_channel,
user: user1,
content: "Hey @#{user2.username}",
)
end
expect(messages.count).to be(1)
message = messages[0].data
expect(message["chat_message"]["mentioned_users"].count).to be(1)
mentioned_user = message["chat_message"]["mentioned_users"][0]
expect(mentioned_user["status"]).to be_blank
end
end
it "creates a chat_mention record without notification when self mentioning" do

View File

@ -124,6 +124,23 @@ describe Chat::MessageUpdater do
expect(events.map { _1[:event_name] }).to include(:chat_message_edited)
end
it "publishes updated message to message bus" do
chat_message = create_chat_message(user1, "This will be changed", public_chat_channel)
new_content = "New content"
messages =
MessageBus.track_publish("/chat/#{public_chat_channel.id}") do
described_class.update(
guardian: guardian,
chat_message: chat_message,
new_content: new_content,
)
end
expect(messages.count).to be(1)
message = messages[0].data
expect(message["chat_message"]["message"]).to eq(new_content)
end
context "with mentions" do
it "sends notifications if a message was updated with new mentions" do
message = create_chat_message(user1, "Mentioning @#{user2.username}", public_chat_channel)
@ -228,6 +245,56 @@ describe Chat::MessageUpdater do
expect(mention.notification).to be_nil
end
it "adds mentioned user and their status to the message bus message" do
SiteSetting.enable_user_status = true
status = { description: "dentist", emoji: "tooth" }
user2.set_status!(status[:description], status[:emoji])
chat_message = create_chat_message(user1, "This will be updated", public_chat_channel)
new_content = "Hey @#{user2.username}"
messages =
MessageBus.track_publish("/chat/#{public_chat_channel.id}") do
described_class.update(
guardian: guardian,
chat_message: chat_message,
new_content: new_content,
)
end
expect(messages.count).to be(1)
message = messages[0].data
expect(message["chat_message"]["mentioned_users"].count).to be(1)
mentioned_user = message["chat_message"]["mentioned_users"][0]
expect(mentioned_user["id"]).to eq(user2.id)
expect(mentioned_user["username"]).to eq(user2.username)
expect(mentioned_user["status"]).to be_present
expect(mentioned_user["status"].slice(:description, :emoji)).to eq(status)
end
it "doesn't add mentioned user's status to the message bus message when status is disabled" do
SiteSetting.enable_user_status = false
user2.set_status!("dentist", "tooth")
chat_message = create_chat_message(user1, "This will be updated", public_chat_channel)
new_content = "Hey @#{user2.username}"
messages =
MessageBus.track_publish("/chat/#{public_chat_channel.id}") do
described_class.update(
guardian: guardian,
chat_message: chat_message,
new_content: new_content,
)
end
expect(messages.count).to be(1)
message = messages[0].data
expect(message["chat_message"]["mentioned_users"].count).to be(1)
mentioned_user = message["chat_message"]["mentioned_users"][0]
expect(mentioned_user["status"]).to be_blank
end
context "when updating a mentioned user" do
it "updates the mention record" do
chat_message = create_chat_message(user1, "ping @#{user2.username}", public_chat_channel)