Merge pull request #139 from velour/ReminderMaxList

Limit the listing pain caused by inflicting reminder pain on others
This commit is contained in:
Scott Kiesel 2019-01-27 16:23:46 -05:00 committed by GitHub
commit 181f243b39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 7 deletions

View File

@ -121,8 +121,8 @@ func (p *ReminderPlugin) Message(message msg.Message) bool {
endTime := time.Now().UTC().Add(dur2) endTime := time.Now().UTC().Add(dur2)
what := strings.Join(parts[6:], " ") what := strings.Join(parts[6:], " ")
max := p.config.GetInt("Reminder.MaxBatchAdd", 10)
for i := 0; when.Before(endTime); i++ { for i := 0; when.Before(endTime); i++ {
max := p.config.GetInt("Reminder.MaxBatchAdd", 10)
if i >= max { if i >= max {
p.Bot.SendMessage(channel, "Easy cowboy, that's a lot of reminders. I'll add some of them.") p.Bot.SendMessage(channel, "Easy cowboy, that's a lot of reminders. I'll add some of them.")
doConfirm = false doConfirm = false
@ -272,9 +272,25 @@ func (p *ReminderPlugin) deleteReminder(id int64) error {
return err 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() p.mutex.Lock()
defer p.mutex.Unlock() 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) rows, err := p.db.Query(queryString)
if err != nil { if err != nil {
log.Print(err) 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) reminders += fmt.Sprintf("%d) %s -> %s :: %s @ %s (%d)\n", counter, reminder.from, reminder.who, reminder.what, when, reminder.id)
counter++ 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 return reminders, nil
} }
func (p *ReminderPlugin) getAllRemindersFormatted(channel string) (string, error) { 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) { 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) { 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() { func (p *ReminderPlugin) queueUpNextReminder() {

View File

@ -201,6 +201,34 @@ func TestCancelMiss(t *testing.T) {
assert.Contains(t, mb.Messages[0], "failed to find and cancel reminder: 1") 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) { func TestHelp(t *testing.T) {
c, mb := setup(t) c, mb := setup(t)
assert.NotNil(t, c) assert.NotNil(t, c)