public ActionResult Details(Guid id, Guid?context = null) { var message = _messageDao.GetMessageById(id); // only private messages can be perma linked if (message.MessageType != MessageType.Private) { throw new NotFoundException(); } if (message.FirstMessage.HasValue) { // the user is looking at the wrong page. // the user should never get here return(Redirect(Url.MessageDetails(message))); } // this will return all the messages for this thread, including the first message that started the conversation var messages = _messageDao.GetMessagesForThread(message.Id); // let's make sure that the user is involved with at least one of these messages if (!_userContext.CurrentUser.IsAdmin) { var userModeratingSubs = _moderationDao.GetSubsModeratoredByUserWithPermissions(_userContext.CurrentUser.Id) .Where(x => x.Value.HasPermission(ModeratorPermissions.Mail)).Select(x => x.Key).ToList(); // NOTE: Should we check for the user being involved with any message in the thread, or is the first message enough? if (message.ToUser.HasValue && message.ToUser.Value == _userContext.CurrentUser.Id || message.AuthorId == _userContext.CurrentUser.Id || message.FromSub.HasValue && userModeratingSubs.Contains(message.FromSub.Value) || message.ToSub.HasValue && userModeratingSubs.Contains(message.ToSub.Value)) { // the user is involved in these discussions! } else { throw new UnauthorizedException(); } } var model = new MessageThreadViewModel(); model.IsModerator = _moderationDao.GetSubsModeratoredByUser(_userContext.CurrentUser.Id).Count > 0; model.Messages.AddRange(_messageWrapper.Wrap(messages, _userContext.CurrentUser)); if (context.HasValue) { model.ContextMessage = model.Messages.SingleOrDefault(x => x.Message.Id == context.Value); } model.FirstMessage = model.Messages.Single(x => !x.Message.FirstMessage.HasValue); return(View(model)); }
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); }