Skip to content

Commit

Permalink
Keep messages in feed me queue for at least a day
Browse files Browse the repository at this point in the history
  • Loading branch information
rcy committed Sep 27, 2023
1 parent c4e0bef commit 3a36012
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 15 deletions.
6 changes: 5 additions & 1 deletion handlers/feed_me.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import (
"goirc/model"
"goirc/model/notes"
"goirc/util"
"time"
)

func FeedMe(params bot.HandlerParams) error {
notes := []notes.Note{}

err := model.DB.Select(&notes, `select id, created_at, nick, text, kind from notes where nick = target order by random() limit 5`)
query := `select id, created_at, nick, text, kind from notes where created_at <= ? and nick = target order by random() limit 5`

t := time.Now().Add(-time.Hour * 24).UTC().Format("2006-01-02T15:04:05Z")
err := model.DB.Select(&notes, query, t)
if err != nil {
if err == sql.ErrNoRows {
return nil
Expand Down
86 changes: 72 additions & 14 deletions handlers/feed_me_test.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,109 @@
package handlers

import (
"fmt"
"goirc/bot"
"goirc/model"
"goirc/model/notes"
"testing"
"time"
)

func TestFeedMe(t *testing.T) {
for i, tc := range []struct {
messages []string
type message struct {
text string
createdAt time.Time
}

for _, tc := range []struct {
name string
messages []message
want int
}{
{
messages: []string{"abc", "def", "ghi", "jkl", "mno"},
want: 4,
name: "6 old messages",
messages: []message{
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
},
want: 5,
},
{
name: "5 old messages",
messages: []message{
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
},
want: 4,
},
{
messages: []string{"abc", "def", "ghi", "jkl"},
want: 4,
name: "4 old messages and 1 new message",
messages: []message{
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now()},
},
want: 5,
},
{
messages: []string{"abc", "def"},
want: 2,
name: "3 old messages and 2 new message",
messages: []message{
{"", time.Now()},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now()},
},
want: 5,
},
{
messages: []string{"abc"},
want: 1,
name: "2 old messages",
messages: []message{
{"", time.Now().Add(-time.Hour * 24)},
{"", time.Now().Add(-time.Hour * 24)},
},
want: 2,
},
{
messages: []string{},
name: "1 old messages",
messages: []message{
{"", time.Now().Add(-time.Hour * 24)},
},
want: 1,
},
{
name: "no messages",
messages: []message{},
want: 0,
},
} {
t.Run(fmt.Sprint(i), func(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
_, err := model.DB.Exec(`delete from notes`)
if err != nil {
t.Fatalf("error deleting notes %s", err)
}

for _, x := range tc.messages {
_, err := notes.Create(notes.CreateParams{Target: "nick", Nick: "nick", Kind: "link", Text: x})
query := `insert into notes(target, nick, text, kind, created_at) values(?, ?, ?, ?, ?)`
createdAt := x.createdAt.UTC().Format("2006-01-02T15:04:05Z")
_, err := model.DB.Exec(query, "nick", "nick", "link", x.text, createdAt)
if err != nil {
t.Fatalf("error creating note %s", err)
}
}
var notes []notes.Note
err = model.DB.Select(&notes, "select * from notes")
if err != nil {
panic(err)
}
err = FeedMe(bot.HandlerParams{
Privmsgf: dummyPrivmsgf,
Msg: "",
Expand Down

0 comments on commit 3a36012

Please sign in to comment.