From e49d338c21885189970e51a60ac79ab8c1fc397e Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Thu, 27 Apr 2023 16:02:21 +0400 Subject: [PATCH] DEV: extract updating status on mentions into a lib function (#21265) We'll be using this lib function for adding status to mentions in chat. It's covered with tests in post-inline-mentions-test.js. --- .../app/lib/update-user-status-on-mention.js | 36 +++++++++++++++++ .../discourse/app/widgets/post-cooked.js | 40 ++----------------- 2 files changed, 39 insertions(+), 37 deletions(-) create mode 100644 app/assets/javascripts/discourse/app/lib/update-user-status-on-mention.js diff --git a/app/assets/javascripts/discourse/app/lib/update-user-status-on-mention.js b/app/assets/javascripts/discourse/app/lib/update-user-status-on-mention.js new file mode 100644 index 00000000000..99227bdb7d7 --- /dev/null +++ b/app/assets/javascripts/discourse/app/lib/update-user-status-on-mention.js @@ -0,0 +1,36 @@ +import { escapeExpression } from "discourse/lib/utilities"; +import { emojiUnescape } from "discourse/lib/text"; +import { until } from "discourse/lib/formatter"; + +export function updateUserStatusOnMention(mention, status, currentUser) { + removeStatus(mention); + if (status) { + const html = statusHtml(status, currentUser); + mention.insertAdjacentHTML("beforeend", html); + } +} + +function removeStatus(mention) { + mention.querySelector("img.user-status")?.remove(); +} + +function statusHtml(status, currentUser) { + const emoji = escapeExpression(`:${status.emoji}:`); + return emojiUnescape(emoji, { + class: "user-status", + title: statusTitle(status, currentUser), + }); +} + +function statusTitle(status, currentUser) { + if (!status.ends_at) { + return status.description; + } + + const timezone = currentUser + ? currentUser.user_option?.timezone + : moment.tz.guess(); + + const until_ = until(status.ends_at, timezone, currentUser?.locale); + return escapeExpression(`${status.description} ${until_}`); +} diff --git a/app/assets/javascripts/discourse/app/widgets/post-cooked.js b/app/assets/javascripts/discourse/app/widgets/post-cooked.js index ca835c16ccc..78b697a22c0 100644 --- a/app/assets/javascripts/discourse/app/widgets/post-cooked.js +++ b/app/assets/javascripts/discourse/app/widgets/post-cooked.js @@ -4,13 +4,12 @@ import { ajax } from "discourse/lib/ajax"; import highlightSearch from "discourse/lib/highlight-search"; import { iconHTML } from "discourse-common/lib/icon-library"; import { isValidLink } from "discourse/lib/click-track"; -import { number, until } from "discourse/lib/formatter"; +import { number } from "discourse/lib/formatter"; import { spinnerHTML } from "discourse/helpers/loading-spinner"; import { escape } from "pretty-text/sanitizer"; import domFromString from "discourse-common/lib/dom-from-string"; import getURL from "discourse-common/lib/get-url"; -import { emojiUnescape } from "discourse/lib/text"; -import { escapeExpression } from "discourse/lib/utilities"; +import { updateUserStatusOnMention } from "discourse/lib/update-user-status-on-mention"; let _beforeAdoptDecorators = []; let _afterAdoptDecorators = []; @@ -392,43 +391,10 @@ export default class PostCooked { const mentions = postElement.querySelectorAll(`a.mention[href="${href}"]`); mentions.forEach((mention) => { - this._updateUserStatus(mention, user.status); + updateUserStatusOnMention(mention, user.status, this.currentUser); }); } - _updateUserStatus(mention, status) { - this._removeUserStatus(mention); - if (status) { - this._insertUserStatus(mention, status); - } - } - - _insertUserStatus(mention, status) { - const emoji = escapeExpression(`:${status.emoji}:`); - const statusHtml = emojiUnescape(emoji, { - class: "user-status", - title: this._userStatusTitle(status), - }); - mention.insertAdjacentHTML("beforeend", statusHtml); - } - - _removeUserStatus(mention) { - mention.querySelector("img.user-status")?.remove(); - } - - _userStatusTitle(status) { - if (!status.ends_at) { - return status.description; - } - - const timezone = this.currentUser - ? this.currentUser.user_option?.timezone - : moment.tz.guess(); - - const until_ = until(status.ends_at, timezone, this.currentUser?.locale); - return escapeExpression(`${status.description} ${until_}`); - } - _trackMentionedUsersStatus() { this._post()?.mentioned_users?.forEach((user) => { user.trackStatus?.();