Skip to content

A platform for community discussion. Free, open, simple.

FIX: dynamic highlighting of self mentions (#37321)

David Battersby9335e71f81f3
diff --git a/app/assets/stylesheets/common/foundation/mixins.scss b/app/assets/stylesheets/common/foundation/mixins.scss
index b000e9e4b79..6cb6373f36d 100644
--- a/app/assets/stylesheets/common/foundation/mixins.scss
+++ b/app/assets/stylesheets/common/foundation/mixins.scss
@@ -270,10 +270,6 @@ $hpad: 0.65em;
&.--wide {
background: var(--highlight-low-or-medium);
}
-
- &.--current {
- background: var(--tertiary-400);
- }
}
@mixin nav-active() {
diff --git a/frontend/discourse/app/instance-initializers/current-user-mention-css.js b/frontend/discourse/app/instance-initializers/current-user-mention-css.js
new file mode 100644
index 00000000000..8ced39a1cd2
--- /dev/null
+++ b/frontend/discourse/app/instance-initializers/current-user-mention-css.js
@@ -0,0 +1,20 @@
+import getURL from "discourse/lib/get-url";
+import { withPluginApi } from "discourse/lib/plugin-api";
+
+export default {
+ before: "hashtag-css-generator",
+
+ initialize() {
+ withPluginApi((api) => {
+ const currentUser = api.getCurrentUser();
+
+ if (currentUser) {
+ const href = getURL(`/u/${currentUser.username.toLowerCase()}`);
+ const style = document.createElement("style");
+ style.id = "current-user-mention-css";
+ style.textContent = `.mention[href="${href}"] { background: var(--tertiary-400); }`;
+ document.head.appendChild(style);
+ }
+ });
+ },
+};
diff --git a/frontend/discourse/app/instance-initializers/highlight-users.js b/frontend/discourse/app/instance-initializers/highlight-users.js
index 0babc50f27d..f75985f463b 100644
--- a/frontend/discourse/app/instance-initializers/highlight-users.js
+++ b/frontend/discourse/app/instance-initializers/highlight-users.js
@@ -8,8 +8,6 @@ export default {
if (user.id < 0) {
value.push("--bot");
- } else if (user.id === api.getCurrentUser()?.id) {
- value.push("--current");
} else if (user.username === "here" || user.username === "all") {
value.push("--wide");
}
diff --git a/frontend/discourse/tests/acceptance/current-user-mention-css-test.js b/frontend/discourse/tests/acceptance/current-user-mention-css-test.js
new file mode 100644
index 00000000000..3515ccfc9c3
--- /dev/null
+++ b/frontend/discourse/tests/acceptance/current-user-mention-css-test.js
@@ -0,0 +1,17 @@
+import { visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+
+acceptance("Current User Mention CSS", function (needs) {
+ needs.user({ username: "eviltrout" });
+
+ test("css is generated for current user mentions", async function (assert) {
+ await visit("/");
+ const cssTag = document.querySelector("style#current-user-mention-css");
+ assert
+ .dom(cssTag)
+ .hasText(
+ `.mention[href="/u/eviltrout"] { background: var(--tertiary-400); }`
+ );
+ });
+});
diff --git a/frontend/discourse/tests/acceptance/transformers/mentions-class-test.js b/frontend/discourse/tests/acceptance/transformers/mentions-class-test.js
index 4be9c5f43ab..6d197422d5a 100644
--- a/frontend/discourse/tests/acceptance/transformers/mentions-class-test.js
+++ b/frontend/discourse/tests/acceptance/transformers/mentions-class-test.js
@@ -1,13 +1,27 @@
import { visit } from "@ember/test-helpers";
import { test } from "qunit";
+import { cloneJSON } from "discourse/lib/object";
+import topicFixtures from "discourse/tests/fixtures/topic";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
acceptance("mentions-class transformer", function (needs) {
needs.user();
+ needs.pretender((server, helper) => {
+ server.get("/t/280.json", () => {
+ const topic = cloneJSON(topicFixtures["/t/280/1.json"]);
+ topic.post_stream.posts[0].cooked =
+ '<p>Hello <a class="mention" href="/u/system">@system</a>, welcome!</p>';
+ topic.post_stream.posts[0].mentioned_users = [
+ { id: -1, username: "system" },
+ ];
+ return helper.response(topic);
+ });
+ });
+
test("applying a value transformation", async function (assert) {
await visit("/t/internationalization-localization/280");
- assert.dom(".mention[href='/u/eviltrout']").hasClass("--current");
+ assert.dom("a.mention[href='/u/system']").hasClass("--bot");
});
});
diff --git a/plugins/chat/assets/stylesheets/common/chat-message.scss b/plugins/chat/assets/stylesheets/common/chat-message.scss
index 9fc875a6d55..4a31602c0c2 100644
--- a/plugins/chat/assets/stylesheets/common/chat-message.scss
+++ b/plugins/chat/assets/stylesheets/common/chat-message.scss
@@ -78,10 +78,12 @@
width: 100%;
}
- a.mention,
- span.mention.--wide {
+ span.mention {
@include mention;
+ }
+ .mention,
+ span.mention.--wide {
&.highlighted {
background: var(--tertiary-low);
}
diff --git a/plugins/chat/spec/system/chat_channel_spec.rb b/plugins/chat/spec/system/chat_channel_spec.rb
index 29bafdf380b..eef4550763c 100644
--- a/plugins/chat/spec/system/chat_channel_spec.rb
+++ b/plugins/chat/spec/system/chat_channel_spec.rb
@@ -201,7 +201,7 @@ RSpec.describe "Chat channel", type: :system do
expect(page).to have_selector(".mention.--wide", text: "@here")
expect(page).to have_selector(".mention.--wide", text: "@all")
- expect(page).to have_selector(".mention.--current", text: "@#{current_user.username}")
+ expect(page).to have_selector(".mention", text: "@#{current_user.username}")
expect(page).to have_selector(".mention", text: "@#{other_user.username}")
expect(page).to have_selector(".mention", text: "@unexisting")
expect(page).to have_selector(".mention.--bot", text: "@system")