-
Notifications
You must be signed in to change notification settings - Fork 13
/
notification.go
109 lines (90 loc) · 3.09 KB
/
notification.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
package notification
import (
"path"
"github.com/qor/admin"
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/qor/utils"
"github.com/qor/roles"
)
type Notification struct {
Config *Config
Channels []ChannelInterface
Actions []*Action
}
func New(config *Config) *Notification {
notification := &Notification{Config: config}
return notification
}
func (notification *Notification) Send(message *Message, context *qor.Context) error {
for _, channel := range notification.Channels {
if err := channel.Send(message, context); err != nil {
return err
}
}
return nil
}
type NotificationsResult struct {
Notification *Notification
Notifications []*QorNotification
Resolved []*QorNotification
}
func (notification *Notification) GetNotifications(user interface{}, context *qor.Context) *NotificationsResult {
var results = NotificationsResult{
Notification: notification,
}
for _, channel := range notification.Channels {
channel.GetNotifications(user, &results, notification, context)
}
return &results
}
func (notification *Notification) GetUnresolvedNotificationsCount(user interface{}, context *qor.Context) uint {
var result uint
for _, channel := range notification.Channels {
result += channel.GetUnresolvedNotificationsCount(user, notification, context)
}
return result
}
func (notification *Notification) GetNotification(user interface{}, messageID string, context *qor.Context) *QorNotification {
for _, channel := range notification.Channels {
if message, err := channel.GetNotification(user, messageID, notification, context); err == nil {
return message
}
}
return nil
}
func (notification *Notification) ConfigureQorResource(res resource.Resourcer) {
if res, ok := res.(*admin.Resource); ok {
Admin := res.GetAdmin()
Admin.RegisterViewPath("github.com/qor/notification/views")
if len(notification.Channels) == 0 {
utils.ExitWithMsg("No channel defined for notification")
}
Admin.RegisterFuncMap("unresolved_notifications_count", func(context *admin.Context) uint {
return notification.GetUnresolvedNotificationsCount(context.CurrentUser, context.Context)
})
router := Admin.GetRouter()
notificationController := controller{Notification: notification}
router.Get("!notifications", notificationController.List, &admin.RouteConfig{
PermissionMode: roles.Read,
Resource: res,
})
for _, action := range notification.Actions {
actionController := controller{Notification: notification, action: action}
router.Get(path.Join("!notifications", res.ParamIDName(), action.ToParam()), actionController.Action, &admin.RouteConfig{
PermissionMode: roles.Update,
Resource: res,
})
router.Put(path.Join("!notifications", res.ParamIDName(), action.ToParam()), actionController.Action, &admin.RouteConfig{
PermissionMode: roles.Update,
Resource: res,
})
if action.Undo != nil {
router.Put(path.Join("!notifications", res.ParamIDName(), action.ToParam(), "undo"), actionController.UndoAction, &admin.RouteConfig{
PermissionMode: roles.Update,
Resource: res,
})
}
}
}
}