示例#1
0
        private void getLogic(bool getUsers)
        {
            APICaller caller = new APICaller("getConversations.php").AddParam("privateKey", PrivateKey);

            if (getUsers)
            {
                caller.AddParam("user", "");
            }
            JObject obj = caller.Call();

            //Console.WriteLine(obj);
            if (obj["status"].ToString() == "success")
            {
                List <Conversation> conversations = new List <Conversation>();

                Dictionary <string, User> users = null;
                obj["conversations"].ToList().ForEach((conversation) =>
                {
                    var conv = new Conversation
                    {
                        Id           = conversation["id"].ToObject <int>(),
                        Title        = conversation["title"].ToString(),
                        Messages     = conversation["messages"].ToObject <int>(),
                        LastActivity = conversation["lastActivity"].ToObject <long>(),
                        LastMessage  = conversation["lastMessage"].ToString()
                    };
                    try
                    {
                        conversation["users"].ToList().ForEach((user) =>
                        {
                            conv.Users.Add(user.First.ToString());
                        });
                    }
                    catch (InvalidOperationException e)
                    {
                        conversation["users"].ToList().ForEach((user) =>
                        {
                            conv.Users.Add(user.ToString());
                        });
                    }
                    conversations.Add(conv);
                });


                if (getUsers)
                {
                    users = new Dictionary <string, User>();
                    obj["users"].ToList().ForEach((user) =>
                    {
                        users[user.First["username"].ToString()] = new User
                        {
                            Username               = user.First["username"].ToString(),
                            FirstName              = user.First["firstName"].ToString(),
                            LastName               = user.First["lastName"].ToString(),
                            KarmaPoints            = user.First["karmaPoints"].ToObject <int>(),
                            IsLecturer             = Convert.ToBoolean(user.First["lecturer"].ToObject <int>()),
                            ProfilePictureLocation = user.First["profilePicture"].ToString(),
                        };
                    });
                }
                GetConversationsResult(GetStatus.Success, conversations, users);
            }
            else
            {
                GetStatus status = GetStatus.UnknownError;
                if (!Enum.TryParse <GetStatus>(obj["message"].ToString(), out status))
                {
                    status = GetStatus.UnknownError;
                }
                GetConversationsResult(status, null, null);
            }
        }