Example #1
0
        internal async Task <List <LCIMTemporaryConversation> > GetTemporaryConversations(IEnumerable <string> convIds)
        {
            if (convIds == null || convIds.Count() == 0)
            {
                return(null);
            }
            ConvCommand convMessage = new ConvCommand();

            convMessage.TempConvIds.AddRange(convIds);
            GenericCommand request = NewCommand(CommandType.Conv, OpType.Query);

            request.ConvMessage = convMessage;
            GenericCommand response = await Connection.SendRequest(request);

            JsonObjectMessage results = response.ConvMessage.Results;
            List <object>     convs   = JsonConvert.DeserializeObject <List <object> >(results.Data,
                                                                                       LCJsonConverter.Default);
            List <LCIMTemporaryConversation> convList = convs.Select(item => {
                LCIMTemporaryConversation temporaryConversation = new LCIMTemporaryConversation(Client);
                temporaryConversation.MergeFrom(item as Dictionary <string, object>);
                return(temporaryConversation);
            }).ToList();

            return(convList);
        }
Example #2
0
        internal async Task <ReadOnlyCollection <LCIMConversation> > Find(LCIMConversationQuery query)
        {
            GenericCommand command = new GenericCommand {
                Cmd    = CommandType.Conv,
                Op     = OpType.Query,
                AppId  = LCCore.AppId,
                PeerId = Client.Id,
            };
            ConvCommand convMessage = new ConvCommand();

            string where = query.Condition.BuildWhere();
            if (!string.IsNullOrEmpty(where))
            {
                try {
                    convMessage.Where = new JsonObjectMessage {
                        Data = where
                    };
                } catch (Exception e) {
                    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;
            List <object>     convs   = JsonConvert.DeserializeObject <List <object> >(results.Data,
                                                                                       LCJsonConverter.Default);

            return(convs.Select(item => {
                Dictionary <string, object> conv = item as Dictionary <string, object>;
                string convId = conv["objectId"] as string;
                if (!Client.ConversationDict.TryGetValue(convId, out LCIMConversation conversation))
                {
                    // 解析是哪种类型的对话
                    if (conv.TryGetValue("tr", out object transient) && (bool)transient == true)
                    {
                        conversation = new LCIMChatRoom(Client);
                    }
                    else if (conv.ContainsKey("tempConv") && conv.ContainsKey("tempConvTTL"))
                    {
                        conversation = new LCIMTemporaryConversation(Client);
                    }
                    else if (conv.TryGetValue("sys", out object sys) && (bool)sys == true)
                    {
                        conversation = new LCIMServiceConversation(Client);
                    }
                    else
                    {
                        conversation = new LCIMConversation(Client);
                    }
                    Client.ConversationDict[convId] = conversation;
                }
                conversation.MergeFrom(conv);
                return conversation;
            }).ToList().AsReadOnly());
        }
Example #3
0
        internal async Task <LCIMConversation> CreateConv(
            IEnumerable <string> members = null,
            string name      = null,
            bool transient   = false,
            bool unique      = true,
            bool temporary   = false,
            int temporaryTtl = 86400,
            Dictionary <string, object> properties = null)
        {
            GenericCommand request = NewCommand(CommandType.Conv, OpType.Start);
            ConvCommand    conv    = new ConvCommand {
                Transient = transient,
                Unique    = unique,
            };

            if (members != null)
            {
                conv.M.AddRange(members);
            }
            if (temporary)
            {
                conv.TempConv    = temporary;
                conv.TempConvTTL = temporaryTtl;
            }
            Dictionary <string, object> attrs = new Dictionary <string, object>();

            if (!string.IsNullOrEmpty(name))
            {
                attrs["name"] = name;
            }
            if (properties != null)
            {
                attrs = properties.Union(attrs.Where(kv => !properties.ContainsKey(kv.Key)))
                        .ToDictionary(k => k.Key, v => v.Value);
            }
            conv.Attr = new JsonObjectMessage {
                Data = JsonConvert.SerializeObject(LCEncoder.Encode(attrs))
            };
            if (Client.SignatureFactory != null)
            {
                LCIMSignature signature = await Client.SignatureFactory.CreateStartConversationSignature(Client.Id, members);

                conv.S = signature.Signature;
                conv.T = signature.Timestamp;
                conv.N = signature.Nonce;
            }
            request.ConvMessage = conv;
            GenericCommand response = await Connection.SendRequest(request);

            string convId = response.ConvMessage.Cid;

            if (!Client.ConversationDict.TryGetValue(convId, out LCIMConversation conversation))
            {
                if (transient)
                {
                    conversation = new LCIMChatRoom(Client);
                }
                else if (temporary)
                {
                    conversation = new LCIMTemporaryConversation(Client);
                }
                else if (properties != null && properties.ContainsKey("system"))
                {
                    conversation = new LCIMServiceConversation(Client);
                }
                else
                {
                    conversation = new LCIMConversation(Client);
                }
                Client.ConversationDict[convId] = conversation;
            }
            // 合并请求数据
            conversation.Id        = convId;
            conversation.Unique    = unique;
            conversation.UniqueId  = response.ConvMessage.UniqueId;
            conversation.Name      = name;
            conversation.CreatorId = Client.Id;
            conversation.ids       = members != null ?
                                     new HashSet <string>(members) : new HashSet <string>();
            // 将自己加入
            conversation.ids.Add(Client.Id);
            conversation.CreatedAt = DateTime.Parse(response.ConvMessage.Cdate);
            conversation.UpdatedAt = conversation.CreatedAt;
            return(conversation);
        }