Skip to content

Commit

Permalink
Merge pull request #43 from dwarvesf/fix/cmd-list-memo-with-time-period
Browse files Browse the repository at this point in the history
fix: cmd list memo with time period
  • Loading branch information
baenv authored Jun 20, 2024
2 parents 0f154aa + 70e3175 commit 43956a4
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 20 deletions.
8 changes: 6 additions & 2 deletions pkg/adapter/fortress/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package fortress

import "github.com/dwarvesf/fortress-discord/pkg/model"
import (
"time"

"github.com/dwarvesf/fortress-discord/pkg/model"
)

type FortressAdapter interface {
GetIcyWeeklyDistribution() (icys *model.AdapterIcy, err error)
Expand Down Expand Up @@ -60,5 +64,5 @@ type FortressAdapter interface {
GetBanks(id, bin, swiftCode string) (banks *model.AdapterBank, err error)

SyncMemoLogs() (memos *model.MemoLogsResponse, err error)
GetMemoLogs() (memos *model.MemoLogsResponse, err error)
GetMemoLogs(from, to *time.Time) (memos *model.MemoLogsResponse, err error)
}
17 changes: 15 additions & 2 deletions pkg/adapter/fortress/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/dwarvesf/fortress-discord/pkg/model"
)
Expand Down Expand Up @@ -38,8 +40,19 @@ func (f *Fortress) SyncMemoLogs() (memos *model.MemoLogsResponse, err error) {
return memos, nil
}

func (f *Fortress) GetMemoLogs() (memos *model.MemoLogsResponse, err error) {
req, err := f.makeReq("/api/v1/memos", http.MethodGet, nil)
func (f *Fortress) GetMemoLogs(from, to *time.Time) (memos *model.MemoLogsResponse, err error) {
params := url.Values{}
if from != nil {
params.Add("from", from.Format(time.RFC3339))
}
if to != nil {
params.Add("to", to.Format(time.RFC3339))
}

req, err := f.makeReq("/api/v1/memos?"+params.Encode(), http.MethodGet, nil)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/discord/command/memo/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (e *Memo) Help(message *model.DiscordMessage) error {
return nil
}

// DefaultCommand handles the default command
func (e *Memo) DefaultCommand(message *model.DiscordMessage) error {
return e.ListMemoLogs(message)
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/discord/command/memo/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package memo

import (
"github.com/dwarvesf/fortress-discord/pkg/discord/base"
"github.com/dwarvesf/fortress-discord/pkg/model"
)

type MemoCommander interface {
base.TextCommander

List(message *model.DiscordMessage) error
Sync(message *model.DiscordMessage) error
}
84 changes: 82 additions & 2 deletions pkg/discord/command/memo/memo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package memo

import (
"errors"
"regexp"
"strconv"
"strings"
"time"

"github.com/dwarvesf/fortress-discord/pkg/config"
"github.com/dwarvesf/fortress-discord/pkg/constant"
"github.com/dwarvesf/fortress-discord/pkg/discord/service"
Expand Down Expand Up @@ -39,15 +45,89 @@ func (e *Memo) List(message *model.DiscordMessage) error {
}

func (e *Memo) ListMemoLogs(message *model.DiscordMessage) error {
now := time.Now()
var (
from *time.Time
timeAmount int
timeUnit string
)

if len(message.ContentArgs) < 3 {
// Default to the last 7 days
tempFrom := now.AddDate(0, 0, -7)
from = &tempFrom
timeAmount = 7
timeUnit = "days"
} else if len(message.ContentArgs) >= 3 {
durationStr := strings.Join(message.ContentArgs[2:], " ")

var (
tempFrom *time.Time
err error
)
tempFrom, timeAmount, timeUnit, err = parseAndCalculateFromDate(now, durationStr)
if err != nil {
return err
}
from = tempFrom
} else {
return errors.New("invalid command format")
}

// 1. get data from service
data, err := e.svc.Memo().GetMemoLogs()
data, err := e.svc.Memo().GetMemoLogs(from, &now)
if err != nil {
e.L.Error(err, "can't get list of Memo")
return err
}

// 2. render
return e.view.Memo().ListMemoLogs(message, data)
return e.view.Memo().ListMemoLogs(message, data, timeAmount, timeUnit)
}

// parseAndCalculateFromDate parses the duration argument and calculates the 'from' date
func parseAndCalculateFromDate(now time.Time, arg string) (*time.Time, int, string, error) {
re := regexp.MustCompile(`(?i)^(\d+)\s*([a-z]+)$`)
matches := re.FindStringSubmatch(arg)
if len(matches) != 3 {
return nil, 0, "", errors.New("invalid duration argument format")
}

num, err := strconv.Atoi(matches[1])
if err != nil {
return nil, 0, "", errors.New("invalid number in duration argument")
}

unit := matches[2]
var from time.Time
switch strings.ToLower(unit) {
case "d", "day", "days":
from = now.AddDate(0, 0, -num)
if num > 1 {
return &from, num, "days", nil
}
return &from, num, "day", nil
case "w", "week", "weeks":
from = now.AddDate(0, 0, -7*num)
if num > 1 {
return &from, num, "weeks", nil
}
return &from, num, "week", nil
case "m", "month", "months":
from = now.AddDate(0, -num, 0)
if num > 1 {
return &from, num, "months", nil
}
return &from, num, "month", nil
case "y", "year", "years":
from = now.AddDate(-num, 0, 0)
if num > 1 {
return &from, num, "years", nil
}
return &from, num, "year", nil
default:
return nil, 0, "", errors.New("invalid time duration unit")
}
}

func (e *Memo) Sync(message *model.DiscordMessage) error {
Expand Down
8 changes: 6 additions & 2 deletions pkg/discord/service/memo/interface.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package memo

import "github.com/dwarvesf/fortress-discord/pkg/model"
import (
"time"

"github.com/dwarvesf/fortress-discord/pkg/model"
)

type MemoServicer interface {
GetMemos() ([]*model.Memo, error)
SyncMemoLogs() ([]model.MemoLog, error)
GetMemoLogs() ([]model.MemoLog, error)
GetMemoLogs(from, to *time.Time) ([]model.MemoLog, error)
}
6 changes: 4 additions & 2 deletions pkg/discord/service/memo/memo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package memo

import (
"time"

"github.com/dwarvesf/fortress-discord/pkg/adapter"
"github.com/dwarvesf/fortress-discord/pkg/logger"
"github.com/dwarvesf/fortress-discord/pkg/model"
Expand Down Expand Up @@ -46,9 +48,9 @@ func (e *Memo) SyncMemoLogs() ([]model.MemoLog, error) {

}

func (e *Memo) GetMemoLogs() ([]model.MemoLog, error) {
func (e *Memo) GetMemoLogs(from, to *time.Time) ([]model.MemoLog, error) {
// get response from fortress
adapterMemoLogs, err := e.adapter.Fortress().GetMemoLogs()
adapterMemoLogs, err := e.adapter.Fortress().GetMemoLogs(from, to)
if err != nil {
e.l.Error(err, "can't get memo logs from fortress")
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/discord/view/memo/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import (

type MemoViewer interface {
List(original *model.DiscordMessage, subs []*model.Memo) error
ListMemoLogs(original *model.DiscordMessage, subs []model.MemoLog) error
ListMemoLogs(original *model.DiscordMessage, subs []model.MemoLog, amount int, unit string) error
Sync(original *model.DiscordMessage, memos []model.MemoLog, channelID, reward string) error
}
30 changes: 25 additions & 5 deletions pkg/discord/view/memo/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,36 @@ func (v *Memo) List(original *model.DiscordMessage, memos []*model.Memo) error {
return base.SendEmbededMessage(v.ses, original, msg)
}

func (v *Memo) ListMemoLogs(original *model.DiscordMessage, memos []model.MemoLog) error {
func (v *Memo) ListMemoLogs(original *model.DiscordMessage, memos []model.MemoLog, timeAmount int, timeUnit string) error {
var content string
for i := range memos {
if i <= 10 {
content += fmt.Sprintf("%d ・ [%s](%s)\n", i+1, memos[i].Title, memos[i].URL)

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

// 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))
}

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)
}

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

msg := &discordgo.MessageEmbed{
Title: "<:pepeyes:885513213431648266> Latest Memos <:pepeyes:885513213431648266> \n",
Title: fmt.Sprintf("<:pepeyes:885513213431648266> Last %d %s Memos <:pepeyes:885513213431648266> \n", timeAmount, timeUnit),
Description: content,
}

Expand Down

0 comments on commit 43956a4

Please sign in to comment.