Browse Source

Add support for Matrix -> WhatsApp captions with MSC2530

Tulir Asokan 3 years ago
parent
commit
794975eaa0
1 changed files with 13 additions and 3 deletions
  1. 13 3
      portal.go

+ 13 - 3
portal.go

@@ -2779,9 +2779,16 @@ func (portal *Portal) convertWebPtoPNG(webpImage []byte) ([]byte, error) {
 }
 }
 
 
 func (portal *Portal) preprocessMatrixMedia(ctx context.Context, sender *User, relaybotFormatted bool, content *event.MessageEventContent, eventID id.EventID, mediaType whatsmeow.MediaType) (*MediaUpload, error) {
 func (portal *Portal) preprocessMatrixMedia(ctx context.Context, sender *User, relaybotFormatted bool, content *event.MessageEventContent, eventID id.EventID, mediaType whatsmeow.MediaType) (*MediaUpload, error) {
+	fileName := content.Body
 	var caption string
 	var caption string
 	var mentionedJIDs []string
 	var mentionedJIDs []string
-	if relaybotFormatted {
+	var hasHTMLCaption bool
+	if content.FileName != "" && content.Body != content.FileName {
+		fileName = content.FileName
+		caption = content.Body
+		hasHTMLCaption = content.Format == event.FormatHTML
+	}
+	if relaybotFormatted || hasHTMLCaption {
 		caption, mentionedJIDs = portal.bridge.Formatter.ParseMatrix(content.FormattedBody)
 		caption, mentionedJIDs = portal.bridge.Formatter.ParseMatrix(content.FormattedBody)
 	}
 	}
 
 
@@ -2839,6 +2846,7 @@ func (portal *Portal) preprocessMatrixMedia(ctx context.Context, sender *User, r
 
 
 	return &MediaUpload{
 	return &MediaUpload{
 		UploadResponse: uploadResp,
 		UploadResponse: uploadResp,
+		FileName:       fileName,
 		Caption:        caption,
 		Caption:        caption,
 		MentionedJIDs:  mentionedJIDs,
 		MentionedJIDs:  mentionedJIDs,
 		Thumbnail:      thumbnail,
 		Thumbnail:      thumbnail,
@@ -2849,6 +2857,7 @@ func (portal *Portal) preprocessMatrixMedia(ctx context.Context, sender *User, r
 type MediaUpload struct {
 type MediaUpload struct {
 	whatsmeow.UploadResponse
 	whatsmeow.UploadResponse
 	Caption       string
 	Caption       string
+	FileName      string
 	MentionedJIDs []string
 	MentionedJIDs []string
 	Thumbnail     []byte
 	Thumbnail     []byte
 	FileLength    int
 	FileLength    int
@@ -3054,10 +3063,11 @@ func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, ev
 		}
 		}
 		msg.DocumentMessage = &waProto.DocumentMessage{
 		msg.DocumentMessage = &waProto.DocumentMessage{
 			ContextInfo:   &ctxInfo,
 			ContextInfo:   &ctxInfo,
+			Caption:       &media.Caption,
 			JpegThumbnail: media.Thumbnail,
 			JpegThumbnail: media.Thumbnail,
 			Url:           &media.URL,
 			Url:           &media.URL,
-			Title:         &content.Body,
-			FileName:      &content.Body,
+			Title:         &media.FileName,
+			FileName:      &media.FileName,
 			MediaKey:      media.MediaKey,
 			MediaKey:      media.MediaKey,
 			Mimetype:      &content.GetInfo().MimeType,
 			Mimetype:      &content.GetInfo().MimeType,
 			FileEncSha256: media.FileEncSHA256,
 			FileEncSha256: media.FileEncSHA256,