Skip to content

Commit

Permalink
Add config option to bundle caption with media message
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jun 17, 2022
1 parent 8012368 commit aa0dace
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions config/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions historysync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit aa0dace

Please sign in to comment.