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; }
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)); }
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; }
public List<SubWrapped> Wrap(List<Guid> subIds, User currentUser = null) { var subs = new List<SubWrapped>(); foreach (var subId in subIds) { var sub = _subDao.GetSubById(subId); if (sub != null) subs.Add(new SubWrapped(sub)); } var subscribed = currentUser != null ? _subDao.GetSubscribedSubsForUser(currentUser.Id) : new List<Guid>(); foreach (var item in subs) { if (currentUser != null) item.IsSubscribed = subscribed.Contains(item.Sub.Id); } return subs; }
public CommentWrapped Wrap(Guid commentId, User currentUser = null) { return Wrap(new List<Guid> {commentId}, currentUser)[0]; }
public PostWrapped Wrap(Guid postId, User currentUser = null) { return Wrap(new List<Guid> {postId}, currentUser)[0]; }
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; }
/// <summary> /// Validate the user (update or insert) /// </summary> /// <param name="user"></param> /// <returns></returns> public UserValidationResult ValidateUser(User user) { var result = UserValidationResult.Success; if (!IsUserNameValid(user.UserName)) result |= result | UserValidationResult.InvalidUserName; if (!IsEmailValid(user.Email)) result |= UserValidationResult.InvalidEmail; if (result != UserValidationResult.Success) return result; if (!CanChangedEmail(user.Id, user.Email)) result |= UserValidationResult.EmailInUse; if (user.Id == Guid.Empty) { // we are inserting this user, let's see if any user has this username if (GetUserByUserName(user.UserName) != null) result |= UserValidationResult.UserNameInUse; } else { // we are updating the user, make sure the user name wasn't changed var dbUser = GetUserByUserName(user.UserName); if (dbUser != null) if (dbUser.Id != user.Id) result |= UserValidationResult.CantChangeUsername; } return result; }
public bool UpdateUser(User user) { if (user.Id == Guid.Empty) throw new Exception("You cannot update a user that doesn't have a user id"); return _conn.Perform(conn => conn.Update(user) == 1); }
public bool InsertUser(User user) { if (user.Id != Guid.Empty) throw new Exception("You cannot insert a user with an existing user id"); user.Id = GuidUtil.NewSequentialId(); user.CreatedDate = Common.CurrentTime(); return _conn.Perform(conn => conn.Insert(user) == 1); }
public SubWrapped Wrap(Guid subId, User currentUser = null) { return Wrap(new List<Guid> {subId})[0]; }