| 1 |
/* |
| 2 |
Copyright 2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
// Package messaging wraps the messaging menu indicator, allowing for persistent |
| 18 |
// notifications to the user. |
| 19 |
package messaging |
| 20 |
|
| 21 |
import ( |
| 22 |
"encoding/json" |
| 23 |
"sync" |
| 24 |
"time" |
| 25 |
|
| 26 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/notifications" |
| 27 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 28 |
"gitlab.com/ubports/development/core/lomiri-push-service/click/cnotificationsettings" |
| 29 |
"gitlab.com/ubports/development/core/lomiri-push-service/launch_helper" |
| 30 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 31 |
"gitlab.com/ubports/development/core/lomiri-push-service/messaging/cmessaging" |
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/messaging/reply" |
| 33 |
) |
| 34 |
|
| 35 |
type MessagingMenu struct { |
| 36 |
Log logger.Logger |
| 37 |
Ch chan *reply.MMActionReply |
| 38 |
notifications map[string]*cmessaging.Payload // keep a ref to the Payload used in the MMU callback |
| 39 |
lock sync.RWMutex |
| 40 |
lastCleanupTime time.Time |
| 41 |
} |
| 42 |
|
| 43 |
type cleanUp func() |
| 44 |
|
| 45 |
// New returns a new MessagingMenu |
| 46 |
func New(log logger.Logger) *MessagingMenu { |
| 47 |
return &MessagingMenu{Log: log, Ch: make(chan *reply.MMActionReply), notifications: make(map[string]*cmessaging.Payload)} |
| 48 |
} |
| 49 |
|
| 50 |
var cAddNotification = cmessaging.AddNotification |
| 51 |
var cRemoveNotification = cmessaging.RemoveNotification |
| 52 |
var cNotificationExists = cmessaging.NotificationExists |
| 53 |
|
| 54 |
// GetCh returns the reply channel, exactly like mm.Ch. |
| 55 |
func (mmu *MessagingMenu) GetCh() chan *reply.MMActionReply { |
| 56 |
return mmu.Ch |
| 57 |
} |
| 58 |
|
| 59 |
func (mmu *MessagingMenu) addNotification(app *click.AppId, notificationId string, tag string, card *launch_helper.Card, actions []string, testingCleanUpFunction cleanUp) { |
| 60 |
mmu.lock.Lock() |
| 61 |
defer mmu.lock.Unlock() |
| 62 |
payload := &cmessaging.Payload{Ch: mmu.Ch, Actions: actions, App: app, Tag: tag} |
| 63 |
mmu.notifications[notificationId] = payload |
| 64 |
cAddNotification(app.DesktopId(), notificationId, card, payload) |
| 65 |
|
| 66 |
// Clean up our internal notifications store if it holds more than 20 messages (and apparently nobody ever calls Tags()) |
| 67 |
if len(mmu.notifications) > 20 && time.Since(mmu.lastCleanupTime).Minutes() > 10 { |
| 68 |
mmu.lastCleanupTime = time.Now() |
| 69 |
if testingCleanUpFunction == nil { |
| 70 |
go mmu.cleanUpNotifications() |
| 71 |
} else { |
| 72 |
testingCleanUpFunction() // Has to implement the asynchronous part itself |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func (mmu *MessagingMenu) RemoveNotification(notificationId string, fromUI bool) { |
| 79 |
mmu.lock.Lock() |
| 80 |
defer mmu.lock.Unlock() |
| 81 |
payload := mmu.notifications[notificationId] |
| 82 |
delete(mmu.notifications, notificationId) |
| 83 |
if payload != nil && payload.App != nil && fromUI { |
| 84 |
cRemoveNotification(payload.App.DesktopId(), notificationId) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func (mmu *MessagingMenu) cleanUpNotifications() { |
| 89 |
mmu.lock.Lock() |
| 90 |
defer mmu.lock.Unlock() |
| 91 |
mmu.doCleanUpNotifications() |
| 92 |
} |
| 93 |
|
| 94 |
// doCleanupNotifications removes notifications that were cleared from the messaging menu |
| 95 |
func (mmu *MessagingMenu) doCleanUpNotifications() { |
| 96 |
for nid, payload := range mmu.notifications { |
| 97 |
if !cNotificationExists(payload.App.DesktopId(), nid) { |
| 98 |
delete(mmu.notifications, nid) |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func (mmu *MessagingMenu) Tags(app *click.AppId) []string { |
| 104 |
orig := app.Original() |
| 105 |
tags := []string(nil) |
| 106 |
mmu.lock.Lock() |
| 107 |
defer mmu.lock.Unlock() |
| 108 |
mmu.lastCleanupTime = time.Now() |
| 109 |
mmu.doCleanUpNotifications() |
| 110 |
for _, payload := range mmu.notifications { |
| 111 |
if payload.App.Original() == orig { |
| 112 |
tags = append(tags, payload.Tag) |
| 113 |
} |
| 114 |
} |
| 115 |
return tags |
| 116 |
} |
| 117 |
|
| 118 |
func (mmu *MessagingMenu) Clear(app *click.AppId, tags ...string) int { |
| 119 |
orig := app.Original() |
| 120 |
var nids []string |
| 121 |
|
| 122 |
mmu.lock.RLock() |
| 123 |
// O(n×m). Should be small n and m though. |
| 124 |
for nid, payload := range mmu.notifications { |
| 125 |
if payload.App.Original() == orig { |
| 126 |
if len(tags) == 0 { |
| 127 |
nids = append(nids, nid) |
| 128 |
} else { |
| 129 |
for _, tag := range tags { |
| 130 |
if payload.Tag == tag { |
| 131 |
nids = append(nids, nid) |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
mmu.lock.RUnlock() |
| 138 |
|
| 139 |
for _, nid := range nids { |
| 140 |
mmu.RemoveNotification(nid, true) |
| 141 |
} |
| 142 |
mmu.cleanUpNotifications() |
| 143 |
|
| 144 |
return len(nids) |
| 145 |
} |
| 146 |
|
| 147 |
var canUseListNotify = cnotificationsettings.CanUseListNotify |
| 148 |
|
| 149 |
func (mmu *MessagingMenu) Present(app *click.AppId, nid string, notification *launch_helper.Notification) bool { |
| 150 |
if notification == nil { |
| 151 |
panic("please check notification is not nil before calling present") |
| 152 |
} |
| 153 |
|
| 154 |
if !canUseListNotify(app) { |
| 155 |
mmu.Log.Debugf("[%s] list notify disabled by user for this app.", nid) |
| 156 |
return false |
| 157 |
} |
| 158 |
|
| 159 |
card := notification.Card |
| 160 |
|
| 161 |
if card == nil || !card.Persist || card.Summary == "" { |
| 162 |
mmu.Log.Debugf("[%s] notification has no persistable card: %#v", nid, card) |
| 163 |
return false |
| 164 |
} |
| 165 |
|
| 166 |
actions := make([]string, 2*len(card.Actions)) |
| 167 |
for i, action := range card.Actions { |
| 168 |
act, err := json.Marshal(¬ifications.RawAction{ |
| 169 |
App: app, |
| 170 |
Nid: nid, |
| 171 |
ActionId: i, |
| 172 |
Action: action, |
| 173 |
}) |
| 174 |
if err != nil { |
| 175 |
mmu.Log.Errorf("failed to build action: %s", action) |
| 176 |
return false |
| 177 |
} |
| 178 |
actions[2*i] = string(act) |
| 179 |
actions[2*i+1] = action |
| 180 |
} |
| 181 |
|
| 182 |
mmu.Log.Debugf("[%s] creating notification centre entry for %s (summary: %s)", nid, app.Base(), card.Summary) |
| 183 |
|
| 184 |
mmu.addNotification(app, nid, notification.Tag, card, actions, nil) |
| 185 |
|
| 186 |
return true |
| 187 |
} |