Esempio n. 1
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>();

            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;
            }

            return posts;
        }
Esempio n. 2
0
        public void A_message_to_a_user_is_marked_as_unread()
        {
            // arrange
            var user1 = _membershipService.GetUserByUserName("skimur");
            var user2 = new User
            {
                UserName = "******"
            };
            _membershipService.InsertUser(user2);

            // act
            var sendResponse = _sendMessageHandler.Handle(new SendMessage
            {
                Author = user1.Id,
                Subject = "subject",
                Body = "body",
                To = user2.UserName
            });

            // assert
            Assert.That(string.IsNullOrEmpty(sendResponse.Error));
            var user1Messages = _messageService.GetUnreadMessagesForUser(user1.Id);
            var user2Messages = _messageService.GetUnreadMessagesForUser(user2.Id);
            Assert.That(user1Messages, Has.Count.EqualTo(0));
            Assert.That(user2Messages, Has.Count.EqualTo(1));
        }
Esempio n. 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);
                        }
                    }
                }
                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;
            }

            return posts;
        }
Esempio n. 4
0
        public bool CanUserDeleteComment(User user, Comment comment)
        {
            if (user == null) return false;

            if (comment.AuthorUserId == user.Id)
                return true;

            return CanUserManageSubPosts(user, comment.SubId);
        }
Esempio n. 5
0
        public void Can_build_node_sorted()
        {
            // arrange
            var sub = new Sub();
            sub.Id = GuidUtil.NewSequentialId();
            sub.Name = "test";
            var author = new User();
            author.Id = GuidUtil.NewSequentialId();
            author.UserName = "******";
            var comments = ConvertTestNodesToComments(new List<TestNodeTreeSorted>
            {
                TestNodeTreeSorted.Create(10,
                    TestNodeTreeSorted.Create(5),
                    TestNodeTreeSorted.Create(4,
                        TestNodeTreeSorted.Create(2),
                        TestNodeTreeSorted.Create(20))),
                TestNodeTreeSorted.Create(9,
                    TestNodeTreeSorted.Create(3,
                        TestNodeTreeSorted.Create(1)),
                    TestNodeTreeSorted.Create(12))
            });
            foreach (var comment in comments)
            {
                comment.SubId = sub.Id;
                comment.AuthorUserId = author.Id;
            }
            SetupComments(comments);
            _membershipService.Setup(x => x.GetUsersByIds(It.IsAny<List<Guid>>())).Returns(new List<User> {author});
            _subDao.Setup(x => x.GetSubsByIds(It.IsAny<List<Guid>>())).Returns(new List<Sub> {sub});

            // act
            var tree = _commentTreeBuilder.GetCommentTree(Guid.Empty);
            var treeContext = _commentTreeContextBuilder.Build(tree,
                comments.ToDictionary(x => x.Id, x => (double) x.SortConfidence));
            var nodes = _commentNodeHierarchyBuilder.Build(tree, treeContext, null);

            //assert
            Assert.That(nodes, Has.Count.EqualTo(2));
            Assert.That(nodes[0].As<CommentNode>().Comment.Comment.SortConfidence, Is.EqualTo(10));
            Assert.That(nodes[0].As<CommentNode>().Children, Has.Count.EqualTo(2));
            Assert.That(nodes[0].As<CommentNode>().Children[0].As<CommentNode>().Comment.Comment.SortConfidence, Is.EqualTo(5));
            Assert.That(nodes[0].As<CommentNode>().Children[1].As<CommentNode>().Comment.Comment.SortConfidence, Is.EqualTo(4));
            Assert.That(nodes[0].As<CommentNode>().Children[1].Children, Has.Count.EqualTo(2));
            Assert.That(nodes[0].As<CommentNode>().Children[1].Children[0].As<CommentNode>().Comment.Comment.SortConfidence, Is.EqualTo(20));
            Assert.That(nodes[0].As<CommentNode>().Children[1].Children[1].As<CommentNode>().Comment.Comment.SortConfidence, Is.EqualTo(2));
            Assert.That(nodes[1].As<CommentNode>().Comment.Comment.SortConfidence, Is.EqualTo(9));
            Assert.That(nodes[1].As<CommentNode>().Children, Has.Count.EqualTo(2));
            Assert.That(nodes[1].As<CommentNode>().Children[0].As<CommentNode>().Comment.Comment.SortConfidence, Is.EqualTo(12));
            Assert.That(nodes[1].As<CommentNode>().Children[1].As<CommentNode>().Comment.Comment.SortConfidence, Is.EqualTo(3));
            Assert.That(nodes[1].As<CommentNode>().Children[1].Children, Has.Count.EqualTo(1));
            Assert.That(nodes[1].As<CommentNode>().Children[1].Children[0].As<CommentNode>().Comment.Comment.SortConfidence, Is.EqualTo(1));
        }
Esempio n. 6
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)
                    // TODO: Check for a specific permission "ban".
                    if (_permissionDao.CanUserModerateSub(currentUser.Id, 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);

                if ((item.Author != null && currentUser != null) && currentUser.Id == item.Author.Id)
                    item.CurrentUserIsAuthor = true;

                item.CanDelete = userCanMod || item.CurrentUserIsAuthor;
                item.CanEdit = item.CurrentUserIsAuthor;
            }

            return result;
        }
Esempio n. 7
0
        public void Can_send_message_to_user_and_then_reply()
        {
            // arrange
            var user1 = _membershipService.GetUserByUserName("skimur");
            var user2 = new User
            {
                UserName = "******"
            };
            _membershipService.InsertUser(user2);

            // act
            var sendResponse = _sendMessageHandler.Handle(new SendMessage
            {
                Author = user1.Id,
                Subject = "subject",
                Body = "body",
                To = user2.UserName
            });

            // assert
            Assert.That(string.IsNullOrEmpty(sendResponse.Error));
            var user1Messages = _messageService.GetPrivateMessagesForUser(user1.Id);
            var user2Messages = _messageService.GetPrivateMessagesForUser(user2.Id);
            Assert.That(user1Messages, Has.Count.EqualTo(0));
            Assert.That(user2Messages, Has.Count.EqualTo(1));

            // act
            var replyResponse = _replyMessageHandler.Handle(new ReplyMessage
            {
                ReplyToMessageId = user2Messages[0],
                Author = user2.Id,
                Body = "reply"
            });

            // assert
            Assert.That(string.IsNullOrEmpty(replyResponse.Error));
            user1Messages = _messageService.GetPrivateMessagesForUser(user1.Id);
            user2Messages = _messageService.GetPrivateMessagesForUser(user2.Id);
            Assert.That(user1Messages, Has.Count.EqualTo(1));
            Assert.That(user2Messages, Has.Count.EqualTo(1));
        }
Esempio n. 8
0
        public List<SubWrapped> Wrap(List<Sub> subs, User currentUser = null)
        {
            var wrapped = new List<SubWrapped>();
            foreach (var sub in subs)
            {
                wrapped.Add(new SubWrapped(sub));
            }

            var subscribed = currentUser != null ? _subDao.GetSubscribedSubsForUser(currentUser.Id) : new List<Guid>();

            foreach (var item in wrapped)
            {
                if (currentUser != null)
                    item.IsSubscribed = subscribed.Contains(item.Sub.Id);

                if (item.Sub.NumberOfSubscribers < 100 && !(currentUser != null && currentUser.IsAdmin))
                {
                    item.FuzzNumberOfSubscribers(_cache.GetAcquire("sub." + item.Sub.Id + ".fuzzed", TimeSpan.FromSeconds(30), () => Common.Fuzz(item.Sub.NumberOfSubscribers)));
                }
            }

            return wrapped;
        }
Esempio n. 9
0
 public SubWrapped Wrap(Sub sub, User currentUser = null)
 {
     return Wrap(new List<Sub> { sub }, currentUser)[0];
 }
Esempio n. 10
0
 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);
 }
Esempio n. 11
0
 public SubWrapped Wrap(Guid subId, User currentUser = null)
 {
     return Wrap(new List<Guid> {subId}, currentUser)[0];
 }
Esempio n. 12
0
 public ModeratorPermissions? GetUserPermissionsForSub(User user, Guid subId)
 {
     return _subModerationService.GetUserPermissionsForSub(user, subId);
 }
Esempio n. 13
0
 public CommentWrapped Wrap(Guid commentId, User currentUser = null)
 {
     return Wrap(new List<Guid> {commentId}, currentUser)[0];
 }
Esempio n. 14
0
 public MessageWrapped Wrap(Guid messageId, User currentUser)
 {
     return Wrap(new List<Guid> { messageId }, currentUser)[0];
 }
Esempio n. 15
0
 public ModeratorWrapped Wrap(Moderator moderator, User currentUser = null)
 {
     return Wrap(new List<Moderator> {moderator}, currentUser)[0];
 }
Esempio n. 16
0
 public PostWrapped Wrap(Guid postId, User currentUser = null)
 {
     return Wrap(new List<Guid> {postId}, currentUser)[0];
 }
Esempio n. 17
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.NumberOfReports > 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;
        }
Esempio n. 18
0
        public ModeratorPermissions? GetUserPermissionsForSub(User user, Guid subId)
        {
            if (user == null) return null;
            if(user.IsAdmin) return ModeratorPermissions.All;

            return _conn.Perform(conn =>
            {
                var query = conn.From<Moderator>().Where(x => x.UserId == user.Id && x.SubId == subId);
                query.SelectExpression = "SELECT \"permissions\"";
                var result = conn.Single(query);
                if (result == null)
                    return (ModeratorPermissions?)null;
                return result.Permissions;
            });
        }
Esempio n. 19
0
 public bool CanUserManageSubPosts(User user, Guid subId)
 {
     var permissions = GetUserPermissionsForSub(user, subId);
     if (permissions == null) return false;
     return permissions.HasPermission(ModeratorPermissions.Posts);
 }
Esempio n. 20
0
 public bool CanUserMarkCommentAsSpam(User user, Comment comment)
 {
     return CanUserManageSubPosts(user, comment.SubId);
 }
Esempio n. 21
0
        public void User_cant_respond_to_incoming_mod_mail_unless_moderator()
        {
            // arrange
            var user1 = _membershipService.GetUserByUserName("skimur");
            var user2 = new User
            {
                UserName = "******"
            };
            _membershipService.InsertUser(user2);
            var user3 = new User
            {
                UserName = "******"
            };
            _membershipService.InsertUser(user3);
            var createSubResponse = _createSubHandler.Handle(new CreateSub { Name = "testsub", CreatedByUserId = user1.Id, Description = "testsub", Type = SubType.Public });
            Assert.That(string.IsNullOrEmpty(createSubResponse.Error));
            var subId = createSubResponse.SubId;
            var moderatorId = user1.Id;
            var userId = user2.Id;
            var invalidUserId = user3.Id;
            // setup the initial message that will get an attempted illegal reply
            var sendResponse = _sendMessageHandler.Handle(new SendMessage
            {
                Author = userId,
                To = "/s/testsub",
                Subject = "subject",
                Body = "body"
            });
            Assert.That(string.IsNullOrEmpty(sendResponse.Error));

            // act
            var replyResponse = _replyMessageHandler.Handle(new ReplyMessage
            {
                ReplyToMessageId = sendResponse.MessageId,
                Author = invalidUserId, // this user isn't a moderator, nor is he involved in the discussion
                Body = "response"
            });

            // assert
            Assert.That(!string.IsNullOrEmpty(replyResponse.Error));
        }
Esempio n. 22
0
        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;
        }
Esempio n. 23
0
        public void User_can_send_and_received_messages_to_a_sub()
        {
            // arrange
            var user1 = _membershipService.GetUserByUserName("skimur");
            var user2 = new User
            {
                UserName = "******"
            };
            _membershipService.InsertUser(user2);
            var createSubResponse = _createSubHandler.Handle(new CreateSub { Name = "testsub", CreatedByUserId = user1.Id, Description = "testsub", Type = SubType.Public });
            Assert.That(string.IsNullOrEmpty(createSubResponse.Error));
            var subId = createSubResponse.SubId;
            var moderatorId = user1.Id;
            var userId = user2.Id;

            #region sending message from user to sub

            // act
            var sendResponse = _sendMessageHandler.Handle(new SendMessage
            {
                Author = userId,
                Subject = "subject",
                Body = "body",
                To = "/s/testsub"
            });

            // assert
            Assert.That(string.IsNullOrEmpty(sendResponse.Error));
            Assert.That(_messageService.GetSentMessagesForUser(userId), Has.Count.EqualTo(1).And.Contains(sendResponse.MessageId));
            Assert.That(_messageService.GetPrivateMessagesForUser(userId), Has.Count.EqualTo(0));
            Assert.That(_messageService.GetModeratorMailForSubs(new List<Guid> { subId }), Has.Count.EqualTo(1).And.Contains(sendResponse.MessageId));
            var message = _messageService.GetMessageById(sendResponse.MessageId);
            Assert.That(message.AuthorId, Is.EqualTo(userId));
            Assert.That(message.FromSub, Is.Null);
            Assert.That(message.ToSub, Is.Not.Null.And.EqualTo(subId));
            Assert.That(message.ToUser, Is.Null);

            #endregion

            #region reply to user from sub

            // act
            var replyResponse = _replyMessageHandler.Handle(new ReplyMessage
            {
                Author = moderatorId,
                ReplyToMessageId = sendResponse.MessageId,
                Body = "response"
            });

            // assert
            Assert.That(string.IsNullOrEmpty(replyResponse.Error));
            Assert.That(_messageService.GetSentMessagesForUser(userId), Has.Count.EqualTo(1).And.Contains(sendResponse.MessageId));
            Assert.That(_messageService.GetPrivateMessagesForUser(userId), Has.Count.EqualTo(1).And.Contains(replyResponse.MessageId));
            message = _messageService.GetMessageById(replyResponse.MessageId);
            Assert.That(message.AuthorId, Is.EqualTo(moderatorId));
            Assert.That(message.FromSub, Is.Not.Null.And.EqualTo(subId));
            Assert.That(message.ToSub, Is.Null);
            Assert.That(message.ToUser, Is.Not.Null.And.EqualTo(userId));

            #endregion

            #region moderator replies dont show in personal inboxes

            // assert
            Assert.That(_messageService.GetSentMessagesForUser(moderatorId), Has.Count.EqualTo(0));
            Assert.That(_messageService.GetPrivateMessagesForUser(moderatorId), Has.Count.EqualTo(0));

            #endregion

            #region moderator mail shows on in the correct moderator inboxes

            // assert
            Assert.That(_messageService.GetModeratorMailForSubs(new List<Guid> { subId }), Has.Count.EqualTo(1).And.Contains(sendResponse.MessageId));
            Assert.That(_messageService.GetSentModeratorMailForSubs(new List<Guid> { subId }), Has.Count.EqualTo(1).And.Contains(replyResponse.MessageId));

            #endregion
        }
Esempio n. 24
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 => _subModerationDao.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 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)
                        {
                            // 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;
        }
        public List<ICommentNode> Build(CommentTree tree, CommentTreeContext treeContext, User currentUser)
        {
            var wrapped = _commentWrapper.Wrap(treeContext.Comments, currentUser).ToDictionary(x => x.Comment.Id, x => new CommentNode(x));
            var final = new List<ICommentNode>();
            var walked = new List<Guid>();

            // lets mark any comments as collapsed if needed
            foreach (var comment in wrapped.Values)
            {
                // TODO: make this configurable per-user
                int minimumScore = 0;
                if ((comment.Comment.Author != null && currentUser != null) && currentUser.Id == comment.Comment.Author.Id)
                {
                    // the current user is the author, don't collapse!
                    comment.Collapsed = false;
                }
                else if (comment.Comment.Score < minimumScore)
                {
                    // too many down votes to show to the user
                    comment.Collapsed = true;
                }
                else
                {
                    // the current user is not the author, and we have enough upvotes to display,
                    // don't collapse
                    comment.Collapsed = false;
                }
            }

            foreach (var comment in wrapped.Values)
            {
                comment.NumberOfChildren = treeContext.CommentsChildrenCount[comment.Comment.Comment.Id];

                CommentNode parent = null;

                if (comment.Comment.Comment.ParentId.HasValue)
                    parent = wrapped.ContainsKey(comment.Comment.Comment.ParentId.Value)
                        ? wrapped[comment.Comment.Comment.ParentId.Value]
                        : null;

                if (comment.Comment.Comment.ParentId.HasValue && treeContext.Comments.Contains(comment.Comment.Comment.ParentId.Value))
                    comment.IsParentVisible = true;

                if (parent != null && comment.Comment.CurrentUserIsAuthor)
                {
                    // this comment is the current user, so lets walk the parents
                    // and uncollapse any comments which may be collapsed
                    var ancestor = parent;
                    while (ancestor != null && !walked.Contains(ancestor.Comment.Comment.Id))
                    {
                        ancestor.Collapsed = false;
                        walked.Add(ancestor.Comment.Comment.Id);
                        ancestor = (ancestor.Comment.Comment.ParentId.HasValue && wrapped.ContainsKey(comment.Comment.Comment.ParentId.Value))
                            ? wrapped[comment.Comment.Comment.ParentId.Value]
                            : null;
                    }
                }

                if (treeContext.MoreRecursion.Contains(comment.Comment.Comment.Id))
                    comment.Children.Add(new MoreRecursionNode(comment.Comment));

                if (parent != null)
                {
                    comment.Parent = comment.Comment;
                    parent.Children.Add(comment);
                }
                else
                {
                    if (comment.Comment.Comment.ParentId.HasValue)
                    {
                        // we don't have a parent here, but this comment does have a parent.
                        // we need to get it
                        comment.Parent = _commentWrapper.Wrap(comment.Comment.Comment.ParentId.Value);
                    }
                    final.Add(comment);
                }
            }

            foreach (var visibleId in wrapped.Keys)
            {
                // this item is already scheduled to have a new link to see all the children
                if (treeContext.MoreRecursion.Contains(visibleId)) continue;

                var children = tree.Tree.ContainsKey(visibleId) ? tree.Tree[visibleId] : new List<Guid>();
                var missingChildren = children.Where(x => !treeContext.Comments.Contains(x)).ToList();

                if (missingChildren.Count > 0)
                {
                    var visibleChildren = children.Where(x => treeContext.Comments.Contains(x)).ToList();
                    var visibleCount = visibleChildren.Sum(x => (treeContext.CommentsChildrenCount[x]) + 1);
                    var missingCount = treeContext.CommentsChildrenCount[visibleId] - visibleCount;
                    var missingDepth = (tree.Depth.ContainsKey(visibleId) ? tree.Depth[visibleId] : 0) + 1 -
                                       treeContext.OffsetDepth;

                    var moreChildren = new MoreChildren();
                    moreChildren.ChildComments.AddRange(missingChildren);
                    moreChildren.MissingCount = missingCount;
                    moreChildren.PostId = tree.PostId;
                    moreChildren.Sort = treeContext.Sort;
                    moreChildren.Depth = missingDepth;
                    wrapped[visibleId].Children.Add(moreChildren);
                }
            }

            if (treeContext.TopLevelCandidates.Count > 0)
            {
                var moreChildren = new MoreChildren();
                moreChildren.ChildComments.AddRange(treeContext.TopLevelCandidates);
                moreChildren.MissingCount = moreChildren.ChildComments.Sum(x => (treeContext.CommentsChildrenCount.ContainsKey(x) ? treeContext.CommentsChildrenCount[x] : 0) + 1);
                moreChildren.PostId = tree.PostId;
                moreChildren.Sort = treeContext.Sort;
                moreChildren.Depth = 0;
                final.Add(moreChildren);
            }

            return final;
        }
Esempio n. 26
0
 public bool CanUserMarkPostAsSpam(User user, Post post)
 {
     return CanUserManageSubPosts(user, post.SubId);
 }