diff --git a/bot/common/invitations/main.go b/bot/common/invitations/main.go index 16c00c7..27edb49 100644 --- a/bot/common/invitations/main.go +++ b/bot/common/invitations/main.go @@ -1,6 +1,7 @@ package invitations import ( + "github.com/PaulSonOfLars/gotgbot/v2" "github.com/PaulSonOfLars/gotgbot/v2/ext" "github.com/PaulSonOfLars/gotgbot/v2/ext/handlers" "github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters/callbackquery" @@ -40,9 +41,36 @@ const ( func InitInvitations(dispatcher *ext.Dispatcher) { rootHandler := NewRootHandler() + // Commands dispatcher.AddHandler(handlers.NewCommand(string(InviteCommand), rootHandler.init(InviteCommand))) dispatcher.AddHandler(handlers.NewCommand(string(RegisterCommand), rootHandler.init(RegisterCommand))) + + // Callbacks dispatcher.AddHandler(handlers.NewCallback(callbackquery.Prefix("inv|g"), rootHandler.init(GenerateInvitationCallback))) dispatcher.AddHandler(handlers.NewCallback(callbackquery.Prefix("inv|reg|c"), rootHandler.init(CancelSendingInvitationCodeCallback))) +} + +func ProcessText(b *gotgbot.Bot, ctx *ext.Context) (bool, error) { + rh := NewRootHandler() + err := rh.HandleUserAndRepos(ctx) + if err != nil { + return false, err + } + switch rh.user.State { + case GeneratingInvitationState: + err = rh.GenerateInvitation(b, ctx) + if err != nil { + return true, err + } + return true, nil + case SendingInvitationCodeState: + err = rh.ValidateCode(b, ctx) + if err != nil { + return true, err + } + return true, nil + default: + return false, nil + } } diff --git a/bot/common/invitations/root_handler.go b/bot/common/invitations/root_handler.go index f4c6faa..a1285d4 100644 --- a/bot/common/invitations/root_handler.go +++ b/bot/common/invitations/root_handler.go @@ -36,11 +36,15 @@ func (r *RootHandler) HandleUserAndRepos(ctx *ext.Context) error { if err != nil { return fmt.Errorf("failed to init db repo: %w", err) } - user, err := r.processUser(userRepo, ctx) - if err != nil || user == nil { - return fmt.Errorf("failed to process user: %w", err) + user, err := userRepo.ReadUserByUserId(ctx.EffectiveUser.Id) + if err != nil { + user, err = userRepo.CreateUser(ctx.EffectiveUser.Id) + if err != nil || user == nil { + return fmt.Errorf("failed to process user: %w", err) + } } + r.user = user r.userRepo = *userRepo @@ -66,8 +70,6 @@ func (r *RootHandler) runCommand(b *gotgbot.Bot, ctx *ext.Context, command inter return r.inviteCommandHandler(b, ctx) case RegisterCommand: return r.registerCommandHandler(b, ctx) - default: - return fmt.Errorf("unknown command: %s", c) } case CallbackCommand: // Reset user state if necessary @@ -83,24 +85,9 @@ func (r *RootHandler) runCommand(b *gotgbot.Bot, ctx *ext.Context, command inter return r.manageInvitation(b, ctx, "GENERATE") case CancelSendingInvitationCodeCallback: return r.invitationCodeCallback(b, ctx, "CANCEL") - default: - return fmt.Errorf("unknown command: %s", c) } - default: - return fmt.Errorf("unknown command: %s", command) } -} - -func (r *RootHandler) processUser(userRepo *users.UserRepository, ctx *ext.Context) (*users.User, error) { - user, err := userRepo.ReadUserByUserId(ctx.EffectiveUser.Id) - if err != nil { - user, err = userRepo.CreateUser(ctx.EffectiveUser.Id) - if err != nil { - return nil, err - } - } - - return user, nil + return nil } func (r *RootHandler) inviteCommandHandler(b *gotgbot.Bot, ctx *ext.Context) error { diff --git a/bot/common/invitations/utils.go b/bot/common/invitations/utils.go index bb68024..3e629b5 100644 --- a/bot/common/invitations/utils.go +++ b/bot/common/invitations/utils.go @@ -11,7 +11,7 @@ import ( // generateRandomString generates a random string of a specified length from a predefined charset. func generateRandomString(length int) (string, error) { - const charset = "abcdefghijklmnopqrstuvwxyz0123456789" + const charset = "abcdefghijkmnpqrstuvwxyz123456789" result := make([]byte, length) for i := range result { diff --git a/bot/common/misc.go b/bot/common/misc.go index 84f859a..af2fd33 100644 --- a/bot/common/misc.go +++ b/bot/common/misc.go @@ -182,22 +182,16 @@ func (r *RootHandler) processText(b *gotgbot.Bot, ctx *ext.Context) error { return r.sendAnonymousMessage(b, ctx) case users.SettingUsername: return r.setUsername(b, ctx) - case invitations.GeneratingInvitationState: - irh := invitations.NewRootHandler() - err := irh.HandleUserAndRepos(ctx) + default: + processed, err := invitations.ProcessText(b, ctx) if err != nil { return err } - return irh.GenerateInvitation(b, ctx) - case invitations.SendingInvitationCodeState: - irh := invitations.NewRootHandler() - err := irh.HandleUserAndRepos(ctx) - if err != nil { - return err + if processed == false { + return r.sendError(b, ctx, i18n.T(i18n.InvalidCommandText)) + } else { + return nil } - return irh.ValidateCode(b, ctx) - default: - return r.sendError(b, ctx, i18n.T(i18n.InvalidCommandText)) } }