From 46874620886d075b63ca7e5884ab8f3a397c4957 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 23 Dec 2024 13:47:58 +0200 Subject: [PATCH] msgconv/from-slack: bridge url preview thumbnail if no image is present --- go.mod | 4 ++-- go.sum | 8 ++++---- pkg/msgconv/blocks.go | 23 +++++++++++++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index f0e7b73..cf2504f 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( go.mau.fi/util v0.8.3 golang.org/x/net v0.32.0 gopkg.in/yaml.v3 v3.0.1 - maunium.net/go/mautrix v0.22.1 + maunium.net/go/mautrix v0.22.2-0.20241223114659-ba210a16b992 ) require ( @@ -43,4 +43,4 @@ require ( maunium.net/go/mauflag v1.0.0 // indirect ) -replace github.com/slack-go/slack => github.com/beeper/slackgo v0.0.0-20241211205744-f532856a1038 +replace github.com/slack-go/slack => github.com/beeper/slackgo v0.0.0-20241223114722-b8b7a4c49a18 diff --git a/go.sum b/go.sum index df122c1..e95d07f 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU= github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= -github.com/beeper/slackgo v0.0.0-20241211205744-f532856a1038 h1:zP5Yl+MGAfNgbuyf1zP7YoXYIndra3iEknlILyUCPko= -github.com/beeper/slackgo v0.0.0-20241211205744-f532856a1038/go.mod h1:axoegr/0xf8uWt4I+coY6x+CVKPbWGs4YqpoYbCBRr8= +github.com/beeper/slackgo v0.0.0-20241223114722-b8b7a4c49a18 h1:s0gVLeb7pgYBIlwuUmFzMvDbxVWEWUvjDJaqurE5LI4= +github.com/beeper/slackgo v0.0.0-20241223114722-b8b7a4c49a18/go.mod h1:axoegr/0xf8uWt4I+coY6x+CVKPbWGs4YqpoYbCBRr8= github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -78,5 +78,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M= maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA= -maunium.net/go/mautrix v0.22.1 h1:2lCM37vmVzZGE0tWD7UOySMtAuC5hq6Pw33KlY2VU/c= -maunium.net/go/mautrix v0.22.1/go.mod h1:1rhqwH34Rz54ZqzdQYkmNW6rQUymNeTdaLA4l9LK6AI= +maunium.net/go/mautrix v0.22.2-0.20241223114659-ba210a16b992 h1:7KLNq84HGVzpb7iBCtKTq5NhjDWh/r3Bj8Qmtv5gXy4= +maunium.net/go/mautrix v0.22.2-0.20241223114659-ba210a16b992/go.mod h1:1rhqwH34Rz54ZqzdQYkmNW6rQUymNeTdaLA4l9LK6AI= diff --git a/pkg/msgconv/blocks.go b/pkg/msgconv/blocks.go index a519d03..7921997 100644 --- a/pkg/msgconv/blocks.go +++ b/pkg/msgconv/blocks.go @@ -92,13 +92,23 @@ func (mc *MessageConverter) attachmentToURLPreview(ctx context.Context, portal * var mxc id.ContentURIString var file *event.EncryptedFileInfo var imageMime string - if attachment.ImageURL != "" { - bytes, err := mc.downloadExternalImage(ctx, attachment.ImageURL) + imageURL := attachment.ImageURL + imageSize := attachment.ImageBytes + imageWidth := attachment.ImageWidth + imageHeight := attachment.ImageHeight + if imageURL == "" { + imageURL = attachment.ThumbURL + imageSize = 0 + imageWidth = attachment.ThumbWidth + imageHeight = attachment.ThumbHeight + } + if imageURL != "" { + bytes, err := mc.downloadExternalImage(ctx, imageURL) if err != nil { zerolog.Ctx(ctx).Err(err).Msg("Failed to download link preview image") } else { imageMime = http.DetectContentType(bytes) - mxc, file, err = intent.UploadMedia(ctx, portal.MXID, bytes, path.Base(attachment.ImageURL), imageMime) + mxc, file, err = intent.UploadMedia(ctx, portal.MXID, bytes, path.Base(imageURL), imageMime) if err != nil { zerolog.Ctx(ctx).Err(err).Msg("Failed to reupload link preview image") } @@ -109,12 +119,13 @@ func (mc *MessageConverter) attachmentToURLPreview(ctx context.Context, portal * LinkPreview: event.LinkPreview{ CanonicalURL: attachment.FromURL, Title: attachment.Title, + SiteName: attachment.ServiceName, Type: "website", Description: attachment.Text, ImageURL: mxc, - ImageSize: attachment.ImageBytes, - ImageWidth: attachment.ImageWidth, - ImageHeight: attachment.ImageHeight, + ImageSize: imageSize, + ImageWidth: imageWidth, + ImageHeight: imageHeight, }, MatchedURL: attachment.OriginalURL, ImageEncryption: file,