FEATURE: Show live user status on inline mentions on posts (#18683)

Note that we don't have a database table and a model for post mentions yet, and I decided to implement it without adding one to avoid heavy data migrations. Still, we may want to add such a model later, that would be convenient, we have such a model for mentions in chat.

Note that status appears on all mentions on all posts in a topic except of the case when you just posted a new post, and it appeared on the bottom of the topic. On such posts, status won't be shown immediately for now (you'll need to reload the page to see the status). I'll take care of it in one of the following PRs.
This commit is contained in:
Andrei Prigorshnev
2022-12-06 19:10:36 +04:00
committed by GitHub
parent d247e5d37c
commit a76d864c51
11 changed files with 400 additions and 20 deletions

View File

@@ -200,6 +200,11 @@
},
"reviewable_score_pending_count": {
"type": "integer"
},
"mentioned_users": {
"type": "array",
"items": {
}
}
},
"required": [

View File

@@ -2684,6 +2684,91 @@ RSpec.describe TopicsController do
expect(response.status).to eq(200)
end
context "with mentions" do
fab!(:post) { Fabricate(:post, user: post_author1) }
fab!(:topic) { post.topic }
fab!(:post2) { Fabricate(
:post,
user: post_author2,
topic: topic,
raw: "I am mentioning @#{post_author1.username}."
) }
it "returns mentions" do
get "/t/#{topic.slug}/#{topic.id}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["post_stream"]["posts"][1]["mentioned_users"].length).to be(1)
mentioned_user = json["post_stream"]["posts"][1]["mentioned_users"][0]
expect(mentioned_user["id"]).to be(post_author1.id)
expect(mentioned_user["name"]).to eq(post_author1.name)
expect(mentioned_user["username"]).to eq(post_author1.username)
end
it "doesn't return status on mentions by default" do
post_author1.set_status!("off to dentist", "tooth")
get "/t/#{topic.slug}/#{topic.id}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["post_stream"]["posts"][1]["mentioned_users"].length).to be(1)
status = json["post_stream"]["posts"][1]["mentioned_users"][0]["status"]
expect(status).to be_nil
end
it "returns mentions with status if user status is enabled" do
SiteSetting.enable_user_status = true
post_author1.set_status!("off to dentist", "tooth")
get "/t/#{topic.slug}/#{topic.id}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["post_stream"]["posts"][1]["mentioned_users"].length).to be(1)
status = json["post_stream"]["posts"][1]["mentioned_users"][0]["status"]
expect(status).to be_present
expect(status["emoji"]).to eq(post_author1.user_status.emoji)
expect(status["description"]).to eq(post_author1.user_status.description)
end
it "returns an empty list of mentioned users if there is no mentions in a post" do
Fabricate(
:post,
user: post_author2,
topic: topic,
raw: "Post without mentions.")
get "/t/#{topic.slug}/#{topic.id}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["post_stream"]["posts"][2]["mentioned_users"].length).to be(0)
end
it "returns an empty list of mentioned users if an unexisting user was mentioned" do
Fabricate(
:post,
user: post_author2,
topic: topic,
raw: "Mentioning an @unexisting_user.")
get "/t/#{topic.slug}/#{topic.id}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["post_stream"]["posts"][2]["mentioned_users"].length).to be(0)
end
end
describe "has_escaped_fragment?" do
context "when the SiteSetting is disabled" do
it "uses the application layout even with an escaped fragment param" do