Skip to content

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

DEV: Make chatMessage.cook function async (#21829)

Andrei Prigorshnevd0868885495e
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-channel.js b/plugins/chat/assets/javascripts/discourse/components/chat-channel.js
index c8cfe550f55..3901de6a369 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-channel.js
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-channel.js
@@ -647,11 +647,11 @@ export default class ChatLivePane extends Component {
}
@action
- onSendMessage(message) {
+ async onSendMessage(message) {
if (message.editing) {
- this.#sendEditMessage(message);
+ await this.#sendEditMessage(message);
} else {
- this.#sendNewMessage(message);
+ await this.#sendNewMessage(message);
}
}
@@ -660,8 +660,8 @@ export default class ChatLivePane extends Component {
this.chatChannelComposer.reset(this.args.channel);
}
- #sendEditMessage(message) {
- message.cook();
+ async #sendEditMessage(message) {
+ await message.cook();
this.chatChannelPane.sending = true;
const data = {
@@ -671,16 +671,21 @@ export default class ChatLivePane extends Component {
this.resetComposer();
- return this.chatApi
- .editMessage(this.args.channel.id, message.id, data)
- .catch(popupAjaxError)
- .finally(() => {
- this.chatDraftsManager.remove({ channelId: this.args.channel.id });
- this.chatChannelPane.sending = false;
- });
+ try {
+ return await this.chatApi.editMessage(
+ this.args.channel.id,
+ message.id,
+ data
+ );
+ } catch (e) {
+ popupAjaxError(e);
+ } finally {
+ this.chatDraftsManager.remove({ channelId: this.args.channel.id });
+ this.chatChannelPane.sending = false;
+ }
}
- #sendNewMessage(message) {
+ async #sendNewMessage(message) {
this.chatChannelPane.sending = true;
resetIdle();
@@ -712,7 +717,7 @@ export default class ChatLivePane extends Component {
);
}
- this.args.channel.stageMessage(message);
+ await this.args.channel.stageMessage(message);
this.resetComposer();
if (!this.args.channel.canLoadMoreFuture) {
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-thread.js b/plugins/chat/assets/javascripts/discourse/components/chat-thread.js
index 180aa2f73af..3b514c94797 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-thread.js
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-thread.js
@@ -216,13 +216,13 @@ export default class ChatThreadPanel extends Component {
}
@action
- onSendMessage(message) {
+ async onSendMessage(message) {
resetIdle();
if (message.editing) {
- this.#sendEditMessage(message);
+ await this.#sendEditMessage(message);
} else {
- this.#sendNewMessage(message);
+ await this.#sendNewMessage(message);
}
}
@@ -236,7 +236,7 @@ export default class ChatThreadPanel extends Component {
this.chat.activeMessage = null;
}
- #sendNewMessage(message) {
+ async #sendNewMessage(message) {
message.thread = this.thread;
if (this.chatChannelThreadPane.sending) {
@@ -245,7 +245,7 @@ export default class ChatThreadPanel extends Component {
this.chatChannelThreadPane.sending = true;
- this.thread.stageMessage(message);
+ await this.thread.stageMessage(message);
this.resetComposer();
this.scrollToBottom();
@@ -272,8 +272,8 @@ export default class ChatThreadPanel extends Component {
});
}
- #sendEditMessage(message) {
- message.cook();
+ async #sendEditMessage(message) {
+ await message.cook();
this.chatChannelThreadPane.sending = true;
const data = {
@@ -283,12 +283,17 @@ export default class ChatThreadPanel extends Component {
this.resetComposer();
- return this.chatApi
- .editMessage(message.channel.id, message.id, data)
- .catch(popupAjaxError)
- .finally(() => {
- this.chatChannelThreadPane.sending = false;
- });
+ try {
+ return await this.chatApi.editMessage(
+ message.channel.id,
+ message.id,
+ data
+ );
+ } catch (e) {
+ popupAjaxError(e);
+ } finally {
+ this.chatChannelThreadPane.sending = false;
+ }
}
// A more consistent way to scroll to the bottom when we are sure this is our goal
diff --git a/plugins/chat/assets/javascripts/discourse/components/styleguide/chat-message.js b/plugins/chat/assets/javascripts/discourse/components/styleguide/chat-message.js
index f57ef71b8ff..b5ec5c39662 100644
--- a/plugins/chat/assets/javascripts/discourse/components/styleguide/chat-message.js
+++ b/plugins/chat/assets/javascripts/discourse/components/styleguide/chat-message.js
@@ -10,7 +10,11 @@ export default class ChatStyleguideChatMessage extends Component {
manager = new ChatMessagesManager(getOwner(this));
- message = fabricators.message({ user: this.currentUser });
+ constructor() {
+ super(...arguments);
+ this.message = fabricators.message({ user: this.currentUser });
+ this.message.cook();
+ }
@action
toggleDeleted() {
@@ -61,9 +65,9 @@ export default class ChatStyleguideChatMessage extends Component {
}
@action
- updateMessage(event) {
+ async updateMessage(event) {
this.message.message = event.target.value;
- this.message.cook();
+ await this.message.cook();
}
@action
diff --git a/plugins/chat/assets/javascripts/discourse/lib/fabricators.js b/plugins/chat/assets/javascripts/discourse/lib/fabricators.js
index 6c514b1e08b..dc8aff12369 100644
--- a/plugins/chat/assets/javascripts/discourse/lib/fabricators.js
+++ b/plugins/chat/assets/javascripts/discourse/lib/fabricators.js
@@ -46,8 +46,6 @@ function messageFabricator(args = {}) {
message.excerpt = text.slice(0, excerptLength) + "...";
}
- message.cook();
-
return message;
}
diff --git a/plugins/chat/assets/javascripts/discourse/models/chat-channel.js b/plugins/chat/assets/javascripts/discourse/models/chat-channel.js
index ef2af33eb38..7e1afa89896 100644
--- a/plugins/chat/assets/javascripts/discourse/models/chat-channel.js
+++ b/plugins/chat/assets/javascripts/discourse/models/chat-channel.js
@@ -286,12 +286,12 @@ export default class ChatChannel {
return thread;
}
- stageMessage(message) {
+ async stageMessage(message) {
message.id = guid();
message.staged = true;
message.draft = false;
message.createdAt ??= moment.utc().format();
- message.cook();
+ await message.cook();
if (message.inReplyTo) {
if (!this.threadingEnabled) {
diff --git a/plugins/chat/assets/javascripts/discourse/models/chat-message.js b/plugins/chat/assets/javascripts/discourse/models/chat-message.js
index 3a2c2b0dab9..c96f095a6ae 100644
--- a/plugins/chat/assets/javascripts/discourse/models/chat-message.js
+++ b/plugins/chat/assets/javascripts/discourse/models/chat-message.js
@@ -7,7 +7,7 @@ import I18n from "I18n";
import { generateCookFunction } from "discourse/lib/text";
import simpleCategoryHashMentionTransform from "discourse/plugins/chat/discourse/lib/simple-category-hash-mention-transform";
import { getOwner } from "discourse-common/lib/get-owner";
-import { next } from "@ember/runloop";
+
export default class ChatMessage {
static cookFunction = null;
@@ -147,40 +147,37 @@ export default class ChatMessage {
}
}
- cook() {
+ async cook() {
const site = getOwner(this).lookup("service:site");
- next(() => {
- if (this.isDestroyed || this.isDestroying) {
- return;
- }
+ if (this.isDestroyed || this.isDestroying) {
+ return;
+ }
- const markdownOptions = {
- featuresOverride:
- site.markdown_additional_options?.chat?.limited_pretty_text_features,
- markdownItRules:
- site.markdown_additional_options?.chat
- ?.limited_pretty_text_markdown_rules,
- hashtagTypesInPriorityOrder:
- site.hashtag_configurations?.["chat-composer"],
- hashtagIcons: site.hashtag_icons,
+ const markdownOptions = {
+ featuresOverride:
+ site.markdown_additional_options?.chat?.limited_pretty_text_features,
+ markdownItRules:
+ site.markdown_additional_options?.chat
+ ?.limited_pretty_text_markdown_rules,
+ hashtagTypesInPriorityOrder:
+ site.hashtag_configurations?.["chat-composer"],
+ hashtagIcons: site.hashtag_icons,
+ };
+
+ if (ChatMessage.cookFunction) {
+ this.cooked = ChatMessage.cookFunction(this.message);
+ } else {
+ const cookFunction = await generateCookFunction(markdownOptions);
+ ChatMessage.cookFunction = (raw) => {
+ return simpleCategoryHashMentionTransform(
+ cookFunction(raw),
+ site.categories
+ );
};
- if (ChatMessage.cookFunction) {
- this.cooked = ChatMessage.cookFunction(this.message);
- } else {
- generateCookFunction(markdownOptions).then((cookFunction) => {
- ChatMessage.cookFunction = (raw) => {
- return simpleCategoryHashMentionTransform(
- cookFunction(raw),
- site.categories
- );
- };
-
- this.cooked = ChatMessage.cookFunction(this.message);
- });
- }
- });
+ this.cooked = ChatMessage.cookFunction(this.message);
+ }
}
get read() {
diff --git a/plugins/chat/assets/javascripts/discourse/models/chat-thread.js b/plugins/chat/assets/javascripts/discourse/models/chat-thread.js
index 0ec29be80d8..bb3ab34b272 100644
--- a/plugins/chat/assets/javascripts/discourse/models/chat-thread.js
+++ b/plugins/chat/assets/javascripts/discourse/models/chat-thread.js
@@ -63,12 +63,12 @@ export default class ChatThread {
}
}
- stageMessage(message) {
+ async stageMessage(message) {
message.id = guid();
message.staged = true;
message.draft = false;
message.createdAt ??= moment.utc().format();
- message.cook();
+ await message.cook();
this.messagesManager.addMessages([message]);
}