Skip to content

Commit 12260d2

Browse files
Use typed ID as key in message mapping (unsure about method logic)
1 parent b4497fc commit 12260d2

3 files changed

Lines changed: 25 additions & 27 deletions

File tree

gateway/gateway.go

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/matterbridge-org/matterbridge/bridge"
1515
"github.com/matterbridge-org/matterbridge/bridge/config"
1616
"github.com/matterbridge-org/matterbridge/internal"
17+
"github.com/rs/xid"
1718
"github.com/sirupsen/logrus"
1819
)
1920

@@ -27,7 +28,7 @@ type Gateway struct {
2728
ChannelOptions map[string]config.ChannelOptions
2829
Message chan config.Message
2930
Name string
30-
Messages *lru.Cache[string, []*BrMsgID]
31+
Messages *lru.Cache[xid.ID, []*BrMsgID]
3132

3233
logger *logrus.Entry
3334
}
@@ -45,7 +46,7 @@ const apiProtocol = "api"
4546
func New(rootLogger *logrus.Logger, cfg *config.Gateway, r *Router) *Gateway {
4647
logger := rootLogger.WithFields(logrus.Fields{"prefix": "gateway"})
4748

48-
cache, _ := lru.New[string, []*BrMsgID](5000)
49+
cache, _ := lru.New[xid.ID, []*BrMsgID](5000)
4950
gw := &Gateway{
5051
Channels: make(map[string]*config.ChannelInfo),
5152
Message: r.Message,
@@ -62,22 +63,19 @@ func New(rootLogger *logrus.Logger, cfg *config.Gateway, r *Router) *Gateway {
6263
}
6364

6465
// FindCanonicalMsgID returns the ID under which a message was stored in the cache.
65-
func (gw *Gateway) FindCanonicalMsgID(protocol string, mID string) string {
66-
ID := protocol + " " + mID
67-
if gw.Messages.Contains(ID) {
68-
return ID
69-
}
70-
71-
// If not keyed, iterate through cache for downstream, and infer upstream.
72-
for _, mid := range gw.Messages.Keys() {
73-
ids, _ := gw.Messages.Peek(mid)
74-
for _, downstreamMsgObj := range ids {
75-
if ID == downstreamMsgObj.ID {
76-
return mid
66+
func (gw *Gateway) FindCanonicalMsgID(protocol string, externalID string) *xid.ID {
67+
// Now that we have internal IDs, mID is never going to be the key
68+
for _, internalID := range gw.Messages.Keys() {
69+
// TODO: should we check if the mapping exists here? or is this method
70+
// only used when we're 100% sure?
71+
externalIDs, _ := gw.Messages.Peek(internalID)
72+
for _, downstreamMsgObj := range externalIDs {
73+
if externalID == downstreamMsgObj.ID {
74+
return &internalID
7775
}
7876
}
7977
}
80-
return ""
78+
return nil
8179
}
8280

8381
// AddBridge sets up a new bridge on startup.
@@ -122,15 +120,15 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
122120
// was restarted, or another client is connected on the same account sending
123121
// messages, or the remote server is melting down and dinosaurs are walking
124122
// the Earth...
125-
brMsgIDs, exists := gw.Messages.Get(ack.ExternalID.Protocol + " " + ack.InternalID.String())
123+
brMsgIDs, exists := gw.Messages.Get(ack.InternalID)
126124

127125
if !exists {
128126
gw.logger.Warnf("Unknown message %s has been acked by %s as ID: %s", ack.InternalID.String(), ack.ExternalID.Protocol, ack.ExternalID.ID)
129127
continue
130128
}
131129

132130
brMsgIDs = append(brMsgIDs, &BrMsgID{ack.DestBridge, ack.ExternalID.Protocol + " " + ack.ExternalID.ID, ack.ExternalID.ChannelID})
133-
gw.Messages.Add(ack.ExternalID.Protocol+" "+ack.InternalID.String(), brMsgIDs)
131+
gw.Messages.Add(ack.InternalID, brMsgIDs)
134132
}
135133
}()
136134

@@ -305,13 +303,12 @@ func (gw *Gateway) getDestChannel(msg *config.Message, dest bridge.Bridge) []con
305303
return channels
306304
}
307305

308-
func (gw *Gateway) getDestMsgID(msgID string, dest *bridge.Bridge, channel *config.ChannelInfo) string {
306+
func (gw *Gateway) getDestMsgID(msgID xid.ID, dest *bridge.Bridge, channel *config.ChannelInfo) string {
309307
if IDs, ok := gw.Messages.Get(msgID); ok {
310308
for _, id := range IDs {
311309
// check protocol, bridge name and channelname
312-
// for people that reuse the same bridge multiple times. see #342
313310
if dest.Protocol == id.br.Protocol && dest.Name == id.br.Name && channel.ID == id.ChannelID {
314-
return strings.Replace(id.ID, dest.Protocol+" ", "", 1)
311+
return id.ID
315312
}
316313
}
317314
}
@@ -480,7 +477,7 @@ func (gw *Gateway) SendMessage(
480477
rmsg *config.Message,
481478
dest *bridge.Bridge,
482479
channel *config.ChannelInfo,
483-
canonicalParentMsgID string,
480+
canonicalParentMsgID *xid.ID,
484481
) {
485482
msg := *rmsg
486483
// Only send the avatar download event to ourselves.
@@ -512,17 +509,17 @@ func (gw *Gateway) SendMessage(
512509

513510
// exclude file delete event as the msg ID here is the native file ID that needs to be deleted
514511
if msg.Event != config.EventFileDelete {
515-
msg.ID = gw.getDestMsgID(rmsg.Protocol+" "+rmsg.ID, dest, channel)
512+
msg.ID = gw.getDestMsgID(rmsg.InternalID, dest, channel)
516513
}
517514

518515
// for api we need originchannel as channel
519516
if dest.Protocol == apiProtocol {
520517
msg.Channel = rmsg.Channel
521518
}
522519

523-
msg.ParentID = gw.getDestMsgID(canonicalParentMsgID, dest, channel)
524-
if msg.ParentID == "" {
525-
msg.ParentID = strings.Replace(canonicalParentMsgID, dest.Protocol+" ", "", 1)
520+
// TODO: ParentID should be typed too
521+
if canonicalParentMsgID != nil {
522+
msg.ParentID = gw.getDestMsgID(*canonicalParentMsgID, dest, channel)
526523
}
527524

528525
// if the parentID is still empty and we have a parentID set in the original message

gateway/handlers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/matterbridge-org/matterbridge/bridge"
1212
"github.com/matterbridge-org/matterbridge/bridge/config"
1313
"github.com/matterbridge-org/matterbridge/gateway/bridgemap"
14+
"github.com/rs/xid"
1415
)
1516

1617
// handleEventFailure handles failures and reconnects bridges.
@@ -177,7 +178,7 @@ func (gw *Gateway) handleMessage(rmsg *config.Message, dest *bridge.Bridge) []*B
177178
}
178179

179180
// Get the ID of the parent message in thread
180-
var canonicalParentMsgID string
181+
var canonicalParentMsgID *xid.ID
181182
if rmsg.ParentID != "" && dest.GetBool("PreserveThreading") {
182183
canonicalParentMsgID = gw.FindCanonicalMsgID(rmsg.Protocol, rmsg.ParentID)
183184
}

gateway/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (r *Router) handleReceive() {
171171
BrMsgIDs = append(BrMsgIDs, &BrMsgID{msgBridge, msg.ID, msg.Channel})
172172
}
173173
// Even if it might be empty, already initialize the mapping
174-
gw.Messages.Add(msg.Protocol+" "+msg.InternalID.String(), BrMsgIDs)
174+
gw.Messages.Add(msg.InternalID, BrMsgIDs)
175175

176176
for _, br := range gw.Bridges {
177177
gw.handleMessage(&msg, br)

0 commit comments

Comments
 (0)