98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
|
package channels
|
||
|
|
||
|
import (
|
||
|
"git.dragon-labs.de/alphyron/group_helper/logic"
|
||
|
"git.dragon-labs.de/alphyron/group_helper/models"
|
||
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
||
|
"log"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func CheckForLeavingUser(ticker *time.Ticker, quitChannel <-chan struct{}, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI) {
|
||
|
for {
|
||
|
select {
|
||
|
case <-ticker.C:
|
||
|
|
||
|
groups, err := groupHelperService.ListGroups()
|
||
|
|
||
|
if err != nil {
|
||
|
log.Println("Error while getting all groups in the LeavingChecking routine")
|
||
|
log.Println(err)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
for _, group := range groups {
|
||
|
time.Sleep(500 * time.Millisecond)
|
||
|
config := tgbotapi.ChatConfig{
|
||
|
ChatID: group.GroupID,
|
||
|
}
|
||
|
|
||
|
teleSize, _ := bot.GetChatMembersCount(config)
|
||
|
teleSize -= 1
|
||
|
sqlSize, err := groupHelperService.GetGroupDatabaseSize(group)
|
||
|
|
||
|
if err != nil {
|
||
|
log.Println("Error while counting the user in group")
|
||
|
log.Println(err)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if teleSize == sqlSize {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if sqlSize < teleSize {
|
||
|
group.Size = teleSize
|
||
|
groupHelperService.UpdateGroup(group)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if teleSize < sqlSize {
|
||
|
checkGroupUser(group, groupHelperService, bot)
|
||
|
groupHelperService.UpdateGroup(group)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
case <-quitChannel:
|
||
|
ticker.Stop()
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func checkGroupUser(group *models.Group, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI) {
|
||
|
rate := time.Second / 5
|
||
|
throttle := time.Tick(rate)
|
||
|
|
||
|
users, err := groupHelperService.ListGroupUsers(group)
|
||
|
|
||
|
if err != nil {
|
||
|
log.Println("Error while getting the users for group while checking for leaved user")
|
||
|
log.Println(err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
for _, user := range users {
|
||
|
<-throttle // rate limit our Service.Method RPCs
|
||
|
|
||
|
userConfig := tgbotapi.ChatConfigWithUser{
|
||
|
ChatID: group.GroupID,
|
||
|
UserID: int(user.UserID),
|
||
|
}
|
||
|
|
||
|
member, _ := bot.GetChatMember(userConfig)
|
||
|
|
||
|
if !member.HasLeft() {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
leaveMessage := group.UserLeaveMessage
|
||
|
//TODO Replace placeholder
|
||
|
|
||
|
msg := tgbotapi.NewMessage(group.GroupID, leaveMessage)
|
||
|
msg.ParseMode = "Markdown"
|
||
|
bot.Send(msg)
|
||
|
groupHelperService.UserLeaveGroup(user, group)
|
||
|
}
|
||
|
}
|