From 4c62fe8b124b27d44fc7485b4be5a6f4fc94b8ee Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 4 Jun 2023 11:33:18 +0300 Subject: [PATCH] Don't add reply sender to mentions array manually Discord already adds it to the native mentions array --- backfill.go | 4 ++-- portal.go | 20 ++++++++++---------- portal_convert.go | 5 +---- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/backfill.go b/backfill.go index c218646..4422163 100644 --- a/backfill.go +++ b/backfill.go @@ -217,8 +217,8 @@ func (portal *Portal) convertMessageBatch(log zerolog.Logger, source *User, mess puppet := portal.bridge.GetPuppetByID(msg.Author.ID) puppet.UpdateInfo(source, msg.Author, msg.WebhookID) intent := puppet.IntentFor(portal) - replyTo, replySenderMXID := portal.getReplyTarget(source, "", msg.MessageReference, msg.Embeds, true) - mentions := portal.convertDiscordMentions(msg, replySenderMXID, false) + replyTo := portal.getReplyTarget(source, "", msg.MessageReference, msg.Embeds, true) + mentions := portal.convertDiscordMentions(msg, false) ts, _ := discordgo.SnowflakeTimestamp(msg.ID) log := log.With(). diff --git a/portal.go b/portal.go index b4f2005..4bd1ab4 100644 --- a/portal.go +++ b/portal.go @@ -636,8 +636,8 @@ func (portal *Portal) handleDiscordMessageCreate(user *User, msg *discordgo.Mess lastThreadEvent = lastInThread.MXID } } - replyTo, replySenderMXID := portal.getReplyTarget(user, discordThreadID, msg.MessageReference, msg.Embeds, false) - mentions := portal.convertDiscordMentions(msg, replySenderMXID, true) + replyTo := portal.getReplyTarget(user, discordThreadID, msg.MessageReference, msg.Embeds, false) + mentions := portal.convertDiscordMentions(msg, true) ts, _ := discordgo.SnowflakeTimestamp(msg.ID) parts := portal.convertDiscordMessage(ctx, puppet, intent, msg) @@ -688,7 +688,7 @@ func isReplyEmbed(embed *discordgo.MessageEmbed) bool { return hackyReplyPattern.MatchString(embed.Description) } -func (portal *Portal) getReplyTarget(source *User, threadID string, ref *discordgo.MessageReference, embeds []*discordgo.MessageEmbed, allowNonExistent bool) (*event.InReplyTo, id.UserID) { +func (portal *Portal) getReplyTarget(source *User, threadID string, ref *discordgo.MessageReference, embeds []*discordgo.MessageEmbed, allowNonExistent bool) *event.InReplyTo { if ref == nil && len(embeds) > 0 { match := hackyReplyPattern.FindStringSubmatch(embeds[0].Description) if match != nil && match[1] == portal.GuildID && (match[2] == portal.Key.ChannelID || match[2] == threadID) { @@ -700,7 +700,7 @@ func (portal *Portal) getReplyTarget(source *User, threadID string, ref *discord } } if ref == nil { - return nil, "" + return nil } isHungry := portal.bridge.Config.Homeserver.Software == bridgeconfig.SoftwareHungry if !isHungry { @@ -713,25 +713,25 @@ func (portal *Portal) getReplyTarget(source *User, threadID string, ref *discord if ref.ChannelID != portal.Key.ChannelID && ref.ChannelID != threadID && crossRoomReplies { targetPortal = portal.bridge.GetExistingPortalByID(database.PortalKey{ChannelID: ref.ChannelID, Receiver: source.DiscordID}) if targetPortal == nil { - return nil, "" + return nil } } replyToMsg := portal.bridge.DB.Message.GetByDiscordID(targetPortal.Key, ref.MessageID) if len(replyToMsg) > 0 { if !crossRoomReplies { - return &event.InReplyTo{EventID: replyToMsg[0].MXID}, replyToMsg[0].SenderMXID + return &event.InReplyTo{EventID: replyToMsg[0].MXID} } return &event.InReplyTo{ EventID: replyToMsg[0].MXID, UnstableRoomID: targetPortal.MXID, - }, replyToMsg[0].SenderMXID + } } else if allowNonExistent { return &event.InReplyTo{ EventID: targetPortal.deterministicEventID(ref.MessageID, ""), UnstableRoomID: targetPortal.MXID, - }, "" + } } - return nil, "" + return nil } const JoinThreadReaction = "join thread" @@ -902,7 +902,7 @@ func (portal *Portal) handleDiscordMessageUpdate(user *User, msg *discordgo.Mess } puppet.addWebhookMeta(converted, msg) puppet.addMemberMeta(converted, msg) - converted.Content.Mentions = portal.convertDiscordMentions(msg, "", false) + converted.Content.Mentions = portal.convertDiscordMentions(msg, false) converted.Content.SetEdit(existing[0].MXID) // Never actually mention new users of edits, only include mentions inside m.new_content converted.Content.Mentions = &event.Mentions{} diff --git a/portal_convert.go b/portal_convert.go index c56ba7e..b11c65b 100644 --- a/portal_convert.go +++ b/portal_convert.go @@ -621,7 +621,7 @@ func isPlainGifMessage(msg *discordgo.Message) bool { (msg.Embeds[0].Type == discordgo.EmbedTypeImage && msg.Embeds[0].Image == nil && msg.Embeds[0].Thumbnail != nil)) } -func (portal *Portal) convertDiscordMentions(msg *discordgo.Message, replySender id.UserID, syncGhosts bool) *event.Mentions { +func (portal *Portal) convertDiscordMentions(msg *discordgo.Message, syncGhosts bool) *event.Mentions { var matrixMentions event.Mentions for _, mention := range msg.Mentions { puppet := portal.bridge.GetPuppetByID(mention.ID) @@ -635,9 +635,6 @@ func (portal *Portal) convertDiscordMentions(msg *discordgo.Message, replySender matrixMentions.UserIDs = append(matrixMentions.UserIDs, puppet.MXID) } } - if replySender != "" { - matrixMentions.UserIDs = append(matrixMentions.UserIDs, replySender) - } slices.Sort(matrixMentions.UserIDs) matrixMentions.UserIDs = slices.Compact(matrixMentions.UserIDs) if msg.MentionEveryone {