public async Task BrowsePostsFilters(BrowsePostFilterTestCase test) { Scene[] CreateScenes() => new[] { new Scene { Name = "My Scene" } }; async Task CreatePost(string postTitle, string postText, int votes, DateTimeOffset created, params string[] postTags) { var post = await Repository.CreatePostAsync( LoginInfo, postTitle, postTags, CreateScenes(), new PostFile[0], "", created ); var viewModel = PostViewModel.From(post, false); viewModel.Published = true; viewModel.Text = postText; await Repository.UpdatePostAsync(LoginInfo, viewModel, post.DateCreated.AddDays(1)); await _votes.VoteAsync(LoginInfo, post.Id, votes); } var now = new DateTimeOffset(2000, 01, 01, 01, 01, 01, TimeSpan.Zero); await CreatePost("2 years ago, 100pts", "", 100, now.AddYears(-2), "tag1"); await CreatePost("11 months ago, 50pts", "", 50, now.AddMonths(-11), "tag2"); await CreatePost("3 weeks ago, 200pts", "", 200, now.AddDays(-7 * 3), "tag1", "tag2"); await CreatePost("6 days ago, 90pts", "cool one", 90, now.AddDays(-6)); await CreatePost("23 hours ago, 30pts", "", 30, now.AddHours(-1)); CreateDbContext(); var posts = await Repository.BrowsePostsAsync(test.SortBy, test.SortDirection, test.Since, test.Page, test.PageSize, test.Tags, test.Author, test.Text, now); CollectionAssert.AreEqual(test.Expected, posts.Select(post => post.Title).ToArray()); }
private PostViewModel PrepareForDisplay(Post post, bool optimize) { var viewModel = PostViewModel.From(post, optimize); if (viewModel.ThumbnailUrn != null) { viewModel.ThumbnailUrl = Url.RouteUrl(nameof(PostFilesController.GetAsync), new { postId = post.Id, urn = viewModel.ThumbnailUrn }); } if (viewModel.Scenes?.Any() ?? false) { foreach (var scene in viewModel.Scenes.Where(scene => scene.ThumbnailUrn != null)) { scene.ThumbnailUrl = Url.RouteUrl(nameof(PostFilesController.GetAsync), new { postId = post.Id, urn = scene.ThumbnailUrn }); } } return(viewModel); }
public async Task <IActionResult> Get() { if (!User.Identity.IsAuthenticated) { return(Unauthorized()); } var login = this.GetUserLoginInfo(); var account = await _usersRepository.LoadPrivateUserAsync(login); if (account == null) { return(NotFound()); } var posts = await _postsRepository.BrowseMyPostsAsync(login); return(Ok(new MyAccountViewModel { Username = account.Username, MyPosts = posts.OrderByDescending(p => p.DatePublished).Select(p => PostViewModel.From(p, true)).ToArray() })); }
public async Task BrowsePostsContainsExpectedFields() { var saved = await Repository.CreatePostAsync( LoginInfo, "My Post", new[] { "my-tag" }, new[] { new Scene { Name = "My Scene" } }, new[] { new PostFile { Filename = "file.json", MimeType = "application/json", Urn = "urn:vambooru:tests:0001", Compressed = true }, new PostFile { Filename = "file.jpg", MimeType = "image/jpeg", Urn = "urn:vambooru:tests:0002" } }, "urn:vambooru:tests:0002", new DateTimeOffset(2005, 02, 03, 04, 05, 06, TimeSpan.Zero) ); var viewModel = PostViewModel.From(saved, false); viewModel.Published = true; viewModel.Text = "Some text..."; await Repository.UpdatePostAsync(LoginInfo, viewModel, new DateTimeOffset(2005, 02, 03, 04, 05, 07, TimeSpan.Zero)); CreateDbContext(); var posts = await Repository.BrowsePostsAsync(PostSortBy.Default, PostSortDirection.Default, PostedSince.Default, 0, 1, null, null, null, DateTimeOffset.MaxValue); Assert.That(posts.Length, Is.EqualTo(1)); posts[0].ShouldDeepEqual(new Post { Title = "My Post", // Text should be excluded Text = null, Author = CurrentUser, DateCreated = new DateTimeOffset(2005, 02, 03, 04, 05, 06, TimeSpan.Zero), DatePublished = new DateTimeOffset(2005, 02, 03, 04, 05, 07, TimeSpan.Zero), Published = true, ThumbnailUrn = "urn:vambooru:tests:0002", Tags = new[] { new PostTag { Tag = new Tag { Name = "my-tag", PostsCount = 1 } } }.ToHashSet() }, c => { c.MembersToIgnore.Add("*Id"); c.MembersToIgnore.Add("UserLogin.User"); c.MembersToIgnore.Add("PostTag.PostId"); c.MembersToIgnore.Add("PostTag.TagId"); c.MembersToIgnore.Add("PostTag.Post"); c.MembersToIgnore.Add("Tag.Id"); c.MembersToIgnore.Add("Scene.Post"); c.MembersToIgnore.Add("User.Posts"); c.MembersToIgnore.Add("User.Logins"); }); }
public async Task Seed() { // Clear database await EntityFrameworkTestsHelper.ClearAndMarkTestDatabase(_dbContext); // Setup users var cloudyLogin = new UserLoginInfo("e2e", "cloudy_dude"); var cloudy = await _usersRepository.LoadOrCreateUserFromLoginAsync(cloudyLogin.Scheme, cloudyLogin.NameIdentifier, "Cloudy Dude", new DateTimeOffset(2018, 01, 15, 15, 23, 12, TimeSpan.Zero)); var billyLogin = new UserLoginInfo("e2e", "H4x0rs4evaaaah"); var billy = await _usersRepository.LoadOrCreateUserFromLoginAsync(billyLogin.Scheme, billyLogin.NameIdentifier, "Billy", new DateTimeOffset(2000, 01, 01, 00, 00, 00, TimeSpan.Zero)); var edLogin = new UserLoginInfo("e2e", "153234"); var ed = await _usersRepository.LoadOrCreateUserFromLoginAsync(edLogin.Scheme, edLogin.NameIdentifier, "ed", new DateTimeOffset(2018, 03, 14, 19, 54, 48, TimeSpan.Zero)); // Setup posts var gettingReadyToJumpJpg = await CreateFile(new PostFile { Filename = "getting_ready_to_jump.jpg", MimeType = "image/jpeg", Compressed = false, }); var gettingReadyToJumpJson = await CreateFile(new PostFile { Filename = "getting_ready_to_jump.json", MimeType = "application/json", Compressed = true, }); var gettingReadyToJumpWav = await CreateFile(new PostFile { Filename = "getting_ready_to_jump.wav", MimeType = "audio/wav", Compressed = true, }); var gettingReadyToJumpPost = await _postsRepository.CreatePostAsync( cloudyLogin, "Jumping Lady", new[] { "jumping", "lady" }, new[] { new Scene { Name = "getting_ready_to_jump", ThumbnailUrn = gettingReadyToJumpJpg.Urn } }, new[] { gettingReadyToJumpJson, gettingReadyToJumpJpg, gettingReadyToJumpWav }, gettingReadyToJumpJpg.Urn, new DateTimeOffset(2018, 01, 16, 21, 02, 54, TimeSpan.Zero) ); var gettingReadyToJumpPostViewModel = PostViewModel.From(gettingReadyToJumpPost, false); gettingReadyToJumpPostViewModel.Published = true; await _postsRepository.UpdatePostAsync(cloudyLogin, gettingReadyToJumpPostViewModel, new DateTimeOffset(2018, 01, 16, 21, 03, 49, TimeSpan.Zero)); // Setup comments await _commentsRepository.CreatePostCommentAsync(billyLogin, gettingReadyToJumpPost.Id, "This scene rocks man", new DateTimeOffset(2018, 01, 16, 21, 02, 54, TimeSpan.Zero)); await _commentsRepository.CreatePostCommentAsync(cloudyLogin, gettingReadyToJumpPost.Id, "Thanks!", new DateTimeOffset(2018, 01, 16, 21, 04, 24, TimeSpan.Zero)); await _commentsRepository.CreatePostCommentAsync(edLogin, gettingReadyToJumpPost.Id, "She's not even *jumping*, _yo_.", new DateTimeOffset(2018, 01, 16, 21, 08, 07, TimeSpan.Zero)); await _commentsRepository.CreatePostCommentAsync(billyLogin, gettingReadyToJumpPost.Id, "Get\na\nlife", new DateTimeOffset(2018, 01, 16, 21, 12, 35, TimeSpan.Zero)); // Setup votes await _votesRepository.VoteAsync(cloudyLogin, gettingReadyToJumpPost.Id, 10); await _votesRepository.VoteAsync(billyLogin, gettingReadyToJumpPost.Id, 10); await _votesRepository.VoteAsync(edLogin, gettingReadyToJumpPost.Id, -2); }
public async Task TagPostsCount() { var post1 = await _posts.CreatePostAsync(LoginInfo, "Post1", new[] { "tag1", "tag2" }, new Scene[0], new PostFile[0], "", DateTimeOffset.MinValue); var post1ViewModel = PostViewModel.From(post1, false); var post2 = await _posts.CreatePostAsync(LoginInfo, "Post2", new[] { "tag2", "tag3" }, new Scene[0], new PostFile[0], "", DateTimeOffset.MinValue); var post2ViewModel = PostViewModel.From(post2, false); await _posts.CreatePostAsync(LoginInfo, "Post3", new[] { "tag4" }, new Scene[0], new PostFile[0], "", DateTimeOffset.MinValue); // Zero by default { CreateDbContext(); var tags = await Repository.SearchTags("tag"); tags.Select(TagViewModel.From).OrderBy(t => t.Name).ToArray().ShouldDeepEqual(new[] { new TagViewModel { Name = "tag1", PostsCount = 0 }, new TagViewModel { Name = "tag2", PostsCount = 0 }, new TagViewModel { Name = "tag3", PostsCount = 0 }, new TagViewModel { Name = "tag4", PostsCount = 0 } }, c => c.MembersToIgnore.Add("*Id")); } // Increase when published { post1ViewModel.Published = true; await _posts.UpdatePostAsync(LoginInfo, post1ViewModel, DateTimeOffset.UtcNow); post2ViewModel.Published = true; await _posts.UpdatePostAsync(LoginInfo, post2ViewModel, DateTimeOffset.UtcNow); CreateDbContext(); var tags = await Repository.SearchTags("tag"); tags.Select(TagViewModel.From).OrderBy(t => t.Name).ToArray().ShouldDeepEqual(new[] { new TagViewModel { Name = "tag1", PostsCount = 1 }, new TagViewModel { Name = "tag2", PostsCount = 2 }, new TagViewModel { Name = "tag3", PostsCount = 1 }, new TagViewModel { Name = "tag4", PostsCount = 0 } }, c => c.MembersToIgnore.Add("*Id")); } // Sort by usage { var tags = await Repository.LoadTopTags(4); tags.Select(TagViewModel.From).ToArray().ShouldDeepEqual(new[] { new TagViewModel { Name = "tag2", PostsCount = 2 }, new TagViewModel { Name = "tag1", PostsCount = 1 }, new TagViewModel { Name = "tag3", PostsCount = 1 } }, c => c.MembersToIgnore.Add("*Id")); } // Track changes { post1ViewModel.Tags = new[] { new TagViewModel { Name = "tag2" }, new TagViewModel { Name = "tag3" } }; await _posts.UpdatePostAsync(LoginInfo, post1ViewModel, DateTimeOffset.UtcNow); CreateDbContext(); var tags = await Repository.SearchTags("tag"); tags.Select(TagViewModel.From).OrderBy(t => t.Name).ToArray().ShouldDeepEqual(new[] { new TagViewModel { Name = "tag1", PostsCount = 0 }, new TagViewModel { Name = "tag2", PostsCount = 2 }, new TagViewModel { Name = "tag3", PostsCount = 2 }, new TagViewModel { Name = "tag4", PostsCount = 0 }, }, c => c.MembersToIgnore.Add("*Id")); } // Decrease when unpublished { post1ViewModel.Published = false; await _posts.UpdatePostAsync(LoginInfo, post1ViewModel, DateTimeOffset.UtcNow); CreateDbContext(); var tags = await Repository.SearchTags("tag"); tags.Select(TagViewModel.From).OrderBy(t => t.Name).ToArray().ShouldDeepEqual(new[] { new TagViewModel { Name = "tag1", PostsCount = 0 }, new TagViewModel { Name = "tag2", PostsCount = 1 }, new TagViewModel { Name = "tag3", PostsCount = 1 }, new TagViewModel { Name = "tag4", PostsCount = 0 }, }, c => c.MembersToIgnore.Add("*Id")); } }