Example #1
0
        public IViewComponentResult Invoke()
        {
            var model = new TopBarViewModel();

            model.SubscibedSubs.AddRange(_subDao.GetSubsByIds(_contextService.GetSubscribedSubIds()).Select(x => x.Name).OrderBy(x => x));
            model.DefaultSubs.AddRange(_subDao.GetSubsByIds(_subDao.GetDefaultSubs()).Select(x => x.Name));
            return(View(model));
        }
Example #2
0
        private UserViewModel BuildUserModel(string userName)
        {
            var user = _membershipService.GetUserByUserName(userName);

            if (user == null)
            {
                throw new NotFoundException();
            }

            var model = new UserViewModel();

            model.User = user;

            var moderatedSubs = _moderationDao.GetSubsModeratoredByUser(user.Id);

            if (moderatedSubs.Count > 0)
            {
                model.IsModerator    = true;
                model.ModeratingSubs = _subDao.GetSubsByIds(moderatedSubs).Select(x => x.Name).ToList();
            }

            var kudos = _karmaDao.GetKarma(user.Id);

            model.CommentKudos = kudos.Keys.Where(x => x.Type == KarmaType.Comment).Sum(x => kudos[x]);
            model.PostKudos    = kudos.Keys.Where(x => x.Type == KarmaType.Post).Sum(x => kudos[x]);

            return(model);
        }
Example #3
0
        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);
        }
Example #4
0
        public List <ModeratorWrapped> Wrap(List <Moderator> moderators, User currentUser = null)
        {
            var items = moderators.Select(x => new ModeratorWrapped(x)).ToList();

            var users            = _membershipService.GetUsersByIds(moderators.Select(x => x.UserId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var subs             = _subDao.GetSubsByIds(moderators.Select(x => x.SubId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var subModeratorInfo = currentUser != null?subs.Keys.ToDictionary(x => x, x => _moderationDao.GetModeratorInfoForUserInSub(currentUser.Id, x)) : new Dictionary <Guid, Moderator>();

            foreach (var item in items)
            {
                item.Sub  = subs[item.Moderator.SubId];
                item.User = users[item.Moderator.UserId];

                if (currentUser == null)
                {
                    item.CanRemove            = false;
                    item.CanChangePermissions = false;
                }
                else
                {
                    if (item.User.Id == currentUser.Id)
                    {
                        // we can remove ourself
                        item.CanRemove = true;
                        // but we cant change our permissions
                        item.CanChangePermissions = false;
                    }
                    else if (currentUser.IsAdmin)
                    {
                        // admins can always remove people anc hange permissions
                        item.CanRemove            = true;
                        item.CanChangePermissions = true;
                    }
                    else
                    {
                        var currentModeratorInfo = subModeratorInfo.ContainsKey(item.Sub.Id)
                            ? subModeratorInfo[item.Sub.Id]
                            : null;

                        if (currentModeratorInfo != null)
                        {
                            // the user is a moderator of some kind in the sub
                            // remember a mod can never remove or change permissions of a mod who has been one longer
                            if (currentModeratorInfo.AddedOn > item.Moderator.AddedOn)
                            {
                                // the current user was added as a moderator later than this current interated one.
                                // seniority!
                                item.CanRemove            = false;
                                item.CanChangePermissions = false;
                            }
                            else
                            {
                                // only users with "all" permission can change/remove another mod
                                if (currentModeratorInfo.Permissions.HasPermission(ModeratorPermissions.All))
                                {
                                    item.CanRemove            = true;
                                    item.CanChangePermissions = true;
                                }
                                else
                                {
                                    item.CanRemove            = false;
                                    item.CanChangePermissions = false;
                                }
                            }
                        }
                        else
                        {
                            // the user is not a moderator in this sub
                            item.CanRemove            = false;
                            item.CanChangePermissions = false;
                        }
                    }
                }
            }

            return(items);
        }
Example #5
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);
        }
Example #6
0
        public List <ModeratorInviteWrapped> Wrap(List <ModeratorInvite> invites, User currentUser = null)
        {
            var items = invites.Select(x => new ModeratorInviteWrapped(x)).ToList();

            var users            = _membershipDao.GetUsersByIds(invites.Select(x => x.UserId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var subs             = _subDao.GetSubsByIds(invites.Select(x => x.SubId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var subModeratorInfo = currentUser != null?subs.Keys.ToDictionary(x => x, x => _moderationDao.GetModeratorInfoForUserInSub(currentUser.Id, x)) : new Dictionary <Guid, Moderator>();

            foreach (var item in items)
            {
                item.Sub  = subs[item.ModeratorInvite.SubId];
                item.User = users[item.ModeratorInvite.UserId];

                if (currentUser == null)
                {
                    item.CanRemove = false;
                }
                else
                {
                    if (item.User.Id == currentUser.Id)
                    {
                        // we can remove ourself
                        item.CanRemove = true;
                        // but we can't change our permissions
                        item.CanChangePermissions = false;
                    }
                    else if (currentUser.IsAdmin)
                    {
                        // admins can always remove people and change permissions
                        item.CanRemove            = true;
                        item.CanChangePermissions = true;
                    }
                    else
                    {
                        var currentModeratorInfo = subModeratorInfo.ContainsKey(item.Sub.Id)
                            ? subModeratorInfo[item.Sub.Id]
                            : null;

                        if (currentModeratorInfo != null)
                        {
                            // only users with "all" permission can change invite permissions
                            if (currentModeratorInfo.Permissions.HasPermission(ModeratorPermissions.All))
                            {
                                item.CanRemove            = true;
                                item.CanChangePermissions = true;
                            }
                            else
                            {
                                item.CanRemove            = false;
                                item.CanChangePermissions = false;
                            }
                        }
                        else
                        {
                            // the user is not a moderator in this sub
                            item.CanRemove            = false;
                            item.CanChangePermissions = false;
                        }
                    }
                }
            }

            return(items);
        }