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

@ -22,11 +22,11 @@ const (
) )
type ReminderPlugin struct { type ReminderPlugin struct {
Bot bot.Bot Bot bot.Bot
db *sqlx.DB db *sqlx.DB
mutex *sync.Mutex mutex *sync.Mutex
timer *time.Timer timer *time.Timer
config *config.Config config *config.Config
} }
type Reminder struct { type Reminder struct {
@ -58,11 +58,11 @@ func New(bot bot.Bot) *ReminderPlugin {
timer.Stop() timer.Stop()
plugin := &ReminderPlugin{ plugin := &ReminderPlugin{
Bot: bot, Bot: bot,
db: bot.DB(), db: bot.DB(),
mutex: &sync.Mutex{}, mutex: &sync.Mutex{},
timer: timer, timer: timer,
config: bot.Config(), config: bot.Config(),
} }
plugin.queueUpNextReminder() plugin.queueUpNextReminder()
@ -155,8 +155,18 @@ func (p *ReminderPlugin) Message(message msg.Message) bool {
return true return true
} }
} else if len(parts) == 2 && strings.ToLower(parts[0]) == "list" && strings.ToLower(parts[1]) == "reminders" { } else if len(parts) >= 2 && strings.ToLower(parts[0]) == "list" && strings.ToLower(parts[1]) == "reminders" {
response, err := p.getAllRemindersFormatted(channel) 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 { if err != nil {
p.Bot.SendMessage(channel, "listing failed.") p.Bot.SendMessage(channel, "listing failed.")
} else { } else {
@ -238,7 +248,7 @@ func (p *ReminderPlugin) addReminder(reminder *Reminder) error {
p.mutex.Lock() p.mutex.Lock()
defer p.mutex.Unlock() defer p.mutex.Unlock()
_, err := p.db.Exec(`insert into reminders (fromWho, toWho, what, remindWhen, channel) values (?, ?, ?, ?, ?);`, _, err := p.db.Exec(`insert into reminders (fromWho, toWho, what, remindWhen, channel) values (?, ?, ?, ?, ?);`,
reminder.from, reminder.who, reminder.what, reminder.when.Format(TIMESTAMP), reminder.channel) reminder.from, reminder.who, reminder.what, reminder.when.Format(TIMESTAMP), reminder.channel)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
@ -262,10 +272,10 @@ func (p *ReminderPlugin) deleteReminder(id int64) error {
return err return err
} }
func (p *ReminderPlugin) getAllRemindersFormatted(channel string) (string, error) { func (p *ReminderPlugin) getRemindersFormatted(queryString string) (string, error) {
p.mutex.Lock() p.mutex.Lock()
defer p.mutex.Unlock() 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 { if err != nil {
log.Print(err) log.Print(err)
return "", nil return "", nil
@ -290,6 +300,18 @@ func (p *ReminderPlugin) getAllRemindersFormatted(channel string) (string, error
return reminders, nil 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() { func (p *ReminderPlugin) queueUpNextReminder() {
nextReminder := p.getNextReminder() nextReminder := p.getNextReminder()
@ -312,7 +334,7 @@ func reminderer(p *ReminderPlugin) {
message := fmt.Sprintf("Hey %s, %s wanted you to be reminded: %s", reminder.who, reminder.from, reminder.what) message := fmt.Sprintf("Hey %s, %s wanted you to be reminded: %s", reminder.who, reminder.from, reminder.what)
p.Bot.SendMessage(reminder.channel, message) p.Bot.SendMessage(reminder.channel, message)
if err:= p.deleteReminder(reminder.id); err != nil { if err := p.deleteReminder(reminder.id); err != nil {
log.Print(reminder.id) log.Print(reminder.id)
log.Print(err) log.Print(err)
log.Fatal("this will cause problems, we need to stop now.") log.Fatal("this will cause problems, we need to stop now.")

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) { func TestReminder(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
c := New(mb) 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 @ ") 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) { func TestBatch(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
c := New(mb) c := New(mb)