TencentCloud / TIMSDK

Tencent Cloud Chat features a comprehensive suite of solutions including global access, one-to-one chat, group chat, message push, profile and relationship chain hosting, and account authentication.
https://trtc.io/products/chat
2.54k stars 2.77k forks source link

【SDk 6.6.3002 - 单聊部分】时间相同的消息被过滤掉了 #1248

Closed captain-miao closed 1 year ago

captain-miao commented 1 year ago

为了更好的了解您使用TIMSDK所遇到的问题,方便快速有效定位解决TIMSDK问题,希望您按如下模板反馈issue,方便我们尽快解决您的问题,目前有TUIKIT集成包,和IMSDK集成包,注意说明是引入了哪个集成包的问题。另外如果需要补充更多信息请及时更新,否则可能被作为无效问题而关闭,这个等待期一般是10天。

issue标题:时间相同的消息被过滤掉了~

【Android】-【SDk 6.6.3002 @2022.08.18 - Enhanced Edition - 增强版 - 单聊部分】 issue内容: 用代码可以清楚说明问题。

    // https://github.com/TencentCloud/TIMSDK/blob/d4c4696016b926c61340c4b720878dc87a668108/Android/TUIKit/TUIChat/tuichat/src/main/java/com/tencent/qcloud/tuikit/tuichat/presenter/ChatPresenter.java
    // 读取消息列表之后做一次重复消息过滤
    private void removeDuplication(List<TUIMessageBean> messageBeans) {
        TreeSet set = new TreeSet(messageBeans);
        messageBeans.clear();
        messageBeans.addAll(set);
    }

    // https://github.com/TencentCloud/TIMSDK/blob/d4c4696016b926c61340c4b720878dc87a668108/Android/TUIKit/TUIChat/tuichat/src/main/java/com/tencent/qcloud/tuikit/tuichat/bean/message/TUIMessageBean.java
    // 看看 TUIMessageBean 的 compareTo 方法,时间相同被认为是相等的。
     @Override
     public int compareTo(TUIMessageBean messageBean) {
          if (TextUtils.equals(getId(), messageBean.getId())) {
               return 0;
          }

          if (messageBean.isGroup()) {
               if (getMsgSeq() > messageBean.getMsgSeq()) {
                    return 1;
               } else if (getMsgSeq() == messageBean.getMsgSeq()) {
                    return 0;
               } else {
                    return -1;
               }
          } else {
               if (getMessageTime() > messageBean.getMessageTime()) {
                    return 1;
               } else if (getMessageTime() == messageBean.getMessageTime()) {
                    return 0; // 这会被认为相等的消息
               } else {
                    return -1;
               }
          }
     }

    // 再看看 getMessageTime() 返回的是时间戳是秒,这很容易相同,如:1664078651。

怎么改

    // 不好的方式,重写 removeDuplication
    private void removeDuplication(List<TUIMessageBean> messages) {
        Set<String> msgIdSet = new HashSet<>();
        Iterator<TUIMessageBean> iterator = messages.iterator();
        while (iterator.hasNext()) {
            TUIMessageBean msg = iterator.next();
            if(msgIdSet.contains(msg.getId())) {
                iterator.remove();
            } else {
                msgIdSet.add(msg.getId());
            }
        }
    }

ps: 希望在 SDK 层更好的解决这个问题