From f269a5b4ec2ad58e85a9044c863ad9a1c049084f Mon Sep 17 00:00:00 2001 From: Gerardo Rodriguez Date: Tue, 10 Nov 2026 06:10:00 +0000 Subject: [PATCH 1/3] handlematrix: send only the caption in media edits --- pkg/connector/handlematrix.go | 9 ++++++--- pkg/msgconv/from-matrix.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/pkg/connector/handlematrix.go b/pkg/connector/handlematrix.go index 84198506..cc2e8436 100644 --- a/pkg/connector/handlematrix.go +++ b/pkg/connector/handlematrix.go @@ -273,9 +273,12 @@ func (wa *WhatsAppClient) HandleMatrixEdit(ctx context.Context, edit *bridgev2.M return fmt.Errorf("failed to parse portal ID: %w", err) } - waMsg, _, err := wa.Main.MsgConv.ToWhatsApp(ctx, wa.Client, edit.Event, edit.Content, nil, nil, edit.Portal) - if err != nil { - return wrapConversionError(fmt.Errorf("failed to convert message: %w", err)) + waMsg := wa.Main.MsgConv.CaptionEditToWhatsApp(ctx, wa.Client, edit.Content, edit.Portal) + if waMsg == nil { + waMsg, _, err = wa.Main.MsgConv.ToWhatsApp(ctx, wa.Client, edit.Event, edit.Content, nil, nil, edit.Portal) + if err != nil { + return wrapConversionError(fmt.Errorf("failed to convert message: %w", err)) + } } defer wa.mcTrack(edit, time.Now(), &retErr) diff --git a/pkg/msgconv/from-matrix.go b/pkg/msgconv/from-matrix.go index ba2adc23..324628f2 100644 --- a/pkg/msgconv/from-matrix.go +++ b/pkg/msgconv/from-matrix.go @@ -204,6 +204,36 @@ func (mc *MessageConverter) ToWhatsApp( return message, extra, nil } +func (mc *MessageConverter) CaptionEditToWhatsApp( + ctx context.Context, + client *whatsmeow.Client, + content *event.MessageEventContent, + portal *bridgev2.Portal, +) *waE2E.Message { + if content.MsgType != event.MsgImage && content.MsgType != event.MsgVideo && content.MsgType != event.MsgFile { + return nil + } + ctx = context.WithValue(ctx, contextKeyClient, client) + ctx = context.WithValue(ctx, contextKeyPortal, portal) + contextInfo := mc.generateContextInfo(ctx, nil, content.RelatesTo, portal, content.BeeperDisappearingTimer, content.Mentions != nil && content.Mentions.Room) + var caption string + if content.FileName != "" && content.Body != content.FileName { + caption, contextInfo.MentionedJID = mc.parseText(ctx, content) + } + switch { + case content.MsgType == event.MsgFile: + return &waE2E.Message{DocumentWithCaptionMessage: &waE2E.FutureProofMessage{ + Message: &waE2E.Message{ + DocumentMessage: &waE2E.DocumentMessage{Caption: proto.String(caption), ContextInfo: contextInfo}, + }, + }} + case content.MsgType == event.MsgVideo, content.GetInfo().MimeType == "image/gif": + return &waE2E.Message{VideoMessage: &waE2E.VideoMessage{Caption: proto.String(caption), ContextInfo: contextInfo}} + default: + return &waE2E.Message{ImageMessage: &waE2E.ImageMessage{Caption: proto.String(caption), ContextInfo: contextInfo}} + } +} + func (mc *MessageConverter) constructMediaMessage( ctx context.Context, content *event.MessageEventContent, From d9525654ae45c6ae97890f69a772b475781a17e7 Mon Sep 17 00:00:00 2001 From: Gerardo Rodriguez Date: Tue, 10 Nov 2026 06:14:14 +0000 Subject: [PATCH 2/3] handlematrix: match GIF edits to how GIFs were sent --- pkg/msgconv/from-matrix.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/msgconv/from-matrix.go b/pkg/msgconv/from-matrix.go index 324628f2..09fad3e0 100644 --- a/pkg/msgconv/from-matrix.go +++ b/pkg/msgconv/from-matrix.go @@ -210,7 +210,7 @@ func (mc *MessageConverter) CaptionEditToWhatsApp( content *event.MessageEventContent, portal *bridgev2.Portal, ) *waE2E.Message { - if content.MsgType != event.MsgImage && content.MsgType != event.MsgVideo && content.MsgType != event.MsgFile { + if (content.MsgType != event.MsgImage && content.MsgType != event.MsgVideo && content.MsgType != event.MsgFile) || content.GetInfo().MimeType == "" { return nil } ctx = context.WithValue(ctx, contextKeyClient, client) @@ -221,14 +221,14 @@ func (mc *MessageConverter) CaptionEditToWhatsApp( caption, contextInfo.MentionedJID = mc.parseText(ctx, content) } switch { + case content.MsgType == event.MsgVideo, content.Info.MimeType == "image/gif": + return &waE2E.Message{VideoMessage: &waE2E.VideoMessage{Caption: proto.String(caption), ContextInfo: contextInfo}} case content.MsgType == event.MsgFile: return &waE2E.Message{DocumentWithCaptionMessage: &waE2E.FutureProofMessage{ Message: &waE2E.Message{ DocumentMessage: &waE2E.DocumentMessage{Caption: proto.String(caption), ContextInfo: contextInfo}, }, }} - case content.MsgType == event.MsgVideo, content.GetInfo().MimeType == "image/gif": - return &waE2E.Message{VideoMessage: &waE2E.VideoMessage{Caption: proto.String(caption), ContextInfo: contextInfo}} default: return &waE2E.Message{ImageMessage: &waE2E.ImageMessage{Caption: proto.String(caption), ContextInfo: contextInfo}} } From 7cf259940d817643acbeabc793ed5fb2ca409504 Mon Sep 17 00:00:00 2001 From: Gerardo Rodriguez Date: Tue, 10 Nov 2026 19:06:21 +0000 Subject: [PATCH 3/3] msgconv/from-matrix: build caption edits through constructMediaMessage --- pkg/connector/handlematrix.go | 2 +- pkg/msgconv/from-matrix.go | 34 ++++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/pkg/connector/handlematrix.go b/pkg/connector/handlematrix.go index cc2e8436..935d7628 100644 --- a/pkg/connector/handlematrix.go +++ b/pkg/connector/handlematrix.go @@ -273,7 +273,7 @@ func (wa *WhatsAppClient) HandleMatrixEdit(ctx context.Context, edit *bridgev2.M return fmt.Errorf("failed to parse portal ID: %w", err) } - waMsg := wa.Main.MsgConv.CaptionEditToWhatsApp(ctx, wa.Client, edit.Content, edit.Portal) + waMsg := wa.Main.MsgConv.CaptionEditToWhatsApp(ctx, wa.Client, edit.Event, edit.Content, edit.Portal) if waMsg == nil { waMsg, _, err = wa.Main.MsgConv.ToWhatsApp(ctx, wa.Client, edit.Event, edit.Content, nil, nil, edit.Portal) if err != nil { diff --git a/pkg/msgconv/from-matrix.go b/pkg/msgconv/from-matrix.go index 09fad3e0..c45a2205 100644 --- a/pkg/msgconv/from-matrix.go +++ b/pkg/msgconv/from-matrix.go @@ -207,30 +207,40 @@ func (mc *MessageConverter) ToWhatsApp( func (mc *MessageConverter) CaptionEditToWhatsApp( ctx context.Context, client *whatsmeow.Client, + evt *event.Event, content *event.MessageEventContent, portal *bridgev2.Portal, ) *waE2E.Message { - if (content.MsgType != event.MsgImage && content.MsgType != event.MsgVideo && content.MsgType != event.MsgFile) || content.GetInfo().MimeType == "" { + mime := content.GetInfo().MimeType + if mime == "" { return nil + } else if mime == "image/gif" { + content.MsgType = event.MsgVideo } ctx = context.WithValue(ctx, contextKeyClient, client) ctx = context.WithValue(ctx, contextKeyPortal, portal) contextInfo := mc.generateContextInfo(ctx, nil, content.RelatesTo, portal, content.BeeperDisappearingTimer, content.Mentions != nil && content.Mentions.Room) - var caption string - if content.FileName != "" && content.Body != content.FileName { - caption, contextInfo.MentionedJID = mc.parseText(ctx, content) - } + msg := mc.constructMediaMessage(ctx, content, evt, &whatsmeow.UploadResponse{}, nil, contextInfo, mime) switch { - case content.MsgType == event.MsgVideo, content.Info.MimeType == "image/gif": - return &waE2E.Message{VideoMessage: &waE2E.VideoMessage{Caption: proto.String(caption), ContextInfo: contextInfo}} - case content.MsgType == event.MsgFile: + case msg.GetImageMessage() != nil: + return &waE2E.Message{ImageMessage: &waE2E.ImageMessage{ + Caption: msg.ImageMessage.Caption, + ContextInfo: contextInfo, + }} + case msg.GetVideoMessage() != nil: + return &waE2E.Message{VideoMessage: &waE2E.VideoMessage{ + Caption: msg.VideoMessage.Caption, + ContextInfo: contextInfo, + }} + case msg.GetDocumentMessage() != nil, msg.GetDocumentWithCaptionMessage() != nil: return &waE2E.Message{DocumentWithCaptionMessage: &waE2E.FutureProofMessage{ - Message: &waE2E.Message{ - DocumentMessage: &waE2E.DocumentMessage{Caption: proto.String(caption), ContextInfo: contextInfo}, - }, + Message: &waE2E.Message{DocumentMessage: &waE2E.DocumentMessage{ + Caption: proto.String(msg.GetDocumentWithCaptionMessage().GetMessage().GetDocumentMessage().GetCaption()), + ContextInfo: contextInfo, + }}, }} default: - return &waE2E.Message{ImageMessage: &waE2E.ImageMessage{Caption: proto.String(caption), ContextInfo: contextInfo}} + return nil } }