Skip to content

Commit

Permalink
refactor: rename appID to platformID (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
MHSaeedkia authored Jan 27, 2025
1 parent 9bfb551 commit 9c4204d
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 52 deletions.
6 changes: 3 additions & 3 deletions internal/engine/command/calculator/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions internal/engine/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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()
},
Expand All @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions internal/engine/command/crowdfund/crowdfund.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/command/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions internal/engine/command/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions internal/engine/command/phoenix/phoenix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions internal/engine/command/voucher/voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
6 changes: 3 additions & 3 deletions internal/engine/command/zealy/zealy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -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,
Expand Down
22 changes: 11 additions & 11 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func newBotEngine(ctx context.Context,
Emoji: "🤖",
Name: "pagu",
Help: "",
AppIDs: entity.AllAppIDs(),
PlatformIDs: entity.AllPlatformIDs(),
SubCommands: make([]*command.Command, 0),
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -268,27 +268,27 @@ 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,
) command.CommandResult {
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 {
Expand Down Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion internal/entity/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (pid PlatformID) String() string {
return fmt.Sprintf("%d", pid)
}

func AllAppIDs() []PlatformID {
func AllPlatformIDs() []PlatformID {
return []PlatformID{
PlatformIDCLI,
PlatformIDDiscord,
Expand Down
2 changes: 1 addition & 1 deletion internal/platforms/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/platforms/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions internal/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 9c4204d

Please sign in to comment.