Skip to content

Commit

Permalink
chore: move telegram mutes response to templates (#18)
Browse files Browse the repository at this point in the history
* chore: move telegram mutes response to templates

* chore: remove old code
  • Loading branch information
freak12techno committed Nov 4, 2022
1 parent cc1c7cf commit d6d5e44
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
29 changes: 9 additions & 20 deletions telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,18 @@ func (reporter *TelegramReporter) HandleListMutes(c tele.Context) error {
Str("text", c.Text()).
Msg("Got list mutes query")

var sb strings.Builder
sb.WriteString("<strong>Active mutes:</strong>\n\n")

mutesCount := 0

for _, mute := range reporter.MutesManager.Mutes.Mutes {
if mute.IsExpired() {
continue
}

mutesCount++

sb.WriteString(fmt.Sprintf(
"<strong>Chain: </strong>%s\n<strong>Proposal ID: </strong>%s\n<strong>Expires: </strong>%s\n\n",
mute.Chain, mute.ProposalID, mute.Expires,
))
}
mutes := Filter(reporter.MutesManager.Mutes.Mutes, func(m Mute) bool {
return !m.IsExpired()
})

if mutesCount == 0 {
sb.WriteString("No active mutes.")
template, _ := reporter.GetTemplate("mutes")
var buffer bytes.Buffer
if err := template.Execute(&buffer, mutes); err != nil {
reporter.Logger.Error().Err(err).Msg("Error rendering votes template")
return err
}

return reporter.BotReply(c, sb.String())
return reporter.BotReply(c, buffer.String())
}

func (reporter *TelegramReporter) HandleProposals(c tele.Context) error {
Expand Down
8 changes: 8 additions & 0 deletions templates/telegram/mutes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- if eq (len .) 0 }}
No active mutes.
{{- end }}
{{ range . }}
<strong>Chain: </strong>{{ .Chain }}
<strong>Proposal ID: </strong>{{ .ProposalID }}
<strong>Expires: </strong>{{ .GetExpirationTime }}
{{ end }}
4 changes: 4 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func (m *Mutes) IsMuted(chain string, proposalID string) bool {
return false
}

func (m Mute) GetExpirationTime() string {
return m.Expires.Format(time.RFC3339Nano)
}

func (m *Mutes) AddMute(mute Mute) {
for index, muteInRange := range m.Mutes {
if mute.Chain == muteInRange.Chain && mute.ProposalID == muteInRange.ProposalID {
Expand Down
11 changes: 11 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

func Filter[T any](slice []T, f func(T) bool) []T {
var n []T
for _, e := range slice {
if f(e) {
n = append(n, e)
}
}
return n
}

0 comments on commit d6d5e44

Please sign in to comment.