@aochuang/client-sdk 1.0.404 → 1.0.405

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.404",
3
+ "version": "1.0.405",
4
4
  "description": "",
5
5
  "files": [
6
6
  "common",
@@ -170,8 +170,11 @@ export default class ClientChatMessage {
170
170
  * "wechatChatroomId": "2089efcc7f8f40b00b7f05fa4c6dfd3bb642774d", //群的时候才有
171
171
  * "wechatTime": 1723710022000
172
172
  * })
173
+ * @param { Boolean } fireEvent - 为false时不触发onAddMessageSuccess和add-message事件
174
+ * @param { Object } options
175
+ * @param { String } options.replaceMessageId - 这条消息替换的临时占位消息id,会话更新时据此判断是否允许用它的时间覆盖会话时间
173
176
  */
174
- addMessage (data, fireEvent) {
177
+ addMessage (data, fireEvent, options) {
175
178
  if (!data || !data.id || !data[this.groupField] || !data[this.sortField]) {
176
179
  this.addDiagnosticLog('message_rejected', this.getMessageDiagnosticData(data, {
177
180
  reason: 'missing_required_field'
@@ -234,7 +237,8 @@ export default class ClientChatMessage {
234
237
  sessionUpdateResult = this.onAddMessageSuccess && this.onAddMessageSuccess({
235
238
  data,
236
239
  lastMessageTime,
237
- isBeforeLastMessage
240
+ isBeforeLastMessage,
241
+ replaceMessageId: options && options.replaceMessageId
238
242
  })
239
243
  } catch (error) {
240
244
  this.handleSessionUpdateError(data, error)
@@ -467,7 +471,11 @@ export default class ClientChatMessage {
467
471
  // 如果返回的是对象,证明发送成功了,如果返回的不是对象,是报错字符串,交给外面处理错误
468
472
  if (data && typeof data === 'object') {
469
473
  this.removeMessage(newMessage.id)
470
- this.addMessage(data)
474
+ // 临时消息的时间是浏览器本地时间,服务端消息的时间是服务端时钟,两者可能有偏差,
475
+ // 告诉会话这条消息是替换临时消息的,这样即使服务端时间比本地时间小,也能把会话时间校正回服务端时钟
476
+ this.addMessage(data, undefined, {
477
+ replaceMessageId: newMessage.id
478
+ })
471
479
  } else if (typeof data === 'string') {
472
480
  this.updateMessage(newMessage.id, {
473
481
  sendStatus: 2,
@@ -43,6 +43,17 @@ function appendLastAtMsgContent (lastAtMsgContent, atMessage) {
43
43
  return messages.concat(atMessage)
44
44
  }
45
45
 
46
+ /**
47
+ * 会话的最后一条消息是否就是被替换的那条消息
48
+ * 发送消息时先插入临时占位消息(时间是浏览器本地时间),之后服务端消息替换它、发送回执回刷真实时间(服务端时钟)。
49
+ * 两个时钟可能有偏差,服务端时间可能比本地时间小,如果只按"时间更新才更新会话"判断,会话时间会一直停留在本地时间,
50
+ * 导致对方紧接着发来的消息被判断为旧消息,不更新会话最后一条消息也不增加未读数量。
51
+ * 所以替换的是会话的最后一条消息时,允许用更小的时间覆盖,把会话时间校正回服务端时钟。
52
+ */
53
+ function isReplaceSessionLastMessage (session, replaceMessageId) {
54
+ return !!replaceMessageId && !!session && session.lastMsgId === replaceMessageId
55
+ }
56
+
46
57
  class ClientChat {
47
58
  data = Vue.observable({
48
59
  connectStatus: CONNECT_STATUS.Normal
@@ -449,7 +460,7 @@ class ClientChat {
449
460
  getSessionIdMethod: data => createSessionId(options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : data && data.accountId, data && data.wechatFriendId),
450
461
  checkMessageSendResultMethod: options.checkMessageSendResultMethod,
451
462
  loadLossMessageDataMethod: options.loadLossFriendMessageDataMethod,
452
- onAddMessageSuccess: ({ data, isBeforeLastMessage }) => {
463
+ onAddMessageSuccess: ({ data, isBeforeLastMessage, replaceMessageId }) => {
453
464
  const accountId = options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : data.accountId
454
465
  let sessionId = createSessionId(accountId, data.wechatFriendId)
455
466
  // 更新会话
@@ -472,7 +483,7 @@ class ClientChat {
472
483
  if (rs && rs.data) {
473
484
  const isAfterServerMessage = data.wechatTime > context.baseLastMsgTime
474
485
  const isNewerMessage = data.wechatTime > (rs.data.lastMsgTime || 0)
475
- if (isNewerMessage) {
486
+ if (isNewerMessage || isReplaceSessionLastMessage(rs.data, replaceMessageId)) {
476
487
  rs.data.lastMsgContent = data.content
477
488
  rs.data.lastMsgType = data.msgType
478
489
  rs.data.lastMsgTime = data.wechatTime
@@ -505,7 +516,7 @@ class ClientChat {
505
516
  // listChange/getSession may already include this message in unreadMsgCount.
506
517
  // Only count messages newer than the session watermark.
507
518
  const isNewerMessage = data.wechatTime > (session.lastMsgTime || 0)
508
- if (!isNewerMessage) {
519
+ if (!isNewerMessage && !isReplaceSessionLastMessage(session, replaceMessageId)) {
509
520
  return {}
510
521
  }
511
522
  const nextSession = {
@@ -573,7 +584,7 @@ class ClientChat {
573
584
  getSessionIdMethod: data => createSessionId(options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : data && data.accountId, data && data.wechatChatroomId),
574
585
  checkMessageSendResultMethod: options.checkMessageSendResultMethod,
575
586
  loadLossMessageDataMethod: options.loadLossRoomMessageDataMethod,
576
- onAddMessageSuccess: ({ data, isBeforeLastMessage }) => {
587
+ onAddMessageSuccess: ({ data, isBeforeLastMessage, replaceMessageId }) => {
577
588
  const accountId = options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : data.accountId
578
589
  let sessionId = createSessionId(accountId, data.wechatChatroomId)
579
590
  const atMessage = getAtMessageContent(data)
@@ -598,7 +609,7 @@ class ClientChat {
598
609
  if (rs && rs.data) {
599
610
  const isAfterServerMessage = data.wechatTime > context.baseLastMsgTime
600
611
  const isNewerMessage = data.wechatTime > (rs.data.lastMsgTime || 0)
601
- if (isNewerMessage) {
612
+ if (isNewerMessage || isReplaceSessionLastMessage(rs.data, replaceMessageId)) {
602
613
  rs.data.lastMsgContent = data.content
603
614
  rs.data.lastMsgType = data.msgType
604
615
  rs.data.lastMsgTime = data.wechatTime
@@ -634,7 +645,7 @@ class ClientChat {
634
645
  // listChange/getSession may already include this message in unreadMsgCount.
635
646
  // Only count messages newer than the session watermark.
636
647
  const isNewerMessage = data.wechatTime > (session.lastMsgTime || 0)
637
- if (!isNewerMessage) {
648
+ if (!isNewerMessage && !isReplaceSessionLastMessage(session, replaceMessageId)) {
638
649
  return {}
639
650
  }
640
651
  const nextSession = {
@@ -879,15 +890,32 @@ class ClientChat {
879
890
  // 发送消息结果,状态回刷
880
891
  onCmdSendMessageResult: ({ data }) => {
881
892
  const { cmdType, friendMessageId, chatroomMessageId, errMsg, seq, ...nextData } = data
882
- if (friendMessageId) {
883
- this.friendMessage.updateMessage(friendMessageId, Object.assign(nextData, {
884
- resultMsg: errMsg
885
- }))
886
- } else {
887
- this.roomMessage.updateMessage(chatroomMessageId, Object.assign(nextData, {
888
- resultMsg: errMsg
889
- }))
893
+ const messageManage = friendMessageId ? this.friendMessage : this.roomMessage
894
+ const messageId = friendMessageId || chatroomMessageId
895
+ const targetField = friendMessageId ? 'wechatFriendId' : 'wechatChatroomId'
896
+ messageManage.updateMessage(messageId, Object.assign(nextData, {
897
+ resultMsg: errMsg
898
+ }))
899
+ // 回执里的wechatTime是服务端时钟的真实时间,如果这条消息是会话的最后一条消息,需要把会话时间同步过去,
900
+ // 避免会话时间停留在临时消息的本地时间上(本地时钟偏快时,会导致对方紧接着的回复不更新会话也不加未读)
901
+ const message = messageManage.getMessage(messageId)
902
+ if (!message || !message.wechatTime || !message[targetField]) {
903
+ // 消息还没插入时回执会先缓存,等消息插入时会合并回执数据,再由onAddMessageSuccess更新会话
904
+ return
905
+ }
906
+ const accountId = options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : message.accountId
907
+ const sessionId = createSessionId(accountId, message[targetField])
908
+ if (!sessionId) {
909
+ return
890
910
  }
911
+ this.session.updateSession(sessionId, ({ session }) => {
912
+ if (session.lastMsgId !== messageId || session.lastMsgTime === message.wechatTime) {
913
+ return {}
914
+ }
915
+ return {
916
+ lastMsgTime: message.wechatTime
917
+ }
918
+ })
891
919
  },
892
920
  onCmdChatroomMemberInfoChanged: ({ data }) => {
893
921
  // 如果没有changeType的时候,目前看应该是群成员或者群信息变化了,需要重新刷新一下群成员和群信息