diff --git a/go.mod b/go.mod index 54d2081..6ee9e3d 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/zyedidia/clipboard v1.0.4 go.mau.fi/libsignal v0.1.0 go.mau.fi/util v0.3.1-0.20240208085450-32294da153ab - go.mau.fi/whatsmeow v0.0.0-20240208121856-0ef58f1cef30 + go.mau.fi/whatsmeow v0.0.0-20240209124521-50c00bd025be golang.org/x/crypto v0.19.0 golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 golang.org/x/image v0.15.0 diff --git a/go.sum b/go.sum index 1fe6df4..5dfdffc 100644 --- a/go.sum +++ b/go.sum @@ -55,8 +55,8 @@ go.mau.fi/libsignal v0.1.0 h1:vAKI/nJ5tMhdzke4cTK1fb0idJzz1JuEIpmjprueC+c= go.mau.fi/libsignal v0.1.0/go.mod h1:R8ovrTezxtUNzCQE5PH30StOQWWeBskBsWE55vMfY9I= go.mau.fi/util v0.3.1-0.20240208085450-32294da153ab h1:XZ8W5vHWlXSGmHn1U+Fvbh+xZr9wuHTvbY+qV7aybDY= go.mau.fi/util v0.3.1-0.20240208085450-32294da153ab/go.mod h1:rRypwgXVEPILomtFPyQcnbOeuRqf+nRN84vh/CICq4w= -go.mau.fi/whatsmeow v0.0.0-20240208121856-0ef58f1cef30 h1:gwmgU+l0OSww0Me3N022hLdZk7lnEoK8XeaWGlESG1I= -go.mau.fi/whatsmeow v0.0.0-20240208121856-0ef58f1cef30/go.mod h1:lQHbhaG/fI+6hfGqz5Vzn2OBJBEZ05H0kCP6iJXriN4= +go.mau.fi/whatsmeow v0.0.0-20240209124521-50c00bd025be h1:VQKRp4od2zRwu99T9bKeIj+ck+mgevVyJBGU+O2DZUU= +go.mau.fi/whatsmeow v0.0.0-20240209124521-50c00bd025be/go.mod h1:lQHbhaG/fI+6hfGqz5Vzn2OBJBEZ05H0kCP6iJXriN4= go.mau.fi/zeroconfig v0.1.2 h1:DKOydWnhPMn65GbXZOafgkPm11BvFashZWLct0dGFto= go.mau.fi/zeroconfig v0.1.2/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70= golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= diff --git a/msgconv/to-whatsapp.go b/msgconv/to-whatsapp.go index 54cb5fa..1dbc3d1 100644 --- a/msgconv/to-whatsapp.go +++ b/msgconv/to-whatsapp.go @@ -17,8 +17,10 @@ package msgconv import ( + "bytes" "context" "fmt" + "image" "strconv" "strings" "time" @@ -130,6 +132,18 @@ func parseGeoURI(uri string) (lat, long float64, err error) { return } +func clampTo400(w, h int) (int, int) { + if w > 400 { + h = h * 400 / w + w = 400 + } + if h > 400 { + w = w * 400 / h + h = 400 + } + return w, h +} + func (mc *MessageConverter) reuploadMediaToWhatsApp(ctx context.Context, evt *event.Event, content *event.MessageEventContent) (*waMediaTransport.WAMediaTransport, string, error) { data, mimeType, fileName, err := mc.downloadMatrixMedia(ctx, content) if err != nil { @@ -161,26 +175,41 @@ func (mc *MessageConverter) reuploadMediaToWhatsApp(ctx context.Context, evt *ev } customInfo["fi.mau.gif"] = true } + if content.MsgType == event.MsgImage && content.Info.Width == 0 { + cfg, _, _ := image.DecodeConfig(bytes.NewReader(data)) + content.Info.Width, content.Info.Height = cfg.Width, cfg.Height + } mediaType := msgToMediaType(content.MsgType) uploaded, err := mc.GetE2EEClient(ctx).Upload(ctx, data, mediaType) if err != nil { return nil, "", err } + w, h := clampTo400(content.Info.Width, content.Info.Height) + if w == 0 && content.MsgType == event.MsgImage { + w, h = 400, 400 + } mediaTransport := &waMediaTransport.WAMediaTransport{ Integral: &waMediaTransport.WAMediaTransport_Integral{ FileSHA256: uploaded.FileSHA256, MediaKey: uploaded.MediaKey, FileEncSHA256: uploaded.FileEncSHA256, DirectPath: uploaded.DirectPath, - MediaKeyTimestamp: time.Now().UnixMilli(), + MediaKeyTimestamp: time.Now().Unix(), }, Ancillary: &waMediaTransport.WAMediaTransport_Ancillary{ FileLength: uint64(len(data)), Mimetype: mimeType, - Thumbnail: nil, - ObjectID: uploaded.ObjectID, + // This field is extremely required for some reason. + // Messenger iOS & Android will refuse to display the media if it's not present. + // iOS also requires that width and height are non-empty. + Thumbnail: &waMediaTransport.WAMediaTransport_Ancillary_Thumbnail{ + ThumbnailWidth: uint32(w), + ThumbnailHeight: uint32(h), + }, + ObjectID: uploaded.ObjectID, }, } + fmt.Printf("Uploaded media transport: %+v\n", mediaTransport) return mediaTransport, fileName, nil }