Переглянути джерело

Add config option to bundle caption with media message

Tulir Asokan 3 роки тому
батько
коміт
aa0daceb85
5 змінених файлів з 32 додано та 0 видалено
  1. 1 0
      config/bridge.go
  2. 1 0
      config/upgrade.go
  3. 3 0
      example-config.yaml
  4. 3 0
      historysync.go
  5. 24 0
      portal.go

+ 1 - 0
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"`

+ 1 - 0
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")

+ 3 - 0
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"

+ 3 - 0
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 {

+ 24 - 0
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,