diff --git a/Realtime/Realtime/Conversation/LCIMConversation.cs b/Realtime/Realtime/Conversation/LCIMConversation.cs index 749cc5a..d961d0a 100644 --- a/Realtime/Realtime/Conversation/LCIMConversation.cs +++ b/Realtime/Realtime/Conversation/LCIMConversation.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Threading.Tasks; using System.Linq; using System.Collections.ObjectModel; -using LeanCloud.Storage; namespace LeanCloud.Realtime { /// @@ -470,6 +469,17 @@ namespace LeanCloud.Realtime { await Client.ConversationController.FetchReciptTimestamp(Id); } + /// + /// Fetch conversation from server. + /// + /// + public async Task Fetch() { + LCIMConversationQuery query = new LCIMConversationQuery(Client); + query.WhereEqualTo("objectId", Id); + await query.Find(); + return this; + } + internal static bool IsTemporayConversation(string convId) { return convId.StartsWith("_tmp:"); } @@ -504,9 +514,28 @@ namespace LeanCloud.Realtime { IEnumerable ids = (muo as IList).Cast(); mutedIds = new HashSet(ids); } - //if (conv.TryGetValue("lm", out object lmo)) { - // LastMessageAt = (DateTime)LCDecoder.Decode(lmo); - //} + if (conv.TryGetValue("msg", out object msgo)) { + if (conv.TryGetValue("bin", out object bino)) { + string msg = msgo as string; + bool bin = (bool)bino; + if (bin) { + byte[] bytes = Convert.FromBase64String(msg); + LastMessage = LCIMBinaryMessage.Deserialize(bytes); + } else { + LastMessage = LCIMTypedMessage.Deserialize(msg); + } + } + LastMessage.ConversationId = Id; + if (conv.TryGetValue("msg_mid", out object msgId)) { + LastMessage.Id = msgId as string; + } + if (conv.TryGetValue("msg_from", out object msgFrom)) { + LastMessage.FromClientId = msgFrom as string; + } + if (conv.TryGetValue("msg_timestamp", out object timestamp)) { + LastMessage.SentTimestamp = (long)timestamp; + } + } } internal void MergeInfo(Dictionary attr) { diff --git a/Realtime/Realtime/Conversation/LCIMConversationQuery.cs b/Realtime/Realtime/Conversation/LCIMConversationQuery.cs index 372c1f8..725aa8d 100644 --- a/Realtime/Realtime/Conversation/LCIMConversationQuery.cs +++ b/Realtime/Realtime/Conversation/LCIMConversationQuery.cs @@ -5,12 +5,29 @@ using LeanCloud.Storage.Internal.Query; namespace LeanCloud.Realtime { public class LCIMConversationQuery { + internal const int CompactFlag = 0x1; + internal const int WithLastMessageFlag = 0x2; + internal LCCompositionalCondition Condition { get; private set; } private readonly LCIMClient client; + /// + /// Ignore the members of conversation. + /// + public bool Compact { + get; set; + } = false; + + /// + /// With the last message. + /// + public bool WithLastMessageRefreshed { + get; set; + } = false; + public LCIMConversationQuery(LCIMClient client) { Condition = new LCCompositionalCondition(); this.client = client; @@ -250,10 +267,6 @@ namespace LeanCloud.Realtime { return this; } - public bool WithLastMessageRefreshed { - get; set; - } - /// /// Retrieves a list of LCObjects matching this query. /// diff --git a/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs b/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs index 8bb4c35..a343635 100644 --- a/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs +++ b/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs @@ -355,6 +355,22 @@ namespace LeanCloud.Realtime.Internal.Controller { LCLogger.Error(e); } } + int flag = 0; + if (query.Compact) { + flag += LCIMConversationQuery.CompactFlag; + } + if (query.WithLastMessageRefreshed) { + flag += LCIMConversationQuery.WithLastMessageFlag; + } + if (flag > 0) { + convMessage.Flag = flag; + } + convMessage.Skip = query.Condition.Skip; + convMessage.Limit = query.Condition.Limit; + string orders = query.Condition.BuildOrders(); + if (!string.IsNullOrEmpty(orders)) { + convMessage.Sort = orders; + } command.ConvMessage = convMessage; GenericCommand response = await Connection.SendRequest(command); JsonObjectMessage results = response.ConvMessage.Results; diff --git a/Storage/Storage/Internal/Query/LCCompositionalCondition.cs b/Storage/Storage/Internal/Query/LCCompositionalCondition.cs index 7217927..3a0ac27 100644 --- a/Storage/Storage/Internal/Query/LCCompositionalCondition.cs +++ b/Storage/Storage/Internal/Query/LCCompositionalCondition.cs @@ -212,14 +212,17 @@ namespace LeanCloud.Storage.Internal.Query { if (conditionList != null && conditionList.Count > 0) { dict["where"] = JsonConvert.SerializeObject(Encode()); } - if (orderByList != null && orderByList.Count > 0) { - dict["order"] = string.Join(",", orderByList); + string order = BuildOrders(); + if (!string.IsNullOrEmpty(order)) { + dict["order"] = order; } - if (includes != null && includes.Count > 0) { - dict["include"] = string.Join(",", includes); + string includes = BuildIncludes(); + if (!string.IsNullOrEmpty(includes)) { + dict["include"] = includes; } - if (selectedKeys != null && selectedKeys.Count > 0) { - dict["keys"] = string.Join(",", selectedKeys); + string keys = BuildKeys(); + if (!string.IsNullOrEmpty(keys)) { + dict["keys"] = keys; } if (IncludeACL) { dict["returnACL"] = "true"; @@ -233,5 +236,26 @@ namespace LeanCloud.Storage.Internal.Query { } return JsonConvert.SerializeObject(Encode()); } + + public string BuildOrders() { + if (orderByList != null && orderByList.Count > 0) { + return string.Join(",", orderByList); + } + return null; + } + + public string BuildIncludes() { + if (includes != null && includes.Count > 0) { + return string.Join(",", includes); + } + return null; + } + + public string BuildKeys() { + if (selectedKeys != null && selectedKeys.Count > 0) { + return string.Join(",", selectedKeys); + } + return null; + } } }