diff --git a/Dockerfile b/Dockerfile index ffd1030..dbfbeda 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,9 @@ FROM golang:1.13-stretch AS build-env COPY . /src WORKDIR /src + +ENV GOPROXY=http://192.168.0.143:3000 + ENV GO113MODULE=on RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go test ./... -cover -coverprofile=c.out #gosetup RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o goapp diff --git a/logic/grouphelperservice.go b/logic/grouphelperservice.go index 535b70c..f13028b 100644 --- a/logic/grouphelperservice.go +++ b/logic/grouphelperservice.go @@ -55,7 +55,8 @@ func (g groupHelperService) CreateUser(user *models.User) (*models.User, error) } func (g groupHelperService) GetUserByID(userID int64) (*models.User, error) { - return g.groupHelperRepo.GetUserByID(userID) + user, err := g.groupHelperRepo.GetUserByID(userID) + return user, err } func (g groupHelperService) UserJoinGroup(user *models.User, group *models.Group) (bool, error) { diff --git a/main.go b/main.go index 19832ec..db193a1 100644 --- a/main.go +++ b/main.go @@ -56,17 +56,19 @@ func main() { service := logic.NewGroupHelperService(groupHelperRepo) cm := InitialCommandManager(bot, service) - rm := InitialRoutineManager(bot, cm) + rm := InitialRoutineManager(bot, cm, service) rm.StartUpdates() } -func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandManager) *telegram.RoutineManager { +func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandManager, service logic.GroupHelperService) *telegram.RoutineManager { rm := &telegram.RoutineManager{ - Bot: bot, + Bot: bot, + GroupHelperService: service, } rm.RegisterRoutine(routines.NewCommandRoutine(commandManager)) + rm.RegisterRoutine(routines.NewDatabaseRoutine(service)) return rm } diff --git a/repository/sql/grouphelprepository.go b/repository/sql/grouphelprepository.go index ee59858..ded1d94 100644 --- a/repository/sql/grouphelprepository.go +++ b/repository/sql/grouphelprepository.go @@ -124,15 +124,22 @@ func (g groupHelperRepository) CreateUser(user *models.User) (*models.User, erro func (g groupHelperRepository) GetUserByID(userID int64) (*models.User, error) { var user models.User - err := g.Conn.Where("user_id = ?", userID).First(&user).Error + db := g.Conn.Where("user_id = ?", userID).First(&user) - if err != nil { - return nil, errors.New("problem while getting the user by his user id - " + err.Error()) + if db.RecordNotFound() { + return g.CreateUser(&models.User{ + UserID: userID, + }) + } + + if db.Error != nil { + return nil, errors.New("problem while getting the user by his user id - " + db.Error.Error()) } return &user, nil } func (g groupHelperRepository) UserJoinGroup(user *models.User, group *models.Group) (bool, error) { + err := g.Conn.Model(user).Association("Groups").Append(group).Error if err != nil { diff --git a/telegram/manager.go b/telegram/manager.go index 72d8c92..b738bfd 100644 --- a/telegram/manager.go +++ b/telegram/manager.go @@ -27,7 +27,7 @@ func (rm RoutineManager) StartUpdates() { } for _, routine := range rm.Routine { - err := routine.Update(&update, group) + err := routine.Update(rm.Bot, &update, group) if err != nil { log.Printf("ERROR - Routine error") diff --git a/telegram/routine.go b/telegram/routine.go index f10f015..c481060 100644 --- a/telegram/routine.go +++ b/telegram/routine.go @@ -6,5 +6,5 @@ import ( ) type Routine interface { - Update(update *tgbotapi.Update, group *models.Group) error + Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) error } diff --git a/telegram/routines/command.go b/telegram/routines/command.go index e5f4ba3..fe3b0c3 100644 --- a/telegram/routines/command.go +++ b/telegram/routines/command.go @@ -15,7 +15,7 @@ func NewCommandRoutine(commandManager *cli.CommandManager) telegram.Routine { return &commandRoutine{CommandManager: commandManager} } -func (cr commandRoutine) Update(update *tgbotapi.Update, group *models.Group) error { +func (cr commandRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) error { _, err := cr.CommandManager.ExecuteUpdate(update, group) return err diff --git a/telegram/routines/database.go b/telegram/routines/database.go new file mode 100644 index 0000000..6dca5f6 --- /dev/null +++ b/telegram/routines/database.go @@ -0,0 +1,72 @@ +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" +) + +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 + } + + if update.Message.LeftChatMember != nil { + user, err := d.groupHelperService.GetUserByID(int64(update.Message.LeftChatMember.ID)) + + if err != nil { + return err + } + + go d.groupHelperService.UserLeaveGroup(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 + } + + user, err := d.groupHelperService.GetUserByID(int64(update.Message.From.ID)) + + if err != nil { + return err + } + + _, err = d.groupHelperService.UserJoinGroup(user, group) + return err +}