From cedc1a5fd1b2fc29d3e2c5001c27046f541fbcf9 Mon Sep 17 00:00:00 2001 From: Pascal Precht <445106+0x-r4bbit@users.noreply.github.com> Date: Fri, 13 Jan 2023 16:28:49 +0100 Subject: [PATCH] fix: add discord message data to quoted messages Because `QuotedMessage` doesn't include imported message data, some of the author information in imported messages is lost in frontends. This commit adds a `discordMessage` (soon replaced by `importedMessage`) to `Quotedmessage`, although only hydrated with a subset of data, namely author display name and avatar URL, as those are the only ones needed by front-end atm. --- protocol/common/message.go | 2 ++ protocol/message_persistence.go | 37 +++++++++++++++++++++++++++++---- protocol/messenger.go | 12 +++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/protocol/common/message.go b/protocol/common/message.go index 5133bfdad..8b471cf3b 100644 --- a/protocol/common/message.go +++ b/protocol/common/message.go @@ -39,6 +39,8 @@ type QuotedMessage struct { CommunityID string `json:"communityId,omitempty"` Deleted bool `json:"deleted,omitempty"` + + DiscordMessage *protobuf.DiscordMessage `json:"discordMessage,omitempty"` } type CommandState int diff --git a/protocol/message_persistence.go b/protocol/message_persistence.go index 7a982d293..1bb94d204 100644 --- a/protocol/message_persistence.go +++ b/protocol/message_persistence.go @@ -25,6 +25,10 @@ LEFT JOIN discord_message_authors dm_author ON dm.author_id = dm_author.id LEFT JOIN discord_message_attachments dm_attachment ON dm.id = dm_attachment.discord_message_id +LEFT JOIN discord_messages m2_dm +ON m2.discord_message_id = m2_dm.id +LEFT JOIN discord_message_authors m2_dm_author +ON m2_dm.author_id = m2_dm_author.id ` var basicInsertDiscordMessageAuthorQuery = `INSERT OR REPLACE INTO discord_message_authors(id,name,discriminator,nickname,avatar_url, avatar_image_payload) VALUES (?,?,?,?,?,?)` @@ -172,7 +176,11 @@ func (db sqlitePersistence) tableUserMessagesAllFieldsJoin() string { m2.content_type, m2.deleted, c.alias, - c.identicon` + c.identicon, + COALESCE(m2.discord_message_id, ""), + COALESCE(m2_dm_author.name, ""), + COALESCE(m2_dm_author.nickname, ""), + COALESCE(m2_dm_author.avatar_url, "")` } func (db sqlitePersistence) tableUserMessagesAllFieldsCount() int { @@ -218,6 +226,10 @@ func (db sqlitePersistence) tableUserMessagesScanAllFields(row scanner, message Attachments: []*protobuf.DiscordMessageAttachment{}, } + quotedDiscordMessage := &protobuf.DiscordMessage{ + Author: &protobuf.DiscordMessageAuthor{}, + } + attachment := &protobuf.DiscordMessageAttachment{} args := []interface{}{ @@ -294,6 +306,10 @@ func (db sqlitePersistence) tableUserMessagesScanAllFields(row scanner, message "edDeleted, &alias, &identicon, + "edDiscordMessage.Id, + "edDiscordMessage.Author.Name, + "edDiscordMessage.Author.Nickname, + "edDiscordMessage.Author.AvatarUrl, } err := row.Scan(append(args, others...)...) if err != nil { @@ -336,6 +352,9 @@ func (db sqlitePersistence) tableUserMessagesScanAllFields(row scanner, message CommunityID: quotedCommunityID.String, Deleted: quotedDeleted.Bool, } + if message.QuotedMessage.ContentType == int64(protobuf.ChatMessage_DISCORD_MESSAGE) { + message.QuotedMessage.DiscordMessage = quotedDiscordMessage + } } } message.Alias = alias.String @@ -1019,10 +1038,20 @@ func (db sqlitePersistence) PinnedMessageByChatIDs(chatIDs []string, currCursor ON dm.author_id = dm_author.id - LEFT JOIN + LEFT JOIN discord_message_attachments dm_attachment - ON - dm.id = dm_attachment.discord_message_id + ON + dm.id = dm_attachment.discord_message_id + + LEFT JOIN + discord_messages m2_dm + ON + m2.discord_message_id = m2_dm.id + + LEFT JOIN + discord_message_authors m2_dm_author + ON + m2_dm.author_id = m2_dm_author.id WHERE pm.pinned = 1 diff --git a/protocol/messenger.go b/protocol/messenger.go index 31e1588ed..3a08aa414 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -4483,6 +4483,18 @@ func (m *Messenger) prepareMessage(msg *common.Message, s *server.MediaServer) { if msg.QuotedMessage != nil && msg.QuotedMessage.ContentType == int64(protobuf.ChatMessage_STICKER) { msg.QuotedMessage.HasSticker = true } + if msg.QuotedMessage != nil && msg.QuotedMessage.ContentType == int64(protobuf.ChatMessage_DISCORD_MESSAGE) { + dm := msg.QuotedMessage.DiscordMessage + exists, err := m.persistence.HasDiscordMessageAuthorImagePayload(dm.Author.Id) + if err != nil { + return + } + + if exists { + msg.QuotedMessage.DiscordMessage.Author.LocalUrl = s.MakeDiscordAuthorAvatarURL(dm.Author.Id) + } + } + if msg.ContentType == protobuf.ChatMessage_IMAGE { msg.ImageLocalURL = s.MakeImageURL(msg.ID) }