public static List <Consumer> GetTop20FriendListOf(long userId, string keyword)
        {
            List <JObject> fetchedFriendListJson = null;

            ServerHub.WorkingInstance.ServerHubProxy.Invoke <List <JObject> >("GetTop20FriendListOf", userId, keyword).ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    fetchedFriendListJson = task.Result;
                }
            }).Wait();
            List <Consumer> fetchedFriendList = new List <Consumer>();

            if (fetchedFriendListJson == null)
            {
                return(fetchedFriendList);
            }
            foreach (JObject consumerJson in fetchedFriendListJson)
            {
                Consumer consumer = new Consumer(consumerJson);
                ServerFileRequest.RefetchProfileImage(consumer.ProfileImageId);
                fetchedFriendList.Add(consumer);
            }
            return(fetchedFriendList);
        }
        public static bool?SetLoggedInUser(JObject loginDataJson)
        {
            List <double> encryptedCookieData      = MatrixCryptography.Encrypt(loginDataJson.ToString());
            List <double> encryptedFetchedUserData = null;

            ServerHub.WorkingInstance.ServerHubProxy.Invoke <List <double> >("LoginWithEncryptedData", encryptedCookieData).ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    encryptedFetchedUserData = task.Result;
                }
                else
                {
                    Console.WriteLine(task.Exception.InnerException.Message);
                }
            }).Wait();
            if (encryptedFetchedUserData == null)
            {
                return(null);
            }
            JObject fetchedUserDataJson = JObject.Parse(MatrixCryptography.Decrypt(encryptedFetchedUserData));
            User    loggedInUser        = null;

            if (bool.Parse(fetchedUserDataJson["found"].ToString()) == true)
            {
                string type = fetchedUserDataJson["type"].ToString();
                if (type == "consumer")
                {
                    Consumer.LoggedIn = new Consumer(fetchedUserDataJson);
                    loggedInUser      = Consumer.LoggedIn;
                    if (fetchedUserDataJson.ContainsKey("profile_img_id") && fetchedUserDataJson["profile_img_id"].ToString().Length >= 5)
                    {
                        ServerFileRequest.RefetchProfileImage(fetchedUserDataJson["profile_img_id"].ToString());
                        Consumer.LoggedIn.SetProfileImage(fetchedUserDataJson["profile_img_id"].ToString());
                    }
                }
            }
            else
            {
                return(false);
            }
            User.LoggedIn = loggedInUser;
            return(true);
        }
        public static List <JObject> GetFriendRequestsByKeyword(long userId, string keyword)
        {
            List <JObject> fetchedFriendListJson = null;

            ServerHub.WorkingInstance.ServerHubProxy.Invoke <List <JObject> >("GetFriendRequestListOf", userId, keyword).ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    fetchedFriendListJson = task.Result;
                }
            }).Wait();
            if (fetchedFriendListJson == null)
            {
                return(new List <JObject>());
            }
            foreach (JObject consumerJson in fetchedFriendListJson)
            {
                try { ServerFileRequest.RefetchProfileImage(consumerJson["profile_img_id"].ToString()); } catch { }
            }
            return(fetchedFriendListJson);
        }
        public static bool SyncConsumer(long userId)
        {
            Time lastSynced = ConsumerRepository.Instance.LastSyncedTime(userId);

            if (lastSynced != null && Time.TimeDistanceInMinute(lastSynced, Time.CurrentTime) <= 360)
            {
                //consumer will never be synced if already synced within last 6 hours
                return(true);
            }
            JObject consumerJson = null;

            Console.WriteLine("SyncConsumer() => " + userId);
            ServerHub.WorkingInstance.ServerHubProxy.Invoke <JObject>("GetConsumer", userId).ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    consumerJson = task.Result;
                }
            }).Wait();
            if (consumerJson == null)
            {
                return(false);
            }
            Consumer consumer = new Consumer(userId, consumerJson["username"] + "", consumerJson["email"] + "", consumerJson["name"] + "");

            try { consumer.ProfileImageId = consumerJson["profile_img_id"].ToString(); }
            catch { }
            ServerFileRequest.RefetchProfileImage(consumer.ProfileImageId);
            bool?success = ConsumerRepository.Instance.Update(consumer);

            if (success == null || success == false)
            {
                return(false);
            }
            return(true);
        }