Fix handling gifs where canonical URL is different

This commit is contained in:
Tulir Asokan
2023-08-03 00:16:47 +03:00
parent 5467ab074d
commit 0391750fea
2 changed files with 12 additions and 4 deletions

View File

@@ -156,11 +156,14 @@ func (br *DiscordBridge) pillConverter(displayname, mxid, eventID string, ctx fo
return displayname return displayname
} }
const discordLinkPattern = `https?://[^<\p{Zs}\x{feff}]*[^"'),.:;\]\p{Zs}\x{feff}]`
// Discord links start with http:// or https://, contain at least two characters afterwards, // Discord links start with http:// or https://, contain at least two characters afterwards,
// don't contain < or whitespace anywhere, and don't end with "'),.:;] // don't contain < or whitespace anywhere, and don't end with "'),.:;]
// //
// Zero-width whitespace is mostly in the Format category and is allowed, except \uFEFF isn't for some reason // Zero-width whitespace is mostly in the Format category and is allowed, except \uFEFF isn't for some reason
var discordLinkRegex = regexp.MustCompile(`https?://[^<\p{Zs}\x{feff}]*[^"'),.:;\]\p{Zs}\x{feff}]`) var discordLinkRegex = regexp.MustCompile(discordLinkPattern)
var discordLinkRegexFull = regexp.MustCompile("^" + discordLinkPattern + "$")
var discordMarkdownEscaper = strings.NewReplacer( var discordMarkdownEscaper = strings.NewReplacer(
`\`, `\\`, `\`, `\\`,

View File

@@ -621,9 +621,14 @@ func getEmbedType(msg *discordgo.Message, embed *discordgo.MessageEmbed) BridgeE
} }
func isPlainGifMessage(msg *discordgo.Message) bool { func isPlainGifMessage(msg *discordgo.Message) bool {
return len(msg.Embeds) == 1 && msg.Embeds[0].URL == msg.Content && if len(msg.Embeds) != 1 {
((msg.Embeds[0].Type == discordgo.EmbedTypeGifv && msg.Embeds[0].Video != nil) || return false
(msg.Embeds[0].Type == discordgo.EmbedTypeImage && msg.Embeds[0].Image == nil && msg.Embeds[0].Thumbnail != nil)) }
embed := msg.Embeds[0]
isGifVideo := embed.Type == discordgo.EmbedTypeGifv && embed.Video != nil
isGifImage := embed.Type == discordgo.EmbedTypeImage && embed.Image == nil && embed.Thumbnail != nil
contentIsOnlyURL := msg.Content == embed.URL || discordLinkRegexFull.MatchString(msg.Content)
return contentIsOnlyURL && (isGifVideo || isGifImage)
} }
func (portal *Portal) convertDiscordMentions(msg *discordgo.Message, syncGhosts bool) *event.Mentions { func (portal *Portal) convertDiscordMentions(msg *discordgo.Message, syncGhosts bool) *event.Mentions {