Skip to content

Commit

Permalink
msgconv/from-slack: bridge url preview thumbnail if no image is present
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 23, 2024
1 parent aba7d8d commit 4687462
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
23 changes: 17 additions & 6 deletions pkg/msgconv/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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,
Expand Down

0 comments on commit 4687462

Please sign in to comment.