@live-change/peer-connection-service 0.8.129 → 0.8.131

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/message.js +61 -31
  2. package/package.json +2 -2
package/message.js CHANGED
@@ -2,7 +2,10 @@ import definition from './definition.js'
2
2
  const config = definition.config
3
3
  const {
4
4
  readerRoles = ['reader', 'speaker', 'vip', 'moderator', 'owner'],
5
- writerRoles = ['speaker', 'vip', 'moderator', 'owner']
5
+ writerRoles = ['speaker', 'vip', 'moderator', 'owner'],
6
+ messageWaitForEvents = false,
7
+ messageSkipEmit = false,
8
+ groupMessages = false
6
9
  } = config
7
10
 
8
11
 
@@ -24,6 +27,12 @@ const messageFields = {
24
27
  data: {
25
28
  type: Object
26
29
  },
30
+ messages: {
31
+ type: Array,
32
+ of: {
33
+ type: Object
34
+ }
35
+ },
27
36
  sent: {
28
37
  type: Date
29
38
  }
@@ -116,7 +125,7 @@ definition.view({
116
125
 
117
126
  let lastMessageTime = new Map()
118
127
 
119
- function postMessage(props, { client, service }, emit, conversation) {
128
+ async function postMessage(props, { client, service }, emit) {
120
129
  console.log("POST MESSAGE", props)
121
130
  const channelId = props.to
122
131
  let lastTime = lastMessageTime.get(channelId)
@@ -139,11 +148,18 @@ function postMessage(props, { client, service }, emit, conversation) {
139
148
  if(!data.user) {
140
149
  data.session = client.session
141
150
  }
142
- emit({
143
- type: "MessageCreated",
144
- message,
145
- data
146
- })
151
+ if(messageSkipEmit) {
152
+ await Message.create({
153
+ id: message,
154
+ ...data
155
+ })
156
+ } else {
157
+ emit({
158
+ type: "MessageCreated",
159
+ message,
160
+ data
161
+ })
162
+ }
147
163
  }
148
164
 
149
165
  definition.action({
@@ -151,7 +167,6 @@ definition.action({
151
167
  properties: {
152
168
  ...messageFields
153
169
  },
154
- //queuedBy: (command) => `${command.toType}_${command.toId})`,
155
170
  access: async ({ from, to }, context) => {
156
171
  const { client, service, visibilityTest } = context
157
172
  if(visibilityTest) return true
@@ -165,15 +180,14 @@ definition.action({
165
180
  },
166
181
  queuedBy: (props) => props.from+':'+props.to, // without this, messages order can be changed
167
182
  // and it will block ice connection state
183
+ waitForEvents: messageWaitForEvents,
168
184
  async execute(props, { client, service }, emit) {
169
185
  console.error('postMessage is deprecated, use postMessages instead')
170
- const result = postMessage(props, { client, service }, emit)
186
+ await postMessage(props, { client, service }, emit)
171
187
  console.log("MESSAGE POSTED!")
172
- return result
173
188
  }
174
189
  })
175
190
 
176
-
177
191
  definition.action({
178
192
  name: "postMessages",
179
193
  properties: {
@@ -207,31 +221,47 @@ definition.action({
207
221
  },
208
222
  queuedBy: (props) => props.from+':'+props.to, // without this, messages order can be changed
209
223
  // and it will block ice connection state
210
- //waitForEvents: true,
224
+ waitForEvents: messageWaitForEvents,
211
225
  async execute(props, { client, service }, emit) {
212
- const lastMessages = await Message.rangeGet({
213
- gte: `${props.to}_`,
214
- lte: `${props.to}_\xFF\xFF\xFF\xFF`,
215
- limit: 10,
216
- reverse: true
217
- })
218
- let lastSent = lastMessages?.[0]?.sent
226
+ let lastSent = ''
227
+ let lastMessages
228
+ if(messageWaitForEvents) {
229
+ lastMessages = await Message.rangeGet({
230
+ gte: `${props.to}_`,
231
+ lte: `${props.to}_\xFF\xFF\xFF\xFF`,
232
+ limit: 10,
233
+ reverse: true
234
+ })
235
+ lastSent = lastMessages?.[0]?.sent
236
+ }
219
237
  const messages = props.messages
220
238
  for(const message of messages) {
221
- message.from = props.from
222
- message.to = props.to
223
- const sent = new Date(message.sent).toISOString()
224
- if(lastSent > sent) {
225
- console.error("Message out of order", lastSent, '>', sent, "BY", props.from)
226
- console.error("LAST MESSAGES", lastMessages)
227
- console.error("RECEIVED MESSAGES", props.messages)
228
- // process.exit(1)
229
- throw new Error("Messages out of order")
239
+ if(messageWaitForEvents) {
240
+ const sent = new Date(message.sent).toISOString()
241
+ if(lastSent > sent) {
242
+ console.error("Message out of order", lastSent, '>', sent, "BY", props.from)
243
+ console.error("LAST MESSAGES", lastMessages)
244
+ console.error("RECEIVED MESSAGES", props.messages)
245
+ // process.exit(1)
246
+ throw new Error("Messages out of order")
247
+ }
248
+ lastSent = sent
230
249
  }
231
- lastSent = sent
232
250
  }
233
- for(const message of messages) {
234
- postMessage(message, { client, service }, emit)
251
+ if(groupMessages) {
252
+ await postMessage({
253
+ from: props.from,
254
+ to: props.to,
255
+ type: 'bucket',
256
+ sent: messages[0].sent,
257
+ messages
258
+ }, { client, service }, emit)
259
+ } else {
260
+ for(const message of messages) {
261
+ message.from = props.from
262
+ message.to = props.to
263
+ await postMessage(message, { client, service }, emit)
264
+ }
235
265
  }
236
266
  // console.log("MESSAGES POSTED!")
237
267
  return 'ok'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/peer-connection-service",
3
- "version": "0.8.129",
3
+ "version": "0.8.131",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -11,5 +11,5 @@
11
11
  },
12
12
  "author": "Michał Łaszczewski <michal@emikse.com>",
13
13
  "license": "BSD-3-Clause",
14
- "gitHead": "4fe6fc3bf87d7d24803c67af97ff295e8fec9cd4"
14
+ "gitHead": "26ec71040a5eea3f61f83e631cb03e420fa90817"
15
15
  }