@aochuang/client-sdk 1.0.364 → 1.0.365

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aochuang/client-sdk",
3
- "version": "1.0.364",
3
+ "version": "1.0.365",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -131,6 +131,7 @@ export default class ClientChatSession {
131
131
  stickyTopSessionMethod: null,
132
132
  muteNotificationSessionMethod: null,
133
133
  getSessionMethod: null,
134
+ onGetSessionLog: null,
134
135
  updateSessionReadMethod: null,
135
136
  starSessionMethod: null,
136
137
  unstarSessionMethod: null,
@@ -166,6 +167,7 @@ export default class ClientChatSession {
166
167
  this.loadSessionData = options.loadSessionDataMethod
167
168
  this.updateSessionRead = options.updateSessionReadMethod
168
169
  this.getSessionMethod = options.getSessionMethod
170
+ this.onGetSessionLog = options.onGetSessionLog
169
171
  this.loadAssignTaskSessionDataMethod = options.loadAssignTaskSessionDataMethod
170
172
  this.stickyTopSessionMethod = options.stickyTopSessionMethod
171
173
  this.muteNotificationSessionMethod = options.muteNotificationSessionMethod
@@ -228,41 +230,144 @@ export default class ClientChatSession {
228
230
  })
229
231
  }
230
232
 
231
- getSession (sessionId, serializeMethod) {
233
+ _addGetSessionLog (title, data) {
234
+ if (this.onGetSessionLog) {
235
+ this.onGetSessionLog({
236
+ group: 'client-chat-session:getSession',
237
+ content: {
238
+ title,
239
+ data
240
+ }
241
+ })
242
+ }
243
+ }
244
+
245
+ _addGetSessionSerializeTask (pollRequest, serializeMethod, taskOptions = {}) {
246
+ if (!serializeMethod) {
247
+ return false
248
+ }
249
+ const messageId = taskOptions.messageId
250
+ if (messageId && pollRequest.serializeTaskMap[messageId]) {
251
+ return false
252
+ }
253
+ const task = {
254
+ messageId,
255
+ wechatTime: taskOptions.wechatTime || 0,
256
+ index: pollRequest.serializeTasks.length,
257
+ serializeMethod
258
+ }
259
+ pollRequest.serializeTasks.push(task)
260
+ if (messageId) {
261
+ pollRequest.serializeTaskMap[messageId] = task
262
+ }
263
+ return true
264
+ }
265
+
266
+ getSession (sessionId, serializeMethod, taskOptions = {}) {
232
267
  if (this.sessionMap[sessionId]) {
233
268
  return Promise.resolve({
234
269
  data: this.sessionMap[sessionId]
235
270
  })
236
271
  }
237
272
  // 这里需要轮询从后端获取会话,后端会话更新和创建是异步的,所以要多轮训请求几次
238
- if (!this.pollRequestMap[sessionId] || !this.pollRequestMap[sessionId].ps) {
239
- const pollRequest = createPollRequest(({ done, fail }) => {
240
- return this.getSessionMethod({
241
- id: sessionId
242
- }).then((rs) => {
243
- if (rs.data) {
244
- done(rs)
245
- }
246
- })
273
+ let pollRequest = this.pollRequestMap[sessionId]
274
+ if (pollRequest && pollRequest.ps) {
275
+ const taskAdded = this._addGetSessionSerializeTask(pollRequest, serializeMethod, taskOptions)
276
+ this._addGetSessionLog('复用会话轮询', {
277
+ sessionId,
278
+ messageId: taskOptions.messageId,
279
+ wechatTime: taskOptions.wechatTime,
280
+ taskAdded,
281
+ taskCount: pollRequest.serializeTasks.length
247
282
  })
248
- this.pollRequestMap[sessionId] = pollRequest
249
- this.pollRequestMap[sessionId].ps = pollRequest.start().then(rs => {
250
- if (serializeMethod) {
251
- rs = serializeMethod(rs)
252
- }
283
+ return pollRequest.ps
284
+ }
285
+
286
+ pollRequest = createPollRequest(({ done }) => {
287
+ pollRequest.requestCount += 1
288
+ return this.getSessionMethod({
289
+ id: sessionId
290
+ }).then((rs) => {
253
291
  if (rs.data) {
254
- this.addSession(rs.data, null)
255
- return rs
292
+ done(rs)
256
293
  } else {
257
- return Promise.reject('未找到会话' + sessionId)
294
+ this._addGetSessionLog('会话轮询返回空', {
295
+ sessionId,
296
+ requestCount: pollRequest.requestCount
297
+ })
258
298
  }
259
- }).finally(() => {
260
- if (this.pollRequestMap[sessionId] === pollRequest) {
261
- delete this.pollRequestMap[sessionId]
299
+ }, (error) => {
300
+ this._addGetSessionLog('会话轮询请求异常', {
301
+ sessionId,
302
+ requestCount: pollRequest.requestCount,
303
+ error: error && (error.message || error.errMsg || error.toString && error.toString()) || error
304
+ })
305
+ return Promise.reject(error)
306
+ })
307
+ })
308
+ pollRequest.serializeTasks = []
309
+ pollRequest.serializeTaskMap = {}
310
+ pollRequest.requestCount = 0
311
+ pollRequest.startedAt = Date.now()
312
+ this._addGetSessionSerializeTask(pollRequest, serializeMethod, taskOptions)
313
+ this.pollRequestMap[sessionId] = pollRequest
314
+ this._addGetSessionLog('创建会话轮询', {
315
+ sessionId,
316
+ messageId: taskOptions.messageId,
317
+ wechatTime: taskOptions.wechatTime,
318
+ taskCount: pollRequest.serializeTasks.length
319
+ })
320
+ pollRequest.ps = pollRequest.start().then(rs => {
321
+ if (!rs.data) {
322
+ return Promise.reject('未找到会话' + sessionId)
323
+ }
324
+ const context = {
325
+ baseLastMsgTime: rs.data.lastMsgTime || 0,
326
+ processedMessageIds: [],
327
+ skippedMessageIds: [],
328
+ unreadMessageIds: []
329
+ }
330
+ const serializeTasks = pollRequest.serializeTasks.slice().sort((a, b) => {
331
+ return a.wechatTime - b.wechatTime || a.index - b.index
332
+ })
333
+ serializeTasks.forEach(task => {
334
+ rs = task.serializeMethod(rs, context) || rs
335
+ if (task.messageId) {
336
+ context.processedMessageIds.push(task.messageId)
262
337
  }
263
338
  })
264
- }
265
- return this.pollRequestMap[sessionId].ps
339
+ this.addSession(rs.data, null)
340
+ const wechatAccountId = rs.data.wechatAccountId
341
+ this._addGetSessionLog('会话轮询合并完成', {
342
+ sessionId,
343
+ requestCount: pollRequest.requestCount,
344
+ duration: Date.now() - pollRequest.startedAt,
345
+ baseLastMsgTime: context.baseLastMsgTime,
346
+ lastMsgTime: rs.data.lastMsgTime,
347
+ lastMsgId: rs.data.lastMsgId,
348
+ unreadMsgCount: rs.data.unreadMsgCount,
349
+ processedMessageIds: context.processedMessageIds,
350
+ skippedMessageIds: context.skippedMessageIds,
351
+ unreadMessageIds: context.unreadMessageIds,
352
+ hasSessionMap: !!this.sessionMap[sessionId],
353
+ hasAllSessionGroup: !!this._getSessionBySessionGroup(sessionId, ALL_WECHAT_ACCOUNT_ID),
354
+ hasAccountSessionGroup: !!this._getSessionBySessionGroup(sessionId, wechatAccountId)
355
+ })
356
+ return rs
357
+ }).catch((error) => {
358
+ this._addGetSessionLog('会话轮询失败', {
359
+ sessionId,
360
+ requestCount: pollRequest.requestCount,
361
+ duration: Date.now() - pollRequest.startedAt,
362
+ error: error && (error.message || error.errMsg || error.toString && error.toString()) || error
363
+ })
364
+ return Promise.reject(error)
365
+ }).finally(() => {
366
+ if (this.pollRequestMap[sessionId] === pollRequest) {
367
+ delete this.pollRequestMap[sessionId]
368
+ }
369
+ })
370
+ return pollRequest.ps
266
371
  }
267
372
 
268
373
  findSession (findFn) {
@@ -741,7 +846,9 @@ export default class ClientChatSession {
741
846
  return true
742
847
  }
743
848
  })
744
- this.data.sessionGroupData[groupId][topField][oldSortFields.day][oldSortFields.milliseconds].splice(index, 1)
849
+ if (index >= 0) {
850
+ this.data.sessionGroupData[groupId][topField][oldSortFields.day][oldSortFields.milliseconds].splice(index, 1)
851
+ }
745
852
  }
746
853
  })
747
854
  })
@@ -295,9 +295,10 @@ class ClientChat {
295
295
  sortField: 'lastMsgTime',
296
296
  name: 'session',
297
297
  loadAssignTaskSessionDataMethod: options.loadAssignTaskSessionDataMethod,
298
- loadSessionDataMethod: options.loadSessionDataMethod,
298
+ loadSessionDataMethod: options.loadSessionDataMethod,
299
299
  updateSessionReadMethod: options.updateSessionReadMethod,
300
300
  getSessionMethod: options.getSessionMethod,
301
+ onGetSessionLog: data => this.socket && this.socket.addLog(data),
301
302
  stickyTopSessionMethod: options.stickyTopSessionMethod,
302
303
  muteNotificationSessionMethod: options.muteNotificationSessionMethod,
303
304
  getNextUnreadSessionMethod: options.getNextUnreadSessionMethod,
@@ -428,42 +429,33 @@ class ClientChat {
428
429
  }
429
430
  }
430
431
  })
431
- return this.session.getSession(sessionId, rs => {
432
- // https://vs.rpa.cn/zentao/bug-view-2411.html
433
- // 如果后端返回的未读数量是0,那么前端先自己兼容一下,因为后端更新会话是异步过程,有可能这里获取到的会话信息还是老的,并不是这个消息更新之后的信息,得看后端会不会异步改成同步
434
- if (rs && rs.data && !rs.data.unreadMsgCount) {
435
- rs.data.unreadMsgCount = data.isSend ? 0 : 1
436
- rs.data.lastMsgContent = data.content
437
- rs.data.lastMsgType = data.msgType
438
- rs.data.lastMsgTime = data.wechatTime
439
- rs.data.lastMsgId = data.id
440
- rs.data.lastUpdateTime = data.updateDate
441
- }
442
- return rs
443
- }).then((rs => {
444
- // 前面更新未读消息数量只是处理会话的时候,因为getSession是异步的,会导致addMessage多次执行到这里,不会倒下面更新会话的逻辑
445
- // 比如连续发了3条消息,然后getSession后端返回可能只会返回2条未读数,所以这里还需要补偿一下
446
- const session = rs.data || {}
447
- const isNewerMessage = data.wechatTime > (session.lastMsgTime || 0)
448
- if (!data.isSend && isNewerMessage) {
449
- if (!(this.sessionManage.currentSession && this.sessionManage.currentSession.conversationId === sessionId && this.shouldMarkCurrentSessionRead(sessionId, data))) {
450
- // 更新账号未读消息数量
451
- this.account.updateAccountUnreadMessageNum(session.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => {
452
- if (session.mute) {
453
- return {
454
- num: (unreadMesasgeNum || 0) + 1,
455
- muteNum: (unreadMessageMuteNum || 0) + 1
456
- }
457
- } else {
458
- return {
459
- num: (unreadMesasgeNum || 0) + 1,
460
- muteNum: unreadMessageMuteNum
461
- }
462
- }
463
- })
432
+ return this.session.getSession(sessionId, (rs, context) => {
433
+ if (rs && rs.data) {
434
+ const isAfterServerMessage = data.wechatTime > context.baseLastMsgTime
435
+ const isNewerMessage = data.wechatTime > (rs.data.lastMsgTime || 0)
436
+ if (isNewerMessage) {
437
+ rs.data.lastMsgContent = data.content
438
+ rs.data.lastMsgType = data.msgType
439
+ rs.data.lastMsgTime = data.wechatTime
440
+ rs.data.lastMsgId = data.id
441
+ rs.data.lastUpdateTime = data.updateDate
442
+ }
443
+ if (!data.isSend && isAfterServerMessage) {
444
+ if (this.sessionManage.currentSession && this.sessionManage.currentSession.conversationId === sessionId && this.shouldMarkCurrentSessionRead(sessionId, data)) {
445
+ rs.data.unreadMsgCount = 0
446
+ } else {
447
+ rs.data.unreadMsgCount = (rs.data.unreadMsgCount || 0) + 1
448
+ context.unreadMessageIds.push(data.id)
449
+ }
450
+ } else {
451
+ context.skippedMessageIds.push(data.id)
464
452
  }
465
453
  }
466
- }), (error) => {
454
+ return rs
455
+ }, {
456
+ messageId: data.id,
457
+ wechatTime: data.wechatTime
458
+ }).then(null, (error) => {
467
459
  this.socket.addLog({
468
460
  group: 'client-chat-message:onAddMessageSuccess',
469
461
  content: {
@@ -581,45 +573,36 @@ class ClientChat {
581
573
  }
582
574
  }
583
575
  })
584
- this.session.getSession(sessionId, (rs) => {
585
- // https://vs.rpa.cn/zentao/bug-view-2411.html
586
- // 如果后端返回的未读数量是0,那么前端先自己兼容一下,因为后端更新会话是异步过程,有可能这里获取到的会话信息还是老的,并不是这个消息更新之后的信息,得看后端会不会异步改成同步
587
- if (rs && rs.data && !rs.data.unreadMsgCount) {
588
- rs.data.unreadMsgCount = data.isSend ? 0 : 1
589
- rs.data.lastMsgContent = data.content
590
- rs.data.lastMsgType = data.msgType
591
- rs.data.lastMsgTime = data.wechatTime
592
- rs.data.lastMsgId = data.id
593
- rs.data.lastUpdateTime = data.updateDate
594
- }
595
- if (rs && rs.data && atMessage && !(this.sessionManage.currentSession && this.sessionManage.currentSession.conversationId === sessionId)) {
596
- rs.data.lastAtMsgContent = appendLastAtMsgContent(rs.data.lastAtMsgContent, atMessage)
597
- }
598
- return rs
599
- }).then(rs => {
600
- // 前面更新未读消息数量只是处理会话的时候,因为getSession是异步的,会导致addMessage多次执行到这里,不会倒下面更新会话的逻辑
601
- // 比如连续发了3条消息,然后getSession后端返回可能只会返回2条未读数,所以这里还需要补偿一下
602
- const session = rs.data || {}
603
- const isNewerMessage = data.wechatTime > (session.lastMsgTime || 0)
604
- if (!data.isSend && isNewerMessage) {
605
- if (!(this.sessionManage.currentSession && this.sessionManage.currentSession.conversationId === sessionId && this.shouldMarkCurrentSessionRead(sessionId, data))) {
606
- // 更新账号未读消息数量
607
- this.account.updateAccountUnreadMessageNum(session.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => {
608
- if (session.mute) {
609
- return {
610
- num: (unreadMesasgeNum || 0) + 1,
611
- muteNum: (unreadMessageMuteNum || 0) + 1
612
- }
613
- } else {
614
- return {
615
- num: (unreadMesasgeNum || 0) + 1,
616
- muteNum: unreadMessageMuteNum
617
- }
576
+ return this.session.getSession(sessionId, (rs, context) => {
577
+ if (rs && rs.data) {
578
+ const isAfterServerMessage = data.wechatTime > context.baseLastMsgTime
579
+ const isNewerMessage = data.wechatTime > (rs.data.lastMsgTime || 0)
580
+ if (isNewerMessage) {
581
+ rs.data.lastMsgContent = data.content
582
+ rs.data.lastMsgType = data.msgType
583
+ rs.data.lastMsgTime = data.wechatTime
584
+ rs.data.lastMsgId = data.id
585
+ rs.data.lastUpdateTime = data.updateDate
586
+ }
587
+ if (!data.isSend && isAfterServerMessage) {
588
+ if (this.sessionManage.currentSession && this.sessionManage.currentSession.conversationId === sessionId && this.shouldMarkCurrentSessionRead(sessionId, data)) {
589
+ rs.data.unreadMsgCount = 0
590
+ } else {
591
+ rs.data.unreadMsgCount = (rs.data.unreadMsgCount || 0) + 1
592
+ context.unreadMessageIds.push(data.id)
593
+ if (atMessage) {
594
+ rs.data.lastAtMsgContent = appendLastAtMsgContent(rs.data.lastAtMsgContent, atMessage)
618
595
  }
619
- })
596
+ }
597
+ } else {
598
+ context.skippedMessageIds.push(data.id)
620
599
  }
621
600
  }
622
- }, (error) => {
601
+ return rs
602
+ }, {
603
+ messageId: data.id,
604
+ wechatTime: data.wechatTime
605
+ }).then(null, (error) => {
623
606
  this.socket.addLog({
624
607
  group: 'client-chat-message:onAddMessageSuccess',
625
608
  content: {