From c65cdc07792a3c8ab88178795057c274c6b7fc1e Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Wed, 15 Feb 2023 17:27:09 +1000 Subject: [PATCH] DEV: Remove chat_channel_id from chat-live-pane details (#20302) This is unnecessary indirection, we can just have the chat_channel_id in the message serializer and use that. --- .../serializers/chat_message_serializer.rb | 9 +++++--- .../discourse/components/chat-live-pane.js | 2 -- .../components/chat-message-info.hbs | 2 +- .../components/chat-message-left-gutter.hbs | 2 +- .../discourse/components/chat-message.hbs | 4 ++-- .../discourse/components/chat-message.js | 12 +++++----- .../discourse/helpers/format-chat-date.js | 22 ++++++------------- .../unit/helpers/format-chat-date-test.js | 5 ++--- 8 files changed, 25 insertions(+), 33 deletions(-) diff --git a/plugins/chat/app/serializers/chat_message_serializer.rb b/plugins/chat/app/serializers/chat_message_serializer.rb index f24b3aa403e..e13ba2bb948 100644 --- a/plugins/chat/app/serializers/chat_message_serializer.rb +++ b/plugins/chat/app/serializers/chat_message_serializer.rb @@ -14,13 +14,18 @@ class ChatMessageSerializer < ApplicationSerializer :reactions, :bookmark, :available_flags, - :thread_id + :thread_id, + :chat_channel_id has_one :user, serializer: BasicUserWithStatusSerializer, embed: :objects has_one :chat_webhook_event, serializer: ChatWebhookEventSerializer, embed: :objects has_one :in_reply_to, serializer: ChatInReplyToSerializer, embed: :objects has_many :uploads, serializer: UploadSerializer, embed: :objects + def channel + @channel ||= @options.dig(:chat_channel) || object.chat_channel + end + def user object.user || DeletedChatUser.new end @@ -131,8 +136,6 @@ class ChatMessageSerializer < ApplicationSerializer return [] if !scope.can_flag_chat_message?(object) return [] if reviewable_id.present? && user_flag_status == ReviewableScore.statuses[:pending] - channel = @options.dig(:chat_channel) || object.chat_channel - PostActionType.flag_types.map do |sym, id| next if channel.direct_message_channel? && %i[notify_moderators notify_user].include?(sym) diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-live-pane.js b/plugins/chat/assets/javascripts/discourse/components/chat-live-pane.js index ae32d91d2cf..deb851761ec 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat-live-pane.js +++ b/plugins/chat/assets/javascripts/discourse/components/chat-live-pane.js @@ -402,8 +402,6 @@ export default Component.extend({ this.setProperties({ messages: this._prepareMessages(messages), details: { - chat_channel_id: this.chatChannel.id, - chatable_type: this.chatChannel.chatable_type, can_delete_self: meta.can_delete_self, can_delete_others: meta.can_delete_others, can_flag: meta.can_flag, diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message-info.hbs b/plugins/chat/assets/javascripts/discourse/components/chat-message-info.hbs index a6e06bb6e71..efea5081cd5 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat-message-info.hbs +++ b/plugins/chat/assets/javascripts/discourse/components/chat-message-info.hbs @@ -25,7 +25,7 @@ {{/if}} - {{format-chat-date @message @details}} + {{format-chat-date @message}} {{#if @message.bookmark}} diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message-left-gutter.hbs b/plugins/chat/assets/javascripts/discourse/components/chat-message-left-gutter.hbs index 68ce6be6d6a..adbf43b3e0c 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat-message-left-gutter.hbs +++ b/plugins/chat/assets/javascripts/discourse/components/chat-message-left-gutter.hbs @@ -13,7 +13,7 @@ {{else}} - {{format-chat-date @message @details "tiny"}} + {{format-chat-date @message "tiny"}} {{/if}} {{#if @message.bookmark}} diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message.hbs b/plugins/chat/assets/javascripts/discourse/components/chat-message.hbs index d5d0926037b..b84694c4b7a 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat-message.hbs +++ b/plugins/chat/assets/javascripts/discourse/components/chat-message.hbs @@ -115,14 +115,14 @@ {{/if}} {{#if this.hideUserInfo}} - + {{else}} {{/if}}
{{#unless this.hideUserInfo}} - + {{/unless}} { @@ -607,7 +607,7 @@ export default class ChatMessage extends Component { _publishReaction(emoji, reactAction) { return ajax( - `/chat/${this.args.details.chat_channel_id}/react/${this.args.message.id}`, + `/chat/${this.args.message.chat_channel_id}/react/${this.args.message.id}`, { type: "PUT", data: { @@ -686,7 +686,7 @@ export default class ChatMessage extends Component { @action restore() { return ajax( - `/chat/${this.args.details.chat_channel_id}/restore/${this.args.message.id}`, + `/chat/${this.args.message.chat_channel_id}/restore/${this.args.message.id}`, { type: "PUT", } @@ -730,7 +730,7 @@ export default class ChatMessage extends Component { @action rebakeMessage() { return ajax( - `/chat/${this.args.details.chat_channel_id}/${this.args.message.id}/rebake`, + `/chat/${this.args.message.chat_channel_id}/${this.args.message.id}/rebake`, { type: "PUT", } @@ -740,7 +740,7 @@ export default class ChatMessage extends Component { @action deleteMessage() { return ajax( - `/chat/${this.args.details.chat_channel_id}/${this.args.message.id}`, + `/chat/${this.args.message.chat_channel_id}/${this.args.message.id}`, { type: "DELETE", } @@ -775,7 +775,7 @@ export default class ChatMessage extends Component { const { protocol, host } = window.location; let url = getURL( - `/chat/c/-/${this.args.details.chat_channel_id}/${this.args.message.id}` + `/chat/c/-/${this.args.message.chat_channel_id}/${this.args.message.id}` ); url = url.indexOf("/") === 0 ? protocol + "//" + host + url : url; clipboardCopy(url); diff --git a/plugins/chat/assets/javascripts/discourse/helpers/format-chat-date.js b/plugins/chat/assets/javascripts/discourse/helpers/format-chat-date.js index 4573ed60484..c31a86ef042 100644 --- a/plugins/chat/assets/javascripts/discourse/helpers/format-chat-date.js +++ b/plugins/chat/assets/javascripts/discourse/helpers/format-chat-date.js @@ -4,22 +4,14 @@ import getURL from "discourse-common/lib/get-url"; import I18n from "I18n"; import User from "discourse/models/user"; -registerUnbound("format-chat-date", function (message, details, mode) { - let currentUser = User.current(); +registerUnbound("format-chat-date", function (message, mode) { + const currentUser = User.current(); + const tz = currentUser ? currentUser.user_option.timezone : moment.tz.guess(); + const date = moment(new Date(message.created_at), tz); + const url = getURL(`/chat/c/-/${message.chat_channel_id}/${message.id}`); + const title = date.format(I18n.t("dates.long_with_year")); - let tz = currentUser ? currentUser.user_option.timezone : moment.tz.guess(); - - let date = moment(new Date(message.created_at), tz); - - let url = ""; - - if (details) { - url = getURL(`/chat/c/-/${details.chat_channel_id}/${message.id}`); - } - - let title = date.format(I18n.t("dates.long_with_year")); - - let display = + const display = mode === "tiny" ? date.format(I18n.t("chat.dates.time_tiny")) : date.format(I18n.t("dates.time")); diff --git a/plugins/chat/test/javascripts/unit/helpers/format-chat-date-test.js b/plugins/chat/test/javascripts/unit/helpers/format-chat-date-test.js index 474dba68dcc..081732a054e 100644 --- a/plugins/chat/test/javascripts/unit/helpers/format-chat-date-test.js +++ b/plugins/chat/test/javascripts/unit/helpers/format-chat-date-test.js @@ -8,9 +8,8 @@ module("Discourse Chat | Unit | Helpers | format-chat-date", function (hooks) { setupRenderingTest(hooks); test("link to chat message", async function (assert) { - this.set("details", { chat_channel_id: 1 }); - this.set("message", { id: 1 }); - await render(hbs`{{format-chat-date this.message this.details}}`); + this.set("message", { id: 1, chat_channel_id: 1 }); + await render(hbs`{{format-chat-date this.message}}`); assert.equal(query(".chat-time").getAttribute("href"), "/chat/c/-/1/1"); });