getOrSyncHistoryMessages method
void
getOrSyncHistoryMessages()
Implementation
void getOrSyncHistoryMessages(
String channelId,
int channelType,
int oldestOrderSeq,
bool contain,
int pullMode,
int limit,
final Function(List<WKMsg>) iGetOrSyncHistoryMsgBack,
final Function() syncBack) async {
//获取原始数据
List<WKMsg> list = await getMessages(
channelId, channelType, oldestOrderSeq, contain, pullMode, limit);
//业务判断数据
List<WKMsg> tempList = [];
for (int i = 0, size = list.length; i < size; i++) {
tempList.add(list[i]);
}
//先通过message_seq排序
if (tempList.isNotEmpty) {
tempList.sort((a, b) => a.messageSeq.compareTo(b.messageSeq));
}
//获取最大和最小messageSeq
int minMessageSeq = 0;
int maxMessageSeq = 0;
for (int i = 0, size = tempList.length; i < size; i++) {
if (tempList[i].messageSeq != 0) {
if (minMessageSeq == 0) minMessageSeq = tempList[i].messageSeq;
if (tempList[i].messageSeq > maxMessageSeq) {
maxMessageSeq = tempList[i].messageSeq;
}
if (tempList[i].messageSeq < minMessageSeq) {
minMessageSeq = tempList[i].messageSeq;
}
}
}
//是否同步消息
bool isSyncMsg = false;
int startMsgSeq = 0;
int endMsgSeq = 0;
//判断页与页之间是否连续
int oldestMsgSeq;
//如果获取到的messageSeq为0说明oldestOrderSeq这条消息是本地消息则获取他上一条或下一条消息的messageSeq做为判断
if (oldestOrderSeq % 1000 != 0) {
oldestMsgSeq =
await getMsgSeq(channelId, channelType, oldestOrderSeq, pullMode);
} else {
oldestMsgSeq = oldestOrderSeq ~/ 1000;
}
if (pullMode == 0) {
//下拉获取消息
if (oldestMsgSeq == 1) {
iGetOrSyncHistoryMsgBack([]);
return;
}
if (maxMessageSeq != 0 &&
oldestMsgSeq != 0 &&
oldestMsgSeq - maxMessageSeq > 1) {
isSyncMsg = true;
if (contain) {
startMsgSeq = oldestMsgSeq;
} else {
startMsgSeq = oldestMsgSeq - 1;
}
endMsgSeq = maxMessageSeq;
}
} else {
//上拉获取消息
if (minMessageSeq != 0 &&
oldestMsgSeq != 0 &&
minMessageSeq - oldestMsgSeq > 1) {
isSyncMsg = true;
if (contain) {
startMsgSeq = oldestMsgSeq;
} else {
startMsgSeq = oldestMsgSeq + 1;
}
endMsgSeq = minMessageSeq;
}
}
if (!isSyncMsg) {
//判断当前页是否连续
for (int i = 0, size = tempList.length; i < size; i++) {
int nextIndex = i + 1;
if (nextIndex < tempList.length) {
if (tempList[nextIndex].messageSeq != 0 &&
tempList[i].messageSeq != 0 &&
tempList[nextIndex].messageSeq - tempList[i].messageSeq > 1) {
//判断该条消息是否被删除
int num = await getDeletedCount(tempList[i].messageSeq,
tempList[nextIndex].messageSeq, channelId, channelType);
if (num <
(tempList[nextIndex].messageSeq - tempList[i].messageSeq) - 1) {
isSyncMsg = true;
int max = tempList[nextIndex].messageSeq;
int min = tempList[i].messageSeq;
if (tempList[nextIndex].messageSeq < tempList[i].messageSeq) {
max = tempList[i].messageSeq;
min = tempList[nextIndex].messageSeq;
}
if (pullMode == 0) {
// 下拉
if (max > startMsgSeq) {
startMsgSeq = max;
}
if (endMsgSeq == 0 || min < endMsgSeq) {
endMsgSeq = min;
}
} else {
if (startMsgSeq == 0 || min < startMsgSeq) {
startMsgSeq = min;
}
if (max > endMsgSeq) {
endMsgSeq = max;
}
}
}
}
}
}
}
if (!isSyncMsg) {
if (minMessageSeq == 1) {
requestCount = 0;
iGetOrSyncHistoryMsgBack(list);
return;
}
}
//计算最后一页后是否还存在消息
int syncLimit = limit;
if (!isSyncMsg && tempList.length < limit) {
isSyncMsg = true;
if (contain) {
startMsgSeq = oldestMsgSeq;
} else {
if (pullMode == 0) {
startMsgSeq = oldestMsgSeq - 1;
} else {
startMsgSeq = oldestMsgSeq + 1;
}
}
endMsgSeq = 0;
}
if (startMsgSeq == 0 && endMsgSeq == 0 && tempList.length < limit) {
isSyncMsg = true;
endMsgSeq = oldestMsgSeq;
startMsgSeq = 0;
}
if (isSyncMsg &&
(startMsgSeq != endMsgSeq || (startMsgSeq == 0 && endMsgSeq == 0)) &&
requestCount < 5) {
if (requestCount == 0) {
syncBack();
}
//同步消息
requestCount++;
WKIM.shared.messageManager.setSyncChannelMsgListener(
channelId, channelType, startMsgSeq, endMsgSeq, syncLimit, pullMode,
(syncChannelMsg) {
if (syncChannelMsg != null) {
if (oldestMsgSeq == 0 ||
(syncChannelMsg.messages != null &&
syncChannelMsg.messages!.length < limit)) {
requestCount = 5;
}
getOrSyncHistoryMessages(channelId, channelType, oldestOrderSeq,
contain, pullMode, limit, iGetOrSyncHistoryMsgBack, syncBack);
} else {
requestCount = 0;
iGetOrSyncHistoryMsgBack(list);
}
});
} else {
requestCount = 0;
iGetOrSyncHistoryMsgBack(list);
}
}