@live-change/notification-service 0.2.32 → 0.2.36

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.
Files changed (2) hide show
  1. package/notification.js +78 -38
  2. 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.ownerType}":"${obj.owner}"_${obj.id}`,
39
- ownerType: obj.ownerType, owner: obj.owner,
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 { ownerType, owner } = obj || oldObj
51
- const group = `"${ownerType}":"${owner}"`
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(notification, { ...data, id: notification })
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
- if(state === 'read'){
76
- await Notification.update(notification, { state: state, readState: state })
77
- } else {
78
- await Notification.update(notification, { state })
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({ ownerType, owner }) {
98
+ async execute({ sessionOrUserType, sessionOrUser }) {
93
99
  const update = { readState: 'read' }
94
- const prefix = `"${ownerType}":"${owner}":"new"_`
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 + "_byOwnerAndReadState",
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({ ownerType, owner }) {
117
- const prefix = `"${ownerType}":"${owner}"_`
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 + "_byOwner",
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('byOwnerAndTime', prefix, range)
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('byOwnerAndTime', prefix, range)
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
- user: {
196
- type: User,
201
+ sessionOrUserType: {
202
+ type: String,
203
+ validation: ['nonEmpty']
197
204
  },
198
- session: {
199
- type: Session,
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 { user, session } = params
209
- if(!user && !session) throw new Error("session or user required")
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, user, session, time, readState: 'new' }
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.ownerType == 'user_User' && notificationRow.owner == client.user
234
- : notificationRow.ownerType == 'session_Session' && notificationRow.owner == client.session
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 [ ownerType, owner ] = client.user
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!!", ownerType, owner)
329
+ console.log("MARK ALL AS READ!!", sessionOrUserType, sessionOrUser)
290
330
  emit({
291
331
  type: "allRead",
292
- ownerType, owner
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 [ ownerType, owner ] = client.user
362
+ const [ sessionOrUserType, sessionOrUser ] = client.user
323
363
  ? [ 'user_User', client.user ]
324
364
  : [ 'session_Session', client.session ]
325
- console.log("DELETE ALL!!", ownerType, owner)
365
+ console.log("DELETE ALL!!", sessionOrUserType, sessionOrUser)
326
366
  emit({
327
367
  type: "allDeleted",
328
- ownerType, owner
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.32",
3
+ "version": "0.2.36",
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.4"
24
+ "@live-change/framework": "0.6.5"
25
25
  },
26
- "gitHead": "91f89af637893c2dbc0e1f55b2886345d408a9ea"
26
+ "gitHead": "7175d4ca2f3308188b65f1093530091a2e8e0ca2"
27
27
  }