@live-change/notification-service 0.2.31 → 0.2.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +11 -0
- package/notification.js +78 -38
- package/package.json +3 -3
package/LICENSE.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Copyright 2019-2022 Michał Łaszczewski
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/notification.js
CHANGED
|
@@ -3,10 +3,6 @@ const app = require("@live-change/framework").app()
|
|
|
3
3
|
const definition = require('./definition.js')
|
|
4
4
|
const config = definition.config
|
|
5
5
|
|
|
6
|
-
const User = definition.foreignModel('users', 'User')
|
|
7
|
-
const Session = definition.foreignModel('session', 'Session')
|
|
8
|
-
|
|
9
|
-
|
|
10
6
|
const Notification = definition.model({
|
|
11
7
|
name: "Notification",
|
|
12
8
|
sessionOrUserItem: {
|
|
@@ -35,8 +31,8 @@ const Notification = definition.model({
|
|
|
35
31
|
function: async function(input, output) {
|
|
36
32
|
await input.table('notification_Notification')
|
|
37
33
|
.map((obj) => obj && obj.readState == 'new' && ({
|
|
38
|
-
id: `"${obj.
|
|
39
|
-
|
|
34
|
+
id: `"${obj.sessionOrUserType}":"${obj.sessionOrUser}"_${obj.id}`,
|
|
35
|
+
sessionOrUserType: obj.sessionOrUserType, sessionOrUser: obj.sessionOrUser,
|
|
40
36
|
to: obj.id
|
|
41
37
|
}))
|
|
42
38
|
.to(output)
|
|
@@ -47,8 +43,8 @@ const Notification = definition.model({
|
|
|
47
43
|
const unreadIndex = await input.index('notification_Notification_unreadNotifications')
|
|
48
44
|
await unreadIndex.onChange(
|
|
49
45
|
async (obj, oldObj) => {
|
|
50
|
-
const {
|
|
51
|
-
const group = `"${
|
|
46
|
+
const { sessionOrUserType, sessionOrUser } = obj || oldObj
|
|
47
|
+
const group = `"${sessionOrUserType}":"${sessionOrUser}"`
|
|
52
48
|
const prefix = group + '_'
|
|
53
49
|
const count = await unreadIndex.count({ gt: prefix, lt: prefix + '\xFF' })
|
|
54
50
|
output.put({
|
|
@@ -65,18 +61,28 @@ const Notification = definition.model({
|
|
|
65
61
|
definition.event({
|
|
66
62
|
name: "created",
|
|
67
63
|
async execute({ notification, data }) {
|
|
68
|
-
await Notification.create(
|
|
64
|
+
await Notification.create({ ...data, id: notification })
|
|
69
65
|
}
|
|
70
66
|
})
|
|
71
67
|
|
|
72
68
|
definition.event({
|
|
73
69
|
name: "marked",
|
|
74
70
|
async execute({ notification, state }) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
71
|
+
await Notification.update(notification, { state })
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
definition.event({
|
|
76
|
+
name: "markedRead",
|
|
77
|
+
async execute({ notification }) {
|
|
78
|
+
await Notification.update(notification, { readState: 'read' })
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
definition.event({
|
|
83
|
+
name: "markedUnread",
|
|
84
|
+
async execute({ notification }) {
|
|
85
|
+
await Notification.update(notification, { readState: 'new' })
|
|
80
86
|
}
|
|
81
87
|
})
|
|
82
88
|
|
|
@@ -89,9 +95,9 @@ definition.event({
|
|
|
89
95
|
|
|
90
96
|
definition.event({
|
|
91
97
|
name: "allRead",
|
|
92
|
-
async execute({
|
|
98
|
+
async execute({ sessionOrUserType, sessionOrUser }) {
|
|
93
99
|
const update = { readState: 'read' }
|
|
94
|
-
const prefix = `"${
|
|
100
|
+
const prefix = `"${sessionOrUserType}":"${sessionOrUser}":"new"_`
|
|
95
101
|
console.log("MARK ALL AS READ PREFIX", prefix)
|
|
96
102
|
await app.dao.request(['database', 'query'], app.databaseName, `(${
|
|
97
103
|
async (input, output, { tableName, indexName, update, range }) => {
|
|
@@ -101,7 +107,7 @@ definition.event({
|
|
|
101
107
|
}
|
|
102
108
|
})`, {
|
|
103
109
|
tableName: Notification.tableName,
|
|
104
|
-
indexName: Notification.tableName + "
|
|
110
|
+
indexName: Notification.tableName + "_bySessionOrUserAndReadState",
|
|
105
111
|
update,
|
|
106
112
|
range: {
|
|
107
113
|
gte: prefix,
|
|
@@ -113,8 +119,8 @@ definition.event({
|
|
|
113
119
|
|
|
114
120
|
definition.event({
|
|
115
121
|
name: "allDeleted",
|
|
116
|
-
async execute({
|
|
117
|
-
const prefix = `"${
|
|
122
|
+
async execute({ sessionOrUserType, sessionOrUser }) {
|
|
123
|
+
const prefix = `"${sessionOrUserType}":"${sessionOrUser}"_`
|
|
118
124
|
console.log("MARK ALL AS READ PREFIX", prefix)
|
|
119
125
|
await app.dao.request(['database', 'query'], app.databaseName, `(${
|
|
120
126
|
async (input, output, { tableName, indexName, update, range }) => {
|
|
@@ -124,7 +130,7 @@ definition.event({
|
|
|
124
130
|
}
|
|
125
131
|
})`, {
|
|
126
132
|
tableName: Notification.tableName,
|
|
127
|
-
indexName: Notification.tableName + "
|
|
133
|
+
indexName: Notification.tableName + "_bySessionOrUser",
|
|
128
134
|
range: {
|
|
129
135
|
gte: prefix,
|
|
130
136
|
lte: prefix + "\xFF\xFF\xFF\xFF"
|
|
@@ -165,11 +171,11 @@ definition.view({
|
|
|
165
171
|
? ["user_User", client.user]
|
|
166
172
|
: ["session_Session", client.session]
|
|
167
173
|
if(!Number.isSafeInteger(range.limit)) range.limit = 100
|
|
168
|
-
const path = Notification.sortedIndexRangePath('
|
|
174
|
+
const path = Notification.sortedIndexRangePath('bySessionOrUserAndTime', prefix, range)
|
|
169
175
|
/*const notifications = await app.dao.get(path)
|
|
170
176
|
console.log("NOTIFICATIONS", path,
|
|
171
177
|
"\n RESULTS", notifications.length, notifications.map(m => m.id))*/
|
|
172
|
-
return Notification.sortedIndexRangePath('
|
|
178
|
+
return Notification.sortedIndexRangePath('bySessionOrUserAndTime', prefix, range)
|
|
173
179
|
}
|
|
174
180
|
})
|
|
175
181
|
|
|
@@ -192,11 +198,13 @@ definition.view({
|
|
|
192
198
|
definition.trigger({
|
|
193
199
|
name: "notify",
|
|
194
200
|
properties: {
|
|
195
|
-
|
|
196
|
-
type:
|
|
201
|
+
sessionOrUserType: {
|
|
202
|
+
type: String,
|
|
203
|
+
validation: ['nonEmpty']
|
|
197
204
|
},
|
|
198
|
-
|
|
199
|
-
type:
|
|
205
|
+
sessionOrUser: {
|
|
206
|
+
type: String,
|
|
207
|
+
validation: ['nonEmpty']
|
|
200
208
|
},
|
|
201
209
|
notificationType: {
|
|
202
210
|
type: String,
|
|
@@ -205,16 +213,16 @@ definition.trigger({
|
|
|
205
213
|
...config.fields
|
|
206
214
|
},
|
|
207
215
|
async execute(params , { service }, emit) {
|
|
208
|
-
const {
|
|
209
|
-
if(!
|
|
216
|
+
const { sessionOrUserType, sessionOrUser, notificationType } = params
|
|
217
|
+
if(!sessionOrUserType || !sessionOrUser) throw new Error("session or user required")
|
|
210
218
|
const notification = app.generateUid()
|
|
211
219
|
const time = new Date()
|
|
212
|
-
let data = {}
|
|
220
|
+
let data = { notificationType }
|
|
213
221
|
for(const key in config.fields) data[key] = params[key]
|
|
214
222
|
emit({
|
|
215
223
|
type: "created",
|
|
216
224
|
notification,
|
|
217
|
-
data: { ...data,
|
|
225
|
+
data: { ...data, sessionOrUserType, sessionOrUser, time, readState: 'new' }
|
|
218
226
|
})
|
|
219
227
|
await app.trigger({
|
|
220
228
|
type: 'notificationCreated',
|
|
@@ -230,8 +238,8 @@ async function notificationAccess({ notification }, { client, visibilityTest })
|
|
|
230
238
|
const notificationRow = await Notification.get(notification)
|
|
231
239
|
if(!notificationRow) throw 'notFound'
|
|
232
240
|
return client.user
|
|
233
|
-
? notificationRow.
|
|
234
|
-
: notificationRow.
|
|
241
|
+
? notificationRow.sessionOrUserType == 'user_User' && notificationRow.sessionOrUser == client.user
|
|
242
|
+
: notificationRow.sessionOrUserType == 'session_Session' && notificationRow.sessionOrUser == client.session
|
|
235
243
|
}
|
|
236
244
|
|
|
237
245
|
definition.action({
|
|
@@ -254,6 +262,38 @@ definition.action({
|
|
|
254
262
|
}
|
|
255
263
|
})
|
|
256
264
|
|
|
265
|
+
definition.action({
|
|
266
|
+
name: "markRead",
|
|
267
|
+
properties: {
|
|
268
|
+
notification: {
|
|
269
|
+
type: Notification
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
access: notificationAccess,
|
|
273
|
+
async execute({notification, state}, {client, service}, emit) {
|
|
274
|
+
emit({
|
|
275
|
+
type: "markedRead",
|
|
276
|
+
notification
|
|
277
|
+
})
|
|
278
|
+
}
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
definition.action({
|
|
282
|
+
name: "markUnread",
|
|
283
|
+
properties: {
|
|
284
|
+
notification: {
|
|
285
|
+
type: Notification
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
access: notificationAccess,
|
|
289
|
+
async execute({notification, state}, {client, service}, emit) {
|
|
290
|
+
emit({
|
|
291
|
+
type: "markedUnread",
|
|
292
|
+
notification
|
|
293
|
+
})
|
|
294
|
+
}
|
|
295
|
+
})
|
|
296
|
+
|
|
257
297
|
definition.action({
|
|
258
298
|
name: "toggleReadStatus",
|
|
259
299
|
properties: {
|
|
@@ -283,13 +323,13 @@ definition.action({
|
|
|
283
323
|
return true
|
|
284
324
|
},
|
|
285
325
|
async execute({ notification, readState }, { client, service }, emit) {
|
|
286
|
-
const [
|
|
326
|
+
const [ sessionOrUserType, sessionOrUser ] = client.user
|
|
287
327
|
? [ 'user_User', client.user ]
|
|
288
328
|
: [ 'session_Session', client.session ]
|
|
289
|
-
console.log("MARK ALL AS READ!!",
|
|
329
|
+
console.log("MARK ALL AS READ!!", sessionOrUserType, sessionOrUser)
|
|
290
330
|
emit({
|
|
291
331
|
type: "allRead",
|
|
292
|
-
|
|
332
|
+
sessionOrUserType, sessionOrUser
|
|
293
333
|
})
|
|
294
334
|
}
|
|
295
335
|
})
|
|
@@ -319,13 +359,13 @@ definition.action({
|
|
|
319
359
|
return true
|
|
320
360
|
},
|
|
321
361
|
async execute({ notification, readState }, { client, service }, emit) {
|
|
322
|
-
const [
|
|
362
|
+
const [ sessionOrUserType, sessionOrUser ] = client.user
|
|
323
363
|
? [ 'user_User', client.user ]
|
|
324
364
|
: [ 'session_Session', client.session ]
|
|
325
|
-
console.log("DELETE ALL!!",
|
|
365
|
+
console.log("DELETE ALL!!", sessionOrUserType, sessionOrUser)
|
|
326
366
|
emit({
|
|
327
367
|
type: "allDeleted",
|
|
328
|
-
|
|
368
|
+
sessionOrUserType, sessionOrUser
|
|
329
369
|
})
|
|
330
370
|
}
|
|
331
371
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/notification-service",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.34",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"url": "https://www.viamage.com/"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@live-change/framework": "0.6.
|
|
24
|
+
"@live-change/framework": "0.6.5"
|
|
25
25
|
},
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "45f52d6c7586d0eaa51b3205c6635a33e32eef6b"
|
|
27
27
|
}
|