@aochuang/client-sdk 1.0.364 → 1.0.366

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.366",
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,139 @@ 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
- })
247
- })
248
- this.pollRequestMap[sessionId] = pollRequest
249
- this.pollRequestMap[sessionId].ps = pollRequest.start().then(rs => {
250
- if (serializeMethod) {
251
- rs = serializeMethod(rs)
252
- }
273
+ let pollRequest = this.pollRequestMap[sessionId]
274
+ if (pollRequest && pollRequest.ps) {
275
+ this._addGetSessionSerializeTask(pollRequest, serializeMethod, taskOptions)
276
+ return pollRequest.ps
277
+ }
278
+
279
+ pollRequest = createPollRequest(({ done }) => {
280
+ pollRequest.requestCount += 1
281
+ return this.getSessionMethod({
282
+ id: sessionId
283
+ }).then((rs) => {
253
284
  if (rs.data) {
254
- this.addSession(rs.data, null)
255
- return rs
285
+ done(rs)
256
286
  } else {
257
- return Promise.reject('未找到会话' + sessionId)
287
+ pollRequest.emptyResponseCount += 1
258
288
  }
259
- }).finally(() => {
260
- if (this.pollRequestMap[sessionId] === pollRequest) {
261
- delete this.pollRequestMap[sessionId]
289
+ }, (error) => {
290
+ pollRequest.requestErrorCount += 1
291
+ pollRequest.lastRequestError = error && (error.message || error.errMsg || error.toString && error.toString()) || error
292
+ return Promise.reject(error)
293
+ })
294
+ })
295
+ pollRequest.serializeTasks = []
296
+ pollRequest.serializeTaskMap = {}
297
+ pollRequest.requestCount = 0
298
+ pollRequest.emptyResponseCount = 0
299
+ pollRequest.requestErrorCount = 0
300
+ pollRequest.lastRequestError = null
301
+ pollRequest.startedAt = Date.now()
302
+ this._addGetSessionSerializeTask(pollRequest, serializeMethod, taskOptions)
303
+ this.pollRequestMap[sessionId] = pollRequest
304
+ this._addGetSessionLog('创建会话轮询', {
305
+ sessionId,
306
+ messageId: taskOptions.messageId,
307
+ wechatTime: taskOptions.wechatTime,
308
+ taskCount: pollRequest.serializeTasks.length
309
+ })
310
+ pollRequest.ps = pollRequest.start().then(rs => {
311
+ if (!rs.data) {
312
+ return Promise.reject('未找到会话' + sessionId)
313
+ }
314
+ const context = {
315
+ baseLastMsgTime: rs.data.lastMsgTime || 0,
316
+ processedMessageIds: [],
317
+ skippedMessageIds: [],
318
+ unreadMessageIds: []
319
+ }
320
+ const serializeTasks = pollRequest.serializeTasks.slice().sort((a, b) => {
321
+ return a.wechatTime - b.wechatTime || a.index - b.index
322
+ })
323
+ serializeTasks.forEach(task => {
324
+ rs = task.serializeMethod(rs, context) || rs
325
+ if (task.messageId) {
326
+ context.processedMessageIds.push(task.messageId)
262
327
  }
263
328
  })
264
- }
265
- return this.pollRequestMap[sessionId].ps
329
+ this.addSession(rs.data, null)
330
+ const wechatAccountId = rs.data.wechatAccountId
331
+ this._addGetSessionLog('会话轮询合并完成', {
332
+ sessionId,
333
+ requestCount: pollRequest.requestCount,
334
+ emptyResponseCount: pollRequest.emptyResponseCount,
335
+ requestErrorCount: pollRequest.requestErrorCount,
336
+ duration: Date.now() - pollRequest.startedAt,
337
+ baseLastMsgTime: context.baseLastMsgTime,
338
+ lastMsgTime: rs.data.lastMsgTime,
339
+ lastMsgId: rs.data.lastMsgId,
340
+ unreadMsgCount: rs.data.unreadMsgCount,
341
+ processedMessageCount: context.processedMessageIds.length,
342
+ skippedMessageCount: context.skippedMessageIds.length,
343
+ unreadMessageCount: context.unreadMessageIds.length,
344
+ hasSessionMap: !!this.sessionMap[sessionId],
345
+ hasAllSessionGroup: !!this._getSessionBySessionGroup(sessionId, ALL_WECHAT_ACCOUNT_ID),
346
+ hasAccountSessionGroup: !!this._getSessionBySessionGroup(sessionId, wechatAccountId)
347
+ })
348
+ return rs
349
+ }).catch((error) => {
350
+ this._addGetSessionLog('会话轮询失败', {
351
+ sessionId,
352
+ requestCount: pollRequest.requestCount,
353
+ emptyResponseCount: pollRequest.emptyResponseCount,
354
+ requestErrorCount: pollRequest.requestErrorCount,
355
+ duration: Date.now() - pollRequest.startedAt,
356
+ error: error && (error.message || error.errMsg || error.toString && error.toString()) || error,
357
+ lastRequestError: pollRequest.lastRequestError
358
+ })
359
+ return Promise.reject(error)
360
+ }).finally(() => {
361
+ if (this.pollRequestMap[sessionId] === pollRequest) {
362
+ delete this.pollRequestMap[sessionId]
363
+ }
364
+ })
365
+ return pollRequest.ps
266
366
  }
267
367
 
268
368
  findSession (findFn) {
@@ -741,7 +841,9 @@ export default class ClientChatSession {
741
841
  return true
742
842
  }
743
843
  })
744
- this.data.sessionGroupData[groupId][topField][oldSortFields.day][oldSortFields.milliseconds].splice(index, 1)
844
+ if (index >= 0) {
845
+ this.data.sessionGroupData[groupId][topField][oldSortFields.day][oldSortFields.milliseconds].splice(index, 1)
846
+ }
745
847
  }
746
848
  })
747
849
  })
@@ -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,
@@ -417,66 +418,49 @@ class ClientChat {
417
418
  }
418
419
  const localSession = this.session.findSession(({ session }) => session.conversationId === sessionId)
419
420
  if (!localSession) {
420
- this.socket.addLog({
421
- group: 'client-chat-message:onAddMessageSuccess',
422
- content: {
423
- title: '未获取到会话,开始从服务端获取',
424
- data: {
425
- sessionId,
426
- accountId,
427
- wechatFriendId: data.wechatFriendId
428
- }
429
- }
430
- })
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
- })
464
- }
465
- }
466
- }), (error) => {
421
+ if (!this.session.pollRequestMap[sessionId]) {
467
422
  this.socket.addLog({
468
423
  group: 'client-chat-message:onAddMessageSuccess',
469
424
  content: {
470
- title: '未获取到会话,从服务端获取失败',
425
+ title: '未获取到会话,开始从服务端获取',
471
426
  data: {
472
- error: error && (error.message || error.errMsg || error.toString && error.toString()) || error,
473
427
  sessionId,
474
428
  accountId,
475
- wechatFriendId: data.wechatFriendId
429
+ wechatFriendId: data.wechatFriendId,
430
+ wechatAccountId: data.wechatAccountId,
431
+ messageId: data.id,
432
+ wechatTime: data.wechatTime
476
433
  }
477
434
  }
478
435
  })
479
- })
436
+ }
437
+ return this.session.getSession(sessionId, (rs, context) => {
438
+ if (rs && rs.data) {
439
+ const isAfterServerMessage = data.wechatTime > context.baseLastMsgTime
440
+ const isNewerMessage = data.wechatTime > (rs.data.lastMsgTime || 0)
441
+ if (isNewerMessage) {
442
+ rs.data.lastMsgContent = data.content
443
+ rs.data.lastMsgType = data.msgType
444
+ rs.data.lastMsgTime = data.wechatTime
445
+ rs.data.lastMsgId = data.id
446
+ rs.data.lastUpdateTime = data.updateDate
447
+ }
448
+ if (!data.isSend && isAfterServerMessage) {
449
+ if (this.sessionManage.currentSession && this.sessionManage.currentSession.conversationId === sessionId && this.shouldMarkCurrentSessionRead(sessionId, data)) {
450
+ rs.data.unreadMsgCount = 0
451
+ } else {
452
+ rs.data.unreadMsgCount = (rs.data.unreadMsgCount || 0) + 1
453
+ context.unreadMessageIds.push(data.id)
454
+ }
455
+ } else {
456
+ context.skippedMessageIds.push(data.id)
457
+ }
458
+ }
459
+ return rs
460
+ }, {
461
+ messageId: data.id,
462
+ wechatTime: data.wechatTime
463
+ }).catch(() => {})
480
464
  } else {
481
465
  this.session.updateSession(sessionId, ({ session }) => {
482
466
  // listChange/getSession may already include this message in unreadMsgCount.
@@ -570,69 +554,52 @@ class ClientChat {
570
554
  // 更新会话未读消息数量
571
555
  const localSession = this.session.findSession(({ session }) => session.conversationId === sessionId)
572
556
  if (!localSession) {
573
- this.socket.addLog({
574
- group: 'client-chat-message:onAddMessageSuccess',
575
- content: {
576
- title: '未获取到会话,开始从服务端获取',
577
- data: {
578
- sessionId,
579
- accountId,
580
- wechatChatroomId: data.wechatChatroomId
581
- }
582
- }
583
- })
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
- }
618
- }
619
- })
620
- }
621
- }
622
- }, (error) => {
557
+ if (!this.session.pollRequestMap[sessionId]) {
623
558
  this.socket.addLog({
624
559
  group: 'client-chat-message:onAddMessageSuccess',
625
560
  content: {
626
- title: '未获取到会话,从服务端获取失败',
561
+ title: '未获取到会话,开始从服务端获取',
627
562
  data: {
628
- error: error && (error.message || error.errMsg || error.toString && error.toString()) || error,
629
563
  sessionId,
630
564
  accountId,
631
- wechatChatroomId: data.wechatChatroomId
565
+ wechatChatroomId: data.wechatChatroomId,
566
+ wechatAccountId: data.wechatAccountId,
567
+ messageId: data.id,
568
+ wechatTime: data.wechatTime
632
569
  }
633
570
  }
634
571
  })
635
- })
572
+ }
573
+ return this.session.getSession(sessionId, (rs, context) => {
574
+ if (rs && rs.data) {
575
+ const isAfterServerMessage = data.wechatTime > context.baseLastMsgTime
576
+ const isNewerMessage = data.wechatTime > (rs.data.lastMsgTime || 0)
577
+ if (isNewerMessage) {
578
+ rs.data.lastMsgContent = data.content
579
+ rs.data.lastMsgType = data.msgType
580
+ rs.data.lastMsgTime = data.wechatTime
581
+ rs.data.lastMsgId = data.id
582
+ rs.data.lastUpdateTime = data.updateDate
583
+ }
584
+ if (!data.isSend && isAfterServerMessage) {
585
+ if (this.sessionManage.currentSession && this.sessionManage.currentSession.conversationId === sessionId && this.shouldMarkCurrentSessionRead(sessionId, data)) {
586
+ rs.data.unreadMsgCount = 0
587
+ } else {
588
+ rs.data.unreadMsgCount = (rs.data.unreadMsgCount || 0) + 1
589
+ context.unreadMessageIds.push(data.id)
590
+ if (atMessage) {
591
+ rs.data.lastAtMsgContent = appendLastAtMsgContent(rs.data.lastAtMsgContent, atMessage)
592
+ }
593
+ }
594
+ } else {
595
+ context.skippedMessageIds.push(data.id)
596
+ }
597
+ }
598
+ return rs
599
+ }, {
600
+ messageId: data.id,
601
+ wechatTime: data.wechatTime
602
+ }).catch(() => {})
636
603
  } else {
637
604
  this.session.updateSession(sessionId, ({ session }) => {
638
605
  // listChange/getSession may already include this message in unreadMsgCount.