Ejemplo n.º 1
0
        public ActionResult ModeratorMail(InboxType type, string subName = null, int?pageNumber = null, int?pageSize = null)
        {
            ViewBag.ManageNavigationKey = "moderatormail";

            if (pageNumber == null || pageNumber < 1)
            {
                pageNumber = 1;
            }
            if (pageSize == null)
            {
                pageSize = 25;
            }
            if (pageSize > 100)
            {
                pageSize = 100;
            }
            if (pageSize < 1)
            {
                pageSize = 1;
            }

            var skip = (pageNumber - 1) * pageSize;
            var take = pageSize;

            var moderatingSubs = _moderationDao.GetSubsModeratoredByUserWithPermissions(_userContext.CurrentUser.Id);

            var model = new InboxViewModel {
                InboxType = type
            };

            model.IsModerator = moderatingSubs.Count > 0;

            if (!string.IsNullOrEmpty(subName))
            {
                var sub = _subDao.GetSubByName(subName);
                if (sub == null)
                {
                    throw new NotFoundException();
                }

                // make the sure that the user is allowed to see this mod mail
                if (!_userContext.CurrentUser.IsAdmin)
                {
                    if (!moderatingSubs.ContainsKey(sub.Id))
                    {
                        throw new UnauthorizedException();
                    }
                    if (!moderatingSubs[sub.Id].HasPermission(ModeratorPermissions.Mail))
                    {
                        throw new UnauthorizedException();
                    }
                }

                model.Sub = sub;
                model.ModeratorMailForSubs = new List <Guid> {
                    sub.Id
                };
            }
            else
            {
                model.ModeratorMailForSubs = new List <Guid>();
                foreach (var key in moderatingSubs.Keys)
                {
                    if (moderatingSubs[key].HasPermission(ModeratorPermissions.Mail))
                    {
                        model.ModeratorMailForSubs.Add(key);
                    }
                }
            }

            SeekedList <Guid> messages;

            if (moderatingSubs.Count == 0)
            {
                messages = new SeekedList <Guid>();
            }
            else
            {
                switch (type)
                {
                case InboxType.ModeratorMail:
                    messages = _messageDao.GetModeratorMailForSubs(moderatingSubs.Select(x => x.Key).ToList(), skip, take);
                    break;

                case InboxType.ModeratorMailUnread:
                    messages = _messageDao.GetUnreadModeratorMailForSubs(moderatingSubs.Select(x => x.Key).ToList(), skip, take);
                    break;

                case InboxType.ModeratorMailSent:
                    messages = _messageDao.GetSentModeratorMailForSubs(moderatingSubs.Select(x => x.Key).ToList(), skip, take);
                    break;

                default:
                    throw new Exception("invalid type");
                }
            }

            model.Messages = new PagedList <MessageWrapped>(_messageWrapper.Wrap(messages, _userContext.CurrentUser), pageNumber.Value, pageSize.Value, messages.HasMore);

            return(View(model));
        }