@@ -20,12 +20,20 @@ _private.fetchAllNotifications = () => {
20
20
return allNotifications ;
21
21
} ;
22
22
23
+ _private . wasSeen = ( notification , user ) => {
24
+ if ( notification . seenBy === undefined ) {
25
+ return notification . seen ;
26
+ } else {
27
+ return notification . seenBy . includes ( user . id ) ;
28
+ }
29
+ } ;
30
+
23
31
module . exports = {
24
32
docName : 'notifications' ,
25
33
26
34
browse : {
27
35
permissions : true ,
28
- query ( ) {
36
+ query ( frame ) {
29
37
let allNotifications = _private . fetchAllNotifications ( ) ;
30
38
allNotifications = _ . orderBy ( allNotifications , 'addedAt' , 'desc' ) ;
31
39
@@ -55,7 +63,7 @@ module.exports = {
55
63
}
56
64
}
57
65
58
- return notification . seen !== true ;
66
+ return ! _private . wasSeen ( notification , frame . user ) ;
59
67
} ) ;
60
68
61
69
return allNotifications ;
@@ -85,7 +93,7 @@ module.exports = {
85
93
} ;
86
94
87
95
let notificationsToCheck = frame . data . notifications ;
88
- let addedNotifications = [ ] ;
96
+ let notificationsToAdd = [ ] ;
89
97
90
98
const allNotifications = _private . fetchAllNotifications ( ) ;
91
99
@@ -95,7 +103,7 @@ module.exports = {
95
103
} ) ;
96
104
97
105
if ( ! isDuplicate ) {
98
- addedNotifications . push ( Object . assign ( { } , defaults , notification , overrides ) ) ;
106
+ notificationsToAdd . push ( Object . assign ( { } , defaults , notification , overrides ) ) ;
99
107
}
100
108
} ) ;
101
109
@@ -111,29 +119,30 @@ module.exports = {
111
119
}
112
120
113
121
// CASE: nothing to add, skip
114
- if ( ! addedNotifications . length ) {
122
+ if ( ! notificationsToAdd . length ) {
115
123
return Promise . resolve ( ) ;
116
124
}
117
125
118
- const addedReleaseNotifications = addedNotifications . filter ( ( notification ) => {
126
+ const releaseNotificationsToAdd = notificationsToAdd . filter ( ( notification ) => {
119
127
return ! notification . custom ;
120
128
} ) ;
121
129
122
- // CASE: only latest release notification
123
- if ( addedReleaseNotifications . length > 1 ) {
124
- addedNotifications = addedNotifications . filter ( ( notification ) => {
130
+ // CASE: reorder notifications before save
131
+ if ( releaseNotificationsToAdd . length > 1 ) {
132
+ notificationsToAdd = notificationsToAdd . filter ( ( notification ) => {
125
133
return notification . custom ;
126
134
} ) ;
127
- addedNotifications . push ( _ . orderBy ( addedReleaseNotifications , 'created_at' , 'desc' ) [ 0 ] ) ;
135
+ notificationsToAdd . push ( _ . orderBy ( releaseNotificationsToAdd , 'created_at' , 'desc' ) [ 0 ] ) ;
128
136
}
129
137
130
138
return api . settings . edit ( {
131
139
settings : [ {
132
140
key : 'notifications' ,
133
- value : allNotifications . concat ( addedNotifications )
141
+ // @NOTE : We always need to store all notifications!
142
+ value : allNotifications . concat ( notificationsToAdd )
134
143
} ]
135
144
} , internalContext ) . then ( ( ) => {
136
- return addedNotifications ;
145
+ return notificationsToAdd ;
137
146
} ) ;
138
147
}
139
148
} ,
@@ -171,12 +180,19 @@ module.exports = {
171
180
} ) ) ;
172
181
}
173
182
174
- if ( notificationToMarkAsSeen . seen ) {
183
+ if ( _private . wasSeen ( notificationToMarkAsSeen , frame . user ) ) {
175
184
return Promise . resolve ( ) ;
176
185
}
177
186
187
+ // @NOTE : We don't remove the notifications, because otherwise we will receive them again from the service.
178
188
allNotifications [ notificationToMarkAsSeenIndex ] . seen = true ;
179
189
190
+ if ( ! allNotifications [ notificationToMarkAsSeenIndex ] . seenBy ) {
191
+ allNotifications [ notificationToMarkAsSeenIndex ] . seenBy = [ ] ;
192
+ }
193
+
194
+ allNotifications [ notificationToMarkAsSeenIndex ] . seenBy . push ( frame . user . id ) ;
195
+
180
196
return api . settings . edit ( {
181
197
settings : [ {
182
198
key : 'notifications' ,
@@ -186,6 +202,11 @@ module.exports = {
186
202
}
187
203
} ,
188
204
205
+ /**
206
+ * Clears all notifications. Method used in tests only
207
+ *
208
+ * @private Not exposed over HTTP
209
+ */
189
210
destroyAll : {
190
211
statusCode : 204 ,
191
212
permissions : {
@@ -195,6 +216,7 @@ module.exports = {
195
216
const allNotifications = _private . fetchAllNotifications ( ) ;
196
217
197
218
allNotifications . forEach ( ( notification ) => {
219
+ // @NOTE : We don't remove the notifications, because otherwise we will receive them again from the service.
198
220
notification . seen = true ;
199
221
} ) ;
200
222
0 commit comments