diff --git a/channels/leaveduserchecker.go b/channels/leaveduserchecker.go index d4252cd..3975ac4 100644 --- a/channels/leaveduserchecker.go +++ b/channels/leaveduserchecker.go @@ -24,31 +24,25 @@ func CheckForLeavingUser(ticker *time.Ticker, quitChannel <-chan struct{}, group 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") + if teleSize == group.Size { + continue + } + + if group.Size < teleSize { + group.Size = teleSize + _, err = groupHelperService.UpdateGroup(group) log.Println(err) continue } - if teleSize == sqlSize { - continue - } - - if sqlSize < teleSize { - group.Size = teleSize - groupHelperService.UpdateGroup(group) - continue - } - - if teleSize < sqlSize { + if teleSize < group.Size { checkGroupUser(group, groupHelperService, bot) groupHelperService.UpdateGroup(group) } @@ -67,12 +61,17 @@ func checkGroupUser(group *models.Group, groupHelperService logic.GroupHelperSer users, err := groupHelperService.ListGroupUsers(group) + log.Println("users") + if err != nil { log.Println("Error while getting the users for group while checking for leaved user") log.Println(err) return } + log.Println("no err") + log.Println(len(users)) + for _, user := range users { <-throttle // rate limit our Service.Method RPCs @@ -81,18 +80,33 @@ func checkGroupUser(group *models.Group, groupHelperService logic.GroupHelperSer UserID: int(user.UserID), } + log.Println(user.UserID) + member, _ := bot.GetChatMember(userConfig) + log.Println(member.User.FirstName) + log.Println(member.HasLeft()) + if !member.HasLeft() { continue } + log.Println("User left") + leaveMessage := group.UserLeaveMessage leaveMessage = message.ReplaceMessage(leaveMessage, member.User) msg := tgbotapi.NewMessage(group.GroupID, leaveMessage) msg.ParseMode = "Markdown" bot.Send(msg) - groupHelperService.UserLeaveGroup(user, group) + _, err = groupHelperService.UserLeaveGroup(user, group) + if err != nil { + log.Println(err) + } + group.Size = group.Size - 1 + groupHelperService.UpdateGroup(group) + if err != nil { + log.Println(err) + } } } diff --git a/repository/sql/grouphelprepository.go b/repository/sql/grouphelprepository.go index 0efdc6c..5fa4c9e 100644 --- a/repository/sql/grouphelprepository.go +++ b/repository/sql/grouphelprepository.go @@ -11,6 +11,7 @@ import ( _ "github.com/jinzhu/gorm/dialects/mysql" _ "github.com/jinzhu/gorm/dialects/postgres" _ "github.com/jinzhu/gorm/dialects/sqlite" + "log" ) type groupHelperRepository struct { @@ -83,7 +84,17 @@ func (g groupHelperRepository) CreateGroup(group *models.Group) (*models.Group, } func (g groupHelperRepository) UpdateGroup(group *models.Group) (*models.Group, error) { - err := g.Conn.Where("id = ?", group.ID).Save(group).Error + err := g.Conn.Model(&models.Group{}).Where("id = ?", group.ID).Update(models.Group{ + Size: group.Size, + UserJoinMessage: group.UserJoinMessage, + UserVerifiedMessage: group.UserVerifiedMessage, + UserLeaveMessage: group.UserLeaveMessage, + UserKickMessage: group.UserKickMessage, + ForbidWriting: group.ForbidWriting, + OnlineCheck: group.OnlineCheck, + KickCooldown: group.KickCooldown, + AdminGroupID: group.AdminGroupID, + }).Error return group, err } @@ -149,7 +160,8 @@ 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").Delete(group).Error + log.Println("DELETE") + err := g.Conn.Exec("DELETE FROM `GroupUser` WHERE `group_id` = ? AND `user_id` = ?", group.ID, user.ID).Error if err != nil { return false, errors.New("problem while disconnecting the user from the group - " + err.Error()) @@ -184,13 +196,12 @@ func (g groupHelperRepository) ListGroups() ([]*models.Group, error) { } func (g groupHelperRepository) ListGroupUsers(group *models.Group) ([]*models.User, error) { - var users []*models.User - err := g.Conn.Model(group).Related(&users, "GroupUser").Error + err := g.Conn.Model(group).Preload("Users").Find(group).Error if err != nil { return nil, errors.New("problem while getting all users from group - " + err.Error()) } - return users, nil + return group.Users, nil } func (g groupHelperRepository) ListUsers() ([]*models.User, error) { diff --git a/telegram/routines/database.go b/telegram/routines/database.go index 9a7865a..7e7d385 100644 --- a/telegram/routines/database.go +++ b/telegram/routines/database.go @@ -5,7 +5,6 @@ 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 { @@ -44,29 +43,29 @@ func (d databaseRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update _, err := d.groupHelperService.UpdateGroup(group) return err } + /* + if update.Message.LeftChatMember != nil { + user, err := d.groupHelperService.GetUserByID(int64(update.Message.LeftChatMember.ID)) - if update.Message.LeftChatMember != nil { - user, err := d.groupHelperService.GetUserByID(int64(update.Message.LeftChatMember.ID)) + if err != nil { + return err + } - if err != nil { + go func() { + success, err := d.groupHelperService.UserLeaveGroup(user, group) + log.Println(success) + log.Println(err) + + }() + config := tgbotapi.ChatConfig{ + ChatID: update.Message.Chat.ID, + } + size, _ := botAPI.GetChatMembersCount(config) + group.Size = size - 1 + _, err = d.groupHelperService.UpdateGroup(group) return err } - - go func() { - success, err := d.groupHelperService.UserLeaveGroup(user, group) - log.Println(success) - log.Println(err) - - }() - config := tgbotapi.ChatConfig{ - ChatID: update.Message.Chat.ID, - } - size, _ := botAPI.GetChatMembersCount(config) - group.Size = size - 1 - _, err = d.groupHelperService.UpdateGroup(group) - return err - } - + */ user, err := d.groupHelperService.GetUserByID(int64(update.Message.From.ID)) if err != nil {