From 6459f2ed7ecf379598fa193cbb0b5cdd891f0a09 Mon Sep 17 00:00:00 2001 From: SL Date: Tue, 27 Feb 2024 16:11:03 +0800 Subject: [PATCH] fix: synchronization channel message multiple synchronization issue --- CHANGELOG.md | 4 ++- README.md | 2 +- example/lib/chat.dart | 1 - example/lib/http.dart | 3 --- example/lib/main.dart | 6 ++--- lib/db/message.dart | 26 +++++++++++------- lib/db/reaction.dart | 46 ++++++++++++++++++-------------- lib/manager/message_manager.dart | 20 ++++++++------ pubspec.yaml | 2 +- 9 files changed, 63 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d426e98..bbe09f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,4 +59,6 @@ ### 1.2.9 * Modification of sending messages containing replies or tag class message parsing errors ### 1.3.0 - * Modification of sending messages containing replies error \ No newline at end of file + * Modification of sending messages containing replies error + ### 1.3.1 + * fix: synchronization channel message multiple synchronization issue \ No newline at end of file diff --git a/README.md b/README.md index 4e9d34a..bfac7ef 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ #### 安装 ``` dependencies: - wukongimfluttersdk: ^1.3.0 + wukongimfluttersdk: ^1.3.1 ``` #### 引入 ```dart diff --git a/example/lib/chat.dart b/example/lib/chat.dart index a6e76d3..ac50e2d 100644 --- a/example/lib/chat.dart +++ b/example/lib/chat.dart @@ -143,7 +143,6 @@ class ChatListDataState extends State { oldestOrderSeq, oldestOrderSeq == 0, pullMode, 20, 0, (list) { List uiList = []; for (int i = 0; i < list.length; i++) { - print(list[i].orderSeq); if (pullMode == 0 && !isReset) { uiList.add(UIMsg(list[i])); // msgList.insert(0, UIMsg(list[i])); diff --git a/example/lib/http.dart b/example/lib/http.dart index a920e45..26ec857 100644 --- a/example/lib/http.dart +++ b/example/lib/http.dart @@ -32,7 +32,6 @@ class HttpUtils { static syncConversation(String lastSsgSeqs, int msgCount, int version, Function(WKSyncConversation) back) async { - print("同步最近会话的参数${version}"); final dio = Dio(); final response = await dio.post('$apiURL/conversation/sync', data: { "uid": UserInfo.uid, // 当前登录用户uid @@ -81,7 +80,6 @@ class HttpUtils { int pullMode, Function(WKSyncChannelMsg) back) async { final dio = Dio(); - print('请求参数${startMsgSeq},$endMsgSeq'); final response = await dio.post('$apiURL/channel/messagesync', data: { "login_uid": UserInfo.uid, // 当前登录用户uid "channel_id": channelID, // 频道ID @@ -117,7 +115,6 @@ class HttpUtils { msg.messageSeq = json['message_seq']; msg.fromUID = json['from_uid']; msg.timestamp = json['timestamp']; - msg.channelID = json['channel_id']; // msg.payload = json['payload']; String payload = json['payload']; try { diff --git a/example/lib/main.dart b/example/lib/main.dart index 1933641..e3b0b44 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -63,11 +63,11 @@ class LoginDemoState extends State { body: SingleChildScrollView( child: Column( children: [ - Padding( - padding: const EdgeInsets.only(top: 60.0), + const Padding( + padding: EdgeInsets.only(top: 60.0), child: Center( child: Column( - children: const [ + children: [ Text( '悟空IM登录', style: diff --git a/lib/db/message.dart b/lib/db/message.dart index a8cc4d7..be897d3 100644 --- a/lib/db/message.dart +++ b/lib/db/message.dart @@ -196,7 +196,6 @@ class MessageDB { List fromUIDs = []; List> results = await WKDBHelper.shared.getDB().rawQuery(sql, args); - if (results.isNotEmpty) { WKChannel? wkChannel = await ChannelDB.shared.query(channelId, channelType); @@ -321,6 +320,7 @@ class MessageDB { //获取原始数据 List list = await getMessages( channelId, channelType, oldestOrderSeq, contain, pullMode, limit); + print("查询总数量${list.length}"); //业务判断数据 List tempList = []; for (int i = 0, size = list.length; i < size; i++) { @@ -413,7 +413,6 @@ class MessageDB { } } } - if (!isSyncMsg) { if (minMessageSeq == 1) { requestCount = 0; @@ -422,20 +421,28 @@ class MessageDB { } } //计算最后一页后是否还存在消息 + int syncLimit = limit; if (!isSyncMsg && tempList.length < limit) { if (pullMode == 0) { //如果下拉获取数据 isSyncMsg = true; - startMsgSeq = oldestMsgSeq; + // startMsgSeq = oldestMsgSeq; + startMsgSeq = minMessageSeq; // 不满足查询数量同步时按查询到的最小seq开始同步 + if (!contain) { + syncLimit = syncLimit + 1; + } endMsgSeq = 0; } else { //如果上拉获取数据 isSyncMsg = true; - startMsgSeq = oldestMsgSeq; + // startMsgSeq = oldestMsgSeq; + startMsgSeq = maxMessageSeq; // 不满足查询数量同步时按查询到的最大seq开始同步 endMsgSeq = 0; + if (!contain) { + syncLimit = syncLimit + 1; + } } } - if (isSyncMsg && (startMsgSeq != endMsgSeq || (startMsgSeq == 0 && endMsgSeq == 0)) && requestCount < 5) { @@ -445,7 +452,7 @@ class MessageDB { //同步消息 requestCount++; WKIM.shared.messageManager.setSyncChannelMsgListener( - channelId, channelType, startMsgSeq, endMsgSeq, limit, pullMode, + channelId, channelType, startMsgSeq, endMsgSeq, syncLimit, pullMode, (syncChannelMsg) { if (syncChannelMsg != null && syncChannelMsg.messages != null && @@ -500,11 +507,11 @@ class MessageDB { return messageSeq; } - insertMsgList(List list) async { - if (list.isEmpty) return; + Future insertMsgList(List list) async { + if (list.isEmpty) return true; if (list.length == 1) { insert(list[0]); - return; + return true; } List saveList = []; for (int i = 0, size = list.length; i < size; i++) { @@ -568,6 +575,7 @@ class MessageDB { } }); } + return true; } Future> queryWithClientMsgNos(List clientMsgNos) async { diff --git a/lib/db/reaction.dart b/lib/db/reaction.dart index 2e01483..df4c7c4 100644 --- a/lib/db/reaction.dart +++ b/lib/db/reaction.dart @@ -57,31 +57,37 @@ class ReactionDB { return list; } - insertOrUpdateReactionList(List list) { - if (list.isEmpty) return; + Future insertOrUpdateReactionList(List list) async { + if (list.isEmpty) return true; + bool isSuccess = true; for (int i = 0, size = list.length; i < size; i++) { - insertOrUpdateReaction(list[i]); + // insertOrUpdateReaction(list[i]); + int row = await insertReaction(list[i]); + if (isSuccess) { + isSuccess = row > 0; + } } + return isSuccess; } - insertOrUpdateReaction(WKMsgReaction reaction) async { - bool isExist = await isExistReaction(reaction.uid, reaction.messageID); - if (isExist) { - updateReaction(reaction); - } else { - insertReaction(reaction); - } - } + // insertOrUpdateReaction(WKMsgReaction reaction) async { + // bool isExist = await isExistReaction(reaction.uid, reaction.messageID); + // if (isExist) { + // updateReaction(reaction); + // } else { + // insertReaction(reaction); + // } + // } - updateReaction(WKMsgReaction reaction) { - var map = {}; - map['is_deleted'] = reaction.isDeleted; - map['seq'] = reaction.seq; - map['emoji'] = reaction.emoji; - WKDBHelper.shared.getDB().update(WKDBConst.tableMessageReaction, map, - where: "message_id=? and uid=?", - whereArgs: [reaction.messageID, reaction.uid]); - } + // updateReaction(WKMsgReaction reaction) { + // var map = {}; + // map['is_deleted'] = reaction.isDeleted; + // map['seq'] = reaction.seq; + // map['emoji'] = reaction.emoji; + // WKDBHelper.shared.getDB().update(WKDBConst.tableMessageReaction, map, + // where: "message_id=? and uid=?", + // whereArgs: [reaction.messageID, reaction.uid]); + // } insertReaction(WKMsgReaction reaction) { WKDBHelper.shared.getDB().insert( diff --git a/lib/manager/message_manager.dart b/lib/manager/message_manager.dart index 59e8c95..6a1ce26 100644 --- a/lib/manager/message_manager.dart +++ b/lib/manager/message_manager.dart @@ -186,19 +186,20 @@ class WKMessageManager { int endMessageSeq, int limit, int pullMode, - Function(WKSyncChannelMsg?) back) { + Function(WKSyncChannelMsg?) back) async { if (_syncChannelMsgBack != null) { _syncChannelMsgBack!(channelID, channelType, startMessageSeq, - endMessageSeq, limit, pullMode, (result) { + endMessageSeq, limit, pullMode, (result) async { if (result != null && result.messages != null) { - _saveSyncChannelMSGs(result.messages!); + _saveSyncChannelMSGs(result.messages!).then((value) => back(result)); + } else { + back(result); } - back(result); }); } } - _saveSyncChannelMSGs(List list) { + Future _saveSyncChannelMSGs(List list) async { List msgList = []; List msgExtraList = []; List msgReactionList = []; @@ -214,15 +215,18 @@ class WKMessageManager { msgReactionList.addAll(wkMsg.reactionList!); } } + bool isSuccess = true; if (msgExtraList.isNotEmpty) { - MessageDB.shared.insertOrUpdateMsgExtras(msgExtraList); + isSuccess = await MessageDB.shared.insertOrUpdateMsgExtras(msgExtraList); } if (msgList.isNotEmpty) { - MessageDB.shared.insertMsgList(msgList); + isSuccess = await MessageDB.shared.insertMsgList(msgList); } if (msgReactionList.isNotEmpty) { - ReactionDB.shared.insertOrUpdateReactionList(msgReactionList); + isSuccess = + await ReactionDB.shared.insertOrUpdateReactionList(msgReactionList); } + return isSuccess; } WKMsgExtra wkSyncExtraMsg2WKMsgExtra( diff --git a/pubspec.yaml b/pubspec.yaml index 4c915b8..466d426 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ description: wukong IM flutter sdk # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.3.0 +version: 1.3.1 homepage: https://github.com/WuKongIM/WuKongIMFlutterSDK environment: