diff --git a/config/bridge.go b/config/bridge.go index f465e43..b4cb137 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -106,6 +106,7 @@ type BridgeConfig struct { AllowUserInvite bool `yaml:"allow_user_invite"` FederateRooms bool `yaml:"federate_rooms"` URLPreviews bool `yaml:"url_previews"` + CaptionInMessage bool `yaml:"caption_in_message"` DisableStatusBroadcastSend bool `yaml:"disable_status_broadcast_send"` DisappearingMessagesInGroups bool `yaml:"disappearing_messages_in_groups"` diff --git a/config/upgrade.go b/config/upgrade.go index ddd2951..985ef8b 100644 --- a/config/upgrade.go +++ b/config/upgrade.go @@ -90,6 +90,7 @@ func DoUpgrade(helper *up.Helper) { helper.Copy(up.Bool, "bridge", "disappearing_messages_in_groups") helper.Copy(up.Bool, "bridge", "disable_bridge_alerts") helper.Copy(up.Bool, "bridge", "url_previews") + helper.Copy(up.Bool, "bridge", "caption_in_message") helper.Copy(up.Str, "bridge", "management_room_text", "welcome") helper.Copy(up.Str, "bridge", "management_room_text", "welcome_connected") helper.Copy(up.Str, "bridge", "management_room_text", "welcome_unconnected") diff --git a/example-config.yaml b/example-config.yaml index a91a92f..308d0cf 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -277,6 +277,9 @@ bridge: # and send it to WhatsApp? URL previews can always be sent using the `com.beeper.linkpreviews` # key in the event content even if this is disabled. url_previews: false + # Send captions in the same message as images. This will send data compatible with both MSC2530 and MSC3552. + # This is currently not supported in most clients. + caption_in_message: false # The prefix for commands. Only required in non-management rooms. command_prefix: "!wa" diff --git a/historysync.go b/historysync.go index f7a30ef..a4bd530 100644 --- a/historysync.go +++ b/historysync.go @@ -629,6 +629,9 @@ func (portal *Portal) appendBatchEvents(converted *ConvertedMessage, info *types if err != nil { return err } + if portal.bridge.Config.Bridge.CaptionInMessage { + converted.MergeCaption() + } if converted.Caption != nil { captionEvt, err := portal.wrapBatchEvent(info, converted.Intent, converted.Type, converted.Caption, nil) if err != nil { diff --git a/portal.go b/portal.go index 255528a..bb44776 100644 --- a/portal.go +++ b/portal.go @@ -680,6 +680,9 @@ func (portal *Portal) handleMessage(source *User, evt *events.Message) { } converted.Extra["fi.mau.whatsapp.source_broadcast_list"] = evt.Info.Chat.String() } + if portal.bridge.Config.Bridge.CaptionInMessage { + converted.MergeCaption() + } var eventID id.EventID var lastEventID id.EventID if existingMsg != nil { @@ -1674,6 +1677,24 @@ type ConvertedMessage struct { MediaKey []byte } +func (cm *ConvertedMessage) MergeCaption() { + if cm.Caption == nil { + return + } + cm.Extra["filename"] = cm.Content.Body + extensibleCaption := map[string]interface{}{ + "org.matrix.msc1767.text": cm.Caption.Body, + } + cm.Extra["org.matrix.msc1767.caption"] = extensibleCaption + cm.Content.Body = cm.Caption.Body + if cm.Caption.Format == event.FormatHTML { + cm.Content.Format = event.FormatHTML + cm.Content.FormattedBody = cm.Caption.FormattedBody + extensibleCaption["org.matrix.msc1767.html"] = cm.Caption.FormattedBody + } + cm.Caption = nil +} + func (portal *Portal) convertTextMessage(intent *appservice.IntentAPI, source *User, msg *waProto.Message) *ConvertedMessage { content := &event.MessageEventContent{ Body: msg.GetConversation(), @@ -2004,6 +2025,9 @@ func (portal *Portal) makeMediaBridgeFailureMessage(info *types.MessageInfo, bri portal.log.Errorfln("Failed to bridge media for %s: %v", info.ID, bridgeErr) } if keys != nil { + if portal.bridge.Config.Bridge.CaptionInMessage { + converted.MergeCaption() + } meta := &FailedMediaMeta{ Type: converted.Type, Content: converted.Content,