Skip to content

Commit

Permalink
improve ProcessText logic for invitations module
Browse files Browse the repository at this point in the history
remove l,o, and 0 from invitation codes character set
  • Loading branch information
bugfloyd committed May 9, 2024
1 parent b4e98f7 commit 8781493
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
28 changes: 28 additions & 0 deletions bot/common/invitations/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
}
29 changes: 8 additions & 21 deletions bot/common/invitations/root_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion bot/common/invitations/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 6 additions & 12 deletions bot/common/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 8781493

Please sign in to comment.