Skip to content

Commit

Permalink
move user & user repo to users module
Browse files Browse the repository at this point in the history
export functions in user repo:
ReadUserByUUID
ReadUserByUsername
ReadUserByLinkKey
UpdateBlacklist
  • Loading branch information
bugfloyd committed May 6, 2024
1 parent 2b4926e commit c7af286
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 30 deletions.
9 changes: 5 additions & 4 deletions bot/common/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion bot/common/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions bot/common/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
})
Expand Down
15 changes: 8 additions & 7 deletions bot/common/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
9 changes: 5 additions & 4 deletions bot/common/root_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -43,8 +44,8 @@ const (
)

type RootHandler struct {
user *User
userRepo UserRepository
user *users.User
userRepo users.UserRepository
}

func NewRootHandler() *RootHandler {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions bot/common/username.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
})
Expand All @@ -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,
Expand Down Expand Up @@ -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,
})
Expand Down
2 changes: 1 addition & 1 deletion bot/common/user.go → bot/common/users/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package users

import (
"github.com/bugfloyd/anonymous-telegram-bot/common/i18n"
Expand Down
10 changes: 5 additions & 5 deletions bot/common/user_repo.go → bot/common/users/user_repo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package users

import (
"fmt"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c7af286

Please sign in to comment.