From a5823e3a0528f07e1652aa5935ff2ad23f8224c3 Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Fri, 24 Mar 2023 21:11:50 +0400 Subject: [PATCH] FEATURE: Make user status on mentions on docs live (#117) This makes status on mentions in docs receive live updates from the server. --- assets/javascripts/discourse/models/docs.js | 10 +++ .../acceptance/docs-user-status-test.js | 63 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 test/javascripts/acceptance/docs-user-status-test.js diff --git a/assets/javascripts/discourse/models/docs.js b/assets/javascripts/discourse/models/docs.js index 48508f2..acf9db1 100644 --- a/assets/javascripts/discourse/models/docs.js +++ b/assets/javascripts/discourse/models/docs.js @@ -1,6 +1,7 @@ import EmberObject from "@ember/object"; import { ajax } from "discourse/lib/ajax"; import Topic from "discourse/models/topic"; +import User from "discourse/models/user"; import { getDocs } from "../../lib/get-docs"; const Docs = EmberObject.extend({}); @@ -41,6 +42,9 @@ Docs.reopenClass({ (topic) => Topic.create(topic) ); data.topic = Topic.create(data.topic); + data.topic.post_stream?.posts.forEach((post) => + this._initUserModels(post) + ); return data; }); }, @@ -53,6 +57,12 @@ Docs.reopenClass({ return data; }); }, + + _initUserModels(post) { + if (post.mentioned_users) { + post.mentioned_users = post.mentioned_users.map((u) => User.create(u)); + } + }, }); export default Docs; diff --git a/test/javascripts/acceptance/docs-user-status-test.js b/test/javascripts/acceptance/docs-user-status-test.js new file mode 100644 index 0000000..b13ce36 --- /dev/null +++ b/test/javascripts/acceptance/docs-user-status-test.js @@ -0,0 +1,63 @@ +import { + acceptance, + publishToMessageBus, +} from "discourse/tests/helpers/qunit-helpers"; +import { test } from "qunit"; +import docsFixtures from "../fixtures/docs"; +import { visit } from "@ember/test-helpers"; + +acceptance("Docs - user status", function (needs) { + needs.user(); + needs.site({ docs_path: "docs" }); + needs.settings({ + docs_enabled: true, + }); + + const mentionedUserId = 1; + + needs.pretender((server, helper) => { + server.get("/docs.json", () => { + docsFixtures.topic = { + post_stream: { + posts: [ + { + id: 427, + topic_id: 1, + username: "admin1", + cooked: + '

This is a document.

\n

I am mentioning another user @andrei4

', + mentioned_users: [ + { + id: mentionedUserId, + username: "andrei4", + name: "andrei", + avatar_template: + "/letter_avatar_proxy/v4/letter/a/a87d85/{size}.png", + assign_icon: "user-plus", + assign_path: "/u/andrei4/activity/assigned", + }, + ], + }, + ], + stream: [427], + }, + }; + + return helper.response(docsFixtures); + }); + }); + + test("user status on mentions is live", async function (assert) { + await visit("/docs?topic=1"); + assert.dom(".mention .user-status").doesNotExist(); + + const newStatus = { emoji: "surfing_man", description: "surfing" }; + await publishToMessageBus(`/user-status`, { [mentionedUserId]: newStatus }); + assert + .dom(".mention .user-status") + .hasAttribute("title", newStatus.description); + + await publishToMessageBus(`/user-status`, { [mentionedUserId]: null }); + assert.dom(".mention .user-status").doesNotExist(); + }); +});