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)); }
public ActionResult Random() { var randomSubId = _subDao.GetRandomSub(nsfw: _userContext.CurrentNsfw); if (randomSubId == null) { return(Redirect(Url.Subs())); } var randomSub = _subDao.GetSubById(randomSubId.Value); if (randomSub == null) { return(Redirect(Url.Subs())); } return(Redirect(Url.Sub(randomSub.Name))); }
public List <SubWrapped> Wrap(List <Guid> subIds, User currentUser = null) { var subs = subIds.Select(subId => _subDao.GetSubById(subId)).Where(sub => sub != null).ToList(); return(Wrap(subs, currentUser)); }
public List <MessageWrapped> Wrap(List <Guid> messageIds, User currentUser) { if (currentUser == null) { throw new Exception("You must provide a user."); } var messages = new List <MessageWrapped>(); foreach (var messageId in messageIds) { var message = _messageDao.GetMessageById(messageId); if (message != null) { messages.Add(new MessageWrapped(message)); } } var users = new Dictionary <Guid, User>(); var subs = new Dictionary <Guid, Sub>(); var comments = _commentWrapper.Wrap( messages.Where(x => x.Message.CommentId.HasValue) .Select(x => x.Message.CommentId.Value) .Distinct() .ToList(), currentUser) .ToDictionary(x => x.Comment.Id, x => x); var posts = _postWrapper.Wrap( messages.Where(x => x.Message.PostId.HasValue) .Select(x => x.Message.PostId.Value) .Distinct() .ToList(), currentUser) .ToDictionary(x => x.Post.Id, x => x); foreach (var message in messages) { if (!users.ContainsKey(message.Message.AuthorId)) { users.Add(message.Message.AuthorId, null); } if (message.Message.ToUser.HasValue && !users.ContainsKey(message.Message.ToUser.Value)) { users.Add(message.Message.ToUser.Value, null); } if (message.Message.FromSub.HasValue && !subs.ContainsKey(message.Message.FromSub.Value)) { subs.Add(message.Message.FromSub.Value, null); } if (message.Message.ToSub.HasValue && !subs.ContainsKey(message.Message.ToSub.Value)) { subs.Add(message.Message.ToSub.Value, null); } } var subsCanModerate = new HashSet <Guid>(); foreach (var sub in subs.Keys) { if (_permissionDao.CanUserManageSubMail(currentUser, sub)) { subsCanModerate.Add(sub); } } foreach (var userId in users.Keys.ToList()) { users[userId] = _membershipService.GetUserById(userId); } foreach (var subId in subs.Keys.ToList()) { subs[subId] = _subDao.GetSubById(subId); } foreach (var message in messages) { message.Author = users[message.Message.AuthorId]; message.FromSub = message.Message.FromSub.HasValue ? subs[message.Message.FromSub.Value] : null; message.ToUser = message.Message.ToUser.HasValue ? users[message.Message.ToUser.Value] : null; message.ToSub = message.Message.ToSub.HasValue ? subs[message.Message.ToSub.Value] : null; if (message.ToUser != null && message.ToUser.Id == currentUser.Id) { // this was a message to the current user, so the current user can reply to it. message.CanReply = true; } else if (message.ToSub != null && subsCanModerate.Contains(message.ToSub.Id)) { // this message was sent to a sub, and this user is a moderator with the correct permissions to reply. message.CanReply = true; } if (message.Author.Id == currentUser.Id) { message.UserIsSender = true; } else if (message.ToUser != null && message.ToUser.Id == currentUser.Id) { message.UserIsRecipiant = true; } else if (message.ToSub != null && subsCanModerate.Contains(message.ToSub.Id)) { message.UserIsRecipiant = true; } if (message.ToUser != null && message.ToUser.Id == currentUser.Id) { message.CanMarkRead = true; } else if (message.ToSub != null && subsCanModerate.Contains(message.ToSub.Id)) { message.CanMarkRead = true; } if (message.CanMarkRead) { message.IsUnread = message.Message.IsNew; } // add any comment or post this message represents (comment reply, mention, etc) if (message.Message.PostId.HasValue && posts.ContainsKey((message.Message.PostId.Value))) { message.Post = posts[message.Message.PostId.Value]; } if (message.Message.CommentId.HasValue && comments.ContainsKey((message.Message.CommentId.Value))) { message.Comment = comments[message.Message.CommentId.Value]; } } return(messages); }