Merge pull request #101 from velour/ReminderListByUser

Listing Reminders "to" or "from" a user
This commit is contained in:
Chris Sexton 2018-02-05 16:15:25 -05:00 committed by GitHub
commit 13c00394f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 109 additions and 16 deletions

View File

@ -155,8 +155,18 @@ func (p *ReminderPlugin) Message(message msg.Message) bool {
return true
}
} else if len(parts) == 2 && strings.ToLower(parts[0]) == "list" && strings.ToLower(parts[1]) == "reminders" {
response, err := p.getAllRemindersFormatted(channel)
} else if len(parts) >= 2 && strings.ToLower(parts[0]) == "list" && strings.ToLower(parts[1]) == "reminders" {
var response string
var err error
if len(parts) == 2 {
response, err = p.getAllRemindersFormatted(channel)
} else if len(parts) == 4 {
if strings.ToLower(parts[2]) == "to" {
response, err = p.getAllRemindersToMeFormatted(channel, strings.ToLower(parts[3]))
} else if strings.ToLower(parts[2]) == "from" {
response, err = p.getAllRemindersFromMeFormatted(channel, strings.ToLower(parts[3]))
}
}
if err != nil {
p.Bot.SendMessage(channel, "listing failed.")
} else {
@ -262,10 +272,10 @@ func (p *ReminderPlugin) deleteReminder(id int64) error {
return err
}
func (p *ReminderPlugin) getAllRemindersFormatted(channel string) (string, error) {
func (p *ReminderPlugin) getRemindersFormatted(queryString string) (string, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
rows, err := p.db.Query("select id, fromWho, toWho, what, remindWhen from reminders order by remindWhen asc;")
rows, err := p.db.Query(queryString)
if err != nil {
log.Print(err)
return "", nil
@ -290,6 +300,18 @@ func (p *ReminderPlugin) getAllRemindersFormatted(channel string) (string, error
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;")
}
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))
}
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))
}
func (p *ReminderPlugin) queueUpNextReminder() {
nextReminder := p.getNextReminder()

View File

@ -27,6 +27,19 @@ func makeMessage(payload string) msg.Message {
}
}
func makeMessageBy(payload, by string) msg.Message {
isCmd := strings.HasPrefix(payload, "!")
if isCmd {
payload = payload[1:]
}
return msg.Message{
User: &user.User{Name: by},
Channel: "test",
Body: payload,
Command: isCmd,
}
}
func TestReminder(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
@ -90,6 +103,64 @@ func TestList(t *testing.T) {
assert.Contains(t, mb.Messages[2], "2) tester -> testuser :: don't fail this test 2 @ ")
}
func TestListBy(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 1", "testuser"))
assert.True(t, res)
res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2"))
assert.True(t, res)
res = c.Message(makeMessage("!list reminders from testuser"))
assert.True(t, res)
assert.Len(t, mb.Messages, 3)
assert.Contains(t, mb.Messages[2], "don't fail this test 1 @ ")
assert.NotContains(t, mb.Messages[2], "don't fail this test 2 @ ")
}
func TestListTo(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser"))
assert.True(t, res)
res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2"))
assert.True(t, res)
res = c.Message(makeMessage("!list reminders to testuser"))
assert.True(t, res)
assert.Len(t, mb.Messages, 3)
assert.NotContains(t, mb.Messages[2], "don't fail this test 1 @ ")
assert.Contains(t, mb.Messages[2], "don't fail this test 2 @ ")
}
func TestToEmptyList(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser"))
assert.True(t, res)
res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2"))
assert.True(t, res)
res = c.Message(makeMessage("!list reminders to test"))
assert.True(t, res)
assert.Len(t, mb.Messages, 3)
assert.Contains(t, mb.Messages[2], "no pending reminders")
}
func TestFromEmptyList(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser"))
assert.True(t, res)
res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2"))
assert.True(t, res)
res = c.Message(makeMessage("!list reminders from test"))
assert.True(t, res)
assert.Len(t, mb.Messages, 3)
assert.Contains(t, mb.Messages[2], "no pending reminders")
}
func TestBatch(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)