From c7af286e2b779c15ab2d06ca48f3ff217e6c9399 Mon Sep 17 00:00:00 2001 From: Yashar Hosseinpour Date: Mon, 6 May 2024 23:23:42 +0200 Subject: [PATCH] move user & user repo to users module export functions in user repo: ReadUserByUUID ReadUserByUsername ReadUserByLinkKey UpdateBlacklist --- bot/common/block.go | 9 +++++---- bot/common/language.go | 3 ++- bot/common/messaging.go | 9 +++++---- bot/common/misc.go | 15 ++++++++------- bot/common/root_handler.go | 9 +++++---- bot/common/username.go | 9 +++++---- bot/common/{ => users}/user.go | 2 +- bot/common/{ => users}/user_repo.go | 10 +++++----- 8 files changed, 36 insertions(+), 30 deletions(-) rename bot/common/{ => users}/user.go (98%) rename bot/common/{ => users}/user_repo.go (94%) diff --git a/bot/common/block.go b/bot/common/block.go index 1d0210e..a891a65 100644 --- a/bot/common/block.go +++ b/bot/common/block.go @@ -5,6 +5,7 @@ import ( "github.com/PaulSonOfLars/gotgbot/v2" "github.com/PaulSonOfLars/gotgbot/v2/ext" "github.com/bugfloyd/anonymous-telegram-bot/common/i18n" + "github.com/bugfloyd/anonymous-telegram-bot/common/users" "slices" "strings" ) @@ -18,7 +19,7 @@ func (r *RootHandler) blockCallback(b *gotgbot.Bot, ctx *ext.Context) error { receiverUUID := split[1] replyMessageID := split[2] - err := r.userRepo.updateBlacklist(r.user, "add", receiverUUID) + err := r.userRepo.UpdateBlacklist(r.user, "add", receiverUUID) if err != nil { return fmt.Errorf("failed to block user: %w", err) @@ -60,7 +61,7 @@ func (r *RootHandler) unBlockCallback(b *gotgbot.Bot, ctx *ext.Context) error { receiverUUID := split[1] replyMessageID := split[2] - err := r.userRepo.updateBlacklist(r.user, "delete", receiverUUID) + err := r.userRepo.UpdateBlacklist(r.user, "delete", receiverUUID) if err != nil { return fmt.Errorf("failed to unblock user: %w", err) @@ -108,7 +109,7 @@ func (r *RootHandler) unBlockCallback(b *gotgbot.Bot, ctx *ext.Context) error { } func (r *RootHandler) unBlockAll(b *gotgbot.Bot, ctx *ext.Context) error { - err := r.userRepo.updateBlacklist(r.user, "clear", "") + err := r.userRepo.UpdateBlacklist(r.user, "clear", "") if err != nil { return fmt.Errorf("failed to unblock all users: %w", err) } @@ -121,7 +122,7 @@ func (r *RootHandler) unBlockAll(b *gotgbot.Bot, ctx *ext.Context) error { return nil } -func blockCheck(sender *User, receiver *User) BlockedBy { +func blockCheck(sender *users.User, receiver *users.User) BlockedBy { if slices.Contains(sender.Blacklist, receiver.UUID) { return Sender } else if slices.Contains(receiver.Blacklist, sender.UUID) { diff --git a/bot/common/language.go b/bot/common/language.go index 2833c31..f2721aa 100644 --- a/bot/common/language.go +++ b/bot/common/language.go @@ -5,6 +5,7 @@ import ( "github.com/PaulSonOfLars/gotgbot/v2" "github.com/PaulSonOfLars/gotgbot/v2/ext" "github.com/bugfloyd/anonymous-telegram-bot/common/i18n" + "github.com/bugfloyd/anonymous-telegram-bot/common/users" "strings" ) @@ -80,7 +81,7 @@ func (r *RootHandler) languageCallback(b *gotgbot.Bot, ctx *ext.Context, action } err := r.userRepo.UpdateUser(r.user, map[string]interface{}{ - "State": Idle, + "State": users.Idle, "ContactUUID": "", "ReplyMessageID": 0, "Language": language, diff --git a/bot/common/messaging.go b/bot/common/messaging.go index 0ef2a8f..6e7c8f8 100644 --- a/bot/common/messaging.go +++ b/bot/common/messaging.go @@ -5,12 +5,13 @@ import ( "github.com/PaulSonOfLars/gotgbot/v2" "github.com/PaulSonOfLars/gotgbot/v2/ext" "github.com/bugfloyd/anonymous-telegram-bot/common/i18n" + "github.com/bugfloyd/anonymous-telegram-bot/common/users" "strconv" "strings" ) func (r *RootHandler) sendAnonymousMessage(b *gotgbot.Bot, ctx *ext.Context) error { - receiver, err := r.userRepo.readUserByUUID(r.user.ContactUUID) + receiver, err := r.userRepo.ReadUserByUUID(r.user.ContactUUID) if err != nil { return fmt.Errorf("failed to get receiver: %w", err) } @@ -96,7 +97,7 @@ func (r *RootHandler) openCallback(b *gotgbot.Bot, ctx *ext.Context) error { return fmt.Errorf("invalid callback data: %s", cb.Data) } uuid := split[1] - sender, err := r.userRepo.readUserByUUID(uuid) + sender, err := r.userRepo.ReadUserByUUID(uuid) if err != nil { return fmt.Errorf("failed to get receiver: %w", err) } @@ -178,7 +179,7 @@ func (r *RootHandler) replyCallback(b *gotgbot.Bot, ctx *ext.Context) error { } // Check if receiver exists - receiver, err := r.userRepo.readUserByUUID(receiverUUID) + receiver, err := r.userRepo.ReadUserByUUID(receiverUUID) if err != nil { return fmt.Errorf("failed to get receiver: %w", err) } @@ -223,7 +224,7 @@ func (r *RootHandler) replyCallback(b *gotgbot.Bot, ctx *ext.Context) error { // Store the message id in the user and set status to replying err = r.userRepo.UpdateUser(r.user, map[string]interface{}{ - "State": Sending, + "State": users.Sending, "ContactUUID": receiverUUID, "ReplyMessageID": messageID, }) diff --git a/bot/common/misc.go b/bot/common/misc.go index 955031b..733e266 100644 --- a/bot/common/misc.go +++ b/bot/common/misc.go @@ -5,12 +5,13 @@ import ( "github.com/PaulSonOfLars/gotgbot/v2" "github.com/PaulSonOfLars/gotgbot/v2/ext" "github.com/bugfloyd/anonymous-telegram-bot/common/i18n" + "github.com/bugfloyd/anonymous-telegram-bot/common/users" "github.com/sqids/sqids-go" "os" "strings" ) -func (r *RootHandler) processUser(userRepo *UserRepository, ctx *ext.Context) (*User, error) { +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) @@ -39,12 +40,12 @@ func (r *RootHandler) start(b *gotgbot.Bot, ctx *ext.Context) error { } if len(args) == 2 && args[0] == "/start" { var err error - var receiverUser *User + var receiverUser *users.User var identity string if strings.HasPrefix(args[1], "_") { username := args[1][1:] - receiverUser, err = r.userRepo.readUserByUsername(username) + receiverUser, err = r.userRepo.ReadUserByUsername(username) if err != nil { return fmt.Errorf("failed to retrieve the link owner: %w", err) } @@ -54,7 +55,7 @@ func (r *RootHandler) start(b *gotgbot.Bot, ctx *ext.Context) error { if err != nil { return fmt.Errorf("failed to read the link key: %w", err) } - receiverUser, err = r.userRepo.readUserByLinkKey(linkKey, createdAt) + receiverUser, err = r.userRepo.ReadUserByLinkKey(linkKey, createdAt) if err != nil { return fmt.Errorf("failed to retrieve the link owner: %w", err) } @@ -112,7 +113,7 @@ func (r *RootHandler) start(b *gotgbot.Bot, ctx *ext.Context) error { // Set user state to sending err = r.userRepo.UpdateUser(r.user, map[string]interface{}{ - "State": Sending, + "State": users.Sending, "ContactUUID": receiverUser.UUID, }) if err != nil { @@ -171,9 +172,9 @@ func (r *RootHandler) getLink(b *gotgbot.Bot, ctx *ext.Context) error { func (r *RootHandler) processText(b *gotgbot.Bot, ctx *ext.Context) error { switch r.user.State { - case Sending: + case users.Sending: return r.sendAnonymousMessage(b, ctx) - case SettingUsername: + case users.SettingUsername: return r.setUsername(b, ctx) default: return r.sendError(b, ctx, i18n.T(i18n.InvalidCommandText)) diff --git a/bot/common/root_handler.go b/bot/common/root_handler.go index 6bb1f54..9ff50f6 100644 --- a/bot/common/root_handler.go +++ b/bot/common/root_handler.go @@ -3,6 +3,7 @@ package common import ( "fmt" "github.com/bugfloyd/anonymous-telegram-bot/common/i18n" + "github.com/bugfloyd/anonymous-telegram-bot/common/users" "github.com/PaulSonOfLars/gotgbot/v2" "github.com/PaulSonOfLars/gotgbot/v2/ext" @@ -43,8 +44,8 @@ const ( ) type RootHandler struct { - user *User - userRepo UserRepository + user *users.User + userRepo users.UserRepository } func NewRootHandler() *RootHandler { @@ -59,7 +60,7 @@ func (r *RootHandler) init(commandName interface{}) handlers.Response { func (r *RootHandler) runCommand(b *gotgbot.Bot, ctx *ext.Context, command interface{}) error { // create user repo - userRepo, err := NewUserRepository() + userRepo, err := users.NewUserRepository() if err != nil { return fmt.Errorf("failed to init db repo: %w", err) } @@ -94,7 +95,7 @@ func (r *RootHandler) runCommand(b *gotgbot.Bot, ctx *ext.Context, command inter } case CallbackCommand: // Reset user state if necessary - if r.user.State != Idle || r.user.ContactUUID != "" || r.user.ReplyMessageID != 0 { + if r.user.State != users.Idle || r.user.ContactUUID != "" || r.user.ReplyMessageID != 0 { err := r.userRepo.ResetUserState(r.user) if err != nil { return err diff --git a/bot/common/username.go b/bot/common/username.go index b6a0dcc..d1e09cf 100644 --- a/bot/common/username.go +++ b/bot/common/username.go @@ -5,6 +5,7 @@ import ( "github.com/PaulSonOfLars/gotgbot/v2" "github.com/PaulSonOfLars/gotgbot/v2/ext" "github.com/bugfloyd/anonymous-telegram-bot/common/i18n" + "github.com/bugfloyd/anonymous-telegram-bot/common/users" "regexp" "strings" ) @@ -78,7 +79,7 @@ func (r *RootHandler) usernameCallback(b *gotgbot.Bot, ctx *ext.Context, action } } else if action == "SET" { err := r.userRepo.UpdateUser(r.user, map[string]interface{}{ - "State": SettingUsername, + "State": users.SettingUsername, "ContactUUID": "", "ReplyMessageID": 0, }) @@ -101,7 +102,7 @@ func (r *RootHandler) usernameCallback(b *gotgbot.Bot, ctx *ext.Context, action } } else if action == "REMOVE" { err := r.userRepo.UpdateUser(r.user, map[string]interface{}{ - "State": Idle, + "State": users.Idle, "Username": "", "ContactUUID": "", "ReplyMessageID": 0, @@ -142,11 +143,11 @@ func (r *RootHandler) setUsername(b *gotgbot.Bot, ctx *ext.Context) error { // Convert to lowercase username = strings.ToLower(username) - existingUser, err := r.userRepo.readUserByUsername(username) + existingUser, err := r.userRepo.ReadUserByUsername(username) if err != nil || existingUser == nil { err := r.userRepo.UpdateUser(r.user, map[string]interface{}{ "Username": username, - "State": Idle, + "State": users.Idle, "ContactUUID": "", "ReplyMessageID": 0, }) diff --git a/bot/common/user.go b/bot/common/users/user.go similarity index 98% rename from bot/common/user.go rename to bot/common/users/user.go index 6f2cd60..cbff754 100644 --- a/bot/common/user.go +++ b/bot/common/users/user.go @@ -1,4 +1,4 @@ -package common +package users import ( "github.com/bugfloyd/anonymous-telegram-bot/common/i18n" diff --git a/bot/common/user_repo.go b/bot/common/users/user_repo.go similarity index 94% rename from bot/common/user_repo.go rename to bot/common/users/user_repo.go index d8efca3..3672974 100644 --- a/bot/common/user_repo.go +++ b/bot/common/users/user_repo.go @@ -1,4 +1,4 @@ -package common +package users import ( "fmt" @@ -53,7 +53,7 @@ func (repo *UserRepository) CreateUser(userId int64) (*User, error) { return &u, nil } -func (repo *UserRepository) readUserByUUID(uuid string) (*User, error) { +func (repo *UserRepository) ReadUserByUUID(uuid string) (*User, error) { var u User err := repo.table.Get("UUID", uuid).One(&u) if err != nil { @@ -71,7 +71,7 @@ func (repo *UserRepository) ReadUserByUserId(userId int64) (*User, error) { return &u, nil } -func (repo *UserRepository) readUserByUsername(username string) (*User, error) { +func (repo *UserRepository) ReadUserByUsername(username string) (*User, error) { var u User err := repo.table.Get("Username", username).Index("Username-GSI").One(&u) if err != nil { @@ -80,7 +80,7 @@ func (repo *UserRepository) readUserByUsername(username string) (*User, error) { return &u, nil } -func (repo *UserRepository) readUserByLinkKey(linkKey int32, createdAt int64) (*User, error) { +func (repo *UserRepository) ReadUserByLinkKey(linkKey int32, createdAt int64) (*User, error) { var u User err := repo.table.Get("LinkKey", linkKey).Index("LinkKey-GSI").Range("CreatedAt", dynamo.Equal, createdAt).One(&u) if err != nil { @@ -128,7 +128,7 @@ func (repo *UserRepository) ResetUserState(user *User) error { return nil } -func (repo *UserRepository) updateBlacklist(user *User, method string, value string) error { +func (repo *UserRepository) UpdateBlacklist(user *User, method string, value string) error { updateBuilder := repo.table.Update("UUID", user.UUID) switch method {