Exemple #1
0
        public ActionResult Post(string subName, Guid id, CommentSortBy?commentsSort, Guid?commentId = null, int?limit = 100, int context = 0)
        {
            var post = _postDao.GetPostById(id);

            if (post == null)
            {
                throw new HttpException(404, "no post found");
            }

            if (post.Deleted)
            {
                if (_userContext.CurrentUser == null)
                {
                    throw new HttpException(404, "no post found");
                }
                if (post.UserId != _userContext.CurrentUser.Id && !_userContext.CurrentUser.IsAdmin)
                {
                    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 (context < 0)
            {
                context = 0;
            }

            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;
            model.ViewingSpecificComment = commentId.HasValue;

            try
            {
                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, context: context);
                commentTreeContext.Sort     = model.Comments.SortBy;
                model.Comments.CommentNodes = _commentNodeHierarchyBuilder.Build(commentTree, commentTreeContext,
                                                                                 _userContext.CurrentUser);
            }
            catch (CommentNotFoundException ex)
            {
                throw new NotFoundException();
            }

            return(View(model));
        }