/
notifications.go
245 lines (214 loc) · 5.51 KB
/
notifications.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package prago
import (
"fmt"
"sync"
)
type notificationCenter struct {
app *App
mutex *sync.RWMutex
notificationMap map[string]*Notification
}
func (nc *notificationCenter) add(notification *Notification) {
nc.mutex.Lock()
defer nc.mutex.Unlock()
nc.notificationMap[notification.uuid] = notification
}
func (nc *notificationCenter) getFromUUID(uuid string) *Notification {
nc.mutex.RLock()
defer nc.mutex.RUnlock()
return nc.notificationMap[uuid]
}
func (nc *notificationCenter) getFromUser(userID int64) (ret []*notificationView) {
ret = []*notificationView{}
nc.mutex.RLock()
defer nc.mutex.RUnlock()
for _, v := range nc.notificationMap {
if v.userID > 0 && userID > 0 && v.userID == userID {
ret = append(ret, v.getView())
}
}
return
}
func (nc *notificationCenter) delete(uuid string) {
nc.mutex.Lock()
defer nc.mutex.Unlock()
delete(nc.notificationMap, uuid)
}
func (app *App) initNotifications() {
app.notificationCenter = ¬ificationCenter{
app: app,
mutex: &sync.RWMutex{},
notificationMap: make(map[string]*Notification),
}
app.API("notifications").Permission(everybodyPermission).Handler(func(request *Request) {
var notifications []*notificationView = []*notificationView{}
userID := request.UserID()
if userID > 0 {
notifications = app.notificationCenter.getFromUser(userID)
}
request.WriteJSON(200, notifications)
})
app.API("notifications").Method("POST").Permission(loggedPermission).Handler(func(request *Request) {
action := request.Param("action")
uuid := request.Param("uuid")
switch action {
case "delete":
app.notificationCenter.delete(uuid)
case "primary":
notification := app.notificationCenter.getFromUUID(uuid)
notification.primaryAction.fn()
case "secondary":
notification := app.notificationCenter.getFromUUID(uuid)
notification.secondaryAction.fn()
default:
panic("unknown action " + action)
}
})
}
// NotificationItem represents item for notification
type Notification struct {
uuid string
app *App
userID int64
preName string
image string
url string
name string
description string
primaryAction *notificationItemAction
secondaryAction *notificationItemAction
disableCancel bool
style string
progress *notificationProgress
}
type notificationProgress struct {
Human string
Percentage float64
}
// Notification creates notification
func (app *App) Notification(name string) *Notification {
return &Notification{
uuid: randomString(10),
app: app,
name: name,
}
}
// SetDescription sets description to notification item
func (n *Notification) SetDescription(description string) *Notification {
n.description = description
return n
}
// SetPreName sets prefix name to notification item
func (n *Notification) SetPreName(preName string) *Notification {
n.preName = preName
return n
}
func (n *Notification) SetImage(image string) *Notification {
n.image = image
return n
}
func (n *Notification) SetURL(url string) *Notification {
n.url = url
return n
}
// SetProgress sets description to notification item
func (n *Notification) SetProgress(progress *float64) *Notification {
if progress == nil {
n.progress = nil
} else {
if *progress < 0 {
n.progress = ¬ificationProgress{
Human: "",
Percentage: -1,
}
} else {
n.progress = ¬ificationProgress{
Human: notificationProgressHuman(*progress),
Percentage: *progress,
}
}
}
return n
}
func (n *Notification) SetPrimaryAction(name string, fn func()) *Notification {
n.primaryAction = ¬ificationItemAction{
name: name,
fn: fn,
}
return n
}
func (n *Notification) SetSecondaryAction(name string, fn func()) *Notification {
n.secondaryAction = ¬ificationItemAction{
name: name,
fn: fn,
}
return n
}
type notificationItemAction struct {
name string
fn func()
}
type notificationView struct {
UUID string
PreName string
Image string
URL string
Name string
Description string
PrimaryAction *string
SecondaryAction *string
DisableCancel bool
Style string
Progress *notificationProgress
}
func (n *Notification) getView() *notificationView {
var primaryAction, secondaryAction *string
if n.primaryAction != nil {
primaryAction = &n.primaryAction.name
}
if n.secondaryAction != nil {
secondaryAction = &n.secondaryAction.name
}
return ¬ificationView{
UUID: n.uuid,
PreName: n.preName,
Image: n.image,
URL: n.url,
Name: n.name,
Description: n.description,
PrimaryAction: primaryAction,
SecondaryAction: secondaryAction,
DisableCancel: n.disableCancel,
Style: n.style,
Progress: n.progress,
}
}
// SetTypeSuccess sets notification item type to success
func (n *Notification) SetStyleSuccess() *Notification {
n.style = "success"
return n
}
// SetTypeFail sets notification item type to fail
func (n *Notification) SetStyleFail() *Notification {
n.style = "fail"
return n
}
func (n *Notification) Push(userID int64) {
n.userID = userID
n.app.notificationCenter.add(n)
}
func (n *Notification) Flash(request *Request) error {
n.app.notificationCenter.add(n)
request.session.session.AddFlash(n.uuid)
request.session.dirty = true
return nil
}
func notificationProgressHuman(in float64) string {
if in <= 0 {
return ""
}
if in > 1 {
return ""
}
return fmt.Sprintf("%.2f %%", in*100)
}