diff --git a/internal/engine/command/calculator/calculator.go b/internal/engine/command/calculator/calculator.go index ea2fa9c7..a615127e 100644 --- a/internal/engine/command/calculator/calculator.go +++ b/internal/engine/command/calculator/calculator.go @@ -20,13 +20,13 @@ func NewCalculatorCmd(clientMgr client.IManager) *CalculatorCmd { func (c *CalculatorCmd) GetCommand() *command.Command { cmd := c.buildCalculatorCommand() - cmd.AppIDs = entity.AllAppIDs() + cmd.PlatformIDs = entity.AllPlatformIDs() cmd.TargetFlag = command.TargetMaskMainnet - c.subCmdReward.AppIDs = entity.AllAppIDs() + c.subCmdReward.PlatformIDs = entity.AllPlatformIDs() c.subCmdReward.TargetFlag = command.TargetMaskMainnet - c.subCmdFee.AppIDs = entity.AllAppIDs() + c.subCmdFee.PlatformIDs = entity.AllPlatformIDs() c.subCmdFee.TargetFlag = command.TargetMaskMainnet return cmd diff --git a/internal/engine/command/command.go b/internal/engine/command/command.go index b33e2e36..3295fe0f 100644 --- a/internal/engine/command/command.go +++ b/internal/engine/command/command.go @@ -103,7 +103,7 @@ type Command struct { ResultTemplate string `yaml:"result_template"` Middlewares []MiddlewareFunc `yaml:"-"` Handler HandlerFunc `yaml:"-"` - AppIDs []entity.PlatformID `yaml:"-"` + PlatformIDs []entity.PlatformID `yaml:"-"` TargetFlag int `yaml:"-"` } @@ -245,8 +245,8 @@ Use "{{.cmd.Name}} help --subcommand=[subcommand]" for more information about a } } -func (cmd *Command) HasAppID(appID entity.PlatformID) bool { - return slices.Contains(cmd.AppIDs, appID) +func (cmd *Command) HasPlatformID(platformID entity.PlatformID) bool { + return slices.Contains(cmd.PlatformIDs, platformID) } func (cmd *Command) HasSubCommand() bool { @@ -267,11 +267,11 @@ func (cmd *Command) AddSubCommand(subCmd *Command) { func (cmd *Command) AddHelpSubCommand() { helpCmd := &Command{ - Emoji: "❓", - Name: "help", - Help: fmt.Sprintf("Help for %v command", cmd.Name), - AppIDs: entity.AllAppIDs(), - TargetFlag: TargetMaskAll, + Emoji: "❓", + Name: "help", + Help: fmt.Sprintf("Help for %v command", cmd.Name), + PlatformIDs: entity.AllPlatformIDs(), + TargetFlag: TargetMaskAll, Handler: func(_ *entity.User, _ *Command, _ map[string]string) CommandResult { return cmd.RenderHelpTemplate() }, @@ -292,7 +292,7 @@ Version : {{.version}} Emoji: "📝", Name: "about", Help: "About Pagu", - AppIDs: entity.AllAppIDs(), + PlatformIDs: entity.AllPlatformIDs(), TargetFlag: TargetMaskAll, ResultTemplate: aboutTemplate, Handler: func(_ *entity.User, _ *Command, _ map[string]string) CommandResult { diff --git a/internal/engine/command/crowdfund/crowdfund.go b/internal/engine/command/crowdfund/crowdfund.go index e1f5e79f..50d685a2 100644 --- a/internal/engine/command/crowdfund/crowdfund.go +++ b/internal/engine/command/crowdfund/crowdfund.go @@ -41,27 +41,27 @@ func (c *CrowdfundCmd) GetCommand() *command.Command { middlewareHandler := command.NewMiddlewareHandler(c.db, c.wallet) cmd := c.buildCrowdfundCommand() - cmd.AppIDs = entity.AllAppIDs() + cmd.PlatformIDs = entity.AllPlatformIDs() cmd.TargetFlag = command.TargetMaskModerator | command.TargetMaskMainnet - c.subCmdCreate.AppIDs = []entity.PlatformID{entity.PlatformIDCLI, entity.PlatformIDDiscord} + c.subCmdCreate.PlatformIDs = []entity.PlatformID{entity.PlatformIDCLI, entity.PlatformIDDiscord} c.subCmdCreate.TargetFlag = command.TargetMaskModerator c.subCmdCreate.Middlewares = []command.MiddlewareFunc{middlewareHandler.OnlyModerator} - c.subCmdDisable.AppIDs = []entity.PlatformID{entity.PlatformIDCLI, entity.PlatformIDDiscord} + c.subCmdDisable.PlatformIDs = []entity.PlatformID{entity.PlatformIDCLI, entity.PlatformIDDiscord} c.subCmdDisable.TargetFlag = command.TargetMaskModerator c.subCmdDisable.Middlewares = []command.MiddlewareFunc{middlewareHandler.OnlyModerator} - c.subCmdReport.AppIDs = entity.AllAppIDs() + c.subCmdReport.PlatformIDs = entity.AllPlatformIDs() c.subCmdReport.TargetFlag = command.TargetMaskMainnet - c.subCmdInfo.AppIDs = entity.AllAppIDs() + c.subCmdInfo.PlatformIDs = entity.AllPlatformIDs() c.subCmdInfo.TargetFlag = command.TargetMaskMainnet - c.subCmdPurchase.AppIDs = entity.AllAppIDs() + c.subCmdPurchase.PlatformIDs = entity.AllPlatformIDs() c.subCmdPurchase.TargetFlag = command.TargetMaskMainnet - c.subCmdClaim.AppIDs = entity.AllAppIDs() + c.subCmdClaim.PlatformIDs = entity.AllPlatformIDs() c.subCmdClaim.TargetFlag = command.TargetMaskMainnet activeCampaign := c.activeCampaign() diff --git a/internal/engine/command/market/market.go b/internal/engine/command/market/market.go index 47b18a3c..83634fd4 100644 --- a/internal/engine/command/market/market.go +++ b/internal/engine/command/market/market.go @@ -23,10 +23,10 @@ func NewMarketCmd(clientMgr client.IManager, priceCache cache.Cache[string, enti func (m *MarketCmd) GetCommand() *command.Command { cmd := m.buildMarketCommand() - cmd.AppIDs = entity.AllAppIDs() + cmd.PlatformIDs = entity.AllPlatformIDs() cmd.TargetFlag = command.TargetMaskMainnet - m.subCmdPrice.AppIDs = entity.AllAppIDs() + m.subCmdPrice.PlatformIDs = entity.AllPlatformIDs() m.subCmdPrice.TargetFlag = command.TargetMaskMainnet return cmd diff --git a/internal/engine/command/network/network.go b/internal/engine/command/network/network.go index 0ce52807..0c0368a4 100644 --- a/internal/engine/command/network/network.go +++ b/internal/engine/command/network/network.go @@ -54,16 +54,16 @@ type NetStatus struct { func (n *NetworkCmd) GetCommand() *command.Command { cmd := n.buildNetworkCommand() - cmd.AppIDs = entity.AllAppIDs() + cmd.PlatformIDs = entity.AllPlatformIDs() cmd.TargetFlag = command.TargetMaskAll - n.subCmdNodeInfo.AppIDs = entity.AllAppIDs() + n.subCmdNodeInfo.PlatformIDs = entity.AllPlatformIDs() n.subCmdNodeInfo.TargetFlag = command.TargetMaskAll - n.subCmdStatus.AppIDs = entity.AllAppIDs() + n.subCmdStatus.PlatformIDs = entity.AllPlatformIDs() n.subCmdStatus.TargetFlag = command.TargetMaskAll - n.subCmdHealth.AppIDs = entity.AllAppIDs() + n.subCmdHealth.PlatformIDs = entity.AllPlatformIDs() n.subCmdHealth.TargetFlag = command.TargetMaskAll return cmd diff --git a/internal/engine/command/phoenix/phoenix.go b/internal/engine/command/phoenix/phoenix.go index 8ecd77bc..cabdc3e9 100644 --- a/internal/engine/command/phoenix/phoenix.go +++ b/internal/engine/command/phoenix/phoenix.go @@ -37,14 +37,14 @@ func (p *PhoenixCmd) GetCommand() *command.Command { middlewareHandler := command.NewMiddlewareHandler(p.db, p.wallet) cmd := p.buildPhoenixCommand() - cmd.AppIDs = entity.AllAppIDs() + cmd.PlatformIDs = entity.AllPlatformIDs() cmd.TargetFlag = command.TargetMaskTestnet p.subCmdFaucet.Middlewares = []command.MiddlewareFunc{middlewareHandler.WalletBalance} - p.subCmdFaucet.AppIDs = entity.AllAppIDs() + p.subCmdFaucet.PlatformIDs = entity.AllPlatformIDs() p.subCmdFaucet.TargetFlag = command.TargetMaskTestnet - p.subCmdWallet.AppIDs = entity.AllAppIDs() + p.subCmdWallet.PlatformIDs = entity.AllPlatformIDs() p.subCmdWallet.TargetFlag = command.TargetMaskTestnet return cmd diff --git a/internal/engine/command/voucher/voucher.go b/internal/engine/command/voucher/voucher.go index a75aaeac..8d2a721e 100644 --- a/internal/engine/command/voucher/voucher.go +++ b/internal/engine/command/voucher/voucher.go @@ -28,22 +28,22 @@ func (v *VoucherCmd) GetCommand() *command.Command { middlewareHandler := command.NewMiddlewareHandler(v.db, v.wallet) cmd := v.buildVoucherCommand() - cmd.AppIDs = entity.AllAppIDs() + cmd.PlatformIDs = entity.AllPlatformIDs() cmd.TargetFlag = command.TargetMaskMainnet | command.TargetMaskModerator - v.subCmdClaim.AppIDs = []entity.PlatformID{entity.PlatformIDDiscord} + v.subCmdClaim.PlatformIDs = []entity.PlatformID{entity.PlatformIDDiscord} v.subCmdClaim.TargetFlag = command.TargetMaskMainnet v.subCmdClaim.Middlewares = []command.MiddlewareFunc{middlewareHandler.WalletBalance} - v.subCmdCreate.AppIDs = []entity.PlatformID{entity.PlatformIDDiscord} + v.subCmdCreate.PlatformIDs = []entity.PlatformID{entity.PlatformIDDiscord} v.subCmdCreate.TargetFlag = command.TargetMaskModerator v.subCmdCreate.Middlewares = []command.MiddlewareFunc{middlewareHandler.OnlyModerator} - v.subCmdCreateBulk.AppIDs = []entity.PlatformID{entity.PlatformIDDiscord} + v.subCmdCreateBulk.PlatformIDs = []entity.PlatformID{entity.PlatformIDDiscord} v.subCmdCreateBulk.TargetFlag = command.TargetMaskModerator v.subCmdCreateBulk.Middlewares = []command.MiddlewareFunc{middlewareHandler.OnlyModerator} - v.subCmdStatus.AppIDs = []entity.PlatformID{entity.PlatformIDDiscord} + v.subCmdStatus.PlatformIDs = []entity.PlatformID{entity.PlatformIDDiscord} v.subCmdStatus.TargetFlag = command.TargetMaskModerator v.subCmdStatus.Middlewares = []command.MiddlewareFunc{middlewareHandler.OnlyModerator} diff --git a/internal/engine/command/zealy/zealy.go b/internal/engine/command/zealy/zealy.go index 55dde13f..a07139d2 100644 --- a/internal/engine/command/zealy/zealy.go +++ b/internal/engine/command/zealy/zealy.go @@ -32,7 +32,7 @@ func (z *ZealyCmd) GetCommand() *command.Command { }, }, SubCommands: nil, - AppIDs: []entity.PlatformID{entity.PlatformIDDiscord}, + PlatformIDs: []entity.PlatformID{entity.PlatformIDDiscord}, Handler: z.claimHandler, TargetFlag: command.TargetMaskMainnet, } @@ -42,7 +42,7 @@ func (z *ZealyCmd) GetCommand() *command.Command { Help: "Check the status of Zealy reward claims", Args: nil, SubCommands: nil, - AppIDs: []entity.PlatformID{entity.PlatformIDDiscord}, + PlatformIDs: []entity.PlatformID{entity.PlatformIDDiscord}, Handler: z.statusHandler, TargetFlag: command.TargetMaskModerator, } @@ -51,7 +51,7 @@ func (z *ZealyCmd) GetCommand() *command.Command { Name: "zealy", Help: "Commands for managing Zealy campaign", Args: nil, - AppIDs: []entity.PlatformID{entity.PlatformIDDiscord}, + PlatformIDs: []entity.PlatformID{entity.PlatformIDDiscord}, SubCommands: make([]*command.Command, 0), Handler: nil, TargetFlag: command.TargetMaskMainnet | command.TargetMaskModerator, diff --git a/internal/engine/engine.go b/internal/engine/engine.go index cd28a516..7e12b9e1 100644 --- a/internal/engine/engine.go +++ b/internal/engine/engine.go @@ -137,7 +137,7 @@ func newBotEngine(ctx context.Context, Emoji: "🤖", Name: "pagu", Help: "", - AppIDs: entity.AllAppIDs(), + PlatformIDs: entity.AllPlatformIDs(), SubCommands: make([]*command.Command, 0), } @@ -167,7 +167,7 @@ func (be *BotEngine) Commands() []*command.Command { // ParseAndExecute parses the input string and executes it. // It returns an error if parsing fails or execution is unsuccessful. func (be *BotEngine) ParseAndExecute( - appID entity.PlatformID, + platformID entity.PlatformID, callerID string, input string, ) command.CommandResult { @@ -184,7 +184,7 @@ func (be *BotEngine) ParseAndExecute( } } - return be.executeCommand(appID, callerID, cmds, args) + return be.executeCommand(platformID, callerID, cmds, args) } func parseCommandInput(cmdInput string) []string { @@ -268,7 +268,7 @@ func parseInput(input string) ([]string, map[string]string, error) { // executeCommand executes the parsed commands with their corresponding arguments. // It returns an error if the execution fails. func (be *BotEngine) executeCommand( - appID entity.PlatformID, + platformID entity.PlatformID, callerID string, commands []string, args map[string]string, @@ -276,19 +276,19 @@ func (be *BotEngine) executeCommand( log.Debug("execute command", "callerID", callerID, "commands", commands, "args", args) cmd := be.getTargetCommand(commands) - if !cmd.HasAppID(appID) { - return cmd.FailedResultF("unauthorized appID: %v", appID) + if !cmd.HasPlatformID(platformID) { + return cmd.FailedResultF("Unauthorized platform: %s", platformID) } if cmd.Handler == nil { return cmd.RenderHelpTemplate() } - caller, err := be.GetUser(appID, callerID) + caller, err := be.GetUser(platformID, callerID) if err != nil { log.Error(err.Error()) - return cmd.ErrorResult(fmt.Errorf("user is not defined in %s application", appID.String())) + return cmd.ErrorResult(fmt.Errorf("user is not defined in %s application", platformID)) } for _, middlewareFunc := range cmd.Middlewares { @@ -358,14 +358,14 @@ func (be *BotEngine) NetworkStatus() (*network.NetStatus, error) { }, nil } -func (be *BotEngine) GetUser(appID entity.PlatformID, platformUserID string) (*entity.User, error) { - existingUser, _ := be.db.GetUserByPlatformID(appID, platformUserID) +func (be *BotEngine) GetUser(platformID entity.PlatformID, platformUserID string) (*entity.User, error) { + existingUser, _ := be.db.GetUserByPlatformID(platformID, platformUserID) if existingUser != nil { return existingUser, nil } newUser := &entity.User{ - PlatformID: appID, + PlatformID: platformID, PlatformUserID: platformUserID, Role: entity.BasicUser, } diff --git a/internal/entity/application.go b/internal/entity/application.go index f6fba1e6..7906f2ba 100644 --- a/internal/entity/application.go +++ b/internal/entity/application.go @@ -31,7 +31,7 @@ func (pid PlatformID) String() string { return fmt.Sprintf("%d", pid) } -func AllAppIDs() []PlatformID { +func AllPlatformIDs() []PlatformID { return []PlatformID{ PlatformIDCLI, PlatformIDDiscord, diff --git a/internal/platforms/discord/discord.go b/internal/platforms/discord/discord.go index ced1a0e0..ba9aee5a 100644 --- a/internal/platforms/discord/discord.go +++ b/internal/platforms/discord/discord.go @@ -83,7 +83,7 @@ func (bot *Bot) registerCommands() error { cmds := bot.engine.Commands() for i, cmd := range cmds { - if !cmd.HasAppID(entity.PlatformIDDiscord) { + if !cmd.HasPlatformID(entity.PlatformIDDiscord) { continue } diff --git a/internal/platforms/telegram/telegram.go b/internal/platforms/telegram/telegram.go index 35a057a7..b0ee13c0 100644 --- a/internal/platforms/telegram/telegram.go +++ b/internal/platforms/telegram/telegram.go @@ -95,7 +95,7 @@ func (bot *Bot) registerCommands() error { commands := make([]tele.Command, 0) for i, beCmd := range bot.engine.Commands() { - if !beCmd.HasAppID(entity.PlatformIDTelegram) { + if !beCmd.HasPlatformID(entity.PlatformIDTelegram) { continue } diff --git a/internal/repository/user.go b/internal/repository/user.go index 71fda61d..14d4391e 100644 --- a/internal/repository/user.go +++ b/internal/repository/user.go @@ -25,10 +25,10 @@ func (db *Database) HasUser(id uint) bool { return exists } -func (db *Database) GetUserByPlatformID(appID entity.PlatformID, callerID string) (*entity.User, error) { +func (db *Database) GetUserByPlatformID(platformID entity.PlatformID, callerID string) (*entity.User, error) { var user *entity.User tx := db.gormDB.Model(&entity.User{}). - Where("platform_id = ?", appID). + Where("platform_id = ?", platformID). Where("platform_user_id = ?", callerID). First(&user)