Fix adding images to Beeper link preview metadata

This commit is contained in:
Tulir Asokan
2023-01-28 14:48:08 +02:00
parent 68b82c4d1b
commit e3a6dd46cb

View File

@@ -671,23 +671,18 @@ type BeeperLinkPreview struct {
ImageEncryption *event.EncryptedFileInfo `json:"beeper:image:encryption,omitempty"` ImageEncryption *event.EncryptedFileInfo `json:"beeper:image:encryption,omitempty"`
} }
func (portal *Portal) convertDiscordLinkEmbedsToBeeper(intent *appservice.IntentAPI, embeds []*discordgo.MessageEmbed) (previews []BeeperLinkPreview) { func (portal *Portal) convertDiscordLinkEmbedImage(intent *appservice.IntentAPI, url string, width, height int, preview *BeeperLinkPreview) {
previews = []BeeperLinkPreview{} dbFile, err := portal.bridge.copyAttachmentToMatrix(intent, url, portal.Encrypted, "", "")
for _, embed := range embeds {
if embed.Type != discordgo.EmbedTypeLink {
continue
}
var preview BeeperLinkPreview
preview.MatchedURL = embed.URL
preview.Title = embed.Title
preview.Description = embed.Description
if embed.Image != nil {
dbFile, err := portal.bridge.copyAttachmentToMatrix(intent, embed.Image.URL, portal.Encrypted, "", "")
if err != nil { if err != nil {
portal.log.Warnfln("Failed to copy image in URL preview: %v", err) portal.log.Warnfln("Failed to copy image in URL preview: %v", err)
} else { } else {
preview.ImageWidth = embed.Image.Width if width != 0 || height != 0 {
preview.ImageHeight = embed.Image.Height preview.ImageWidth = width
preview.ImageHeight = height
} else {
preview.ImageWidth = dbFile.Width
preview.ImageHeight = dbFile.Height
}
preview.ImageSize = dbFile.Size preview.ImageSize = dbFile.Size
preview.ImageType = dbFile.MimeType preview.ImageType = dbFile.MimeType
if dbFile.Encrypted { if dbFile.Encrypted {
@@ -699,6 +694,22 @@ func (portal *Portal) convertDiscordLinkEmbedsToBeeper(intent *appservice.Intent
preview.ImageURL = dbFile.MXC.CUString() preview.ImageURL = dbFile.MXC.CUString()
} }
} }
}
func (portal *Portal) convertDiscordLinkEmbedsToBeeper(intent *appservice.IntentAPI, embeds []*discordgo.MessageEmbed) (previews []BeeperLinkPreview) {
previews = []BeeperLinkPreview{}
for _, embed := range embeds {
if embed.Type != discordgo.EmbedTypeLink && embed.Type != discordgo.EmbedTypeArticle {
continue
}
var preview BeeperLinkPreview
preview.MatchedURL = embed.URL
preview.Title = embed.Title
preview.Description = embed.Description
if embed.Image != nil {
portal.convertDiscordLinkEmbedImage(intent, embed.Image.URL, embed.Image.Width, embed.Image.Height, &preview)
} else if embed.Thumbnail != nil {
portal.convertDiscordLinkEmbedImage(intent, embed.Thumbnail.URL, embed.Thumbnail.Width, embed.Thumbnail.Height, &preview)
} }
previews = append(previews, preview) previews = append(previews, preview)
} }
@@ -1015,7 +1026,7 @@ func (portal *Portal) handleDiscordMessageCreate(user *User, msg *discordgo.Mess
switch { switch {
case embed.Video != nil: // gif/video embeds (hopefully no rich content) case embed.Video != nil: // gif/video embeds (hopefully no rich content)
part = portal.handleDiscordVideoEmbed(intent, embed, msg.ID, i, ts, threadRelation) part = portal.handleDiscordVideoEmbed(intent, embed, msg.ID, i, ts, threadRelation)
case embed.Type == discordgo.EmbedTypeLink: case embed.Type == discordgo.EmbedTypeLink, embed.Type == discordgo.EmbedTypeArticle:
// skip link previews, these are handled earlier // skip link previews, these are handled earlier
default: // rich embeds default: // rich embeds
part = portal.handleDiscordRichEmbed(intent, embed, msg.ID, i, ts, threadRelation) part = portal.handleDiscordRichEmbed(intent, embed, msg.ID, i, ts, threadRelation)