From b652281682e43e313fbba0cd0684c85eafe5bd5e Mon Sep 17 00:00:00 2001 From: abmantis Date: Tue, 9 Nov 2021 15:12:03 +0200 Subject: [PATCH] Send portal notice on reactions Closes #373 --- config/bridge.go | 1 + example-config.yaml | 3 +++ matrix.go | 18 ++++++++++++++++++ portal.go | 21 +++++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/config/bridge.go b/config/bridge.go index da794b3..f8052ba 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -35,6 +35,7 @@ type BridgeConfig struct { DeliveryReceipts bool `yaml:"delivery_receipts"` PortalMessageBuffer int `yaml:"portal_message_buffer"` CallStartNotices bool `yaml:"call_start_notices"` + ReactionNotices bool `yaml:"reaction_notices"` HistorySync struct { CreatePortals bool `yaml:"create_portals"` diff --git a/example-config.yaml b/example-config.yaml index 0dfa57d..a88be4a 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -92,6 +92,9 @@ bridge: delivery_receipts: false # Should incoming calls send a message to the Matrix room? call_start_notices: true + # Since WhatsApp does not support reactions, should a warning notice be sent to the Matrix room when a user reacts to a message? + reaction_notices: true + portal_message_buffer: 128 # Settings for handling history sync payloads. These settings only apply right after login, diff --git a/matrix.go b/matrix.go index 523b6a2..ee0424e 100644 --- a/matrix.go +++ b/matrix.go @@ -50,6 +50,7 @@ func NewMatrixHandler(bridge *Bridge) *MatrixHandler { bridge.EventProcessor.On(event.EventMessage, handler.HandleMessage) bridge.EventProcessor.On(event.EventEncrypted, handler.HandleEncrypted) bridge.EventProcessor.On(event.EventSticker, handler.HandleMessage) + bridge.EventProcessor.On(event.EventReaction, handler.HandleReaction) bridge.EventProcessor.On(event.EventRedaction, handler.HandleRedaction) bridge.EventProcessor.On(event.StateMember, handler.HandleMembership) bridge.EventProcessor.On(event.StateRoomName, handler.HandleRoomMetadata) @@ -428,6 +429,23 @@ func (mx *MatrixHandler) HandleMessage(evt *event.Event) { } } +func (mx *MatrixHandler) HandleReaction(evt *event.Event) { + defer mx.bridge.Metrics.TrackMatrixEvent(evt.Type)() + if mx.shouldIgnoreEvent(evt) { + return + } + + user := mx.bridge.GetUserByMXID(evt.Sender) + if user == nil { + return + } + + portal := mx.bridge.GetPortalByMXID(evt.RoomID) + if portal != nil && (user.Whitelisted || portal.HasRelaybot()) { + portal.HandleMatrixReaction(user, evt) + } +} + func (mx *MatrixHandler) HandleRedaction(evt *event.Event) { defer mx.bridge.Metrics.TrackMatrixEvent(evt.Type)() diff --git a/portal.go b/portal.go index d76b4f3..f851974 100644 --- a/portal.go +++ b/portal.go @@ -2113,6 +2113,27 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) { } } +func (portal *Portal) HandleMatrixReaction(sender *User, evt *event.Event) { + if !portal.canBridgeFrom(sender, "message") { + return + } + portal.log.Debugfln("Received reaction event %s from %s", evt.ID, evt.Sender) + + if evt.Type.Type != event.EventReaction.Type { + portal.log.Warnfln("Reaction event is not of Reaction type: %s", evt.Type.Type) + } + + if portal.bridge.Config.Bridge.ReactionNotices { + _, err := portal.sendMainIntentMessage(&event.MessageEventContent{ + MsgType: event.MsgNotice, + Body: fmt.Sprintf("\u26a0 Reactions are not supported by WhatsApp."), + }) + if err != nil { + portal.log.Warnfln("Failed to send reaction notice message:", err) + } + } +} + func (portal *Portal) HandleMatrixRedaction(sender *User, evt *event.Event) { if !portal.canBridgeFrom(sender, "redaction") { return