-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
notifications.go
201 lines (164 loc) · 5.03 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
package utils
import (
"net/http"
"encoding/json"
"time"
"fmt"
"strings"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/bson"
)
type NotificationActions struct {
Text string
Link string
}
type Notification struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Title string
Message string
Icon string
Link string
Date time.Time
Level string
Read bool
Recipient string
Actions []NotificationActions
}
func NotifGet(w http.ResponseWriter, req *http.Request) {
_from := req.URL.Query().Get("from")
from, _ := primitive.ObjectIDFromHex(_from)
if LoggedInOnly(w, req) != nil {
return
}
nickname := req.Header.Get("x-cosmos-user")
if(req.Method == "GET") {
c, errCo := GetCollection(GetRootAppId(), "notifications")
if errCo != nil {
Error("Database Connect", errCo)
HTTPError(w, "Database", http.StatusInternalServerError, "DB001")
return
}
Debug("Notifications: Get notif for " + nickname)
notifications := []Notification{}
reqdb := map[string]interface{}{
"Recipient": nickname,
}
if from != primitive.NilObjectID {
reqdb = map[string]interface{}{
// nickname or role
"Recipient": nickname,
// get notif before from
"_id": map[string]interface{}{
"$lt": from,
},
}
}
limit := int64(20)
cursor, err := c.Find(nil, reqdb, &options.FindOptions{
Sort: map[string]interface{}{
"Date": -1,
},
Limit: &limit,
})
if err != nil {
Error("Notifications: Error while getting notifications", err)
HTTPError(w, "notifications Get Error", http.StatusInternalServerError, "UD001")
return
}
defer cursor.Close(nil)
if err = cursor.All(nil, ¬ifications); err != nil {
Error("Notifications: Error while decoding notifications", err)
HTTPError(w, "notifications Get Error", http.StatusInternalServerError, "UD002")
return
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
"data": notifications,
})
} else {
Error("Notifications: Method not allowed" + req.Method, nil)
HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}
func MarkAsRead(w http.ResponseWriter, req *http.Request) {
if(req.Method == "GET") {
if LoggedInOnly(w, req) != nil {
return
}
notificationIDs := []primitive.ObjectID{}
nickname := req.Header.Get("x-cosmos-user")
notificationIDsRawRunes := req.URL.Query().Get("ids")
notificationIDsRaw := strings.Split(notificationIDsRawRunes, ",")
Debug(fmt.Sprintf("Marking %v notifications as read",notificationIDsRaw))
for _, notificationIDRaw := range notificationIDsRaw {
notificationID, err := primitive.ObjectIDFromHex(notificationIDRaw)
if err != nil {
HTTPError(w, "Invalid notification ID " + notificationIDRaw, http.StatusBadRequest, "InvalidID")
return
}
notificationIDs = append(notificationIDs, notificationID)
}
c, errCo := GetCollection(GetRootAppId(), "notifications")
if errCo != nil {
Error("Database Connect", errCo)
HTTPError(w, "Database connection error", http.StatusInternalServerError, "DB001")
return
}
filter := bson.M{"_id": bson.M{"$in": notificationIDs}, "Recipient": nickname}
update := bson.M{"$set": bson.M{"Read": true}}
result, err := c.UpdateMany(nil, filter, update)
if err != nil {
Error("Notifications: Error while marking notification as read", err)
HTTPError(w, "Error updating notification", http.StatusInternalServerError, "UpdateError")
return
}
if result.MatchedCount == 0 {
HTTPError(w, "No matching notification found", http.StatusNotFound, "NotFound")
return
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
"message": "Notification marked as read",
})
} else {
Error("Notifications: Method not allowed" + req.Method, nil)
HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}
func WriteNotification(notification Notification) {
notification.Date = time.Now()
notification.Read = false
if notification.Recipient == "all" || notification.Recipient == "admin" || notification.Recipient == "user" {
// list all users
users := ListAllUsers(notification.Recipient)
Debug("Notifications: Sending notification to " + string(len(users)) + " users")
for _, user := range users {
BufferedDBWrite("notifications", map[string]interface{}{
"Title": notification.Title,
"Message": notification.Message,
"Icon": notification.Icon,
"Link": notification.Link,
"Date": notification.Date,
"Level": notification.Level,
"Read": notification.Read,
"Recipient": user.Nickname,
"Actions": notification.Actions,
})
}
} else {
BufferedDBWrite("notifications", map[string]interface{}{
"Title": notification.Title,
"Message": notification.Message,
"Icon": notification.Icon,
"Link": notification.Link,
"Date": notification.Date,
"Level": notification.Level,
"Read": notification.Read,
"Recipient": notification.Recipient,
"Actions": notification.Actions,
})
}
}