Ejemplo n.º 1
0
        internal async Task <bool> SendMessageAsync(string content, int userid)
        {
            try
            {
                JSONRepresentations.Post.Message m = new JSONRepresentations.Post.Message()
                {
                    message = content,
                    to      = userid
                };

                SparklrResponse <string> result = await webClient.PostJsonAsyncRawResponse("chat", m);

                if (result.IsOkAndTrue())
                {
                    return(true);
                }
                else if (result.Code == System.Net.HttpStatusCode.BadRequest)
                {
                    throw new ArgumentException("The message you wanted to send was not accepted by the server, maybe it was too long?", content);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exceptions.NotAuthorizedException)
            {
                throw new Exceptions.NotAuthorizedException("You are either not authorized or blocked by the specified user.");
            }
        }
Ejemplo n.º 2
0
        internal async Task <Sparklr.Notification[]> GetNotificationsAsync()
        {
            SparklrResponse <JSONRepresentations.Get.Notification[]> result = await webClient.GetJSONResponseAsync <JSONRepresentations.Get.Notification[]>("notifications");

            if (result.Code == System.Net.HttpStatusCode.OK)
            {
                Sparklr.Notification[] notifications = new Sparklr.Notification[result.Response.Length];

                for (int i = 0; i < result.Response.Length; i++)
                {
                    notifications[i] = new Sparklr.Notification(
                        result.Response[i].id,
                        await Sparklr.User.InstanciateUserAsync(result.Response[i].from, this),
                        await Sparklr.User.InstanciateUserAsync(result.Response[i].to, this),
                        (Sparklr.NotificationType)result.Response[i].type,
                        result.Response[i].time,
                        result.Response[i].body,
                        result.Response[i].action
                        );
                }

                return(notifications);
            }
            else
            {
                throw new Exceptions.NoDataFoundException();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Retreives a conversation asynchronously
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="since"></param>
        /// <returns></returns>
        internal async Task <Message[]> GetConversationSinceAsync(int userId, long since)
        {
            SparklrResponse <JSONRepresentations.Get.Message[]> response = await webClient.GetJSONResponseAsync <JSONRepresentations.Get.Message[]>("chat", userId + "?since=" + since);

            Sparklr.Message[] messages = await parseJSONMessages(response);

            return(messages);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// retreives the post by the given id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal async Task <Post> GetPostByIdAsync(int id)
        {
            SparklrResponse <JSONRepresentations.Get.Post> response = await webClient.GetJSONResponseAsync <JSONRepresentations.Get.Post>("post", id);

            Post p = await Post.InstanciatePostAsync(response.Response, this);

            return(p);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Retreives all messages
        /// </summary>
        /// <returns></returns>
        internal async Task <Sparklr.Message[]> GetInboxAsync()
        {
            SparklrResponse <JSONRepresentations.Get.Message[]> response = await webClient.GetJSONResponseAsync <JSONRepresentations.Get.Message[]>("inbox");

            Sparklr.Message[] messages = await parseJSONMessages(response);

            return(messages);
        }
        /// <summary>
        /// Retreives the staff members from sparklr
        /// </summary>
        /// <returns>An Array of Staff members</returns>
        public async Task <User[]> GetStaffAsync()
        {
            SparklrResponse <SparklrSharp.JSONRepresentations.Get.UserMinimal[]> response = await webClient.GetJSONResponseAsync <SparklrSharp.JSONRepresentations.Get.UserMinimal[]>("staff");

            User[] staffMembers = new User[response.Response.Length];

            for (int i = 0; i < response.Response.Length; i++)
            {
                staffMembers[i] = await User.InstanciateUserAsync(response.Response[i].id, this);
            }

            return(staffMembers);
        }
Ejemplo n.º 7
0
        private async Task<Post[]> extractPostsAsync(SparklrResponse<JSONRepresentations.Get.Post[]> response)
        {
            List<Post> posts = new List<Post>();

            foreach (JSONRepresentations.Get.Post p in response.Response)
            {
                Post po = await Post.InstanciatePostAsync(p, this);

                if (p != null)
                    posts.Add(po);
            }

            return posts.ToArray();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Extracts message information from a JSON object and turns them into a Sparklr.Message
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        private async Task <Message[]> parseJSONMessages(SparklrResponse <JSONRepresentations.Get.Message[]> response)
        {
            Sparklr.Message[] messages = new Sparklr.Message[response.Response.Length];

            for (int i = 0; i < response.Response.Length; i++)
            {
                messages[i] = await Message.CreateMessageAsync(
                    response.Response[i].message,
                    response.Response[i].time,
                    response.Response[i].from,
                    this
                    );
            }
            return(messages);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Extracts message information from a JSON object and turns them into a Sparklr.Message
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        private async Task<Message[]> parseJSONMessages(SparklrResponse<JSONRepresentations.Get.Message[]> response)
        {
            Sparklr.Message[] messages = new Sparklr.Message[response.Response.Length];

            for (int i = 0; i < response.Response.Length; i++)
            {
                messages[i] = await Message.CreateMessageAsync(
                        response.Response[i].message,
                        response.Response[i].time,
                        response.Response[i].from,
                        this
                    );
            }
            return messages;
        }
Ejemplo n.º 10
0
        private async Task <Post[]> extractPostsAsync(SparklrResponse <JSONRepresentations.Get.Post[]> response)
        {
            Post[] posts = new Post[response.Response.Length];

            int i = 0;

            foreach (JSONRepresentations.Get.Post p in response.Response)
            {
                posts[i] = await Post.InstanciatePostAsync(p, this);

                i++;
            }

            return(posts);
        }
Ejemplo n.º 11
0
        private async Task<Comment[]> extractComments(SparklrResponse<JSONRepresentations.Get.Comment[]> response)
        {
            Comment[] comments = new Comment[response.Response.Length];

            int i = 0;
            foreach (JSONRepresentations.Get.Comment c in response.Response)
            {
                comments[i] = Comment.InstanciateComment(
                                            c.id,
                                            await Post.GetPostByIdAsync(c.postid, this),
                                            await User.InstanciateUserAsync(c.from, this),
                                            c.message,
                                            c.time
                                        );
                i++;
            }

            return comments;
        }
        private async Task<Comment[]> extractComments(SparklrResponse<JSONRepresentations.Get.Comment[]> response)
        {
            Comment[] comments = new Comment[response.Response.Length];

            int i = 0;
            foreach (JSONRepresentations.Get.Comment c in response.Response)
            {
                comments[i] = Comment.InstanciateComment(
                                            c.id,
                                            await Post.GetPostByIdAsync(c.postid, this),
                                            await User.InstanciateUserAsync(c.from, this),
                                            c.message,
                                            c.time
                                        );
                i++;
            }

            return comments;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Retreives the user from the sparklr service. Throws a NoDataFoundException if the specified user is invalid.
        /// Retreived Users are cached.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal async Task <Sparklr.User> GetUserAsync(int id)
        {
            try
            {
                SparklrResponse <JSONRepresentations.Get.User> result = await webClient.GetJSONResponseAsync <JSONRepresentations.Get.User>("user", id);

                if (result.Code == System.Net.HttpStatusCode.OK)
                {
                    User u = User.InstanciateUser(
                        result.Response.user,
                        result.Response.name,
                        result.Response.handle,
                        result.Response.avatarid ?? -1,
                        result.Response.following,
                        result.Response.bio);

                    foreach (JSONRepresentations.Get.Post p in result.Response.timeline)
                    {
                        u.timeline.Add(
                            await Post.InstanciatePostAsync(p, this)
                            );
                    }

                    return(u);
                }
                else
                {
                    throw new Exceptions.NoDataFoundException();
                }
            }
            catch (Exceptions.InvalidResponseException ex)
            {
                if (ex.Response.IsOkAndFalse() || ex.Response.Code == System.Net.HttpStatusCode.NotFound)
                {
                    throw new Exceptions.NoDataFoundException();
                }
                else
                {
                    throw;
                }
            }
        }
        public async Task <bool> SigninAsync(string username, string password)
        {
            try
            {
                SparklrResponse <string> response = await webClient.GetRawResponseAsync("signin", username, password);

                if (response.IsOkAndTrue())
                {
                    Authenticated = true;
                    return(true);
                }
                else
                {
                    Authenticated = false;
                    return(false);
                }
            }
            catch (NotAuthorizedException)
            {
                Authenticated = false;
                return(false);
            }
        }
Ejemplo n.º 15
0
        //TODO: Will dismiss similar notifications (i.e. notifications for the same conversation) as well.
        //TODO: Set Dismissed-Flag properly
        internal async Task <bool> DismissNotificationAsync(Notification n)
        {
            SparklrResponse <JSONRepresentations.Get.MysqlResult> response = await webClient.GetJSONResponseAsync <JSONRepresentations.Get.MysqlResult>("dismiss", n.Id);

            return((response.Code == System.Net.HttpStatusCode.OK) && (response.Response.affectedRows >= 1));
        }
 internal static bool IsOkAndFalse(this SparklrResponse <string> response)
 {
     return((response.Code == System.Net.HttpStatusCode.OK) && (response.Response == "false"));
 }
        internal async Task <Post[]> GetMentionsAsync(int userid, int starttime)
        {
            SparklrResponse <JSONRepresentations.Get.Post[]> response = await webClient.GetJSONResponseAsync <JSONRepresentations.Get.Post[]>("mentions", userid + "?starttime=" + starttime);

            return(await extractPostsAsync(response));
        }
Ejemplo n.º 18
0
        internal async Task <Post[]> GetStreamAsync(string name, int starttime)
        {
            SparklrResponse <JSONRepresentations.Get.Post[]> response = await webClient.GetJSONResponseAsync <JSONRepresentations.Get.Post[]>("stream", name + "?starttime=" + starttime);

            return(await extractPostsAsync(response));
        }
        /// <summary>
        /// Checks if sparklr is running
        /// </summary>
        /// <returns>true if it is, otherwise false</returns>
        public async Task <bool> GetAwakeAsync()
        {
            SparklrResponse <string> response = await webClient.GetRawResponseAsync("areyouawake");

            return(response.IsOkAndTrue());
        }
        /// <summary>
        /// Retreives the comments for the given post
        /// </summary>
        /// <param name="id"></param>
        /// <param name="time">The time after which to retreive the comments</param>
        /// <returns></returns>
        internal async Task<Comment[]> GetCommentsForPostAsync(int id, int time)
        {
            SparklrResponse<JSONRepresentations.Get.Comment[]> response = await webClient.GetJSONResponseAsync<JSONRepresentations.Get.Comment[]>("comments", id + "?since=" + time);

            return await extractComments(response);
        }