diff --git a/Realtime/Conversation/LCIMChatRoom.cs b/Realtime/Conversation/LCIMChatRoom.cs index b521d8a..352b057 100644 --- a/Realtime/Conversation/LCIMChatRoom.cs +++ b/Realtime/Conversation/LCIMChatRoom.cs @@ -2,7 +2,7 @@ namespace LeanCloud.Realtime { public class LCIMChatRoom : LCIMConversation { - public LCIMChatRoom() { + public LCIMChatRoom(LCIMClient client) : base(client) { } } } diff --git a/Realtime/Conversation/LCIMConversation.cs b/Realtime/Conversation/LCIMConversation.cs index 8566857..4dc9867 100644 --- a/Realtime/Conversation/LCIMConversation.cs +++ b/Realtime/Conversation/LCIMConversation.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using System.Linq; +using LeanCloud.Realtime.Protocol; namespace LeanCloud.Realtime { public class LCIMConversation { @@ -34,8 +36,10 @@ namespace LeanCloud.Realtime { public virtual bool IsTransient => false; - public LCIMConversation() { + private readonly LCIMClient client; + internal LCIMConversation(LCIMClient client) { + this.client = client; } public void Set(string key, object value) { @@ -114,5 +118,23 @@ namespace LeanCloud.Realtime { public async Task> GetAllMemberInfo() { return null; } + + internal void MergeFrom(ConvCommand conv) { + if (conv.HasCid) { + Id = conv.Cid; + } + if (conv.HasInitBy) { + CreatorId = conv.InitBy; + } + if (conv.HasCdate) { + CreatedAt = DateTime.Parse(conv.Cdate); + } + if (conv.HasUdate) { + UpdatedAt = DateTime.Parse(conv.Udate); + } + if (conv.M.Count > 0) { + MemberIdList = conv.M.ToList(); + } + } } } diff --git a/Realtime/Conversation/LCIMTemporaryConversation.cs b/Realtime/Conversation/LCIMTemporaryConversation.cs index bbd0322..b4e26b3 100644 --- a/Realtime/Conversation/LCIMTemporaryConversation.cs +++ b/Realtime/Conversation/LCIMTemporaryConversation.cs @@ -2,7 +2,7 @@ namespace LeanCloud.Realtime { public class LCIMTemporaryConversation : LCIMConversation { - public LCIMTemporaryConversation() { + public LCIMTemporaryConversation(LCIMClient client) : base(client) { } } } diff --git a/Realtime/LCIMClient.cs b/Realtime/LCIMClient.cs index a38ddeb..3cadfa6 100644 --- a/Realtime/LCIMClient.cs +++ b/Realtime/LCIMClient.cs @@ -13,6 +13,8 @@ namespace LeanCloud.Realtime { private LCWebSocketClient client; + private Dictionary conversationDict; + /// /// 当前用户被加入某个对话的黑名单 /// @@ -68,6 +70,7 @@ namespace LeanCloud.Realtime { public LCIMClient(string clientId) { this.clientId = clientId; + conversationDict = new Dictionary(); } public async Task Open() { @@ -132,9 +135,9 @@ namespace LeanCloud.Realtime { } command.ConvMessage = conv; GenericCommand response = await client.SendRequest(command); - // TODO 实例化对话对象 - - LCIMConversation conversation = new LCIMConversation(); + LCIMConversation conversation = new LCIMConversation(this); + conversation.MergeFrom(response.ConvMessage); + conversationDict[conversation.Id] = conversation; return conversation; } @@ -174,11 +177,17 @@ namespace LeanCloud.Realtime { } private void OnConversationJoined(ConvCommand conv) { - OnInvited?.Invoke(null, conv.InitBy); + if (conversationDict.TryGetValue(conv.Cid, out LCIMConversation conversation)) { + conversation.MergeFrom(conv); + } + OnInvited?.Invoke(conversation, conv.InitBy); } private void OnConversationMembersJoined(ConvCommand conv) { - OnMembersJoined?.Invoke(null, conv.M.ToList(), conv.InitBy); + if (conversationDict.TryGetValue(conv.Cid, out LCIMConversation conversation)) { + conversation.MergeFrom(conv); + } + OnMembersJoined?.Invoke(conversation, conv.M.ToList(), conv.InitBy); } private GenericCommand NewCommand(CommandType cmd, OpType op) { diff --git a/Test/Realtime.Test/Conversation.cs b/Test/Realtime.Test/Conversation.cs index 8e97e4e..f747d58 100644 --- a/Test/Realtime.Test/Conversation.cs +++ b/Test/Realtime.Test/Conversation.cs @@ -30,10 +30,14 @@ namespace Realtime.Test { client.OnInvited = (conv, initBy) => { TestContext.WriteLine($"on invited: {initBy}"); + TestContext.WriteLine(conv.CreatorId); }; client.OnMembersJoined = (conv, memberList, initBy) => { TestContext.WriteLine($"on members joined: {initBy}"); + foreach (string memberId in conv.MemberIdList) { + TestContext.WriteLine(memberId); + } tcs.SetResult(null); };