From 1dc46ee9bfbfba89fab44a72d2e37cfa1940110e Mon Sep 17 00:00:00 2001 From: Techno Freak Date: Mon, 15 Jan 2024 02:03:06 +0300 Subject: [PATCH] feat: refactored aliases template --- pkg/alias_manager/alias_manager.go | 46 +++++++++++++++++++-------- pkg/reporters/telegram/get_aliases.go | 7 ++-- templates/telegram/Aliases.html | 10 ++++++ 3 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 templates/telegram/Aliases.html diff --git a/pkg/alias_manager/alias_manager.go b/pkg/alias_manager/alias_manager.go index 91d7aa0..e164607 100644 --- a/pkg/alias_manager/alias_manager.go +++ b/pkg/alias_manager/alias_manager.go @@ -1,7 +1,6 @@ package alias_manager import ( - "bytes" "os" "main/pkg/config" @@ -14,16 +13,6 @@ import ( type Aliases *map[string]string type TomlAliases map[string]Aliases -func (t TomlAliases) ToTomlString() (string, error) { - buffer := new(bytes.Buffer) - - if err := toml.NewEncoder(buffer).Encode(t); err != nil { - return "", err - } - - return buffer.String(), nil -} - type ChainAliases struct { Chain *types.Chain Aliases Aliases @@ -46,6 +35,36 @@ func (a AllChainAliases) ToTomlAliases() TomlAliases { return tomlAliases } +type ChainAliasesLinks struct { + Chain *types.Chain + Links map[string]types.Link +} + +func (a AllChainAliases) ToAliasesLinks() []ChainAliasesLinks { + aliasesLinks := make([]ChainAliasesLinks, 0) + + for _, chainAliases := range a { + links := make(map[string]types.Link) + + if chainAliases.Aliases == nil { + continue + } + + for wallet, alias := range *chainAliases.Aliases { + link := chainAliases.Chain.GetWalletLink(wallet) + link.Title = alias + links[wallet] = link + } + + aliasesLinks = append(aliasesLinks, ChainAliasesLinks{ + Chain: chainAliases.Chain, + Links: links, + }) + } + + return aliasesLinks +} + func NewAliasManager(logger *zerolog.Logger, config *config.AppConfig) *AliasManager { return &AliasManager{ Logger: logger.With().Str("component", "alias_manager").Logger(), @@ -168,7 +187,6 @@ func (m *AliasManager) Set(chain, address, alias string) error { return m.Save() } -func (m *AliasManager) GetAsToml() (string, error) { - tomlAliases := m.Aliases.ToTomlAliases() - return tomlAliases.ToTomlString() +func (m *AliasManager) GetAliasesLinks() []ChainAliasesLinks { + return m.Aliases.ToAliasesLinks() } diff --git a/pkg/reporters/telegram/get_aliases.go b/pkg/reporters/telegram/get_aliases.go index 1018eb8..eec1f63 100644 --- a/pkg/reporters/telegram/get_aliases.go +++ b/pkg/reporters/telegram/get_aliases.go @@ -16,10 +16,11 @@ func (reporter *TelegramReporter) HandleGetAliases(c tele.Context) error { return reporter.BotReply(c, "Aliases manager not enabled") } - aliases, err := reporter.AliasManager.GetAsToml() + aliases := reporter.AliasManager.GetAliasesLinks() + template, err := reporter.Render("Aliases", aliases) if err != nil { - return reporter.BotReply(c, fmt.Sprintf("Error getting aliases: %s", err)) + return reporter.BotReply(c, fmt.Sprintf("Error displaying aliases: %s", err)) } - return reporter.BotReply(c, fmt.Sprintf("
%s
", aliases)) + return reporter.BotReply(c, template) } diff --git a/templates/telegram/Aliases.html b/templates/telegram/Aliases.html new file mode 100644 index 0000000..adad4bd --- /dev/null +++ b/templates/telegram/Aliases.html @@ -0,0 +1,10 @@ +{{ if not . }} +No aliases set! +{{ end }} +{{ range . }} +{{ $chain := .Chain }} +{{ .Chain.GetName }} +{{- range $address, $link := .Links }} +- {{ SerializeLink $link }} -> {{ $address }} +{{- end -}} +{{ end }} \ No newline at end of file