Merge pull request #137 from onerain88/fix-conv-read

Fix conv read
oneRain 2021-06-15 14:26:54 +08:00 committed by GitHub
commit b76c9f6f76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 37 deletions

View File

@ -168,6 +168,7 @@ namespace LeanCloud.Realtime.Internal.Connection {
// 应答
int requestIndex = command.I;
if (requestToResponses.TryGetValue(command, out TaskCompletionSource<GenericCommand> tcs)) {
requestToResponses.Remove(command);
if (command.HasErrorMessage) {
// 错误
ErrorCommand error = command.ErrorMessage;
@ -179,7 +180,6 @@ namespace LeanCloud.Realtime.Internal.Connection {
} else {
tcs.TrySetResult(command);
}
requestToResponses.Remove(command);
} else {
LCLogger.Error($"No request for {requestIndex}");
}

View File

@ -186,10 +186,13 @@ namespace LeanCloud.Realtime.Internal.Controller {
LCIMMessage msg) {
ReadCommand read = new ReadCommand();
ReadTuple tuple = new ReadTuple {
Cid = convId,
Mid = msg.Id,
Timestamp = msg.SentTimestamp
Cid = convId
};
// 当不传 msg 时,服务端将其收到的最后一条消息标记为已读,可能与客户端不一致
if (msg != null) {
tuple.Mid = msg.Id;
tuple.Timestamp = msg.SentTimestamp;
}
read.Convs.Add(tuple);
GenericCommand command = NewCommand(CommandType.Read);
command.ReadMessage = read;

View File

@ -185,7 +185,7 @@ namespace LeanCloud.Realtime {
/// </summary>
/// <returns></returns>
public virtual async Task Read() {
if (LastMessage == null) {
if (Unread == 0) {
return;
}
await Client.MessageController.Read(Id, LastMessage);

View File

@ -384,21 +384,12 @@ namespace LeanCloud.Realtime {
if (string.IsNullOrEmpty(id)) {
throw new ArgumentNullException(nameof(id));
}
if (LCIMConversation.IsTemporayConversation(id)) {
List<LCIMTemporaryConversation> temporaryConversationList = await ConversationController.GetTemporaryConversations(new string[] { id });
if (temporaryConversationList == null || temporaryConversationList.Count < 1) {
ReadOnlyCollection<LCIMConversation> convs = await GetConversationList(new string[] { id });
if (convs == null || convs.Count() == 0) {
return null;
}
return temporaryConversationList[0];
}
LCIMConversationQuery query = GetQuery()
.WhereEqualTo("objectId", id)
.Limit(1);
ReadOnlyCollection<LCIMConversation> results = await ConversationController.Find(query);
if (results == null || results.Count < 1) {
return null;
}
return results[0];
return convs.First();
}
/// <summary>
@ -410,26 +401,12 @@ namespace LeanCloud.Realtime {
if (ids == null || ids.Count() == 0) {
throw new ArgumentNullException(nameof(ids));
}
// 区分临时对话
IEnumerable<string> tempConvIds = ids.Where(item => {
return LCIMConversation.IsTemporayConversation(item);
});
IEnumerable<string> convIds = ids.Where(item => {
return !tempConvIds.Contains(item);
});
List<LCIMConversation> conversationList = new List<LCIMConversation>();
if (tempConvIds.Count() > 0) {
List<LCIMTemporaryConversation> temporaryConversations = await ConversationController.GetTemporaryConversations(tempConvIds);
conversationList.AddRange(temporaryConversations);
IEnumerable<LCIMConversation> convs = await GetOrQueryConversations(ids);
if (convs == null) {
return default;
}
if (convIds.Count() > 0) {
LCIMConversationQuery query = GetQuery()
.WhereContainedIn("objectId", convIds)
.Limit(convIds.Count());
ReadOnlyCollection<LCIMConversation> conversations = await ConversationController.Find(query);
conversationList.AddRange(conversations);
}
return conversationList.AsReadOnly();
return convs.ToList().AsReadOnly();
}
/// <summary>
@ -485,5 +462,57 @@ namespace LeanCloud.Realtime {
conversation = await GetConversation(convId);
return conversation;
}
internal async Task<IEnumerable<LCIMConversation>> GetOrQueryConversations(IEnumerable<string> ids) {
if (ids == null || ids.Count() == 0) {
return default;
}
List<LCIMConversation> convs = new List<LCIMConversation>();
// 区分是否是缓存对话
IEnumerable<string> cachedConvIds = ids.Where(id => ConversationDict.ContainsKey(id));
IEnumerable<string> noCachedConvIds = ids.Except(cachedConvIds);
// 查找对话缓存
IEnumerable<LCIMConversation> cachedConvs = cachedConvIds.Select(id => {
return ConversationDict[id];
});
if (cachedConvs != null && cachedConvs.Count() > 0) {
convs.AddRange(cachedConvs);
}
// 区分临时对话
IEnumerable<LCIMConversation> queriedConvs = await QueryConversations(noCachedConvIds);
if (queriedConvs != null && queriedConvs.Count() > 0) {
convs.AddRange(queriedConvs);
}
return convs;
}
internal async Task<IEnumerable<LCIMConversation>> QueryConversations(IEnumerable<string> ids) {
if (ids == null || ids.Count() == 0) {
return default;
}
List<LCIMConversation> convs = new List<LCIMConversation>();
// 区分临时对话
IEnumerable<string> tempConvIds = ids.Where(item => {
return LCIMConversation.IsTemporayConversation(item);
});
IEnumerable<string> convIds = ids.Except(tempConvIds);
if (tempConvIds.Count() > 0) {
List<LCIMTemporaryConversation> temporaryConversations = await ConversationController.GetTemporaryConversations(tempConvIds);
convs.AddRange(temporaryConversations);
}
if (convIds.Count() > 0) {
LCIMConversationQuery query = GetQuery()
.WhereContainedIn("objectId", convIds)
.Limit(convIds.Count());
ReadOnlyCollection<LCIMConversation> conversations = await ConversationController.Find(query);
convs.AddRange(conversations);
}
return convs;
}
}
}