simply and hopefully fix channel leave/join
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-07-20 20:40:55 +02:00
parent b249a495a4
commit de04d6069b
+25 -34
View File
@@ -6,9 +6,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/gissleh/irc"
"git.aiterp.net/rpdata/logbot3/internal/models"
"git.aiterp.net/rpdata/logbot3/internal/models/channels" "git.aiterp.net/rpdata/logbot3/internal/models/channels"
"github.com/gissleh/irc"
) )
var botKey = "git.aiterp.net/rpdata/logbot.Bot.key" var botKey = "git.aiterp.net/rpdata/logbot.Bot.key"
@@ -118,45 +117,37 @@ func (bot *Bot) runCommands(commands []string, target irc.Target, replacers map[
func (bot *Bot) loop() { func (bot *Bot) loop() {
bot.loopCtx, bot.loopCancel = context.WithCancel(bot.ctx) bot.loopCtx, bot.loopCancel = context.WithCancel(bot.ctx)
channelChanges, err := channels.SubscribeLogged(bot.ctx) ticker := time.NewTicker(time.Second * 10)
if err != nil { defer ticker.Stop()
log.Println("Failed to get channel changes:", err)
return
}
for { for {
select { select {
case channel := <-channelChanges: case <-ticker.C:
{ {
channels := []models.Channel{channel} loggedChannels, err := channels.ListLogged(bot.ctx)
deadline := time.After(time.Second * 1) if err != nil {
buffering := true log.Println("Failed to list logged channels:", err)
for buffering { continue
select { }
case channel := <-channelChanges: loggedMap := make(map[string]bool)
channels = append(channels, channel) for _, channel := range loggedChannels {
case <-deadline: loggedMap[channel.Name] = true
buffering = false }
} clientMap := make(map[string]bool)
for _, channel := range bot.client.Channels() {
clientMap[channel.Name()] = true
} }
decisions := make(map[string]bool) joins := make([]string, 0, len(loggedMap))
for _, channel := range channels { parts := make([]string, 0, len(clientMap))
decisions[channel.Name] = channel.Logged for key := range loggedMap {
if !clientMap[key] {
joins = append(joins, key)
}
} }
joins := make([]string, 0, len(decisions)) for key := range clientMap {
parts := make([]string, 0, len(decisions)) if !loggedMap[key] {
for channelName, logged := range decisions { parts = append(parts, key)
if logged {
if bot.client.Channel(channelName) != nil {
continue
}
joins = append(joins, channelName)
} else {
if bot.client.Channel(channelName) == nil {
continue
}
parts = append(parts, channelName)
} }
} }