From fb713ed91bfb48c0037e489f80be85c54e69953a Mon Sep 17 00:00:00 2001 From: Wim Date: Fri, 18 Jan 2019 18:35:31 +0100 Subject: Add initial support for getting ChannelMember info of all bridges (#678) * Add initial support for getting ChannelMember info of all bridges. Adds an EventGetChannelMembers event, which gets send every x time to all bridges. Bridges should respond on this event with a Message containing ChannelMembers in the EventGetChannelMembers key in the Extra field. handleEventGetChannelMembers will handle this Message and sets the contained ChannelMembers to the Bridge struct. * Add ChannelMembers support to the slack bridge --- gateway/handlers.go | 17 +++++++++++++++++ gateway/router.go | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) (limited to 'gateway') diff --git a/gateway/handlers.go b/gateway/handlers.go index 741c312e..5af13c14 100644 --- a/gateway/handlers.go +++ b/gateway/handlers.go @@ -30,6 +30,23 @@ func (r *Router) handleEventFailure(msg *config.Message) { } } +// handleEventGetChannelMembers handles channel members +func (r *Router) handleEventGetChannelMembers(msg *config.Message) { + if msg.Event != config.EventGetChannelMembers { + return + } + for _, gw := range r.Gateways { + for _, br := range gw.Bridges { + if msg.Account == br.Account { + cMembers := msg.Extra[config.EventGetChannelMembers][0].(config.ChannelMembers) + flog.Debugf("Syncing channelmembers from %s", msg.Account) + br.SetChannelMembers(&cMembers) + return + } + } + } +} + // handleEventRejoinChannels handles rejoining of channels. func (r *Router) handleEventRejoinChannels(msg *config.Message) { if msg.Event != config.EventRejoinChannels { diff --git a/gateway/router.go b/gateway/router.go index a7181b96..d3c33b2a 100644 --- a/gateway/router.go +++ b/gateway/router.go @@ -2,6 +2,7 @@ package gateway import ( "fmt" + "sync" "time" "github.com/42wim/matterbridge/bridge" @@ -16,6 +17,7 @@ type Router struct { Gateways map[string]*Gateway Message chan config.Message MattermostPlugin chan config.Message + sync.RWMutex } func NewRouter(cfg config.Config, bridgeMap map[string]bridge.Factory) (*Router, error) { @@ -81,6 +83,7 @@ func (r *Router) Start() error { } } go r.handleReceive() + go r.updateChannelMembers() return nil } @@ -108,6 +111,7 @@ func (r *Router) getBridge(account string) *bridge.Bridge { func (r *Router) handleReceive() { for msg := range r.Message { msg := msg // scopelint + r.handleEventGetChannelMembers(&msg) r.handleEventFailure(&msg) r.handleEventRejoinChannels(&msg) for _, gw := range r.Gateways { @@ -129,3 +133,21 @@ func (r *Router) handleReceive() { } } } + +// updateChannelMembers sends every minute an GetChannelMembers event to all bridges. +func (r *Router) updateChannelMembers() { + // TODO sleep a minute because slack can take a while + // fix this by having actually connectionDone events send to the router + time.Sleep(time.Minute) + for { + for _, gw := range r.Gateways { + for _, br := range gw.Bridges { + flog.Debugf("sending %s to %s", config.EventGetChannelMembers, br.Account) + if _, err := br.Send(config.Message{Event: config.EventGetChannelMembers}); err != nil { + flog.Errorf("updateChannelMembers: %s", err) + } + } + } + time.Sleep(time.Minute) + } +} -- cgit v1.2.3