Skip to content

Commit b4497fc

Browse files
Add ID mapping on incoming messages
1 parent a0c0fc1 commit b4497fc

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

gateway/gateway.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,21 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
116116
// For some bridges we always add/update the message ID.
117117
// This is necessary as msgIDs will change if a bridge returns
118118
// a different ID in response to edits.
119-
values, exists := gw.Messages.Get(ack.ExternalID.Protocol + " " + ack.InternalID.String())
120119

121-
var brMsgIDs []*BrMsgID
122-
if exists {
123-
// We want to append, not overwrite
124-
brMsgIDs = values
120+
// brMsgIDs is always initialized in Router.handleReceive(). However,
121+
// we may still receive a message we don't know about. Maybe matterbridge
122+
// was restarted, or another client is connected on the same account sending
123+
// messages, or the remote server is melting down and dinosaurs are walking
124+
// the Earth...
125+
brMsgIDs, exists := gw.Messages.Get(ack.ExternalID.Protocol + " " + ack.InternalID.String())
126+
127+
if !exists {
128+
gw.logger.Warnf("Unknown message %s has been acked by %s as ID: %s", ack.InternalID.String(), ack.ExternalID.Protocol, ack.ExternalID.ID)
129+
continue
125130
}
126131

127132
brMsgIDs = append(brMsgIDs, &BrMsgID{ack.DestBridge, ack.ExternalID.Protocol + " " + ack.ExternalID.ID, ack.ExternalID.ChannelID})
128133
gw.Messages.Add(ack.ExternalID.Protocol+" "+ack.InternalID.String(), brMsgIDs)
129-
130-
// Just for testing that we added everything correctly
131-
// TODO: remove this useless debug log
132-
values, _ = gw.Messages.Get(ack.ExternalID.Protocol + " " + ack.InternalID.String())
133-
for _, id := range values {
134-
gw.logger.Warnf(" | %s -> %s (%s)", ack.InternalID.String(), id.ID, id.ChannelID)
135-
}
136134
}
137135
}()
138136

gateway/router.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,16 @@ func (r *Router) handleReceive() {
140140

141141
// We add an internal UUID which will allow destination protocols
142142
// to send back their own ID(s) corresponding to the message go the
143-
// gateway in an asynchronous manner.
143+
// gateway in an asynchronous manner (for replies/reactions).
144144
msg.InternalID = xid.New()
145145

146146
r.handleEventGetChannelMembers(&msg)
147147
r.handleEventFailure(&msg)
148148
r.handleEventRejoinChannels(&msg)
149149

150+
msgBridge := r.getBridge(msg.Account)
150151
// Set message protocol based on the account it came from
151-
msg.Protocol = r.getBridge(msg.Account).Protocol
152+
msg.Protocol = msgBridge.Protocol
152153

153154
filesHandled := false
154155
for _, gw := range r.Gateways {
@@ -162,6 +163,16 @@ func (r *Router) handleReceive() {
162163
gw.handleFiles(&msg)
163164
filesHandled = true
164165
}
166+
167+
// If the origin bridge brought us a message ID, map it with our
168+
// internal ID for replies/reactions.
169+
BrMsgIDs := []*BrMsgID{}
170+
if msg.ID != "" {
171+
BrMsgIDs = append(BrMsgIDs, &BrMsgID{msgBridge, msg.ID, msg.Channel})
172+
}
173+
// Even if it might be empty, already initialize the mapping
174+
gw.Messages.Add(msg.Protocol+" "+msg.InternalID.String(), BrMsgIDs)
175+
165176
for _, br := range gw.Bridges {
166177
gw.handleMessage(&msg, br)
167178
}

0 commit comments

Comments
 (0)