Skip to content

Commit

Permalink
Merge pull request #84 from arborchat/notify-policy
Browse files Browse the repository at this point in the history
Restructure notification logic to be more configurable
  • Loading branch information
whereswaldon authored Jan 26, 2019
2 parents 10ffa67 + f27be49 commit 611790b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
6 changes: 4 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func TCPDial(address string) (io.ReadWriteCloser, error) {
type NetClient struct {
*archive.Manager
Composer
*Notifier
address string

arbor.ReadWriteCloser
connectFunc Connector
disconnectHandler func(types.Connection)
Expand Down Expand Up @@ -183,8 +185,8 @@ func (nc *NetClient) handleMessage(m *arbor.ProtocolMessage) {
if !nc.Archive.Has(m.UUID) {
if nc.receiveHandler != nil {
nc.receiveHandler(m.ChatMessage)
// ask notificationEngine to display the message
notificationEngine(nc, m.ChatMessage)
// ask Notifier to handle the message
nc.Notifier.Handle(nc, m.ChatMessage)
}
if m.Parent != "" && !nc.Archive.Has(m.Parent) {
nc.Query(m.Parent)
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func main() {
log.Println("Error creating client", err)
return
}
// configure notification logic to send desktop notification on all recent messages
notifier := &Notifier{ShouldNotify: Recent}
client.Notifier = notifier
ui, err = tui.NewTUI(client)
if err != nil {
log.Fatal("Error creating TUI", err)
Expand Down
23 changes: 19 additions & 4 deletions notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@ import (
"github.com/gen2brain/beeep"
)

// This method makes notifications and handles all notification logic
func notificationEngine(cli *NetClient, msg *arbor.ChatMessage) {
// Notifier manages sending notifications about new messages
type Notifier struct {
// a function to decide whether to send notifications
ShouldNotify func(*NetClient, *arbor.ChatMessage) bool
}

// Handle processes a message and sends any notifications based on the
// current notification policy.
func (n *Notifier) Handle(cli *NetClient, msg *arbor.ChatMessage) {
if n.ShouldNotify(cli, msg) {
beeep.Notify("Muscadine", msg.Username+": "+msg.Content, "")
}
}

// Recent sends a notification for every incoming message within the recent
// past that wasn't authored by the current user.
func Recent(cli *NetClient, msg *arbor.ChatMessage) bool {
// is the message new?
if msg.Timestamp > (time.Now().Unix() - int64(5)) {
// do not reply to self
if cli.username != msg.Username {
toSend := msg.Username + ": " + msg.Content
beeep.Notify("Muscadine", toSend, "")
return true
}
}
return false
}

0 comments on commit 611790b

Please sign in to comment.