2020-05-09 20:44:28 +00:00
|
|
|
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/telegram"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
2021-04-02 09:16:22 +00:00
|
|
|
"log"
|
2020-05-09 20:44:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type databaseRoutine struct {
|
|
|
|
groupHelperService logic.GroupHelperService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatabaseRoutine(groupHelperService logic.GroupHelperService) telegram.Routine {
|
|
|
|
return &databaseRoutine{groupHelperService: groupHelperService}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d databaseRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) error {
|
|
|
|
|
|
|
|
if update.Message == nil { // ignore any non-Message Updates
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if update.Message.Chat.IsPrivate() || update.Message.Chat.IsChannel() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if update.Message.NewChatMembers != nil {
|
|
|
|
for _, newUser := range *update.Message.NewChatMembers {
|
|
|
|
user, err := d.groupHelperService.GetUserByID(int64(newUser.ID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go d.groupHelperService.UserJoinGroup(user, group)
|
|
|
|
}
|
|
|
|
config := tgbotapi.ChatConfig{
|
|
|
|
ChatID: update.Message.Chat.ID,
|
|
|
|
}
|
|
|
|
size, _ := botAPI.GetChatMembersCount(config)
|
|
|
|
group.Size = size - 1
|
|
|
|
_, err := d.groupHelperService.UpdateGroup(group)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-02 09:16:22 +00:00
|
|
|
if update.Message.LeftChatMember != nil {
|
|
|
|
user, err := d.groupHelperService.GetUserByID(int64(update.Message.LeftChatMember.ID))
|
2020-06-13 18:26:15 +00:00
|
|
|
|
2021-04-02 09:16:22 +00:00
|
|
|
if err != nil {
|
2021-04-01 20:14:25 +00:00
|
|
|
return err
|
2020-05-09 20:44:28 +00:00
|
|
|
}
|
2021-04-02 09:16:22 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-09 20:44:28 +00:00
|
|
|
user, err := d.groupHelperService.GetUserByID(int64(update.Message.From.ID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.groupHelperService.UserJoinGroup(user, group)
|
|
|
|
return err
|
|
|
|
}
|