예제 #1
0
        public IViewComponentResult Invoke(string subName, Guid?subId, bool showSearch, bool showCreateSub, bool showSubmit)
        {
            var model = new SidebarViewModel();

            model.ShowSearch    = showSearch;
            model.ShowCreateSub = showCreateSub;
            model.ShowSubmit    = showSubmit;

            var currentUser = _userContext.CurrentUser;

            if (subId.HasValue)
            {
                model.CurrentSub = _subWrapper.Wrap(_subDao.GetSubById(subId.Value), _userContext.CurrentUser);
            }
            else if (!string.IsNullOrEmpty(subName))
            {
                model.CurrentSub = _subWrapper.Wrap(_subDao.GetSubByName(subName), _userContext.CurrentUser);
            }

            if (model.CurrentSub != null)
            {
                if (_userContext.CurrentUser != null)
                {
                    model.Permissions = _moderationDao.GetUserPermissionsForSub(_userContext.CurrentUser, model.CurrentSub.Sub.Id);
                }

                if (!model.IsModerator)
                {
                    // we only show list of mods if the requesting user is not a mod of this sub
                    model.Moderators = _membershipDao.GetUsersByIds(_moderationDao.GetAllModsForSub(model.CurrentSub.Sub.Id).Select(x => x.UserId).ToList());
                }

                // get the number of active users currently viewing this sub.
                // for normal users, this number may be fuzzed (if low enough) for privacy reasons.
                if (_userContext.CurrentUser != null && _userContext.CurrentUser.IsAdmin)
                {
                    model.NumberOfActiveUsers = _subActivityDao.GetActiveNumberOfUsersForSub(model.CurrentSub.Sub.Id);
                }
                else
                {
                    bool wasFuzzed;
                    model.NumberOfActiveUsers         = _subActivityDao.GetActiveNumberOfUsersForSubFuzzed(model.CurrentSub.Sub.Id, out wasFuzzed);
                    model.IsNumberOfActiveUsersFuzzed = wasFuzzed;
                }
            }

            if (model.ShowCreateSub)
            {
                if (currentUser != null)
                {
                    var age = Common.CurrentTime() - currentUser.CreatedDate;
                    if (currentUser.IsAdmin || age.TotalDays >= _subSettings.Settings.MinUserAgeCreateSub)
                    {
                        model.CanCreateSub = true;
                    }
                }
            }

            return(View(model));
        }
예제 #2
0
        public List <SubUserBanWrapped> Wrap(List <SubUserBan> items)
        {
            var users = _membershipDao.GetUsersByIds(
                items.Select(x => x.UserId).Union(items.Select(x => x.BannedBy)).Distinct().ToList())
                        .ToDictionary(x => x.Id, x => x);

            return(items.Select(x =>
            {
                var wrapped = new SubUserBanWrapped(x)
                {
                    User = users.ContainsKey(x.UserId) ? users[x.UserId] : null,
                    BannedBy = users.ContainsKey(x.BannedBy) ? users[x.BannedBy] : null
                };

                return wrapped;
            })
                   .ToList());
        }
예제 #3
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);
        }