public static CommentListing GetCommentsForPost(Session session, string id, CommentSortBy sort, string more) { var url = "http://www.reddit.com/comments/" + id + ".json"; var request = new Request { Url = url, Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } // use some tricky recursive linq to get the comment structure, // understanding this required the use of caffeine. I'm tired. var o = JArray.Parse(json); // skip / ignore the post // o[0] // loop over the top level comments var list = new CommentListing(); var comments = o[1]["data"]["children"]; //.Select(t => t["data"]); list.ModHash = o[1]["data"]["modhash"].ToString(); foreach (var comment in comments) { switch (comment["kind"].ToString()) { case "t1": list.Add(new Comment(comment["data"])); break; case "more": var test = comment["data"]["children"].Values().Select(t => t.ToString()).ToList(); list.More.AddRange(test); break; } } return(list); }
public Dictionary <Guid, double> GetCommentTreeSorter(Guid postId, CommentSortBy sortBy) { // todo: this should be cached and updated periodically return(_conn.Perform(conn => { var query = conn.From <Comment>().Where(x => x.PostId == postId); switch (sortBy) { case CommentSortBy.Best: query.OrderBy(x => x.SortConfidence); break; case CommentSortBy.Top: query.OrderByExpression = "ORDER BY (score(up_vote_count, vote_down_count), created_at)"; break; case CommentSortBy.New: query.OrderBy(x => x.CreatedAt); break; case CommentSortBy.Controversial: query.OrderByExpression = "ORDER BY (controversy(vote_up_count, vote_down_count), created_at)"; break; case CommentSortBy.Old: query.OrderByDescending(x => x.CreatedAt); break; case CommentSortBy.Qa: query.OrderBy(x => x.SortQa); break; default: throw new Exception("Unknown sort."); } query.SelectExpression = "SELECT \"id\""; var commentsSorted = conn.Select(query).Select(x => x.Id).ToList(); return commentsSorted.ToDictionary(x => x, x => (double)commentsSorted.IndexOf(x)); })); }
public Dictionary<Guid, double> GetCommentTreeSorter(Guid postId, CommentSortBy sortBy) { // TODO: This should be cached and updated periodically return _conn.Perform(conn => { var query = conn.From<Comment>().Where(x => x.PostId == postId); switch (sortBy) { case CommentSortBy.Best: query.OrderBy(x => x.SortConfidence); break; case CommentSortBy.Top: query.OrderByExpression = "ORDER BY (score(vote_up_count, vote_down_count), date_created)"; break; case CommentSortBy.New: query.OrderBy(x => x.DateCreated); break; case CommentSortBy.Controversial: query.OrderByExpression = "ORDER BY (controversy(vote_up_count, vote_down_count), date_created)"; break; case CommentSortBy.Old: query.OrderByDescending(x => x.DateCreated); break; case CommentSortBy.Qa: query.OrderBy(x => x.SortQa); break; default: throw new Exception("unknown sort"); } query.SelectExpression = "SELECT \"id\""; var commentsSorted = conn.Select(query).Select(x => x.Id).ToList(); return commentsSorted.ToDictionary(x => x, x => (double)commentsSorted.IndexOf(x)); }); }
public List<Comment> GetAllCommentsForPost(Guid postId, CommentSortBy? sortBy = null) { return _conn.Perform(conn => { var query = conn.From<Comment>().Where(x => x.PostId == postId); if (sortBy.HasValue) { switch (sortBy) { case CommentSortBy.Best: query.OrderByDescending(x => x.SortConfidence); break; case CommentSortBy.Hot: query.OrderByExpression = "ORDER BY (hot(vote_up_count, vote_down_count, date_created), date_created) DESC"; break; case CommentSortBy.Top: query.OrderByExpression = "ORDER BY (score(vote_up_count, vote_down_count), date_created) DESC"; break; case CommentSortBy.New: query.OrderByDescending(x => x.DateCreated); break; case CommentSortBy.Controversial: query.OrderByExpression = "ORDER BY (controversy(vote_up_count, vote_down_count), date_created) DESC"; break; case CommentSortBy.Old: query.OrderBy(x => x.DateCreated); break; case CommentSortBy.Qa: query.OrderByDescending(x => x.SortQa); break; default: throw new Exception("unknown sort"); } } return conn.Select(query); }); }
public ActionResult Post(string subName, Guid id, CommentSortBy? commentsSort, Guid? commentId = null, int? limit = 100) { var post = _postDao.GetPostById(id); if (post == null) throw new HttpException(404, "no post found"); var sub = _subDao.GetSubByName(subName); if (sub == null) throw new HttpException(404, "no post found"); if (!sub.Name.Equals(subName, StringComparison.InvariantCultureIgnoreCase)) throw new HttpException(404, "no post found"); // TODO: redirect to correct url if (!limit.HasValue) limit = 100; if (limit < 1) limit = 100; if (limit > 200) limit = 200; if (!commentsSort.HasValue) commentsSort = CommentSortBy.Best; // TODO: get suggested sort for this link, and if none, from the sub var model = new PostDetailsModel(); model.Post = _postWrapper.Wrap(id, _userContext.CurrentUser); model.Sub = _subWrapper.Wrap(sub.Id, _userContext.CurrentUser); model.Comments = new CommentListModel(); model.Comments.SortBy = commentsSort.Value; var commentTree = _commentDao.GetCommentTree(model.Post.Post.Id); var commentTreeSorter = _commentDao.GetCommentTreeSorter(model.Post.Post.Id, model.Comments.SortBy); var commentTreeContext = _commentTreeContextBuilder.Build(commentTree, commentTreeSorter, comment: commentId, limit: limit, maxDepth: 5); commentTreeContext.Sort = model.Comments.SortBy; model.Comments.CommentNodes = _commentNodeHierarchyBuilder.Build(commentTree, commentTreeContext, _userContext.CurrentUser); return View(model); }
public ActionResult MoreComments(Guid postId, CommentSortBy? sort, string children, int depth) { if (!sort.HasValue) sort = CommentSortBy.Best; // TODO: get suggested sort for this link, and if none, from the sub var commentTree = _commentDao.GetCommentTree(postId); var commentTreeSorter = _commentDao.GetCommentTreeSorter(postId, sort.Value); var commentTreeContext = _commentTreeContextBuilder.Build(commentTree, commentTreeSorter, children.Split(',').Select(x => Guid.Parse(x)).ToList(), limit: 20, maxDepth: 5); commentTreeContext.Sort = sort.Value; var model = new CommentListModel(); model.SortBy = sort.Value; model.CommentNodes = _commentNodeHierarchyBuilder.Build(commentTree, commentTreeContext, _userContext.CurrentUser); return Json(new { success=true, error=(string)null, html = RenderView("_CommentNodes", model) }); }
public SeekedList<Guid> GetCommentsForUser(Guid userId, CommentSortBy? sortBy = null, CommentsTimeFilter? timeFilter = null, bool showDeleted = false, int? skip = null, int? take = null) { return _conn.Perform(conn => { var query = conn.From<Comment>(); query.Where(x => x.AuthorUserId == userId); if (sortBy.HasValue) { switch (sortBy) { case CommentSortBy.Best: query.OrderByDescending(x => x.SortConfidence); break; case CommentSortBy.Hot: query.OrderByExpression = "ORDER BY (hot(vote_up_count, vote_down_count, date_created), date_created) DESC"; break; case CommentSortBy.Top: query.OrderByExpression = "ORDER BY (score(vote_up_count, vote_down_count), date_created) DESC"; break; case CommentSortBy.New: query.OrderByDescending(x => x.DateCreated); break; case CommentSortBy.Controversial: query.OrderByExpression = "ORDER BY (controversy(vote_up_count, vote_down_count), date_created) DESC"; break; case CommentSortBy.Old: query.OrderBy(x => x.DateCreated); break; case CommentSortBy.Qa: query.OrderByDescending(x => x.SortQa); break; default: throw new Exception("unknown sort"); } } else { query.OrderByDescending(x => x.DateCreated); } if (!showDeleted) query.Where(x => !x.Deleted); if (timeFilter.HasValue) { TimeSpan timeSpan; if (timeFilter.Value != CommentsTimeFilter.All) { switch (timeFilter.Value) { case CommentsTimeFilter.Hour: timeSpan = TimeSpan.FromHours(1); break; case CommentsTimeFilter.Day: timeSpan = TimeSpan.FromDays(1); break; case CommentsTimeFilter.Week: timeSpan = TimeSpan.FromDays(7); break; case CommentsTimeFilter.Month: timeSpan = TimeSpan.FromDays(30); break; case CommentsTimeFilter.Year: timeSpan = TimeSpan.FromDays(365); break; default: throw new Exception("unknown time filter"); } var from = Common.CurrentTime() - timeSpan; query.Where(x => x.DateCreated >= from); } } var totalCount = conn.Count(query); query.Skip(skip).Take(take); query.SelectExpression = "SELECT \"id\""; return new SeekedList<Guid>(conn.Select(query).Select(x => x.Id), skip ?? 0, take, totalCount); }); }
public static CommentListing GetCommentsForPost(Session session, string id, CommentSortBy sort) { return(GetCommentsForPost(session, id, sort, string.Empty)); }
public static CommentListing GetCommentsForPost(Session session, string id, CommentSortBy sort, string more) { var url = "http://www.reddit.com/comments/" + id + ".json"; var request = new Request { Url = url, Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) throw new RedditException(json); // use some tricky recursive linq to get the comment structure, // understanding this required the use of caffeine. I'm tired. var o = JArray.Parse(json); // skip / ignore the post // o[0] // loop over the top level comments var list = new CommentListing(); var comments = o[1]["data"]["children"]; //.Select(t => t["data"]); list.ModHash = o[1]["data"]["modhash"].ToString(); foreach (var comment in comments) { switch(comment["kind"].ToString()) { case "t1": list.Add(new Comment(comment["data"])); break; case "more": var test = comment["data"]["children"].Values().Select(t => t.ToString()).ToList(); list.More.AddRange(test); break; } } return list; }
public static CommentListing GetCommentsForPost(Session session, string id, CommentSortBy sort) { return GetCommentsForPost(session, id, sort, string.Empty); }