From 33d68aaa142441fd707500b108b385eaeeed85c6 Mon Sep 17 00:00:00 2001 From: Alphyron Date: Sat, 13 Jun 2020 20:26:15 +0200 Subject: [PATCH] :bug: Remove user when they leave from database and implement a already exists check for verification --- channels/verifiychecker.go | 8 ------- main.go | 2 +- obj/verifydatas.go | 33 +++++++++++++++++++++++++++ repository/sql/grouphelprepository.go | 2 +- telegram/routines/database.go | 8 ++++++- telegram/routines/joinchecker.go | 2 +- telegram/routines/verifier.go | 12 ++++++---- 7 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 obj/verifydatas.go diff --git a/channels/verifiychecker.go b/channels/verifiychecker.go index eee1cf7..0e12394 100644 --- a/channels/verifiychecker.go +++ b/channels/verifiychecker.go @@ -7,14 +7,10 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "log" "strconv" - "sync" "time" ) 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 { select { case <-ticker.C: @@ -25,7 +21,6 @@ func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, group users := make(map[int]*models.User, 0) for _, user := range data.Users { - defer wg.Done() user.LeftTime -= 1 if user.LeftTime <= 0 { @@ -128,7 +123,4 @@ func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, group return } } - - wg.Wait() - } diff --git a/main.go b/main.go index fcf6fe9..11ccb92 100644 --- a/main.go +++ b/main.go @@ -82,7 +82,7 @@ func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandMana rm.RegisterRoutine(routines.NewCommandRoutine(commandManager)) rm.RegisterRoutine(routines.NewDatabaseRoutine(service)) rm.RegisterRoutine(routines.NewJoinCheckerRoutine(userData)) - rm.RegisterRoutine(routines.NewVerifierRoutine(service)) + rm.RegisterRoutine(routines.NewVerifierRoutine(service, userData)) return rm } diff --git a/obj/verifydatas.go b/obj/verifydatas.go new file mode 100644 index 0000000..c227538 --- /dev/null +++ b/obj/verifydatas.go @@ -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:]...) + } + } +} diff --git a/repository/sql/grouphelprepository.go b/repository/sql/grouphelprepository.go index ded1d94..65d1581 100644 --- a/repository/sql/grouphelprepository.go +++ b/repository/sql/grouphelprepository.go @@ -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) { - err := g.Conn.Model(user).Association("Groups").Append(group).Error + err := g.Conn.Model(user).Association("Groups").Delete(group).Error if err != nil { return false, errors.New("problem while disconnecting the user from the group - " + err.Error()) diff --git a/telegram/routines/database.go b/telegram/routines/database.go index 6dca5f6..9a7865a 100644 --- a/telegram/routines/database.go +++ b/telegram/routines/database.go @@ -5,6 +5,7 @@ import ( "git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/telegram" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "log" ) type databaseRoutine struct { @@ -51,7 +52,12 @@ func (d databaseRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update 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{ ChatID: update.Message.Chat.ID, } diff --git a/telegram/routines/joinchecker.go b/telegram/routines/joinchecker.go index 5f6a8de..3151f16 100644 --- a/telegram/routines/joinchecker.go +++ b/telegram/routines/joinchecker.go @@ -34,7 +34,7 @@ func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Upd if update.Message.NewChatMembers != nil { for _, newUser := range *update.Message.NewChatMembers { - length := 50 + length := 3 tasks := make([][]tgbotapi.InlineKeyboardButton, length) diff --git a/telegram/routines/verifier.go b/telegram/routines/verifier.go index 846bd7f..6d94cec 100644 --- a/telegram/routines/verifier.go +++ b/telegram/routines/verifier.go @@ -3,6 +3,7 @@ package routines import ( "git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/models" + "git.dragon-labs.de/alphyron/group_helper/obj" "git.dragon-labs.de/alphyron/group_helper/telegram" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "strconv" @@ -11,11 +12,13 @@ import ( type verifierRoutine struct { 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{ groupHelperService: groupHelperService, + verifyData: verifyData, } } @@ -36,11 +39,10 @@ func (v verifierRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update if success { - //TODO check if the user is really in countdown handler - /*if isInGroup { + if !v.verifyData.ExistCountdownForUserInGroup(update.CallbackQuery.From.ID, group.GroupID) { _, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are already verified!")) return err - }*/ + } // Delete after accept the Message botAPI.DeleteMessage(tgbotapi.DeleteMessageConfig{ @@ -56,7 +58,7 @@ func (v verifierRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update msg.ParseMode = "Markdown" _, 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 } else { _, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "Wrong answer"))