mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 20:03:15 +08:00
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.
This commit is contained in:
parent
a89b3c27aa
commit
e49d338c21
|
@ -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_}`);
|
||||
}
|
|
@ -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?.();
|
||||
|
|
Loading…
Reference in New Issue
Block a user