From 64a364d97de77cf707e3960a7f992c924a5f9728 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 7 Jun 2024 12:57:36 +0300 Subject: [PATCH] Update reaction deduplication --- go.mod | 2 +- go.sum | 4 ++-- pkg/connector/connector.go | 47 ++++++++++++-------------------------- 3 files changed, 17 insertions(+), 36 deletions(-) diff --git a/go.mod b/go.mod index 726ed5a7..7d766d8a 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( golang.org/x/net v0.25.0 google.golang.org/protobuf v1.34.1 gopkg.in/yaml.v3 v3.0.1 - maunium.net/go/mautrix v0.18.2-0.20240606175822-a150a4760419 + maunium.net/go/mautrix v0.18.2-0.20240607095501-6466bf945260 nhooyr.io/websocket v1.8.11 ) diff --git a/go.sum b/go.sum index d6720a8d..78957fa7 100644 --- a/go.sum +++ b/go.sum @@ -95,7 +95,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M= maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA= -maunium.net/go/mautrix v0.18.2-0.20240606175822-a150a4760419 h1:6JOCYkxS/nfhK/J3o+kkJgUDRaPRGgQXaxZj5HX5jys= -maunium.net/go/mautrix v0.18.2-0.20240606175822-a150a4760419/go.mod h1:P/FV8cXY262MezYX7ViuhfzeJ0nK4+M8K6ZmxEC/aEA= +maunium.net/go/mautrix v0.18.2-0.20240607095501-6466bf945260 h1:PRjq8wEtssdiUhwVpo9h2LAlVEB04r3y/YR2XZsdlbg= +maunium.net/go/mautrix v0.18.2-0.20240607095501-6466bf945260/go.mod h1:P/FV8cXY262MezYX7ViuhfzeJ0nK4+M8K6ZmxEC/aEA= nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0= nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/pkg/connector/connector.go b/pkg/connector/connector.go index f46270e8..69ec6b35 100644 --- a/pkg/connector/connector.go +++ b/pkg/connector/connector.go @@ -444,7 +444,8 @@ var ( _ bridgev2.RemoteMessage = (*Bv2ChatEvent)(nil) _ bridgev2.RemoteEdit = (*Bv2ChatEvent)(nil) _ bridgev2.RemoteEventWithTimestamp = (*Bv2ChatEvent)(nil) - _ bridgev2.RemoteReactionWithMeta = (*Bv2ChatEvent)(nil) + _ bridgev2.RemoteReaction = (*Bv2ChatEvent)(nil) + _ bridgev2.RemoteReactionRemove = (*Bv2ChatEvent)(nil) _ bridgev2.RemoteMessageRemove = (*Bv2ChatEvent)(nil) ) @@ -562,14 +563,8 @@ func (evt *Bv2ChatEvent) GetReactionEmoji() (string, networkid.EmojiID) { return dataMsg.GetReaction().GetEmoji(), "" } -func (evt *Bv2ChatEvent) GetReactionDBMetadata() map[string]any { - dataMsg, ok := evt.Event.(*signalpb.DataMessage) - if !ok || dataMsg.Reaction == nil { - return map[string]any{} - } - return map[string]any{ - "emoji": dataMsg.GetReaction().GetEmoji(), - } +func (evt *Bv2ChatEvent) GetRemovedEmojiID() networkid.EmojiID { + return "" } func (evt *Bv2ChatEvent) ConvertMessage(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI) (*bridgev2.ConvertedMessage, error) { @@ -695,8 +690,6 @@ func (s *SignalClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.Ma } dbMsg := &database.Message{ ID: makeMessageID(s.Client.Store.ACI, converted.GetTimestamp()), - MXID: msg.Event.ID, - RoomID: msg.Portal.ID, SenderID: makeUserID(s.Client.Store.ACI), Timestamp: time.UnixMilli(int64(converted.GetTimestamp())), Metadata: meta, @@ -763,16 +756,15 @@ func (s *SignalClient) sendMessage(ctx context.Context, portalID networkid.Porta } } +func (s *SignalClient) PreHandleMatrixReaction(ctx context.Context, msg *bridgev2.MatrixReaction) (bridgev2.MatrixReactionPreResponse, error) { + return bridgev2.MatrixReactionPreResponse{ + SenderID: makeUserID(s.Client.Store.ACI), + EmojiID: "", + Emoji: variationselector.FullyQualify(msg.Content.RelatesTo.Key), + }, nil +} + func (s *SignalClient) HandleMatrixReaction(ctx context.Context, msg *bridgev2.MatrixReaction) (reaction *database.Reaction, err error) { - senderID := makeUserID(s.Client.Store.ACI) - // emojiID is always empty because only one reaction is allowed per message+user - var emojiID networkid.EmojiID - signalEmoji := variationselector.FullyQualify(msg.Content.RelatesTo.Key) - if existing, err := msg.GetExisting(ctx, senderID, emojiID); err != nil { - return nil, fmt.Errorf("failed to check for duplicate reaction: %w", err) - } else if existing != nil && existing.Metadata["emoji"] == signalEmoji { - return nil, nil - } targetAuthorACI, targetSentTimestamp, err := parseMessageID(msg.TargetMessage.ID) if err != nil { return nil, fmt.Errorf("failed to parse target message ID: %w", err) @@ -782,7 +774,7 @@ func (s *SignalClient) HandleMatrixReaction(ctx context.Context, msg *bridgev2.M Timestamp: proto.Uint64(uint64(msg.Event.Timestamp)), RequiredProtocolVersion: proto.Uint32(uint32(signalpb.DataMessage_REACTIONS)), Reaction: &signalpb.DataMessage_Reaction{ - Emoji: proto.String(signalEmoji), + Emoji: proto.String(msg.PreHandleResp.Emoji), Remove: proto.Bool(false), TargetAuthorAci: proto.String(targetAuthorACI.String()), TargetSentTimestamp: proto.Uint64(targetSentTimestamp), @@ -795,18 +787,7 @@ func (s *SignalClient) HandleMatrixReaction(ctx context.Context, msg *bridgev2.M } // TODO check result fmt.Println(res) - return &database.Reaction{ - RoomID: msg.Portal.ID, - MessageID: msg.TargetMessage.ID, - MessagePartID: msg.TargetMessage.PartID, - SenderID: senderID, - EmojiID: emojiID, - MXID: msg.Event.ID, - Timestamp: time.UnixMilli(msg.Event.Timestamp), - Metadata: map[string]any{ - "emoji": signalEmoji, - }, - }, nil + return &database.Reaction{}, nil } func (s *SignalClient) HandleMatrixReactionRemove(ctx context.Context, msg *bridgev2.MatrixReactionRemove) error {