@live-change/notification-service 0.2.32 → 0.2.33
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/notification.js +30 -32
- package/package.json +3 -3
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,7 +61,7 @@ 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
|
|
|
@@ -89,9 +85,9 @@ definition.event({
|
|
|
89
85
|
|
|
90
86
|
definition.event({
|
|
91
87
|
name: "allRead",
|
|
92
|
-
async execute({
|
|
88
|
+
async execute({ sessionOrUserType, sessionOrUser }) {
|
|
93
89
|
const update = { readState: 'read' }
|
|
94
|
-
const prefix = `"${
|
|
90
|
+
const prefix = `"${sessionOrUserType}":"${sessionOrUser}":"new"_`
|
|
95
91
|
console.log("MARK ALL AS READ PREFIX", prefix)
|
|
96
92
|
await app.dao.request(['database', 'query'], app.databaseName, `(${
|
|
97
93
|
async (input, output, { tableName, indexName, update, range }) => {
|
|
@@ -101,7 +97,7 @@ definition.event({
|
|
|
101
97
|
}
|
|
102
98
|
})`, {
|
|
103
99
|
tableName: Notification.tableName,
|
|
104
|
-
indexName: Notification.tableName + "
|
|
100
|
+
indexName: Notification.tableName + "_bySessionOrUserAndReadState",
|
|
105
101
|
update,
|
|
106
102
|
range: {
|
|
107
103
|
gte: prefix,
|
|
@@ -113,8 +109,8 @@ definition.event({
|
|
|
113
109
|
|
|
114
110
|
definition.event({
|
|
115
111
|
name: "allDeleted",
|
|
116
|
-
async execute({
|
|
117
|
-
const prefix = `"${
|
|
112
|
+
async execute({ sessionOrUserType, sessionOrUser }) {
|
|
113
|
+
const prefix = `"${sessionOrUserType}":"${sessionOrUser}"_`
|
|
118
114
|
console.log("MARK ALL AS READ PREFIX", prefix)
|
|
119
115
|
await app.dao.request(['database', 'query'], app.databaseName, `(${
|
|
120
116
|
async (input, output, { tableName, indexName, update, range }) => {
|
|
@@ -124,7 +120,7 @@ definition.event({
|
|
|
124
120
|
}
|
|
125
121
|
})`, {
|
|
126
122
|
tableName: Notification.tableName,
|
|
127
|
-
indexName: Notification.tableName + "
|
|
123
|
+
indexName: Notification.tableName + "_bySessionOrUser",
|
|
128
124
|
range: {
|
|
129
125
|
gte: prefix,
|
|
130
126
|
lte: prefix + "\xFF\xFF\xFF\xFF"
|
|
@@ -165,11 +161,11 @@ definition.view({
|
|
|
165
161
|
? ["user_User", client.user]
|
|
166
162
|
: ["session_Session", client.session]
|
|
167
163
|
if(!Number.isSafeInteger(range.limit)) range.limit = 100
|
|
168
|
-
const path = Notification.sortedIndexRangePath('
|
|
164
|
+
const path = Notification.sortedIndexRangePath('bySessionOrUserAndTime', prefix, range)
|
|
169
165
|
/*const notifications = await app.dao.get(path)
|
|
170
166
|
console.log("NOTIFICATIONS", path,
|
|
171
167
|
"\n RESULTS", notifications.length, notifications.map(m => m.id))*/
|
|
172
|
-
return Notification.sortedIndexRangePath('
|
|
168
|
+
return Notification.sortedIndexRangePath('bySessionOrUserAndTime', prefix, range)
|
|
173
169
|
}
|
|
174
170
|
})
|
|
175
171
|
|
|
@@ -192,11 +188,13 @@ definition.view({
|
|
|
192
188
|
definition.trigger({
|
|
193
189
|
name: "notify",
|
|
194
190
|
properties: {
|
|
195
|
-
|
|
196
|
-
type:
|
|
191
|
+
sessionOrUserType: {
|
|
192
|
+
type: String,
|
|
193
|
+
validation: ['nonEmpty']
|
|
197
194
|
},
|
|
198
|
-
|
|
199
|
-
type:
|
|
195
|
+
sessionOrUser: {
|
|
196
|
+
type: String,
|
|
197
|
+
validation: ['nonEmpty']
|
|
200
198
|
},
|
|
201
199
|
notificationType: {
|
|
202
200
|
type: String,
|
|
@@ -205,8 +203,8 @@ definition.trigger({
|
|
|
205
203
|
...config.fields
|
|
206
204
|
},
|
|
207
205
|
async execute(params , { service }, emit) {
|
|
208
|
-
const {
|
|
209
|
-
if(!
|
|
206
|
+
const { sessionOrUserType, sessionOrUser } = params
|
|
207
|
+
if(!sessionOrUserType || !sessionOrUser) throw new Error("session or user required")
|
|
210
208
|
const notification = app.generateUid()
|
|
211
209
|
const time = new Date()
|
|
212
210
|
let data = {}
|
|
@@ -214,7 +212,7 @@ definition.trigger({
|
|
|
214
212
|
emit({
|
|
215
213
|
type: "created",
|
|
216
214
|
notification,
|
|
217
|
-
data: { ...data,
|
|
215
|
+
data: { ...data, sessionOrUserType, sessionOrUser, time, readState: 'new' }
|
|
218
216
|
})
|
|
219
217
|
await app.trigger({
|
|
220
218
|
type: 'notificationCreated',
|
|
@@ -230,8 +228,8 @@ async function notificationAccess({ notification }, { client, visibilityTest })
|
|
|
230
228
|
const notificationRow = await Notification.get(notification)
|
|
231
229
|
if(!notificationRow) throw 'notFound'
|
|
232
230
|
return client.user
|
|
233
|
-
? notificationRow.
|
|
234
|
-
: notificationRow.
|
|
231
|
+
? notificationRow.sessionOrUserType == 'user_User' && notificationRow.sessionOrUser == client.user
|
|
232
|
+
: notificationRow.sessionOrUserType == 'session_Session' && notificationRow.sessionOrUser == client.session
|
|
235
233
|
}
|
|
236
234
|
|
|
237
235
|
definition.action({
|
|
@@ -283,13 +281,13 @@ definition.action({
|
|
|
283
281
|
return true
|
|
284
282
|
},
|
|
285
283
|
async execute({ notification, readState }, { client, service }, emit) {
|
|
286
|
-
const [
|
|
284
|
+
const [ sessionOrUserType, sessionOrUser ] = client.user
|
|
287
285
|
? [ 'user_User', client.user ]
|
|
288
286
|
: [ 'session_Session', client.session ]
|
|
289
|
-
console.log("MARK ALL AS READ!!",
|
|
287
|
+
console.log("MARK ALL AS READ!!", sessionOrUserType, sessionOrUser)
|
|
290
288
|
emit({
|
|
291
289
|
type: "allRead",
|
|
292
|
-
|
|
290
|
+
sessionOrUserType, sessionOrUser
|
|
293
291
|
})
|
|
294
292
|
}
|
|
295
293
|
})
|
|
@@ -319,13 +317,13 @@ definition.action({
|
|
|
319
317
|
return true
|
|
320
318
|
},
|
|
321
319
|
async execute({ notification, readState }, { client, service }, emit) {
|
|
322
|
-
const [
|
|
320
|
+
const [ sessionOrUserType, sessionOrUser ] = client.user
|
|
323
321
|
? [ 'user_User', client.user ]
|
|
324
322
|
: [ 'session_Session', client.session ]
|
|
325
|
-
console.log("DELETE ALL!!",
|
|
323
|
+
console.log("DELETE ALL!!", sessionOrUserType, sessionOrUser)
|
|
326
324
|
emit({
|
|
327
325
|
type: "allDeleted",
|
|
328
|
-
|
|
326
|
+
sessionOrUserType, sessionOrUser
|
|
329
327
|
})
|
|
330
328
|
}
|
|
331
329
|
})
|
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.33",
|
|
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": "8918e5113c031c3d9766df32283de7fb6421e0be"
|
|
27
27
|
}
|