Skip to content

Commit

Permalink
Add pending, unsend and cancel commands
Browse files Browse the repository at this point in the history
  • Loading branch information
godwhoa committed Apr 12, 2019
1 parent 26482d4 commit e2bac53
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
4 changes: 2 additions & 2 deletions plugins/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ func Register(deps *oodle.Deps) error {
go remindin.Watch()
seen := &Seen{db: sqlx.NewDb(db, "sqlite3")}
seen.Migrate()
tellCmd, tellTrig := Tell(irc, db)
tellCmds, tellTrig := Tell(irc, db)
bot.RegisterTriggers(
CustomCommands(irc),
ExecCommands(irc),
TitleScraper(irc),
seen.Trigger(),
tellTrig,
)
bot.RegisterCommands(tellCmds...)
bot.RegisterCommands(
Echo(),
Version(),
Expand All @@ -53,7 +54,6 @@ func Register(deps *oodle.Deps) error {
Help(bot),
remindin.Command(),
seen.Command(),
tellCmd,
)

return nil
Expand Down
78 changes: 72 additions & 6 deletions plugins/core/tell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package core

import (
"database/sql"
"fmt"
"strconv"
"strings"
"time"

Expand All @@ -11,10 +13,10 @@ import (
)

// Tell lets users send a msg. to an inactive user
func Tell(irc oodle.IRCClient, db *sql.DB) (oodle.Command, oodle.Trigger) {
func Tell(irc oodle.IRCClient, db *sql.DB) ([]oodle.Command, oodle.Trigger) {
store := NewMailBox(db)

cmd := oodle.Command{
tell := oodle.Command{
Prefix: ".",
Name: "tell",
Description: "Lets you send a msg. to an inactive user. It will notify them once they are active.",
Expand All @@ -32,22 +34,86 @@ func Tell(irc oodle.IRCClient, db *sql.DB) (oodle.Command, oodle.Trigger) {
return "okie dokie!", nil
},
}
unsend := oodle.Command{
Prefix: ".",
Name: "unsend",
Description: "Cancels the last .tell message from you which is still pending.",
Usage: `.unsend`,
Fn: func(nick string, args []string) (string, error) {
letters := store.LettersFrom(nick)
if len(letters) < 1 {
return "No pending msgs. to cancel.", nil
}
store.Delete(letters[0].ID)
return fmt.Sprintf("Your msg. to %s cancelled.", letters[0].To), nil
},
}

pending := oodle.Command{
Prefix: ".",
Name: "pending",
Description: "Shows pending .tell messages sent by you.",
Usage: `.pending`,
Fn: func(nick string, args []string) (string, error) {
letters := store.LettersFrom(nick)
if len(letters) < 1 {
return "No pending messages.", nil
}
output := ""
for _, letter := range letters {
limit := u.Min(20, len(letter.Body))
output += fmt.Sprintf("ID: %d To: %s Msg: %s...\n", letter.ID, letter.To, letter.Body[0:limit])
}
return strings.TrimSuffix(output, "\n"), nil
},
}

cancel := oodle.Command{
Prefix: ".",
Name: "cancel",
Description: "Cancels the last .tell msg from you which is still pending.",
Usage: `.cancel <id>`,
Fn: func(nick string, args []string) (string, error) {
letters := store.LettersFrom(nick)
if len(letters) < 1 {
return "No pending msgs. to cancel.", nil
}

id, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return "Invalid id", nil
}

letter, err := store.Get(uint(id))
if err != nil {
return "No msg. with id " + args[0], nil
}

if letter.From != nick {
return "No msg. with id " + args[0] + " belonging to you.", nil
}
store.Delete(uint(id))
return fmt.Sprintf("Your msg. to %s cancelled.", letters[0].To), nil
},
}

notify := func(nick string) {
if !store.HasMail(nick) {
return
}
letters := store.Letters(nick)
letters := store.LettersTo(nick)
for _, l := range letters {
irc.Sendf("%s, %s left this message for you: %s\n%s ago", nick, l.From, l.Body, u.FmtTime(l.When))
}
store.Delete(letters)
store.BatchDelete(letters)
}
trigger := func(event interface{}) {
switch event.(type) {
case oodle.Message:
notify(event.(oodle.Message).Nick)
}
}
cmd = m.Chain(cmd, m.MinArg(2))
return cmd, trigger
tell = m.Chain(tell, m.MinArg(2))
cancel = m.Chain(cancel, m.MinArg(1))
return []oodle.Command{tell, cancel, unsend, pending}, trigger
}
20 changes: 18 additions & 2 deletions plugins/core/tell_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,36 @@ func (t *MailBox) Send(l Letter) error {
return err
}

func (t *MailBox) Letters(to string) []*Letter {
func (t *MailBox) Get(id uint) (*Letter, error) {
letter := &Letter{}
err := t.db.Get(letter, `SELECT * FROM letters WHERE "id" = ? AND "deleted_at" IS NULL;`, id)
return letter, err
}

func (t *MailBox) LettersTo(to string) []*Letter {
letters := []*Letter{}
t.db.Select(&letters, `SELECT * FROM letters WHERE "to" = ? AND "deleted_at" IS NULL;`, to)
return letters
}

func (t *MailBox) Delete(letters []*Letter) {
func (t *MailBox) LettersFrom(from string) []*Letter {
letters := []*Letter{}
t.db.Select(&letters, `SELECT * FROM letters WHERE "from" = ? AND "deleted_at" IS NULL ORDER BY "when" DESC;`, from)
return letters
}

func (t *MailBox) BatchDelete(letters []*Letter) {
tx := t.db.MustBegin()
for _, letter := range letters {
tx.Exec(`UPDATE letters SET "deleted_at" = ? WHERE id = ?;`, time.Now(), letter.ID)
}
tx.Commit()
}

func (t *MailBox) Delete(id uint) {
t.db.Exec(`UPDATE letters SET "deleted_at" = ? WHERE id = ?;`, time.Now(), id)
}

func (t *MailBox) HasMail(to string) bool {
exists := false
stmt := `SELECT EXISTS(SELECT * FROM letters WHERE "to" = ? AND "deleted_at" IS NULL);`
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"gopkg.in/neurosnap/sentences.v1/english"
)

func min(x, y int) int {
func Min(x, y int) int {
if x < y {
return x
}
Expand Down

0 comments on commit e2bac53

Please sign in to comment.