diff --git a/plugins/reminder/reminder.go b/plugins/reminder/reminder.go index f1ec707..4e00e9c 100644 --- a/plugins/reminder/reminder.go +++ b/plugins/reminder/reminder.go @@ -121,8 +121,8 @@ func (p *ReminderPlugin) Message(message msg.Message) bool { endTime := time.Now().UTC().Add(dur2) what := strings.Join(parts[6:], " ") + max := p.config.GetInt("Reminder.MaxBatchAdd", 10) for i := 0; when.Before(endTime); i++ { - max := p.config.GetInt("Reminder.MaxBatchAdd", 10) if i >= max { p.Bot.SendMessage(channel, "Easy cowboy, that's a lot of reminders. I'll add some of them.") doConfirm = false @@ -272,9 +272,25 @@ func (p *ReminderPlugin) deleteReminder(id int64) error { return err } -func (p *ReminderPlugin) getRemindersFormatted(queryString string) (string, error) { +func (p *ReminderPlugin) getRemindersFormatted(filter string) (string, error) { + max := p.config.GetInt("Reminder.MaxList", 25) + queryString := fmt.Sprintf("select id, fromWho, toWho, what, remindWhen from reminders %s order by remindWhen asc limit %d;", filter, max) + countString := fmt.Sprintf("select COUNT(*) from reminders %s;", filter) + p.mutex.Lock() defer p.mutex.Unlock() + + var total int + err := p.db.Get(&total, countString) + if err != nil { + log.Print(err) + return "", nil + } + + if total == 0 { + return "no pending reminders", nil + } + rows, err := p.db.Query(queryString) if err != nil { log.Print(err) @@ -293,23 +309,25 @@ func (p *ReminderPlugin) getRemindersFormatted(queryString string) (string, erro reminders += fmt.Sprintf("%d) %s -> %s :: %s @ %s (%d)\n", counter, reminder.from, reminder.who, reminder.what, when, reminder.id) counter++ } - if counter == 1 { - return "no pending reminders", nil + + remaining := total - max + if remaining > 0 { + reminders += fmt.Sprintf("...%d more...\n", remaining) } return reminders, nil } func (p *ReminderPlugin) getAllRemindersFormatted(channel string) (string, error) { - return p.getRemindersFormatted("select id, fromWho, toWho, what, remindWhen from reminders order by remindWhen asc;") + return p.getRemindersFormatted("") } func (p *ReminderPlugin) getAllRemindersFromMeFormatted(channel, me string) (string, error) { - return p.getRemindersFormatted(fmt.Sprintf("select id, fromWho, toWho, what, remindWhen from reminders where fromWho = '%s' order by remindWhen asc;", me)) + return p.getRemindersFormatted(fmt.Sprintf("where fromWho = '%s'", me)) } func (p *ReminderPlugin) getAllRemindersToMeFormatted(channel, me string) (string, error) { - return p.getRemindersFormatted(fmt.Sprintf("select id, fromWho, toWho, what, remindWhen from reminders where toWho = '%s' order by remindWhen asc;", me)) + return p.getRemindersFormatted(fmt.Sprintf("where toWho = '%s'", me)) } func (p *ReminderPlugin) queueUpNextReminder() { diff --git a/plugins/reminder/reminder_test.go b/plugins/reminder/reminder_test.go index d2cc838..c9095d9 100644 --- a/plugins/reminder/reminder_test.go +++ b/plugins/reminder/reminder_test.go @@ -201,6 +201,34 @@ func TestCancelMiss(t *testing.T) { assert.Contains(t, mb.Messages[0], "failed to find and cancel reminder: 1") } +func TestLimitList(t *testing.T) { + c, mb := setup(t) + c.config.Set("Reminder.MaxBatchAdd", "10") + c.config.Set("Reminder.MaxList", "25") + assert.NotNil(t, c) + + //Someone can redo this with a single batch add, but I can't locally due to an old version of sqllite (maybe). + res := c.Message(makeMessage("!remind testuser every 1h for 10h don't fail this test")) + assert.True(t, res) + res = c.Message(makeMessage("!remind testuser every 1h for 10h don't fail this test")) + assert.True(t, res) + res = c.Message(makeMessage("!remind testuser every 1h for 10h don't fail this test")) + assert.True(t, res) + res = c.Message(makeMessage("!list reminders")) + assert.True(t, res) + assert.Len(t, mb.Messages, 4) + assert.Contains(t, mb.Messages[0], "Sure tester, I'll remind testuser.") + assert.Contains(t, mb.Messages[1], "Sure tester, I'll remind testuser.") + assert.Contains(t, mb.Messages[2], "Sure tester, I'll remind testuser.") + + for i := 0; i < 25; i++ { + assert.Contains(t, mb.Messages[3], fmt.Sprintf("%d) tester -> testuser :: don't fail this test", i+1)) + } + assert.Contains(t, mb.Messages[3], "...5 more...") + + assert.NotContains(t, mb.Messages[3], "26) tester -> testuser") +} + func TestHelp(t *testing.T) { c, mb := setup(t) assert.NotNil(t, c)