@aochuang/client-sdk 1.0.381-dev-3 → 1.0.381-dev-7

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.381-dev-3",
3
+ "version": "1.0.381-dev-7",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -135,9 +135,11 @@ export default class ClientChatSession {
135
135
  getSessionMethod: null,
136
136
  onGetSessionLog: null,
137
137
  updateSessionReadMethod: null,
138
+ updateSessionUnreadMethod: null,
138
139
  starSessionMethod: null,
139
140
  unstarSessionMethod: null,
140
141
  onSessionReadChange: null,
142
+ onSessionUnreadChange: null,
141
143
  onUpdateSession: null,
142
144
  onRemoveSession: null,
143
145
  onDeleteSession: null,
@@ -168,6 +170,7 @@ export default class ClientChatSession {
168
170
  this.onAddSession = options.onAddSession
169
171
  this.loadSessionData = options.loadSessionDataMethod
170
172
  this.updateSessionRead = options.updateSessionReadMethod
173
+ this.updateSessionUnread = options.updateSessionUnreadMethod
171
174
  this.getSessionMethod = options.getSessionMethod
172
175
  this.onGetSessionLog = options.onGetSessionLog
173
176
  this.loadAssignTaskSessionDataMethod = options.loadAssignTaskSessionDataMethod
@@ -177,6 +180,7 @@ export default class ClientChatSession {
177
180
  this.unstarSessionMethod = options.unstarSessionMethod
178
181
  this.deleteSessionMethod = options.deleteSessionMethod
179
182
  this.onSessionReadChange = options.onSessionReadChange
183
+ this.onSessionUnreadChange = options.onSessionUnreadChange
180
184
  this.onUpdateSession = options.onUpdateSession
181
185
  this.onRemoveSession = options.onRemoveSession
182
186
  this.onDeleteSession = options.onDeleteSession
@@ -990,6 +994,101 @@ export default class ClientChatSession {
990
994
  return Promise.reject(error)
991
995
  })
992
996
  }
997
+
998
+ /**
999
+ * 设置会话未读
1000
+ */
1001
+ setSessionUnread (sessionId) {
1002
+ let session = this.sessionMap[sessionId]
1003
+ if (!session) {
1004
+ session = this.data.virtualSessionData.find(v => v.conversationId === sessionId)
1005
+ }
1006
+ if (!session) {
1007
+ return Promise.reject(new Error('未找到会话' + sessionId))
1008
+ }
1009
+ if (!this.updateSessionUnread) {
1010
+ return Promise.reject(new Error('缺少updateSessionUnreadMethod配置'))
1011
+ }
1012
+ return this.updateSessionUnread({
1013
+ conversationId: sessionId
1014
+ }).then(rs => {
1015
+ if (rs && rs.data) {
1016
+ return this.mergeMarkUnreadResult(sessionId, rs.data)
1017
+ }
1018
+ // 兼容旧服务端成功后返回空 body,详情仍经过防旧合并。
1019
+ return this.getSessionMethod({
1020
+ id: sessionId
1021
+ }).then(detailRs => {
1022
+ if (!detailRs || !detailRs.data) {
1023
+ return Promise.reject(new Error('未找到会话' + sessionId))
1024
+ }
1025
+ return this.mergeMarkUnreadResult(sessionId, detailRs.data)
1026
+ })
1027
+ })
1028
+ }
1029
+
1030
+ /**
1031
+ * 合并标记未读返回的会话快照
1032
+ */
1033
+ mergeMarkUnreadResult (sessionId, dto) {
1034
+ let session = this.sessionMap[sessionId]
1035
+ if (!session) {
1036
+ session = this.data.virtualSessionData.find(v => v.conversationId === sessionId)
1037
+ }
1038
+ // 请求期间会话可能已被删除,不重新加回本地。
1039
+ if (!session) {
1040
+ return {
1041
+ applied: false,
1042
+ reason: 'session-not-found'
1043
+ }
1044
+ }
1045
+ if (!dto || dto.conversationId !== sessionId) {
1046
+ throw new Error('标记未读返回的会话不匹配')
1047
+ }
1048
+ if (typeof dto.unreadMsgCount !== 'number') {
1049
+ throw new Error('标记未读返回的未读数无效')
1050
+ }
1051
+ const localLastMsgTime = session.lastMsgTime || 0
1052
+ const serverLastMsgTime = dto.lastMsgTime || 0
1053
+ if (serverLastMsgTime < localLastMsgTime) {
1054
+ return {
1055
+ applied: false,
1056
+ reason: 'stale-last-message'
1057
+ }
1058
+ }
1059
+ const beforeUnreadMsgCount = session.unreadMsgCount || 0
1060
+ const patch = {
1061
+ unreadMsgCount: dto.unreadMsgCount
1062
+ }
1063
+ const lastMessageFields = [
1064
+ 'lastMsgId',
1065
+ 'lastMsgTime',
1066
+ 'lastMsgType',
1067
+ 'lastMsgContent',
1068
+ 'senderWechatIdHash'
1069
+ ]
1070
+ lastMessageFields.forEach(field => {
1071
+ if (typeof dto[field] !== 'undefined' && dto[field] !== null) {
1072
+ patch[field] = dto[field]
1073
+ }
1074
+ })
1075
+ this.updateSession(sessionId, patch)
1076
+ const unreadChangeResult = this.onSessionUnreadChange && this.onSessionUnreadChange({
1077
+ session,
1078
+ beforeUnreadMsgCount,
1079
+ afterUnreadMsgCount: dto.unreadMsgCount
1080
+ })
1081
+ const result = {
1082
+ applied: true,
1083
+ session,
1084
+ beforeUnreadMsgCount,
1085
+ afterUnreadMsgCount: dto.unreadMsgCount
1086
+ }
1087
+ if (unreadChangeResult && typeof unreadChangeResult.then === 'function') {
1088
+ return unreadChangeResult.then(() => result)
1089
+ }
1090
+ return result
1091
+ }
993
1092
 
994
1093
  /**
995
1094
  * 标记会话已读,不经过服务器请求
@@ -83,6 +83,7 @@ class ClientChat {
83
83
  loadSessionDataMethod: null,
84
84
  loadAssignTaskSessionDataMethod: null,
85
85
  updateSessionReadMethod: null,
86
+ updateSessionUnreadMethod: null,
86
87
  loadStarSessionDataMethod: null,
87
88
  loadUnreadSessionDataMethod: null,
88
89
  starSessionMethod: null,
@@ -310,6 +311,29 @@ class ClientChat {
310
311
  }
311
312
  }, 'onSessionReadChange')
312
313
  }
314
+ options.onSessionUnreadChange = ({ session, beforeUnreadMsgCount, afterUnreadMsgCount }) => {
315
+ if (instance !== this.session && !this.session.sessionMap[session.conversationId]) {
316
+ // 搜索等独立列表可能先加载到会话,补入主列表时不重复累加账号未读。
317
+ this.session.addSession(Object.assign({}, session), session.wechatAccountId)
318
+ }
319
+ const delta = afterUnreadMsgCount - beforeUnreadMsgCount
320
+ if (!delta) {
321
+ return
322
+ }
323
+ return this.account.updateAccountUnreadMessageNum(session.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => {
324
+ const nextUnreadMessageNum = Math.max(0, (unreadMesasgeNum || 0) + delta)
325
+ if (session.mute) {
326
+ return {
327
+ num: nextUnreadMessageNum,
328
+ muteNum: Math.max(0, (unreadMessageMuteNum || 0) + delta)
329
+ }
330
+ }
331
+ return {
332
+ num: nextUnreadMessageNum,
333
+ muteNum: unreadMessageMuteNum || 0
334
+ }
335
+ }, 'onSessionUnreadChange')
336
+ }
313
337
  }
314
338
  this.session = new ClientChatSession({
315
339
  groupField: 'wechatAccountId',
@@ -318,6 +342,7 @@ class ClientChat {
318
342
  loadAssignTaskSessionDataMethod: options.loadAssignTaskSessionDataMethod,
319
343
  loadSessionDataMethod: options.loadSessionDataMethod,
320
344
  updateSessionReadMethod: options.updateSessionReadMethod,
345
+ updateSessionUnreadMethod: options.updateSessionUnreadMethod,
321
346
  getSessionMethod: options.getSessionMethod,
322
347
  onGetSessionLog: data => this.socket && this.socket.addLog(data),
323
348
  stickyTopSessionMethod: options.stickyTopSessionMethod,
@@ -337,6 +362,7 @@ class ClientChat {
337
362
  loadAssignTaskSessionDataMethod: options.loadAssignTaskSessionDataMethod,
338
363
  loadSessionDataMethod: options.loadSessionDataMethod,
339
364
  updateSessionReadMethod: options.updateSessionReadMethod,
365
+ updateSessionUnreadMethod: options.updateSessionUnreadMethod,
340
366
  getSessionMethod: options.getSessionMethod,
341
367
  stickyTopSessionMethod: options.stickyTopSessionMethod,
342
368
  muteNotificationSessionMethod: options.muteNotificationSessionMethod,
@@ -361,6 +387,7 @@ class ClientChat {
361
387
  return options.loadStarSessionDataMethod(params, context)
362
388
  },
363
389
  updateSessionReadMethod: options.updateSessionReadMethod,
390
+ updateSessionUnreadMethod: options.updateSessionUnreadMethod,
364
391
  getSessionMethod: options.getSessionMethod,
365
392
  stickyTopSessionMethod: options.stickyTopSessionMethod,
366
393
  muteNotificationSessionMethod: options.muteNotificationSessionMethod,
@@ -385,6 +412,7 @@ class ClientChat {
385
412
  return options.loadUnreadSessionDataMethod(params, context)
386
413
  },
387
414
  updateSessionReadMethod: options.updateSessionReadMethod,
415
+ updateSessionUnreadMethod: options.updateSessionUnreadMethod,
388
416
  getSessionMethod: options.getSessionMethod,
389
417
  stickyTopSessionMethod: options.stickyTopSessionMethod,
390
418
  muteNotificationSessionMethod: options.muteNotificationSessionMethod,