Process flags once and make them available across functions

This commit is contained in:
Alfred Kamanda 2024-09-03 19:06:37 +03:00
parent 19b2fa65fa
commit 5105e902f1
Signed by untrusted user: Alfred-mk
GPG Key ID: 7EA3D01708908703

View File

@ -9,6 +9,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync"
"git.defalsify.org/vise.git/asm" "git.defalsify.org/vise.git/asm"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
@ -52,6 +53,33 @@ type FSData struct {
St *state.State St *state.State
} }
// FlagManager handles centralized flag management
type FlagManager struct {
parser *asm.FlagParser
mu sync.RWMutex
}
// NewFlagManager creates a new FlagManager instance
func NewFlagManager(csvPath string) (*FlagManager, error) {
parser := asm.NewFlagParser()
_, err := parser.Load(csvPath)
if err != nil {
return nil, fmt.Errorf("failed to load flag parser: %v", err)
}
return &FlagManager{
parser: parser,
}, nil
}
// GetFlag retrieves a flag value by its label
func (fm *FlagManager) GetFlag(label string) (uint32, error) {
fm.mu.RLock()
defer fm.mu.RUnlock()
return fm.parser.GetFlag(label)
}
type FlagParserInterface interface { type FlagParserInterface interface {
GetFlag(key string) (uint32, error) GetFlag(key string) (uint32, error)
} }
@ -59,7 +87,7 @@ type FlagParserInterface interface {
type Handlers struct { type Handlers struct {
fs *FSData fs *FSData
db *gdbm.Database db *gdbm.Database
parser FlagParserInterface flagManager *FlagManager
accountFileHandler utils.AccountFileHandlerInterface accountFileHandler utils.AccountFileHandlerInterface
accountService server.AccountServiceInterface accountService server.AccountServiceInterface
} }
@ -71,10 +99,9 @@ func NewHandlers(dir string, st *state.State, sessionId string) (*Handlers, erro
panic(err) panic(err)
} }
pfp := path.Join(scriptDir, "pp.csv") pfp := path.Join(scriptDir, "pp.csv")
parser := asm.NewFlagParser() flagManager, err := NewFlagManager(pfp)
_, err = parser.Load(pfp)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to create flag manager: %v", err)
} }
return &Handlers{ return &Handlers{
db: db, db: db,
@ -82,7 +109,7 @@ func NewHandlers(dir string, st *state.State, sessionId string) (*Handlers, erro
Path: dir, Path: dir,
St: st, St: st,
}, },
parser: parser, flagManager: flagManager,
accountFileHandler: utils.NewAccountFileHandler(dir + "_data"), accountFileHandler: utils.NewAccountFileHandler(dir + "_data"),
accountService: &server.AccountService{}, accountService: &server.AccountService{},
}, nil }, nil
@ -100,7 +127,7 @@ func isValidPIN(pin string) bool {
func (h *Handlers) PreloadFlags(flagKeys []string) (map[string]uint32, error) { func (h *Handlers) PreloadFlags(flagKeys []string) (map[string]uint32, error) {
flags := make(map[string]uint32) flags := make(map[string]uint32)
for _, key := range flagKeys { for _, key := range flagKeys {
flag, err := h.parser.GetFlag(key) flag, err := h.flagManager.GetFlag(key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -113,13 +140,6 @@ func (h *Handlers) PreloadFlags(flagKeys []string) (map[string]uint32, error) {
func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flag
flagKeys := []string{"flag_language_set"}
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
inputStr := string(input) inputStr := string(input)
switch inputStr { switch inputStr {
case "0": case "0":
@ -131,7 +151,11 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r
default: default:
} }
res.FlagSet = append(res.FlagSet, flags["flag_language_set"]) languageSetFlag, err := h.flagManager.GetFlag("flag_language_set")
if err != nil {
return res, err
}
res.FlagSet = append(res.FlagSet, languageSetFlag)
return res, nil return res, nil
} }
@ -141,18 +165,14 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r
// sets the default values and flags // sets the default values and flags
func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flags
flagKeys := []string{"flag_account_created", "flag_account_creation_failed"} _, err := h.db.Fetch([]byte(AccountCreated))
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
_, err = h.db.Fetch([]byte(AccountCreated))
if err != nil { if err != nil {
if errors.Is(err, gdbm.ErrItemNotFound) { if errors.Is(err, gdbm.ErrItemNotFound) {
accountResp, err := h.accountService.CreateAccount() accountResp, err := h.accountService.CreateAccount()
if err != nil { if err != nil {
res.FlagSet = append(res.FlagSet, flags["flag_account_creation_failed"]) flag_account_creation_failed, _ := h.flagManager.GetFlag("flag_account_creation_failed")
res.FlagSet = append(res.FlagSet, flag_account_creation_failed)
return res, err return res, err
} }
data := map[string]string{ data := map[string]string{
@ -170,7 +190,8 @@ func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte)
key := []byte(AccountCreated) key := []byte(AccountCreated)
value := []byte("1") value := []byte("1")
h.db.Store(key, value, true) h.db.Store(key, value, true)
res.FlagSet = append(res.FlagSet, flags["flag_account_created"]) flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
res.FlagSet = append(res.FlagSet, flag_account_created)
return res, err return res, err
} else { } else {
return res, err return res, err
@ -183,19 +204,17 @@ func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte)
// SavePin persists the user's PIN choice into the filesystem // SavePin persists the user's PIN choice into the filesystem
func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
flagKeys := []string{"flag_incorrect_pin"}
flags, err := h.PreloadFlags(flagKeys) flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
if err != nil {
return res, err
}
accountPIN := string(input) accountPIN := string(input)
// Validate that the PIN is a 4-digit number // Validate that the PIN is a 4-digit number
if !isValidPIN(accountPIN) { if !isValidPIN(accountPIN) {
res.FlagSet = append(res.FlagSet, flags["flag_incorrect_pin"]) res.FlagSet = append(res.FlagSet, flag_incorrect_pin)
return res, nil return res, nil
} }
res.FlagReset = append(res.FlagReset, flags["flag_incorrect_pin"]) res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
key := []byte(AccountPin) key := []byte(AccountPin)
value := []byte(accountPIN) value := []byte(accountPIN)
@ -209,25 +228,21 @@ func (h *Handlers) SetResetSingleEdit(ctx context.Context, sym string, input []b
res := resource.Result{} res := resource.Result{}
menuOption := string(input) menuOption := string(input)
// Preload the required flags flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update")
flagKeys := []string{"flag_allow_update", "flag_single_edit"} flag_single_edit, _ := h.flagManager.GetFlag("flag_single_edit")
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
switch menuOption { switch menuOption {
case "2": case "2":
res.FlagReset = append(res.FlagReset, flags["flag_allow_update"]) res.FlagReset = append(res.FlagReset, flag_allow_update)
res.FlagSet = append(res.FlagSet, flags["flag_single_edit"]) res.FlagSet = append(res.FlagSet, flag_single_edit)
case "3": case "3":
res.FlagReset = append(res.FlagReset, flags["flag_allow_update"]) res.FlagReset = append(res.FlagReset, flag_allow_update)
res.FlagSet = append(res.FlagSet, flags["flag_single_edit"]) res.FlagSet = append(res.FlagSet, flag_single_edit)
case "4": case "4":
res.FlagReset = append(res.FlagReset, flags["flag_allow_update"]) res.FlagReset = append(res.FlagReset, flag_allow_update)
res.FlagSet = append(res.FlagSet, flags["flag_single_edit"]) res.FlagSet = append(res.FlagSet, flag_single_edit)
default: default:
res.FlagReset = append(res.FlagReset, flags["flag_single_edit"]) res.FlagReset = append(res.FlagReset, flag_single_edit)
} }
return res, nil return res, nil
@ -239,23 +254,20 @@ func (h *Handlers) SetResetSingleEdit(ctx context.Context, sym string, input []b
func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flags flag_valid_pin, _ := h.flagManager.GetFlag("flag_valid_pin")
flagKeys := []string{"flag_valid_pin", "flag_pin_mismatch", "flag_pin_set"} flag_pin_mismatch, _ := h.flagManager.GetFlag("flag_pin_mismatch")
flags, err := h.PreloadFlags(flagKeys) flag_pin_set, _ := h.flagManager.GetFlag("flag_pin_set")
if err != nil {
return res, err
}
AccountPin, err := h.db.Fetch([]byte(AccountPin)) AccountPin, err := h.db.Fetch([]byte(AccountPin))
if err != nil { if err != nil {
return res, err return res, err
} }
if bytes.Equal(input, AccountPin) { if bytes.Equal(input, AccountPin) {
res.FlagSet = []uint32{flags["flag_valid_pin"]} res.FlagSet = []uint32{flag_valid_pin}
res.FlagReset = []uint32{flags["flag_pin_mismatch"]} res.FlagReset = []uint32{flag_pin_mismatch}
res.FlagSet = append(res.FlagSet, flags["flag_pin_set"]) res.FlagSet = append(res.FlagSet, flag_pin_set)
} else { } else {
res.FlagSet = []uint32{flags["flag_pin_mismatch"]} res.FlagSet = []uint32{flag_pin_mismatch}
} }
return res, nil return res, nil
@ -362,14 +374,9 @@ func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte)
func (h *Handlers) ResetAllowUpdate(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) ResetAllowUpdate(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flag flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update")
flagKeys := []string{"flag_allow_update"}
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
res.FlagReset = append(res.FlagReset, flags["flag_allow_update"]) res.FlagReset = append(res.FlagReset, flag_allow_update)
return res, nil return res, nil
} }
@ -377,14 +384,9 @@ func (h *Handlers) ResetAllowUpdate(ctx context.Context, sym string, input []byt
func (h *Handlers) ResetAccountAuthorized(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) ResetAccountAuthorized(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flags flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
flagKeys := []string{"flag_account_authorized"}
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
res.FlagReset = append(res.FlagReset, flags["flag_account_authorized"]) res.FlagReset = append(res.FlagReset, flag_account_authorized)
return res, nil return res, nil
} }
@ -403,26 +405,25 @@ func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte
// It sets the required flags that control the flow. // It sets the required flags that control the flow.
func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flags
flagKeys := []string{"flag_incorrect_pin", "flag_account_authorized", "flag_allow_update"} flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
flags, err := h.PreloadFlags(flagKeys) flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
if err != nil { flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update")
return res, err
}
storedpin, err := h.db.Fetch([]byte(AccountPin)) storedpin, err := h.db.Fetch([]byte(AccountPin))
if err == nil { if err == nil {
if len(input) == 4 { if len(input) == 4 {
if bytes.Equal(input, storedpin) { if bytes.Equal(input, storedpin) {
if h.fs.St.MatchFlag(flags["flag_account_authorized"], false) { if h.fs.St.MatchFlag(flag_account_authorized, false) {
res.FlagReset = append(res.FlagReset, flags["flag_incorrect_pin"]) res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
res.FlagSet = append(res.FlagSet, flags["flag_allow_update"], flags["flag_account_authorized"]) res.FlagSet = append(res.FlagSet, flag_allow_update, flag_account_authorized)
} else { } else {
res.FlagSet = append(res.FlagSet, flags["flag_allow_update"]) res.FlagSet = append(res.FlagSet, flag_allow_update)
res.FlagReset = append(res.FlagReset, flags["flag_account_authorized"]) res.FlagReset = append(res.FlagReset, flag_account_authorized)
} }
} else { } else {
res.FlagSet = append(res.FlagSet, flags["flag_incorrect_pin"]) res.FlagSet = append(res.FlagSet, flag_incorrect_pin)
res.FlagReset = append(res.FlagReset, flags["flag_account_authorized"]) res.FlagReset = append(res.FlagReset, flag_account_authorized)
return res, nil return res, nil
} }
} }
@ -438,14 +439,9 @@ func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (res
func (h *Handlers) ResetIncorrectPin(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) ResetIncorrectPin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flag flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
flagKeys := []string{"flag_incorrect_pin"}
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
res.FlagReset = append(res.FlagReset, flags["flag_incorrect_pin"]) res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
return res, nil return res, nil
} }
@ -454,12 +450,9 @@ func (h *Handlers) ResetIncorrectPin(ctx context.Context, sym string, input []by
func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flags flag_account_success, _ := h.flagManager.GetFlag("flag_account_success")
flagKeys := []string{"flag_account_success", "flag_account_pending"} flag_account_pending, _ := h.flagManager.GetFlag("flag_account_pending")
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
trackingId, err := h.db.Fetch([]byte(TrackingIdKey)) trackingId, err := h.db.Fetch([]byte(TrackingIdKey))
if err != nil { if err != nil {
@ -485,11 +478,11 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
} }
if status == "SUCCESS" { if status == "SUCCESS" {
res.FlagSet = append(res.FlagSet, flags["flag_account_success"]) res.FlagSet = append(res.FlagSet, flag_account_success)
res.FlagReset = append(res.FlagReset, flags["flag_account_pending"]) res.FlagReset = append(res.FlagReset, flag_account_pending)
} else { } else {
res.FlagReset = append(res.FlagReset, flags["flag_account_success"]) res.FlagReset = append(res.FlagReset, flag_account_success)
res.FlagSet = append(res.FlagSet, flags["flag_account_pending"]) res.FlagSet = append(res.FlagSet, flag_account_pending)
} }
return res, nil return res, nil
} }
@ -498,19 +491,14 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
func (h *Handlers) Quit(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) Quit(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flags flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
flagKeys := []string{"flag_account_authorized"}
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
code := codeFromCtx(ctx) code := codeFromCtx(ctx)
l := gotext.NewLocale(translationDir, code) l := gotext.NewLocale(translationDir, code)
l.AddDomain("default") l.AddDomain("default")
res.Content = l.Get("Thank you for using Sarafu. Goodbye!") res.Content = l.Get("Thank you for using Sarafu. Goodbye!")
res.FlagReset = append(res.FlagReset, flags["flag_account_authorized"]) res.FlagReset = append(res.FlagReset, flag_account_authorized)
return res, nil return res, nil
} }
@ -518,25 +506,20 @@ func (h *Handlers) Quit(ctx context.Context, sym string, input []byte) (resource
func (h *Handlers) VerifyYob(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) VerifyYob(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flag flag_incorrect_date_format, _ := h.flagManager.GetFlag("flag_incorrect_date_format")
flagKeys := []string{"flag_incorrect_date_format"}
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
date := string(input) date := string(input)
_, err = strconv.Atoi(date) _, err := strconv.Atoi(date)
if err != nil { if err != nil {
// If conversion fails, input is not numeric // If conversion fails, input is not numeric
res.FlagSet = append(res.FlagSet, flags["flag_incorrect_date_format"]) res.FlagSet = append(res.FlagSet, flag_incorrect_date_format)
return res, nil return res, nil
} }
if len(date) == 4 { if len(date) == 4 {
res.FlagReset = append(res.FlagReset, flags["flag_incorrect_date_format"]) res.FlagReset = append(res.FlagReset, flag_incorrect_date_format)
} else { } else {
res.FlagSet = append(res.FlagSet, flags["flag_incorrect_date_format"]) res.FlagSet = append(res.FlagSet, flag_incorrect_date_format)
} }
return res, nil return res, nil
@ -546,14 +529,9 @@ func (h *Handlers) VerifyYob(ctx context.Context, sym string, input []byte) (res
func (h *Handlers) ResetIncorrectYob(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) ResetIncorrectYob(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flags flag_incorrect_date_format, _ := h.flagManager.GetFlag("flag_incorrect_date_format")
flagKeys := []string{"flag_incorrect_date_format"}
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
res.FlagReset = append(res.FlagReset, flags["flag_incorrect_date_format"]) res.FlagReset = append(res.FlagReset, flag_incorrect_date_format)
return res, nil return res, nil
} }
@ -581,16 +559,12 @@ func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []by
res := resource.Result{} res := resource.Result{}
recipient := string(input) recipient := string(input)
flagKeys := []string{"flag_invalid_recipient"} flag_invalid_recipient, _ := h.flagManager.GetFlag("flag_invalid_recipient")
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
if recipient != "0" { if recipient != "0" {
// mimic invalid number check // mimic invalid number check
if recipient == "000" { if recipient == "000" {
res.FlagSet = append(res.FlagSet, flags["flag_invalid_recipient"]) res.FlagSet = append(res.FlagSet, flag_invalid_recipient)
res.Content = recipient res.Content = recipient
return res, nil return res, nil
@ -611,14 +585,10 @@ func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []by
func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flags flag_invalid_recipient, _ := h.flagManager.GetFlag("flag_invalid_recipient")
flagKeys := []string{"flag_invalid_recipient", "flag_invalid_recipient_with_invite"} flag_invalid_recipient_with_invite, _ := h.flagManager.GetFlag("flag_invalid_recipient_with_invite")
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
err = h.db.Delete([]byte(Amount)) err := h.db.Delete([]byte(Amount))
if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) { if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) {
return res, err return res, err
} }
@ -627,7 +597,7 @@ func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byt
return res, err return res, err
} }
res.FlagReset = append(res.FlagReset, flags["flag_invalid_recipient"], flags["flag_invalid_recipient_with_invite"]) res.FlagReset = append(res.FlagReset, flag_invalid_recipient, flag_invalid_recipient_with_invite)
return res, nil return res, nil
} }
@ -636,19 +606,14 @@ func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byt
func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flag flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount")
flagKeys := []string{"flag_invalid_amount"}
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
err = h.db.Delete([]byte(Amount)) err := h.db.Delete([]byte(Amount))
if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) { if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) {
return res, err return res, err
} }
res.FlagReset = append(res.FlagReset, flags["flag_invalid_amount"]) res.FlagReset = append(res.FlagReset, flag_invalid_amount)
return res, nil return res, nil
} }
@ -676,12 +641,8 @@ func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (res
// it is not more than the current balance. // it is not more than the current balance.
func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flag
flagKeys := []string{"flag_invalid_amount"} flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount")
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
amountStr := string(input) amountStr := string(input)
publicKey, err := h.db.Fetch([]byte(PublicKeyKey)) publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
@ -711,20 +672,20 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
re := regexp.MustCompile(`^(\d+(\.\d+)?)\s*(?:CELO)?$`) re := regexp.MustCompile(`^(\d+(\.\d+)?)\s*(?:CELO)?$`)
matches := re.FindStringSubmatch(strings.TrimSpace(amountStr)) matches := re.FindStringSubmatch(strings.TrimSpace(amountStr))
if len(matches) < 2 { if len(matches) < 2 {
res.FlagSet = append(res.FlagSet, flags["flag_invalid_amount"]) res.FlagSet = append(res.FlagSet, flag_invalid_amount)
res.Content = amountStr res.Content = amountStr
return res, nil return res, nil
} }
inputAmount, err := strconv.ParseFloat(matches[1], 64) inputAmount, err := strconv.ParseFloat(matches[1], 64)
if err != nil { if err != nil {
res.FlagSet = append(res.FlagSet, flags["flag_invalid_amount"]) res.FlagSet = append(res.FlagSet, flag_invalid_amount)
res.Content = amountStr res.Content = amountStr
return res, nil return res, nil
} }
if inputAmount > balanceValue { if inputAmount > balanceValue {
res.FlagSet = append(res.FlagSet, flags["flag_invalid_amount"]) res.FlagSet = append(res.FlagSet, flag_invalid_amount)
res.Content = amountStr res.Content = amountStr
return res, nil return res, nil
} }
@ -784,12 +745,7 @@ func (h *Handlers) GetAmount(ctx context.Context, sym string, input []byte) (res
func (h *Handlers) QuitWithBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) QuitWithBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
// Preload the required flag flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
flagKeys := []string{"flag_account_authorized"}
flags, err := h.PreloadFlags(flagKeys)
if err != nil {
return res, err
}
code := codeFromCtx(ctx) code := codeFromCtx(ctx)
l := gotext.NewLocale(translationDir, code) l := gotext.NewLocale(translationDir, code)
@ -803,7 +759,7 @@ func (h *Handlers) QuitWithBalance(ctx context.Context, sym string, input []byte
return res, nil return res, nil
} }
res.Content = l.Get("Your account balance is %s", balance) res.Content = l.Get("Your account balance is %s", balance)
res.FlagReset = append(res.FlagReset, flags["flag_account_authorized"]) res.FlagReset = append(res.FlagReset, flag_account_authorized)
return res, nil return res, nil
} }
@ -832,10 +788,9 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []
res.Content = l.Get("Your request has been sent. %s will receive %s from %s.", string(recipient), string(amount), string(publicKey)) res.Content = l.Get("Your request has been sent. %s will receive %s from %s.", string(recipient), string(amount), string(publicKey))
account_authorized_flag, err := h.parser.GetFlag("flag_account_authorized") account_authorized_flag, err := h.flagManager.GetFlag("flag_account_authorized")
if err != nil { if err != nil {
return res, nil return res, err
} }
res.FlagReset = append(res.FlagReset, account_authorized_flag) res.FlagReset = append(res.FlagReset, account_authorized_flag)