Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move antiIdle to matterclient package. Fixes #327 #329

Merged
merged 1 commit into from Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 6 additions & 11 deletions bridge/mattermost/mattermost.go
Expand Up @@ -51,6 +51,10 @@ func New(v *viper.Viper, cred bridge.Credentials, eventChan chan *bridge.Event,
mc.SetLogLevel("debug")
}

if v.GetBool("trace") {
mc.SetLogLevel("trace")
}

m.mc = mc
m.connected = true

Expand All @@ -63,6 +67,8 @@ func (m *Mattermost) loginToMattermost(onWsConnect func()) (*matterclient.Client
mc.Credentials.NoTLS = true
}

// do anti idle on town-square, every installation should have this channel
mc.AntiIdle = !m.v.GetBool("mattermost.DisableAutoView")
mc.OnWsConnect = onWsConnect

if m.v.GetBool("debug") {
Expand Down Expand Up @@ -95,17 +101,6 @@ func (m *Mattermost) loginToMattermost(onWsConnect func()) (*matterclient.Client

go m.handleWsMessage(quitChan)

// do anti idle on town-square, every installation should have this channel
channels := m.mc.GetChannels()
for _, channel := range channels {
if channel.Name == "town-square" && !m.v.GetBool("mattermost.DisableAutoView") {
idleChan := make(chan struct{})
m.quitChan = append(m.quitChan, idleChan)
go m.antiIdle(channel.Id, idleChan)
continue
}
}

return mc, nil
}

Expand Down
31 changes: 31 additions & 0 deletions pkg/matterclient/matterclient.go
Expand Up @@ -63,6 +63,7 @@ type Client struct {
Users map[string]*model.User
MessageChan chan *Message
WsClient *model.WebSocketClient
AntiIdle bool
WsQuit bool
WsConnected bool
OnWsConnect func()
Expand Down Expand Up @@ -161,6 +162,17 @@ func (m *Client) Login() error {

go m.checkConnection(ctx)

if m.AntiIdle {
channels := m.GetChannels()
for _, channel := range channels {
if channel.Name == "town-square" {
go m.antiIdle(ctx, channel.Id)

continue
}
}
}

return nil
}

Expand Down Expand Up @@ -685,3 +697,22 @@ func (m *Client) HandleRatelimit(name string, resp *model.Response) error {

return nil
}

func (m *Client) antiIdle(ctx context.Context, channelID string) {
m.logger.Debugf("starting antiIdle for %s", channelID)

ticker := time.NewTicker(time.Second * 60)

for {
select {
case <-ctx.Done():
m.logger.Debugf("antiIlde: ctx.Done() triggered, exiting for %s", channelID)

return
case <-ticker.C:
m.logger.Tracef("antiIdle %s", channelID)

m.UpdateLastViewed(channelID)
}
}
}