Skip to content

Commit

Permalink
Merge pull request #44 from dwarvesf/fix/update-memo-cmd-layout
Browse files Browse the repository at this point in the history
fix: update memo cmd layout
  • Loading branch information
baenv authored Jun 28, 2024
2 parents 43956a4 + 90b4153 commit a621b5e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
3 changes: 3 additions & 0 deletions pkg/discord/command/memo/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package memo

import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -81,6 +82,8 @@ func (e *Memo) ListMemoLogs(message *model.DiscordMessage) error {
return err
}

fmt.Println(data[0].Category)

// 2. render
return e.view.Memo().ListMemoLogs(message, data, timeAmount, timeUnit)
}
Expand Down
47 changes: 31 additions & 16 deletions pkg/discord/view/memo/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,44 @@ func (v *Memo) List(original *model.DiscordMessage, memos []*model.Memo) error {
func (v *Memo) ListMemoLogs(original *model.DiscordMessage, memos []model.MemoLog, timeAmount int, timeUnit string) error {
var content string

tooLarge := false
if len(memos) > 20 {
tooLarge = true
memos = memos[:20]
memosByCategory := make(map[string][]model.MemoLog)
for _, mem := range memos {
if len(mem.Category) > 0 {
memosByCategory[mem.Category[len(mem.Category)-1]] = append(memosByCategory[mem.Category[len(mem.Category)-1]], mem)
continue
}

memosByCategory["others"] = append(memosByCategory["others"], mem)
}

// TODO: paging (no need currently)
for i, memo := range memos {
authors := make([]string, 0, len(memo.Authors))
for _, author := range memo.Authors {
authors = append(authors, fmt.Sprintf("<@%s>", author.DiscordID))
for category, memos := range memosByCategory {
content += fmt.Sprintf("🔹 **%s** - %v posts\n", strings.ToUpper(category), len(memos))

tooLarge := false
if len(memos) > 20 {
tooLarge = true
memos = memos[:20]
}

authorsStr := "<@anonymous>"
if len(authors) > 0 {
authorsStr = strings.Join(authors, ", ")
for i, memo := range memos {
authors := make([]string, 0, len(memo.Authors))
for _, author := range memo.Authors {
authors = append(authors, fmt.Sprintf("<@%s>", author.DiscordID))
}

authorsStr := "<@anonymous>"
if len(authors) > 0 {
authorsStr = strings.Join(authors, ", ")
}

content += fmt.Sprintf("[[%d](%s)] %s - %s\n", i+1, memo.URL, memo.Title, authorsStr)
}

content += fmt.Sprintf("[[%d](%s)] %s - %s\n", i+1, memo.URL, memo.Title, authorsStr)
}
if tooLarge {
content += "... and more"
}

if tooLarge {
content += "... and more"
content += "\n"
}

msg := &discordgo.MessageEmbed{
Expand Down
1 change: 1 addition & 0 deletions pkg/model/memos.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type MemoLog struct {
Description string `json:"description"`
PublishedAt *time.Time `json:"publishedAt"`
Reward decimal.Decimal `json:"reward"`
Category []string `json:"category"`
}

// MemoLogAuthor is the author of the memo log
Expand Down

0 comments on commit a621b5e

Please sign in to comment.