コード例 #1
0
ファイル: PostsController.cs プロジェクト: snetts/skimur
        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));
        }
コード例 #2
0
ファイル: PostWrapper.cs プロジェクト: snetts/skimur
        public List <PostWrapped> Wrap(List <Guid> postIds, User currentUser = null)
        {
            var posts = new List <PostWrapped>();

            foreach (var postId in postIds)
            {
                var post = _postDao.GetPostById(postId);
                if (post != null)
                {
                    posts.Add(new PostWrapped(post));
                }
            }

            var authors = _membershipService.GetUsersByIds(posts.Select(x => x.Post.UserId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var subs    = _subDao.GetSubsByIds(posts.Select(x => x.Post.SubId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var likes   = currentUser != null?_voteDao.GetVotesOnPostsByUser(currentUser.Id, postIds) : new Dictionary <Guid, VoteType>();

            var canManagePosts = currentUser != null
                ? subs.Values.Where(x => _permissionDao.CanUserManageSubPosts(currentUser, x.Id))
                                 .Select(x => x.Id)
                                 .ToList()
                : new List <Guid>();

            foreach (var item in posts)
            {
                item.Author = authors.ContainsKey(item.Post.UserId) ? authors[item.Post.UserId] : null;
                item.Sub    = subs.ContainsKey(item.Post.SubId) ? subs[item.Post.SubId] : null;
                if (currentUser != null)
                {
                    item.CurrentUserVote = likes.ContainsKey(item.Post.Id) ? likes[item.Post.Id] : (VoteType?)null;
                }
                if (canManagePosts.Contains(item.Post.SubId))
                {
                    // this user can approve/disapprove of a post, mark it NSFW, etc
                    item.CanManage = true;
                    item.Verdict   = item.Post.PostVerdict;

                    if (item.Post.NumberOfReports > 0)
                    {
                        var reports = _reportDao.GetReportsForPost(item.Post.Id);
                        item.Reports = new List <ReportSummary>();
                        foreach (var report in reports)
                        {
                            var summary = new ReportSummary();
                            if (!authors.ContainsKey(report.ReportedBy))
                            {
                                authors.Add(report.ReportedBy, _membershipService.GetUserById(report.ReportedBy));
                            }
                            var user = authors[report.ReportedBy];
                            if (user != null)
                            {
                                summary.UserName = user.UserName;
                            }
                            summary.Reason = report.Reason;
                            item.Reports.Add(summary);
                        }
                    }

                    item.CanSticky = true;
                }
                if (currentUser != null)
                {
                    item.CanReport = true;
                }

                // authors can only edit text posts
                if (item.Post.PostType == PostType.Text && currentUser != null && currentUser.Id == item.Post.UserId)
                {
                    item.CanEdit = true;
                }

                item.CanDelete = currentUser != null && (currentUser.IsAdmin || item.Post.UserId == currentUser.Id);
            }

            return(posts);
        }
コード例 #3
0
        public List <CommentWrapped> Wrap(List <Guid> commentIds, User currentUser = null)
        {
            var result = new List <CommentWrapped>();

            foreach (var commentId in commentIds)
            {
                var comment = _commentDao.GetCommentById(commentId);
                if (comment != null)
                {
                    result.Add(new CommentWrapped(comment));
                }
            }

            var authors = _membershipService.GetUsersByIds(result.Select(x => x.Comment.AuthorUserId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var subs    = _subDao.GetSubsByIds(result.Select(x => x.Comment.SubId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var posts   = result.Select(x => x.Comment.PostId).Distinct().Select(x => _postDao.GetPostById(x)).Where(x => x != null).ToDictionary(x => x.Id, x => x);

            var userCanModInSubs = new List <Guid>();

            if (currentUser != null)
            {
                foreach (var sub in subs.Values)
                {
                    if (_permissionDao.CanUserManageSubPosts(currentUser, sub.Id))
                    {
                        userCanModInSubs.Add(sub.Id);
                    }
                }
            }

            var likes = currentUser != null?_voteDao.GetVotesOnCommentsByUser(currentUser.Id, commentIds) : new Dictionary <Guid, VoteType>();

            foreach (var item in result)
            {
                item.Author          = authors.ContainsKey(item.Comment.AuthorUserId) ? authors[item.Comment.AuthorUserId] : null;
                item.CurrentUserVote = likes.ContainsKey(item.Comment.Id) ? likes[item.Comment.Id] : (VoteType?)null;
                item.Sub             = subs.ContainsKey(item.Comment.SubId) ? subs[item.Comment.SubId] : null;
                item.Score           = item.Comment.VoteUpCount - item.Comment.VoteDownCount;
                item.Post            = posts.ContainsKey(item.Comment.PostId) ? posts[item.Comment.PostId] : null;

                var userCanMod = item.Sub != null && userCanModInSubs.Contains(item.Sub.Id);
                item.CanManage = userCanMod;

                if ((item.Author != null && currentUser != null) && currentUser.Id == item.Author.Id)
                {
                    item.CurrentUserIsAuthor = true;
                }

                item.CanDelete = item.CanManage || item.CurrentUserIsAuthor;
                item.CanEdit   = item.CurrentUserIsAuthor;

                if (currentUser != null)
                {
                    item.CanReport = true;
                }

                if (item.CanManage && item.Comment.Reports > 0)
                {
                    var reports = _reportDao.GetReportsForComment(item.Comment.Id);
                    item.Reports = new List <ReportSummary>();
                    foreach (var report in reports)
                    {
                        var summary = new ReportSummary();
                        if (!authors.ContainsKey(report.ReportedBy))
                        {
                            authors.Add(report.ReportedBy, _membershipService.GetUserById(report.ReportedBy));
                        }

                        var user = authors[report.ReportedBy];
                        if (user != null)
                        {
                            summary.Username = user.UserName;
                        }

                        summary.Reason = report.Reason;
                        item.Reports.Add(summary);
                    }
                }
            }

            return(result);
        }