@@ -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"
4546func 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
0 commit comments