Ejemplo n.º 1
0
        private void ParseComments(JToken data, Thing sender)
        {
            // Parse sub comments
            var replies     = data["data"]["replies"];
            var subComments = new List <Thing>();

            if (replies != null && replies.Count() > 0)
            {
                foreach (var comment in replies["data"]["children"])
                {
                    var newComment = new Comment(WebAgent, comment, sender);
                    if (newComment.Kind != "more")
                    {
                        subComments.Add(newComment);
                    }
                    else if (newComment.Kind == "more")
                    {
                        var more = new More(WebAgent, comment);
                        if (more.Children.Count() > 0)
                        {
                            subComments.Add(more);
                        }
                    }
                }
            }
            Comments = subComments.ToArray();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Enumerate more comments.
        /// </summary>
        /// <returns></returns>
        public IEnumerable <Comment> EnumerateComments()
        {
            var  url          = string.Format(GetCommentsUrl, Id);
            var  request      = WebAgent.CreateGet(url);
            var  response     = request.GetResponse();
            var  data         = WebAgent.GetResponseString(response.GetResponseStream());
            var  json         = JArray.Parse(data);
            var  postJson     = json.Last()["data"]["children"];
            More moreComments = null;

            foreach (var comment in postJson)
            {
                Comment newComment = new Comment().Init(Reddit, comment, WebAgent, this);
                if (newComment.Kind == "more")
                {
                    moreComments = new More().Init(Reddit, comment, WebAgent);
                }
                else
                {
                    yield return(newComment);
                }
            }


            if (moreComments != null)
            {
                IEnumerator <Thing> things = moreComments.Things().GetEnumerator();
                things.MoveNext();
                Thing currentThing = null;
                while (currentThing != things.Current)
                {
                    currentThing = things.Current;
                    if (things.Current is Comment)
                    {
                        Comment next = ((Comment)things.Current).PopulateComments(things);
                        yield return(next);
                    }
                    if (things.Current is More)
                    {
                        More more = (More)things.Current;
                        if (more.ParentId != FullName)
                        {
                            break;
                        }
                        things = more.Things().GetEnumerator();
                        things.MoveNext();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Fill the object with comments.
        /// </summary>
        /// <param name="things"></param>
        /// <returns></returns>
        public Comment PopulateComments(IEnumerator <Thing> things)
        {
            Thing first = things.Current;
            Dictionary <string, Tuple <Comment, List <Comment> > > comments = new Dictionary <string, Tuple <Comment, List <Comment> > >
            {
                [this.FullName] = Tuple.Create(this, new List <Comment>())
            };

            while (things.MoveNext() && (first is Comment || first is More))
            {
                first = things.Current;
                if (first is Comment)
                {
                    Comment comment = (Comment)first;
                    comments[comment.FullName] = Tuple.Create <Comment, List <Comment> >(comment, new List <Comment>());
                    if (comments.ContainsKey(comment.ParentId))
                    {
                        comments[comment.ParentId].Item2.Add(comment);
                    }
                    else if (comment.ParentId == this.ParentId)
                    {
                        //only want sub comments.
                        break;
                    }
                }
                else if (first is More)
                {
                    More more = (More)first;
                    if (comments.ContainsKey(more.ParentId))
                    {
                        comments[more.ParentId].Item1.More = more;
                    }
                    else if (more.ParentId == this.ParentId)
                    {
                        // This is more for parent.
                        // Need to process the comments dictionary.
                        break;
                    }
                }
                //things.MoveNext();
            }

            foreach (KeyValuePair <string, Tuple <Comment, List <Comment> > > kvp in comments)
            {
                kvp.Value.Item1.Comments = kvp.Value.Item2.ToArray();
            }

            return(this);
        }
Ejemplo n.º 4
0
        // Awaitables don't have to be called asyncronously
        /// <summary>
        /// Tries to find the "Thing" you are looking for
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="agent"></param>
        /// <param name="json"></param>
        /// <returns>The "Thing"</returns>
        public static T Parse <T>(IWebAgent agent, JToken json) where T : Thing
        {
            Thing result = Parse(agent, json);

            if (result == null)
            {
                if (typeof(T) == typeof(WikiPageRevision))
                {
                    result = new WikiPageRevision(agent, json);
                }
                else if (typeof(T) == typeof(ModAction))
                {
                    result = new ModAction(agent, json);
                }
                else if (typeof(T) == typeof(Contributor))
                {
                    result = new Contributor(agent, json);
                }
                else if (typeof(T) == typeof(BannedUser))
                {
                    result = new BannedUser(agent, json);
                }
                else if (typeof(T) == typeof(More))
                {
                    result = new More(agent, json);
                }
                else if (typeof(T) == typeof(LiveUpdate))
                {
                    result = new LiveUpdate(agent, json);
                }
                else if (typeof(T) == typeof(LiveUpdateEvent))
                {
                    result = new LiveUpdateEvent(agent, json);
                }
            }
            return(result as T);
        }