Add support for Matrix -> WhatsApp captions with MSC2530

This commit is contained in:
Tulir Asokan 2022-07-11 14:38:47 +03:00
parent 73f4449761
commit 794975eaa0

View file

@ -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, Title: &media.FileName,
FileName: &content.Body, FileName: &media.FileName,
MediaKey: media.MediaKey, MediaKey: media.MediaKey,
Mimetype: &content.GetInfo().MimeType, Mimetype: &content.GetInfo().MimeType,
FileEncSha256: media.FileEncSHA256, FileEncSha256: media.FileEncSHA256,