🐛 Remove user when they leave from database and implement a already exists check for verification

This commit is contained in:
Alphyron 2020-06-13 20:26:15 +02:00
parent 52bb59bc21
commit 33d68aaa14
7 changed files with 50 additions and 17 deletions

View File

@ -7,14 +7,10 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"log" "log"
"strconv" "strconv"
"sync"
"time" "time"
) )
func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI, data *obj.VerifyData) { func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI, data *obj.VerifyData) {
var wg sync.WaitGroup
wg.Add(len(data.Users))
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
@ -25,7 +21,6 @@ func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, group
users := make(map[int]*models.User, 0) users := make(map[int]*models.User, 0)
for _, user := range data.Users { for _, user := range data.Users {
defer wg.Done()
user.LeftTime -= 1 user.LeftTime -= 1
if user.LeftTime <= 0 { if user.LeftTime <= 0 {
@ -128,7 +123,4 @@ func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, group
return return
} }
} }
wg.Wait()
} }

View File

@ -82,7 +82,7 @@ func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandMana
rm.RegisterRoutine(routines.NewCommandRoutine(commandManager)) rm.RegisterRoutine(routines.NewCommandRoutine(commandManager))
rm.RegisterRoutine(routines.NewDatabaseRoutine(service)) rm.RegisterRoutine(routines.NewDatabaseRoutine(service))
rm.RegisterRoutine(routines.NewJoinCheckerRoutine(userData)) rm.RegisterRoutine(routines.NewJoinCheckerRoutine(userData))
rm.RegisterRoutine(routines.NewVerifierRoutine(service)) rm.RegisterRoutine(routines.NewVerifierRoutine(service, userData))
return rm return rm
} }

33
obj/verifydatas.go Normal file
View File

@ -0,0 +1,33 @@
package obj
type VerifyData struct {
Users []*VerifyUser
}
type VerifyUser struct {
UserID int
GroupID int64
LeftTime int
MessageID int
}
func (vd *VerifyData) AddUser(user *VerifyUser) {
vd.Users = append(vd.Users, user)
}
func (vd VerifyData) ExistCountdownForUserInGroup(userID int, groupID int64) bool {
for _, user := range vd.Users {
if user.UserID == userID && user.GroupID == groupID {
return true
}
}
return false
}
func (vd *VerifyData) RemoveUser(userID int, groupID int64) {
for i, user := range vd.Users {
if user.UserID == userID && user.GroupID == groupID {
vd.Users = append(vd.Users[:i], vd.Users[i+1:]...)
}
}
}

View File

@ -149,7 +149,7 @@ func (g groupHelperRepository) UserJoinGroup(user *models.User, group *models.Gr
} }
func (g groupHelperRepository) UserLeaveGroup(user *models.User, group *models.Group) (bool, error) { func (g groupHelperRepository) UserLeaveGroup(user *models.User, group *models.Group) (bool, error) {
err := g.Conn.Model(user).Association("Groups").Append(group).Error err := g.Conn.Model(user).Association("Groups").Delete(group).Error
if err != nil { if err != nil {
return false, errors.New("problem while disconnecting the user from the group - " + err.Error()) return false, errors.New("problem while disconnecting the user from the group - " + err.Error())

View File

@ -5,6 +5,7 @@ import (
"git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/telegram" "git.dragon-labs.de/alphyron/group_helper/telegram"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"log"
) )
type databaseRoutine struct { type databaseRoutine struct {
@ -51,7 +52,12 @@ func (d databaseRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update
return err return err
} }
go d.groupHelperService.UserLeaveGroup(user, group) go func() {
success, err := d.groupHelperService.UserLeaveGroup(user, group)
log.Println(success)
log.Println(err)
}()
config := tgbotapi.ChatConfig{ config := tgbotapi.ChatConfig{
ChatID: update.Message.Chat.ID, ChatID: update.Message.Chat.ID,
} }

View File

@ -34,7 +34,7 @@ func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Upd
if update.Message.NewChatMembers != nil { if update.Message.NewChatMembers != nil {
for _, newUser := range *update.Message.NewChatMembers { for _, newUser := range *update.Message.NewChatMembers {
length := 50 length := 3
tasks := make([][]tgbotapi.InlineKeyboardButton, length) tasks := make([][]tgbotapi.InlineKeyboardButton, length)

View File

@ -3,6 +3,7 @@ package routines
import ( import (
"git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/logic"
"git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/obj"
"git.dragon-labs.de/alphyron/group_helper/telegram" "git.dragon-labs.de/alphyron/group_helper/telegram"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strconv" "strconv"
@ -11,11 +12,13 @@ import (
type verifierRoutine struct { type verifierRoutine struct {
groupHelperService logic.GroupHelperService groupHelperService logic.GroupHelperService
verifyData *obj.VerifyData
} }
func NewVerifierRoutine(groupHelperService logic.GroupHelperService) telegram.Routine { func NewVerifierRoutine(groupHelperService logic.GroupHelperService, verifyData *obj.VerifyData) telegram.Routine {
return verifierRoutine{ return verifierRoutine{
groupHelperService: groupHelperService, groupHelperService: groupHelperService,
verifyData: verifyData,
} }
} }
@ -36,11 +39,10 @@ func (v verifierRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update
if success { if success {
//TODO check if the user is really in countdown handler if !v.verifyData.ExistCountdownForUserInGroup(update.CallbackQuery.From.ID, group.GroupID) {
/*if isInGroup {
_, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are already verified!")) _, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are already verified!"))
return err return err
}*/ }
// Delete after accept the Message // Delete after accept the Message
botAPI.DeleteMessage(tgbotapi.DeleteMessageConfig{ botAPI.DeleteMessage(tgbotapi.DeleteMessageConfig{
@ -56,7 +58,7 @@ func (v verifierRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
_, err := botAPI.Send(msg) _, err := botAPI.Send(msg)
//TODO Remove the user from the validator so dont get banned v.verifyData.RemoveUser(update.CallbackQuery.From.ID, group.GroupID)
return err return err
} else { } else {
_, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "Wrong answer")) _, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "Wrong answer"))