Don't add reply sender to mentions array manually

Discord already adds it to the native mentions array
This commit is contained in:
Tulir Asokan
2023-06-04 11:33:18 +03:00
parent 8c57b7a69b
commit 4c62fe8b12
3 changed files with 13 additions and 16 deletions

View File

@@ -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().

View File

@@ -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{}

View File

@@ -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 {