示例#1
0
        public void GetCommentsWithEditsAndMoreAndTruncate()
        {
            CommentContainer res = reddit.Models.Listings.GetComments("8gmf99", new ListingsGetCommentsInput(0, true, true, "top", true, 50));

            Assert.IsNotNull(res);
            Validate(res);
        }
示例#2
0
        public void GetCommentsWithContext()
        {
            CommentContainer res = reddit.Models.Listings.GetComments("8gmf99", new ListingsGetCommentsInput(8, true, true, "top", true, 0, "dyd2vtc"), "FloridaMan");

            Assert.IsNotNull(res);
            Validate(res);
        }
示例#3
0
        public static Models.CommentContainer RemoveComment(string commentId, string containerType, string containerId, string portalId = null, string userId = null)
        {
            portalId = string.IsNullOrEmpty(portalId) ? Portal.CurrentPortalId : portalId;
            userId   = string.IsNullOrEmpty(userId) ? Account.AuditId : userId;
            var container = Get(containerType, containerId, portalId);

            if (container == null)
            {
                container = new CommentContainer()
                {
                    ContainerType = containerType, ContainerId = containerId, PortalId = portalId
                }
            }
            ;

            var comment = container.Comments.Where(c => c.Id == commentId).FirstOrDefault();

            if (comment != null)
            {
                container.Comments.Remove(comment);
                Save(container, userId);
            }
            else
            {
                throw new Exception("Comment not found");   //todo:  exception?
            }
            return(container);
        }
示例#4
0
        public void GetComments()
        {
            CommentContainer res = reddit.Models.Listings.GetComments("9gaze5", new ListingsGetCommentsInput(0, false, false, "top", true, 0));

            Assert.IsNotNull(res);
            Validate(res);
        }
        // Regarding loadReplies:  https://www.reddit.com/r/redditdev/comments/dqz2jy/api_bug_the_apiinfo_endpoint_does_not_populate/
        /// <summary>
        /// Return a listing of things specified by their fullnames.
        /// Only Links, Comments, and Subreddits are allowed.
        /// </summary>
        /// <param name="id">A comma-separated list of thing fullnames</param>
        /// <param name="subreddit">The subreddit with the listing</param>
        /// <param name="loadReplies">The Info endpoint doesn't include replies; if loadReplies is true, an additional API call will be triggered to retrieve the replies (default: true)</param>
        /// <returns>The requested listings.</returns>
        public Info Info(string id = null, string subreddit = null, bool loadReplies = true, string url = null)
        {
            RestRequest restRequest = PrepareRequest(Sr(subreddit) + "api/info");

            if (id != null)
            {
                restRequest.AddParameter("id", id);
            }
            if (url != null)
            {
                restRequest.AddParameter("url", url);
            }

            DynamicListingContainer res = JsonConvert.DeserializeObject <DynamicListingContainer>(ExecuteRequest(restRequest));

            List <Post>      posts      = new List <Post>();
            List <Comment>   comments   = new List <Comment>();
            List <Subreddit> subreddits = new List <Subreddit>();

            foreach (DynamicListingChild child in res.Data.Children)
            {
                switch (child.Kind)
                {
                case "t3":
                    posts.Add(JsonConvert.DeserializeObject <Post>(JsonConvert.SerializeObject(child.Data)));
                    break;

                case "t1":
                    Comment comment = JsonConvert.DeserializeObject <Comment>(JsonConvert.SerializeObject(child.Data));
                    if (loadReplies)
                    {
                        CommentContainer commentContainer = Common.GetComments(comment.ParentId.Substring(3), new ListingsGetCommentsInput(comment: comment.Id), comment.Subreddit);
                        if (commentContainer != null &&
                            commentContainer.Data != null &&
                            commentContainer.Data.Children != null &&
                            !commentContainer.Data.Children.Count.Equals(0) &&
                            commentContainer.Data.Children[0].Data != null)
                        {
                            comment.Replies = commentContainer.Data.Children[0].Data.Replies;
                        }
                    }

                    comments.Add(comment);
                    break;

                case "t5":
                    subreddits.Add(JsonConvert.DeserializeObject <Subreddit>(JsonConvert.SerializeObject(child.Data)));
                    break;
                }
            }

            return(new Info(posts, comments, subreddits));
        }
示例#6
0
        public void GetAllCommentsByPostIdTest_Null_ReturnEqual()
        {
            //Arrange
            CommentContainer commentcontainer = new CommentContainer();
            List <Comment>   expectedcomments = new List <Comment>();

            //Act
            commentcontainer.GetAllCommentsByPostId(1);
            List <Comment> resultcomments = commentcontainer.Comments;

            //Assert
            CollectionAssert.AreEquivalent(expectedcomments, resultcomments);
        }
示例#7
0
        public void GetAllCommentsByPostIdTest_NotNull_ReturnEqual()
        {
            //Arrange
            CommentContainer commentcontainer = new CommentContainer();

            //Act
            commentcontainer.GetAllCommentsByPostId(2);
            List <Comment> resultcomments = commentcontainer.Comments;

            //Assert
            CollectionAssert.AllItemsAreUnique(resultcomments);
            CollectionAssert.AllItemsAreNotNull(resultcomments);
        }
示例#8
0
        public void Validate(CommentContainer commentContainer, bool allowEmpty = false)
        {
            Assert.IsNotNull(commentContainer);
            Assert.IsNotNull(commentContainer.Data);
            Assert.IsNotNull(commentContainer.Data.Children);
            Assert.IsTrue(commentContainer.Kind.Equals("Listing"));

            if (!allowEmpty)
            {
                Assert.IsTrue(commentContainer.Data.Children.Count > 0);
                Assert.IsTrue(commentContainer.Data.Children[0].Kind.Equals("t1"));
                Assert.IsNotNull(commentContainer.Data.Children[0].Data);
            }
        }
示例#9
0
        public List <Comment> GetComments(CommentContainer commentContainer, Dispatch dispatch)
        {
            if (commentContainer == null || commentContainer.Data == null || commentContainer.Data.Children == null)
            {
                return(null);
            }

            List <Comment> comments = new List <Comment>();

            foreach (CommentChild commentChild in commentContainer.Data.Children)
            {
                if (commentChild.Data != null)
                {
                    comments.Add(new Comment(dispatch, commentChild.Data));
                }
            }

            return(comments);
        }
示例#10
0
        public void GetComments()
        {
            List <(PostContainer, CommentContainer)> res = reddit.Models.Listings.GetComments("9gaze5", new ListingsGetCommentsInput(0, false, false, "top", true, 0));

            Assert.IsNotNull(res);
            Assert.IsTrue(res.Count == 1);

            PostContainer    post     = res[0].Item1;
            CommentContainer comments = res[0].Item2;

            Validate(post);
            Assert.IsTrue(post.Data.Children[0].Data.Approved);
            Assert.IsFalse(post.Data.Children[0].Data.IsSelf);
            Assert.IsTrue(post.Data.Children[0].Data.Id.Equals("9gaze5"));
            Assert.IsTrue(post.Data.Children[0].Data.Author.Equals("KrisCraig"));
            Assert.IsTrue(post.Data.Children[0].Data.Subreddit.Equals("StillSandersForPres"));
            Assert.IsTrue(post.Data.Children[0].Data.Title.Equals("Reports of Widespread Voter Suppression in New York State Democratic Primary"));

            Validate(comments);
        }
示例#11
0
        public void GetCommentsWithContext()
        {
            List <(PostContainer, CommentContainer)> res = reddit.Models.Listings.GetComments("8gmf99", new ListingsGetCommentsInput(8, true, true, "top", true, 0, "dyd2vtc"), "FloridaMan");

            Assert.IsNotNull(res);
            Assert.IsTrue(res.Count == 1);

            PostContainer    post     = res[0].Item1;
            CommentContainer comments = res[0].Item2;

            Validate(post);
            Assert.IsFalse(post.Data.Children[0].Data.IsSelf);
            Assert.IsTrue(post.Data.Children[0].Data.Id.Equals("8gmf99"));
            Assert.IsTrue(post.Data.Children[0].Data.Author.Equals("KrisCraig"));
            Assert.IsTrue(post.Data.Children[0].Data.Subreddit.Equals("FloridaMan"));
            Assert.IsTrue(post.Data.Children[0].Data.Title.Equals("Florida man with handlebar mustache assaults woman on plane, starts a fight with several passengers, " +
                                                                  "yells at police to tase him and \"you'll see what happens\", then gets tased 10 times."));

            Validate(comments);
        }
 public LectureViewModel Get(int id)
 {
     LectureViewModel vm = new LectureViewModel();
     var lec = (from l in db.Lectures
                where l.ID == id
                select l).FirstOrDefault();
     vm.Title = lec.Name;
     vm.ID = id;
     vm.VideoURL = lec.LectureURL;
     var result = (from c in db.Comments
                  where c.Lecture.ID == id
                   select c).AsEnumerable().Reverse();
     if (result == null)
         throw new FieldAccessException("Nothing found in db.Comments");
     foreach( Comment c in result) {
         CommentContainer cc = new CommentContainer();
         cc.AuthorName = c.Poster.FullName;
         cc.PublishedDate = c.DatePublished.ToUniversalTime().ToString();
         cc.CommentText = c.CommentText;
         vm.comments.Add(cc);
     }
     return vm;
 }
示例#13
0
        private MoreChildren BuildResult(JToken jToken)
        {
            CommentContainer listing = jToken.ToObject <CommentContainer>();

            if (listing.Data != null &&
                listing.Data.Children != null &&
                !listing.Data.Children.Count.Equals(0))
            {
                switch (listing.Data.Children[0].Kind)
                {
                default:
                    return(new MoreChildren());

                case "t1":
                    List <Comment> comments = new List <Comment>();
                    foreach (CommentChild commentChild in listing.Data.Children)
                    {
                        comments.Add(commentChild.Data);
                    }

                    return(new MoreChildren(comments, null));

                case "more":
                    List <More> more = new List <More>();
                    foreach (MoreChild moreChild in jToken.ToObject <MoreContainer>().Data.Children)
                    {
                        more.Add(moreChild.Data);
                    }

                    return(new MoreChildren(null, more));
                }
            }
            else
            {
                return(new MoreChildren());
            }
        }
示例#14
0
        public static Models.CommentContainer SaveComment(Models.Comment comment, string containerType, string containerId, string portalId = null, string userId = null)
        {
            portalId = string.IsNullOrEmpty(portalId) ? Portal.CurrentPortalId : portalId;
            userId   = string.IsNullOrEmpty(userId) ? Account.AuditId : userId;
            var container = Get(containerType, containerId, portalId);

            if (container == null)
            {
                container = new CommentContainer()
                {
                    ContainerType = containerType, ContainerId = containerId, PortalId = portalId
                }
            }
            ;

            //TODO: validate comment!
            var existing = container.Comments.Where(c => c.Id == comment.Id).FirstOrDefault();

            if (existing == null)
            {
                comment.Id          = Guid.NewGuid().ToString(); //use guid or have counter on container?
                comment.CreatedDate = DateTime.UtcNow;
                container.Comments.Add(comment);
            }
            else
            {
                comment.Id = existing.Id;
                //todo: easy way to replace an element in collection instead of this copy?
                existing.Text         = comment.Text;
                existing.Email        = comment.Email;
                existing.ApprovedDate = comment.ApprovedDate;
                existing.CreatedDate  = comment.CreatedDate;
            }

            Save(container, userId);
            return(container);
        }
示例#15
0
        // **************** Class members *************************************************** //

        // TODO: Have to figure out how to get the selected item and it's index into here
        private void OnCommentDoubleClicked([CanBeNull] object obj)
        {
            OnSelectedComment(obj as ICommentBase ??
                              CommentContainer.FirstOrDefault());
        }
示例#16
0
        public void CommentHistoryGilded()
        {
            CommentContainer history = reddit.Models.Users.CommentHistory("KrisCraig", "gilded", new UsersHistoryInput(type: "comments", sort: "top", context: 10));

            Validate(history, true);
        }
示例#17
0
 public override void VisitCommentContainer(CommentContainer commentContainer)
 {
     VisitChildren(commentContainer);
 }