A platform for community discussion. Free, open, simple.
FIX: add mention warnings for composer rich text (#33857)
David Battersby9e59e38e9f43
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjs b/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjsindex 1ed1f9a810c..7574fae98ca 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjs+++ b/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjs@@ -73,6 +73,7 @@ export default class ProsemirrorEditor extends Component {@service toasts;@service site;@service siteSettings;+ @service appEvents;schema = createSchema(this.extensions, this.args.includeDefault);view;@@ -109,6 +110,7 @@ export default class ProsemirrorEditor extends Component {toasts: this.toasts,site: this.site,siteSettings: this.siteSettings,+ appEvents: this.appEvents,replaceToolbar: this.args.replaceToolbar,addGlimmerNodeView: (nodeView) => this.glimmerNodeViews.push(nodeView),removeGlimmerNodeView: (nodeView) =>diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/mention.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/mention.jsindex b8c7982e200..c141cc4fe39 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/mention.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/mention.js@@ -1,6 +1,8 @@import { mentionRegex } from "pretty-text/mentions";import { ajax } from "discourse/lib/ajax";+import getURL from "discourse/lib/get-url";import { isBoundary } from "discourse/static/prosemirror/lib/markdown-it";+import { i18n } from "discourse-i18n";const VALID_MENTIONS = new Set();const INVALID_MENTIONS = new Set();@@ -92,7 +94,7 @@ const extension = {},},- plugins({ pmState: { Plugin, PluginKey } }) {+ plugins({ pmState: { Plugin, PluginKey }, getContext }) {const key = new PluginKey("mention");return new Plugin({@@ -126,7 +128,7 @@ const extension = {mentionNodes.sort((a, b) => b.pos - a.pos);const invalidateMentions = async () => {- await fetchMentions(mentionNames);+ await fetchMentions(mentionNames, getContext());for (const mentionNode of mentionNodes) {const { name, node, pos } = mentionNode;@@ -153,7 +155,7 @@ const extension = {},};-async function fetchMentions(names) {+async function fetchMentions(names, context) {// only fetch new mentions that are not already validatednames = names.uniq().filter((name) => {return !VALID_MENTIONS.has(name) && !INVALID_MENTIONS.has(name);@@ -164,7 +166,7 @@ async function fetchMentions(names) {}const response = await ajax("/composer/mentions", {- data: { names },+ data: { names, topic_id: context.topicId },});const lowerGroupNames = Object.keys(response.groups).map((groupName) =>@@ -182,7 +184,73 @@ async function fetchMentions(names) {} else {INVALID_MENTIONS.add(name);}++ checkMentionWarning(name, response, context);});}+function checkMentionWarning(name, response, context) {+ const hereCount = parseInt(response?.here_count, 10) || 0;+ const maxMentions = parseInt(+ response?.max_users_notified_per_group_mention,+ 10+ );++ let reason;+ let body;++ if (hereCount > 0) {+ body = i18n(`composer.here_mention`, {+ here: context.siteSettings.here_mention,+ count: hereCount,+ });+ } else if (response.users.includes(name)) {+ reason = response.user_reasons?.[name];++ if (reason) {+ body = i18n(`composer.cannot_see_mention.${reason}`, {+ username: name,+ });+ }+ } else if (response.groups[name]) {+ const userCount = response.groups[name]?.user_count || 0;+ const notifiedCount = response.groups[name]?.notified_count || 0;+ reason = response.group_reasons?.[name];++ const groupLink = getURL(`/g/${name}/members`);++ if (reason) {+ body = i18n(`composer.cannot_see_group_mention.${reason}`, {+ group: name,+ count: notifiedCount,+ });+ } else if (notifiedCount > maxMentions) {+ body = i18n("composer.group_mentioned_limit", {+ group: `@${name}`,+ count: maxMentions,+ group_link: groupLink,+ });+ } else if (userCount > 0) {+ const translationKey =+ userCount >= 5+ ? "composer.larger_group_mentioned"+ : "composer.group_mentioned";++ body = i18n(translationKey, {+ group: `@${name}`,+ count: userCount,+ group_link: groupLink,+ });+ }+ }++ if (body) {+ context.appEvents.trigger("composer-messages:create", {+ extraClass: "custom-body",+ templateName: "education",+ body,+ });+ }+}+export default extension;diff --git a/app/assets/stylesheets/common/base/compose.scss b/app/assets/stylesheets/common/base/compose.scssindex 12b8331f822..5bbfcb7d1bb 100644--- a/app/assets/stylesheets/common/base/compose.scss+++ b/app/assets/stylesheets/common/base/compose.scss@@ -916,7 +916,7 @@ html.composer-open:not(.has-full-page-chat) {align-items: center;position: absolute;right: 0.5rem;- top: 0.5rem;+ top: 0;color: var(--primary-medium);font-size: var(--font-0);diff --git a/spec/system/composer/prosemirror_editor_spec.rb b/spec/system/composer/prosemirror_editor_spec.rbindex 8caa50bd65f..80e5766cb09 100644--- a/spec/system/composer/prosemirror_editor_spec.rb+++ b/spec/system/composer/prosemirror_editor_spec.rb@@ -131,6 +131,19 @@ describe "Composer - ProseMirror editor", type: :system doendend+ context "with composer messages" do+ fab!(:category)++ it "shows a popup" do+ open_composer+ composer.type_content("Maybe @staff can help?")++ expect(composer).to have_popup_content(+ I18n.t("js.composer.cannot_see_group_mention.not_mentionable", group: "staff"),+ )+ end+ end+context "with inputRules" doit "supports > to create a blockquote" doopen_composer