Skip to content

Commit

Permalink
Fix notification expiry on Linux with URGENT, and also make sure to n…
Browse files Browse the repository at this point in the history
…ot flood users with too many notifications
  • Loading branch information
olabini committed Aug 23, 2016
1 parent 166b769 commit 2fa1ad6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
15 changes: 14 additions & 1 deletion gui/desktop_notifications_linux.go
Expand Up @@ -3,6 +3,7 @@ package gui
import (
"fmt"
"log"
"time"

"github.com/twstrike/coyim/Godeps/_workspace/src/github.com/TheCreeper/go-notify"
"github.com/twstrike/coyim/Godeps/_workspace/src/github.com/godbus/dbus"
Expand Down Expand Up @@ -47,9 +48,11 @@ func newDesktopNotifications() *desktopNotifications {
return dn
}

const defaultExpirationMs = 5000

func (dn *desktopNotifications) expiration() int32 {
if dn.notificationExpires {
return notify.ExpiresDefault
return defaultExpirationMs
}
return notify.ExpiresNever
}
Expand All @@ -74,7 +77,17 @@ func (dn *desktopNotifications) show(jid, from, message string) error {
if err != nil {
return fmt.Errorf("Error showing notification: %v", err)
}

if dn.notificationExpires {
go expireNotification(nid, defaultExpirationMs)
}

dn.notifications[jid] = nid
dn.notification = notification
return nil
}

func expireNotification(id uint32, expiry int) {
time.Sleep(time.Duration(expiry) * time.Millisecond)
notify.CloseNotification(id)
}
35 changes: 29 additions & 6 deletions gui/roster.go
Expand Up @@ -24,8 +24,9 @@ type roster struct {
isCollapsed map[string]bool
toCollapse []gtki.TreePath

ui *gtkUI
deNotify *desktopNotifications
ui *gtkUI
deNotify *desktopNotifications
actionTimes map[string]time.Time
}

const (
Expand All @@ -47,6 +48,7 @@ func (u *gtkUI) newRoster() *roster {

r := &roster{
isCollapsed: make(map[string]bool),
actionTimes: make(map[string]time.Time),
deNotify: newDesktopNotifications(),

ui: u,
Expand Down Expand Up @@ -340,6 +342,30 @@ func (r *roster) presenceUpdated(account *account, from, show, showStatus string
})
}

const mergeNotificationsThreshold = 7

func (r *roster) lastActionTimeFor(f string) time.Time {
return r.actionTimes[f]
}

func (r *roster) registerLastActionTimeFor(f string, t time.Time) {
r.actionTimes[f] = t
}

func (r *roster) maybeNotify(timestamp time.Time, jid, from, message string) {
if timestamp.Before(r.lastActionTimeFor(from).Add(time.Duration(mergeNotificationsThreshold) * time.Second)) {
fmt.Println("Decided to not show notification, since the time is not ready")
return
}

r.registerLastActionTimeFor(from, timestamp)

err := r.deNotify.show(from, jid, message)
if err != nil {
log.Println(err)
}
}

func (r *roster) messageReceived(account *account, from, resource string, timestamp time.Time, encrypted bool, message []byte) {
p, ok := r.ui.getPeer(account, from)
if ok {
Expand All @@ -355,10 +381,7 @@ func (r *roster) messageReceived(account *account, from, resource string, timest
conv.appendMessage(r.displayNameFor(account, from), timestamp, encrypted, ui.StripSomeHTML(message), false)

if !conv.isVisible() && r.deNotify != nil {
err := r.deNotify.show(from, r.displayNameFor(account, from), string(ui.StripSomeHTML(message)))
if err != nil {
log.Println(err)
}
r.maybeNotify(timestamp, from, r.displayNameFor(account, from), string(ui.StripSomeHTML(message)))
}
})
}
Expand Down

0 comments on commit 2fa1ad6

Please sign in to comment.