Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactored aliases template #45

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions pkg/alias_manager/alias_manager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package alias_manager

import (
"bytes"
"os"

"main/pkg/config"
Expand All @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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()
}
7 changes: 4 additions & 3 deletions pkg/reporters/telegram/get_aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<pre>%s</pre>", aliases))
return reporter.BotReply(c, template)
}
10 changes: 10 additions & 0 deletions templates/telegram/Aliases.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{ if not . }}
No aliases set!
{{ end }}
{{ range . }}
{{ $chain := .Chain }}
<strong>{{ .Chain.GetName }}</strong>
{{- range $address, $link := .Links }}
- {{ SerializeLink $link }} -> <code>{{ $address }}</code>
{{- end -}}
{{ end }}
Loading