mirror of
https://github.com/42wim/matterbridge.git
synced 2024-11-15 16:39:26 +01:00
7c773ebae0
Our Message type has an extra ID field which contains the message ID of the specific bridge. The Send() function has been modified to return a msg ID (after the message to that specific bridge has been created). There is a lru cache of 5000 entries (message IDs). All in memory, so editing messages will only work for messages the bot has seen. Currently we go out from the idea that every message ID is unique, so we don't keep the ID separate for each bridge. (we do for each gateway though) If there's a new message from a bridge, we put that message ID in the LRU cache as key and the []*BrMsgID as value (this slice contains the message ID's of each bridge that received the new message) If there's a new message and this message ID already exists in the cache, it must be an updated message. The value from the cache gets checked for each bridge and if there is a message ID for this bridge, the ID will be added to the Message{} sent to that bridge. If the bridge sees that the ID isn't empty, it'll know it has to update the message with that specific ID instead of creating a new message.
113 lines
2.6 KiB
Go
113 lines
2.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/42wim/matterbridge/bridge"
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
"github.com/42wim/matterbridge/gateway/samechannel"
|
|
log "github.com/Sirupsen/logrus"
|
|
// "github.com/davecgh/go-spew/spew"
|
|
"time"
|
|
)
|
|
|
|
type Router struct {
|
|
Gateways map[string]*Gateway
|
|
Message chan config.Message
|
|
*config.Config
|
|
}
|
|
|
|
func NewRouter(cfg *config.Config) (*Router, error) {
|
|
r := &Router{}
|
|
r.Config = cfg
|
|
r.Message = make(chan config.Message)
|
|
r.Gateways = make(map[string]*Gateway)
|
|
sgw := samechannelgateway.New(cfg)
|
|
gwconfigs := sgw.GetConfig()
|
|
|
|
for _, entry := range append(gwconfigs, cfg.Gateway...) {
|
|
if !entry.Enable {
|
|
continue
|
|
}
|
|
if entry.Name == "" {
|
|
return nil, fmt.Errorf("%s", "Gateway without name found")
|
|
}
|
|
if _, ok := r.Gateways[entry.Name]; ok {
|
|
return nil, fmt.Errorf("Gateway with name %s already exists", entry.Name)
|
|
}
|
|
r.Gateways[entry.Name] = New(entry, r)
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
func (r *Router) Start() error {
|
|
m := make(map[string]*bridge.Bridge)
|
|
for _, gw := range r.Gateways {
|
|
for _, br := range gw.Bridges {
|
|
m[br.Account] = br
|
|
}
|
|
}
|
|
for _, br := range m {
|
|
log.Infof("Starting bridge: %s ", br.Account)
|
|
err := br.Connect()
|
|
if err != nil {
|
|
return fmt.Errorf("Bridge %s failed to start: %v", br.Account, err)
|
|
}
|
|
err = br.JoinChannels()
|
|
if err != nil {
|
|
return fmt.Errorf("Bridge %s failed to join channel: %v", br.Account, err)
|
|
}
|
|
}
|
|
go r.handleReceive()
|
|
return nil
|
|
}
|
|
|
|
func (r *Router) getBridge(account string) *bridge.Bridge {
|
|
for _, gw := range r.Gateways {
|
|
if br, ok := gw.Bridges[account]; ok {
|
|
return br
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Router) handleReceive() {
|
|
for msg := range r.Message {
|
|
if msg.Event == config.EVENT_FAILURE {
|
|
Loop:
|
|
for _, gw := range r.Gateways {
|
|
for _, br := range gw.Bridges {
|
|
if msg.Account == br.Account {
|
|
go gw.reconnectBridge(br)
|
|
break Loop
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if msg.Event == config.EVENT_REJOIN_CHANNELS {
|
|
for _, gw := range r.Gateways {
|
|
for _, br := range gw.Bridges {
|
|
if msg.Account == br.Account {
|
|
br.Joined = make(map[string]bool)
|
|
br.JoinChannels()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for _, gw := range r.Gateways {
|
|
// record all the message ID's of the different bridges
|
|
var msgIDs []*BrMsgID
|
|
if !gw.ignoreMessage(&msg) {
|
|
msg.Timestamp = time.Now()
|
|
gw.modifyMessage(&msg)
|
|
for _, br := range gw.Bridges {
|
|
msgIDs = append(msgIDs, gw.handleMessage(msg, br)...)
|
|
}
|
|
// only add the message ID if it doesn't already exists
|
|
if _, ok := gw.Messages.Get(msg.ID); !ok {
|
|
gw.Messages.Add(msg.ID, msgIDs)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|