Skip to content

Commit

Permalink
events: fix target message for reactions in DMs
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Nov 14, 2024
1 parent 5aee9f2 commit d838491
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 28 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
golang.org/x/sync v0.9.0
google.golang.org/protobuf v1.35.1
gopkg.in/yaml.v3 v3.0.1
maunium.net/go/mautrix v0.21.2-0.20241113163141-3f9a63784ec5
maunium.net/go/mautrix v0.21.2-0.20241114125808-21aa3291f31f
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ 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.21.2-0.20241113163141-3f9a63784ec5 h1:dBD+xOAzvhQBcO3RFUK20PwOKhWnY10iXWrehgSTI7c=
maunium.net/go/mautrix v0.21.2-0.20241113163141-3f9a63784ec5/go.mod h1:X7VB/wOIUo6c3wACyVwA//v2k8BpMLFB2rvaX2Y0984=
maunium.net/go/mautrix v0.21.2-0.20241114125808-21aa3291f31f h1:NyPane3nuWA3Pisj7Z0uG1rmNDOkDFSrXEB8xGN0gzs=
maunium.net/go/mautrix v0.21.2-0.20241114125808-21aa3291f31f/go.mod h1:X7VB/wOIUo6c3wACyVwA//v2k8BpMLFB2rvaX2Y0984=
27 changes: 5 additions & 22 deletions pkg/connector/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ func (evt *WAMessageEvent) isViewOnce() bool {
}

func (evt *WAMessageEvent) AddLogContext(c zerolog.Context) zerolog.Context {
if targetMsg := evt.GetTargetMessage(); targetMsg != "" {
c = c.Str("target_message_id", string(targetMsg))
}
return evt.MessageInfoWrapper.AddLogContext(c).Str("parsed_message_type", evt.parsedMessageType)
}

Expand Down Expand Up @@ -154,29 +157,9 @@ func (evt *WAMessageEvent) ConvertEdit(ctx context.Context, portal *bridgev2.Por

func (evt *WAMessageEvent) GetTargetMessage() networkid.MessageID {
if reactionMsg := evt.Message.GetReactionMessage(); reactionMsg != nil {
key := reactionMsg.GetKey()
senderID := key.GetParticipant()
if senderID == "" {
if key.GetFromMe() {
senderID = evt.Info.Sender.ToNonAD().String()
} else {
senderID = evt.wa.Client.Store.ID.ToNonAD().String() // could be false in groups
}
}
senderJID, _ := types.ParseJID(senderID)
return waid.MakeMessageID(evt.Info.Chat, senderJID, *key.ID)
return msgconv.KeyToMessageID(evt.wa.Client, evt.Info.Chat, evt.Info.Sender, reactionMsg.GetKey())
} else if protocolMsg := evt.Message.GetProtocolMessage(); protocolMsg != nil {
key := protocolMsg.GetKey()
senderID := key.GetParticipant()
if senderID == "" {
if key.GetFromMe() {
senderID = evt.Info.Sender.ToNonAD().String()
} else {
senderID = evt.wa.Client.Store.ID.ToNonAD().String() // could be false in groups
}
}
senderJID, _ := types.ParseJID(senderID)
return waid.MakeMessageID(evt.Info.Chat, senderJID, *key.ID)
return msgconv.KeyToMessageID(evt.wa.Client, evt.Info.Chat, evt.Info.Sender, protocolMsg.GetKey())
}
return ""
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/msgconv/wa-poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/rs/zerolog"
"go.mau.fi/util/ptr"
"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/proto/waCommon"
"go.mau.fi/whatsmeow/proto/waE2E"
"go.mau.fi/whatsmeow/types"
Expand Down Expand Up @@ -94,7 +95,7 @@ func (mc *MessageConverter) convertPollCreationMessage(ctx context.Context, msg
}, msg.GetContextInfo()
}

func (mc *MessageConverter) keyToMessageID(ctx context.Context, chat, sender types.JID, key *waCommon.MessageKey) networkid.MessageID {
func KeyToMessageID(client *whatsmeow.Client, chat, sender types.JID, key *waCommon.MessageKey) networkid.MessageID {
sender = sender.ToNonAD()
var err error
if !key.GetFromMe() {
Expand All @@ -108,7 +109,7 @@ func (mc *MessageConverter) keyToMessageID(ctx context.Context, chat, sender typ
sender.Server = types.DefaultUserServer
}
} else if chat.Server == types.DefaultUserServer {
ownID := ptr.Val(getClient(ctx).Store.ID).ToNonAD()
ownID := ptr.Val(client.Store.ID).ToNonAD()
if sender.User == ownID.User {
sender = chat
} else {
Expand Down Expand Up @@ -137,7 +138,7 @@ var failedPollUpdatePart = &bridgev2.ConvertedMessagePart{

func (mc *MessageConverter) convertPollUpdateMessage(ctx context.Context, info *types.MessageInfo, msg *waE2E.PollUpdateMessage) (*bridgev2.ConvertedMessagePart, *waE2E.ContextInfo) {
log := zerolog.Ctx(ctx)
pollMessageID := mc.keyToMessageID(ctx, info.Chat, info.Sender, msg.PollCreationMessageKey)
pollMessageID := KeyToMessageID(getClient(ctx), info.Chat, info.Sender, msg.PollCreationMessageKey)
pollMessage, err := mc.Bridge.DB.Message.GetPartByID(ctx, getPortal(ctx).Receiver, pollMessageID, "")
if err != nil {
log.Err(err).Msg("Failed to get poll update target message")
Expand Down

0 comments on commit d838491

Please sign in to comment.