Skip to content

Commit

Permalink
Only feed messages if there are at least 5 in the queue
Browse files Browse the repository at this point in the history
  • Loading branch information
rcy committed Sep 27, 2023
1 parent 3d47ef3 commit bdc723b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
12 changes: 9 additions & 3 deletions handlers/feed_me.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ import (
)

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

err := model.DB.Get(&note, `select id, created_at, nick, text, kind from notes where nick = target order by random() limit 1`)
err := model.DB.Select(&notes, `select id, created_at, nick, text, kind from notes where nick = target order by random() limit 5`)
if err != nil {
if err == sql.ErrNoRows {
params.Privmsgf(params.Target, "no")
return nil
}
return err
}

if len(notes) < 5 {
return nil
}

note := notes[0]

_, err = model.DB.Exec(`update notes set target = ? where id = ?`, params.Target, note.Id)
if err != nil {
return err
Expand Down
73 changes: 73 additions & 0 deletions handlers/feed_me_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package handlers

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

func TestFeedMe(t *testing.T) {
for i, tc := range []struct {
messages []string
want int
}{
{
messages: []string{"abc", "def", "ghi", "jkl", "mno"},
want: 4,
},
{
messages: []string{"abc", "def", "ghi", "jkl"},
want: 4,
},
{
messages: []string{"abc", "def"},
want: 2,
},
{
messages: []string{"abc"},
want: 1,
},
{
messages: []string{},
want: 0,
},
} {
t.Run(fmt.Sprint(i), 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})
if err != nil {
t.Fatalf("error creating note %s", err)
}
}
err = FeedMe(bot.HandlerParams{
Privmsgf: dummyPrivmsgf,
Msg: "",
Target: "",
Matches: []string{},
})
if err != nil {
t.Fatalf("error running FeedMe %s", err)
}

var count int
err = model.DB.Get(&count, `select count(*) from notes where nick = target`)
if err != nil {
t.Fatalf("error getting note count %s", err)
}
if count != tc.want {
t.Fatalf("want %d got %d", tc.want, count)
}
})
}
}

func dummyPrivmsgf(x string, y string, z ...interface{}) {
return
}

0 comments on commit bdc723b

Please sign in to comment.