public async Task Test_05_AccessControl_GetBlogsAsync() { using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext); PersonHandler perHandler = new PersonHandler(dbContext); PersonXPersonHandler pxpHandler = new PersonXPersonHandler(dbContext); Person faker = CreatePerson("TestFaker", "TestFaker"); Person marin = CreatePerson("TestMarin", "TestMarin"); Person deft = CreatePerson("TestDeft", "TestDeft"); Follow(faker.ID, marin.ID, deft.ID); //1. test access info MySelfOnly. await blogHandler.CreateBlogAsync(faker.ID, "TestContentByFakerOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(marin.ID, "TestContentByMarinOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(marin.ID, "TestContentByMarinTwo", BlogInfoAccessInfo.MyselfOnly); await blogHandler.CreateBlogAsync(deft.ID, "TestContentByDeftOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(deft.ID, "TestContentByDeftTwo", BlogInfoAccessInfo.MyselfOnly); List<Blog> blogs = await blogHandler.GetBlogsAsync(faker.ID); Assert.AreEqual(blogs.Count, 3); foreach (var blog in blogs) { Assert.AreNotEqual(blog.Content, "TestContentByMarinTwo"); Assert.AreNotEqual(blog.Content, "TestContentByDeftTwo"); } //2. test access info GroupOnly. Person paul = CreatePerson("TestPaul", "TestPaul"); Person judy = CreatePerson("TestJudy", "TestJudy"); Person anne = CreatePerson("TestAnne", "TestAnne"); Person alisa = CreatePerson("TestAlisa", "TestAlisa"); Follow(paul.ID, judy.ID, anne.ID, alisa.ID); //judy group not include paul. Group judyGroup = await groupHandler.CreateGroupAsync(judy.ID, "TestJudyGroup", GroupType.GroupList); //anne group include paul. Group anneGroup = await groupHandler.CreateGroupAsync(anne.ID, "TestAnneGroup", GroupType.GroupList); GroupMember groupMemberByAnne = new GroupMember() { GroupID = anneGroup.ID, PersonID = paul.ID }; gmHandler.Add(groupMemberByAnne); gmHandler.SaveChanges(); //alisa group include judy, anne but not paul. Group alisaGroup = await groupHandler.CreateGroupAsync(alisa.ID, "TestAlisaGroup", GroupType.GroupList); GroupMember groupMemberByAlisaOne = new GroupMember() { GroupID = alisaGroup.ID, PersonID = judy.ID }; GroupMember groupMemberByAlisaTwo = new GroupMember() { GroupID = alisaGroup.ID, PersonID = anne.ID }; gmHandler.Add(groupMemberByAlisaOne); gmHandler.Add(groupMemberByAlisaTwo); gmHandler.SaveChanges(); await blogHandler.CreateBlogAsync(paul.ID, "TestContentByPaul", BlogInfoAccessInfo.MyselfOnly); await blogHandler.CreateBlogAsync(judy.ID, "TestContentByJudyOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(judy.ID, "TestContentByJudyTwo", BlogInfoAccessInfo.GroupOnly, judyGroup.ID); await blogHandler.CreateBlogAsync(anne.ID, "TestContentByAnne", BlogInfoAccessInfo.GroupOnly, anneGroup.ID); await blogHandler.CreateBlogAsync(alisa.ID, "TestContentByAlisa", BlogInfoAccessInfo.GroupOnly, alisaGroup.ID); blogs = await blogHandler.GetBlogsAsync(paul.ID); Assert.AreEqual(blogs.Count, 3); foreach (var blog in blogs) { Assert.AreNotEqual(blog.Content, "TestContentByJudyTwo"); Assert.AreNotEqual(blog.Content, "TestContentByAlisa"); } //3. test access info FriendOnly. Person sam = CreatePerson("TestSam", "TestSam"); Person joan = CreatePerson("TestJoan", "TestJoan"); Person lily = CreatePerson("TestLily", "TestLily"); Follow(sam.ID, joan.ID, lily.ID); //3.1 test joan and sam is friend but not lily. Follow(joan.ID, sam.ID); await blogHandler.CreateBlogAsync(joan.ID, "TestContentByJoanOne", BlogInfoAccessInfo.FriendOnly); await blogHandler.CreateBlogAsync(joan.ID, "TestContentByJoanTwo", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(lily.ID, "TestContentByLilyOne", BlogInfoAccessInfo.FriendOnly); await blogHandler.CreateBlogAsync(lily.ID, "TestContentByLilyTwo", BlogInfoAccessInfo.FriendOnly); blogs = await blogHandler.GetBlogsAsync(sam.ID); Assert.AreEqual(blogs.Count, 2); foreach (var blog in blogs) { Assert.AreNotEqual(blog.Content, "TestContentByLilyOne"); Assert.AreNotEqual(blog.Content, "TestContentByLilyTwo"); } //3.2 now lily follow sam too. Follow(lily.ID, sam.ID); blogs = await blogHandler.GetBlogsAsync(sam.ID); Assert.AreEqual(blogs.Count, 4); } }
public async Task Test_01_CreateBlogWithoutContentAsync() { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext); BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext); //1. test normal. Blog newBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlog", BlogInfoAccessInfo.All, null); Blog testBlog_1 = await blogHandler.GetByIdAsync(newBlog.ID); BlogAccessControl testAC_1 = await acHandler.Fetch(x => x.BlogID == testBlog_1.ID).FirstOrDefaultAsync(); Assert.IsNotNull(testBlog_1); Assert.IsNotNull(testAC_1); Assert.AreEqual(testBlog_1.PersonID, testPerson.ID); Assert.AreEqual(testBlog_1.Content, "testBlog"); Assert.AreEqual(testAC_1.AccessLevel, BlogInfoAccessInfo.All); //2. set BlogInfoAccessInfo GroupOnly and set GroupID is null then check it. bool isChecked = false; try { newBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogTwo", BlogInfoAccessInfo.GroupOnly, null); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "未指定组"); } Assert.IsTrue(isChecked); //3. set not exist GroupID. expect Exception is type of foreign key exception. isChecked = false; try { newBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogThree", BlogInfoAccessInfo.GroupOnly, 9999); } catch (Exception ex) { isChecked = true; Assert.AreNotEqual(ex.GetType(), typeof(AssertException)); } Assert.IsTrue(isChecked); } using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext); BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext); //4. create test group object and test it. Group testGroup = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroup", GroupType.GroupList); bool isChecked = false; try { Blog testBlog4Group = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogFour", BlogInfoAccessInfo.GroupOnly, testGroup.ID); Blog testBlog_2 = await blogHandler.GetByIdAsync(testBlog4Group.ID); BlogAccessControl testAC_2 = await acHandler.Fetch(x => x.BlogID == testBlog_2.ID).FirstOrDefaultAsync(); BlogAccessControlXGroup testACXG = await acxgHandler.Fetch(x => x.GroupID == testGroup.ID && x.BlogAccessControlID == testAC_2.ID).FirstOrDefaultAsync(); Assert.IsNotNull(testBlog_2); Assert.IsNotNull(testAC_2); Assert.IsNotNull(testACXG); Assert.AreEqual(testBlog_2.PersonID, testPerson.ID); Assert.AreEqual(testBlog_2.Content, "testBlogFour"); Assert.AreEqual(testAC_2.AccessLevel, BlogInfoAccessInfo.GroupOnly); } catch (Exception) { isChecked = true; } Assert.IsFalse(isChecked); } }
public async Task Test_04_Normal_GetBlogsAsync() { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext); PersonHandler perHandler = new PersonHandler(dbContext); PersonXPersonHandler pxpHandler = new PersonXPersonHandler(dbContext); //1. test normal. bool isChecked = false; try { List<Blog> invalidPersonIdBlogs = await blogHandler.GetBlogsAsync(999999999); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.Message, "该用户不存在"); } Assert.IsTrue(isChecked); Person master = CreatePerson("TestMasterPer", "TestMasterMind"); Person mary = CreatePerson("TestMary", "TestMary"); Person nick = CreatePerson("TestNick", "TestNick"); Person tony = CreatePerson("TestTony", "TestTony"); //1. create some blog and test it. await blogHandler.CreateBlogAsync(master.ID, "TestContentByMaster", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(mary.ID, "TestContentByMaryOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(mary.ID, "TestContentByMaryTwo", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(nick.ID, "TestContentByNickOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(tony.ID, "TestContentByTonyOne", BlogInfoAccessInfo.All); Follow(master.ID, mary.ID, nick.ID, tony.ID); List<Blog> blogs = await blogHandler.GetBlogsAsync(master.ID); Assert.AreEqual(blogs.Count, 5); Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2); Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1); Assert.AreEqual(blogs.Count(x => x.PersonID == tony.ID), 1); Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1); //2. add a group and add some group member test it. Group masterGroup = new Group() { PersonID = master.ID, Name = "TestMasterGroup", Type = GroupType.GroupList }; groupHandler.Add(masterGroup); groupHandler.SaveChanges(); GroupMember GroupMemberByMary = new GroupMember() { GroupID = masterGroup.ID, PersonID = mary.ID }; GroupMember GroupMemberByNick = new GroupMember() { GroupID = masterGroup.ID, PersonID = nick.ID }; gmHandler.Add(GroupMemberByMary); gmHandler.Add(GroupMemberByNick); gmHandler.SaveChanges(); //3. test get blog by group. blogs = await blogHandler.GetBlogsAsync(master.ID, masterGroup.ID); Assert.AreEqual(blogs.Count, 4); Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2); Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1); Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1); //4. add shield group and test it. Person mike = CreatePerson("TestMike", "TestMike"); Person yoyo = CreatePerson("TestYOYO", "TestYOYO"); Person pipi = CreatePerson("TestPIPI", "TestPIPI"); Person poko = CreatePerson("TestPoko", "TestPoko"); Follow(master.ID, mike.ID, yoyo.ID, pipi.ID, poko.ID); await blogHandler.CreateBlogAsync(mike.ID, "TestContentByMikeOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(mike.ID, "TestContentByMikeTwo", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(yoyo.ID, "TestContentByYoyoOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(yoyo.ID, "TestContentByYoyoTwo", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(pipi.ID, "TestContentByPipiOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoOne", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoTwo", BlogInfoAccessInfo.All); await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoThree", BlogInfoAccessInfo.All); Group masterShieldGroup = new Group() { PersonID = master.ID, Name = "TestMasterShield", Type = GroupType.ShieldList }; groupHandler.Add(masterShieldGroup); groupHandler.SaveChanges(); GroupMember GroupMemberByPoko = new GroupMember() { GroupID = masterShieldGroup.ID, PersonID = poko.ID }; gmHandler.Add(GroupMemberByPoko); gmHandler.SaveChanges(); blogs = await blogHandler.GetBlogsAsync(master.ID); Assert.AreEqual(blogs.Count, 10); foreach (var blog in blogs) { Assert.AreNotEqual(blog.PersonID, poko.ID); Assert.AreNotEqual(blog.Content, "TestContentByPokoOne"); Assert.AreNotEqual(blog.Content, "TestContentByPokoTwo"); Assert.AreNotEqual(blog.Content, "TestContentByPokoThree"); } //5. add group member to normal group and test it. GroupMember GroupMemberByPoko_Normal = new GroupMember() { GroupID = masterGroup.ID, PersonID = poko.ID }; gmHandler.Add(GroupMemberByMary); blogs = await blogHandler.GetBlogsAsync(master.ID, masterGroup.ID); Assert.AreEqual(blogs.Count, 4); Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2); Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1); Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1); } }
public async Task Test_02_CreateBlogIncludeContentAsync() { using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext); BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext); List<long> contentIds = new List<long>(); Content content = new Content() { ContentPath = "testPath", ContentBinary = new byte[] { 1, 3, 5 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(content); contentHandler.SaveChanges(); contentIds.Add(content.ID); Stopwatch sw = Stopwatch.StartNew(); //1. test normal. Blog testBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlog", BlogInfoAccessInfo.All, null, contentIds); var time = sw.ElapsedMilliseconds; Blog testBlog_1 = await blogHandler.GetByIdAsync(testBlog.ID); BlogAccessControl testAC_1 = await acHandler.Fetch(x => x.BlogID == testBlog_1.ID).FirstOrDefaultAsync(); BlogXContent testBXC_1 = await bxcHandler.Fetch(x => x.BlogID == testBlog_1.ID).FirstOrDefaultAsync(); Content testContent_1 = testBXC_1.Content; Assert.IsNotNull(testBlog_1); Assert.IsNotNull(testAC_1); Assert.IsNotNull(testBXC_1); Assert.IsNotNull(testContent_1); Assert.AreEqual(testBlog_1.PersonID, testPerson.ID); Assert.AreEqual(testBlog_1.Content, "testBlog"); Assert.AreEqual(testAC_1.AccessLevel, BlogInfoAccessInfo.All); Assert.AreEqual(testContent_1.ContentPath, "testPath"); Assert.AreEqual(testContent_1.ContentBinary, new byte[] { 1, 3, 5 }); //2. create test group object and test it. contentIds = new List<long>(); Content content1 = new Content() { ContentPath = "testFilePathYesOrNo", ContentBinary = new byte[] { 23, 31, 45, 78, 99 }, Type = ContentType.Video, MimeType = "jpg" }; contentHandler.Add(content1); contentHandler.SaveChanges(); contentIds.Add(content1.ID); Group testGroup = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroup", GroupType.GroupList); testBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogGroupOnly", BlogInfoAccessInfo.GroupOnly, testGroup.ID, contentIds); Blog testBlog_2 = await blogHandler.GetByIdAsync(testBlog.ID); BlogAccessControl testAC_2 = await acHandler.Fetch(x => x.BlogID == testBlog_2.ID).FirstOrDefaultAsync(); BlogAccessControlXGroup testACXG_2 = await acxgHandler.Fetch(x => x.GroupID == testGroup.ID && x.BlogAccessControlID == testAC_2.ID).FirstOrDefaultAsync(); BlogXContent testBXC_2 = await bxcHandler.Fetch(x => x.BlogID == testBlog_2.ID).FirstOrDefaultAsync(); Content testContent_2 = testBXC_2.Content; Assert.IsNotNull(testBlog_2); Assert.IsNotNull(testAC_2); Assert.IsNotNull(testACXG_2); Assert.IsNotNull(testBXC_2); Assert.IsNotNull(testContent_2); Assert.AreEqual(testBlog_2.PersonID, testPerson.ID); Assert.AreEqual(testBlog_2.Content, "testBlogGroupOnly"); Assert.AreEqual(testAC_2.AccessLevel, BlogInfoAccessInfo.GroupOnly); Assert.AreEqual(testContent_2.ContentPath, "testFilePathYesOrNo"); Assert.AreEqual(testContent_2.ContentBinary, new byte[] { 23, 31, 45, 78, 99 }); Assert.AreEqual(testContent_2.Type, ContentType.Video); } using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext); BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext); List<long> contentIds = new List<long>(); //3. create test group object but set BlogInfoAccessInfo other value. Content content1 = new Content() { ContentPath = "testFilePathYesOrNo", ContentBinary = new byte[] { 23, 31, 45, 78, 99 }, Type = ContentType.Video, MimeType = "jpg" }; contentHandler.Add(content1); contentHandler.SaveChanges(); contentIds.Add(content1.ID); Group testGroup_1 = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroupWithTest", GroupType.GroupList); Blog testBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogNotSetGroupOnly", BlogInfoAccessInfo.MyselfOnly, testGroup_1.ID, contentIds); Blog testBlog_3 = await blogHandler.GetByIdAsync(testBlog.ID); BlogAccessControl testAC_3 = await acHandler.Fetch(x => x.BlogID == testBlog_3.ID).FirstOrDefaultAsync(); BlogAccessControlXGroup testACXG_3 = await acxgHandler.Fetch(x => x.GroupID == testGroup_1.ID && x.BlogAccessControlID == testAC_3.ID).FirstOrDefaultAsync(); BlogXContent testBXC_3 = await bxcHandler.Fetch(x => x.BlogID == testBlog_3.ID).FirstOrDefaultAsync(); Content testContent_3 = testBXC_3.Content; Assert.IsNotNull(testBlog_3); Assert.IsNotNull(testAC_3); Assert.IsNotNull(testBXC_3); Assert.IsNotNull(testContent_3); //it expect null because BlogInfoAccessInfo is not GroupOnly. Assert.IsNull(testACXG_3); Assert.AreEqual(testBlog_3.PersonID, testPerson.ID); Assert.AreEqual(testBlog_3.Content, "testBlogNotSetGroupOnly"); Assert.AreEqual(testAC_3.AccessLevel, BlogInfoAccessInfo.MyselfOnly); Assert.AreEqual(testContent_3.ContentPath, "testFilePathYesOrNo"); Assert.AreEqual(testContent_3.ContentBinary, new byte[] { 23, 31, 45, 78, 99 }); Assert.AreEqual(testContent_3.Type, ContentType.Video); //4. create multiple content and test it. contentIds = new List<long>(); for (int i = 0; i < 3; i++) { Content c = new Content() { ContentPath = "testMultiplePath", ContentBinary = new byte[] { 12, 3, 4, 5, 7 }, Type = ContentType.Video, MimeType = "jpg" }; contentHandler.Add(c); contentHandler.SaveChanges(); contentIds.Add(c.ID); } bool isChecked = false; try { Blog testBlog_MultipleContent_1 = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogMultipleContent1", BlogInfoAccessInfo.MyselfOnly, testGroup_1.ID, contentIds); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "发表视频不能超过1个"); } Assert.IsTrue(isChecked); //5. create multiple content but different type and test it. contentIds = new List<long>(); Content cPhoto = new Content() { ContentPath = "testMultiplePath", ContentBinary = new byte[] { 12, 3, 4, 5, 7 }, Type = ContentType.Photo, MimeType = "jpg" }; Content cMusic = new Content() { ContentPath = "testMultiplePath", ContentBinary = new byte[] { 12, 3, 4, 5, 7 }, Type = ContentType.Photo, MimeType = "jpg" }; Content cVideo = new Content() { ContentPath = "testMultiplePath", ContentBinary = new byte[] { 12, 3, 4, 5, 7 }, Type = ContentType.Video, MimeType = "jpg" }; contentHandler.Add(cPhoto); contentHandler.Add(cMusic); contentHandler.Add(cVideo); contentHandler.SaveChanges(); contentIds.Add(cPhoto.ID); contentIds.Add(cMusic.ID); contentIds.Add(cVideo.ID); isChecked = false; try { Blog testBlog_MultipleContent_2 = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogMultipleContent1", BlogInfoAccessInfo.MyselfOnly, testGroup_1.ID, contentIds); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "不能发不同类型内容的Blog"); } Assert.IsTrue(isChecked); //6. create some photo content and test it. contentIds = new List<long>(); for (int i = 0; i < 6; i++) { Content photo = new Content() { ContentPath = "testPhotoContentPath", ContentBinary = new byte[] { 12, 3, 4, 5, 7 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(photo); contentHandler.SaveChanges(); contentIds.Add(photo.ID); } Group testGroup_2 = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroupWithTest", GroupType.GroupList); Blog testBlog_MultiplePhoto_3 = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogMultiplePhoto", BlogInfoAccessInfo.GroupOnly, testGroup_2.ID, contentIds); Blog testPhotoBlog = await blogHandler.GetByIdAsync(testBlog_MultiplePhoto_3.ID); BlogAccessControl testPhotoAC = await acHandler.Fetch(x => x.BlogID == testPhotoBlog.ID).FirstOrDefaultAsync(); BlogAccessControlXGroup testPhotoACXG = await acxgHandler.Fetch(x => x.GroupID == testGroup_2.ID && x.BlogAccessControlID == testPhotoAC.ID).FirstOrDefaultAsync(); List<BlogXContent> testPhotoBXC = await bxcHandler.Fetch(x => x.BlogID == testPhotoBlog.ID).ToListAsync(); Assert.IsFalse(testPhotoBlog.IsDeleted); Assert.IsNotNull(testPhotoBlog); Assert.IsNotNull(testPhotoAC); Assert.IsNotNull(testPhotoACXG); Assert.IsNotNull(testPhotoBXC); Assert.AreEqual(testPhotoBXC.Count, 6); Assert.AreEqual(testPhotoBlog.PersonID, testPerson.ID); Assert.AreEqual(testPhotoBlog.Content, "testBlogMultiplePhoto"); Assert.AreEqual(testPhotoAC.AccessLevel, BlogInfoAccessInfo.GroupOnly); foreach (var photoBXC in testPhotoBXC) { Assert.IsNotNull(photoBXC.Content); Assert.AreEqual(photoBXC.Content.Type, ContentType.Photo); Assert.AreEqual(photoBXC.Content.ContentPath, "testPhotoContentPath"); Assert.AreEqual(photoBXC.Content.ContentBinary, new byte[] { 12, 3, 4, 5, 7 }); } } }
public async Task Test_03_ForwardBlogAsync() { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); BlogXBlogHandler bxbHandler = new BlogXBlogHandler(dbContext); ContentHandler contentHandler = new ContentHandler(dbContext); BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext); BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext); //1. without content and test it. Blog testBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testWithoutContentForwardBlog", BlogInfoAccessInfo.All, null); Blog beForwardBlog = await blogHandler.GetByIdAsync(testBlog.ID); Assert.IsNotNull(beForwardBlog); Assert.AreEqual(beForwardBlog.Content, "testWithoutContentForwardBlog"); //create forward blog Blog testForwardBlog_NoContent = await blogHandler.CreateBlogAsync(testPerson.ID, "testForwardBlogNoContent", BlogInfoAccessInfo.All, null, null, beForwardBlog.ID); Blog testForwardBlog = await blogHandler.GetByIdAsync(testForwardBlog_NoContent.ID); Assert.IsNotNull(testForwardBlog); Assert.IsFalse(testForwardBlog.IsDeleted); Assert.AreEqual(testForwardBlog.Content, "testForwardBlogNoContent"); Assert.AreEqual(testForwardBlog.NewBlogXBlogs.Count, 1); Assert.IsNotNull(testForwardBlog.NewBlogXBlogs.First().NewBlog); Assert.IsNotNull(testForwardBlog.NewBlogXBlogs.First().BaseBlog); Assert.AreEqual(testForwardBlog.NewBlogXBlogs.First().NewBlog.Content, "testForwardBlogNoContent"); Assert.AreEqual(testForwardBlog.NewBlogXBlogs.First().BaseBlog.Content, "testWithoutContentForwardBlog"); //2. set error forward blog id and test it. bool isChecked = false; try { Blog testErrorForwardBlogID = await blogHandler.CreateBlogAsync(testPerson.ID, "testForwardBlogNoContent", BlogInfoAccessInfo.All, null, null, 99999); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "该Blog不存在或者已经被删除"); } Assert.IsTrue(isChecked); //3. include content and test it. List<long> contentIds = new List<long>(); for (int i = 0; i < 6; i++) { Content photo = new Content() { ContentPath = "testPhotoContentPath", ContentBinary = new byte[] { 12, 3, 4, 5, 7 }, Type = ContentType.Photo, MimeType = "jpg" }; contentHandler.Add(photo); contentHandler.SaveChanges(); contentIds.Add(photo.ID); } Blog testIncludeContentBeForwardBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testIncludeContentBeForwardBlog", BlogInfoAccessInfo.All, attachContentIds: contentIds); Blog testBeForwardBlog = await blogHandler.GetByIdAsync(testIncludeContentBeForwardBlog.ID); Assert.IsNotNull(testBeForwardBlog); Assert.IsFalse(testBeForwardBlog.IsDeleted); Assert.AreEqual(testBeForwardBlog.Content, "testIncludeContentBeForwardBlog"); Assert.AreEqual(testBeForwardBlog.BlogXContents.Count, 6); Assert.IsTrue(testBeForwardBlog.BlogXContents.Any(x => x.BlogID == testBeForwardBlog.ID && x.Content.ContentPath == "testPhotoContentPath" && x.Content.Type == ContentType.Photo)); //forward the blog. contentIds = new List<long>(); Content video = new Content() { ContentPath = "testVideoContentPath", ContentBinary = new byte[] { 1, 3, 5, 6, 99 }, Type = ContentType.Video, MimeType = "jpg" }; contentHandler.Add(video); contentHandler.SaveChanges(); contentIds.Add(video.ID); Blog testIncludeContentForwardBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testIncludeContentForwardBlog", BlogInfoAccessInfo.All, attachContentIds: contentIds, forwardBlogId: testBeForwardBlog.ID); Blog testForwardBlogIncludeContent = await blogHandler.GetByIdAsync(testIncludeContentForwardBlog.ID); Assert.IsNotNull(testForwardBlogIncludeContent); Assert.IsFalse(testForwardBlogIncludeContent.IsDeleted); Assert.AreEqual(testForwardBlogIncludeContent.Content, "testIncludeContentForwardBlog"); Assert.AreEqual(testForwardBlogIncludeContent.BlogXContents.Count, 1); Assert.IsTrue(testForwardBlogIncludeContent.BlogXContents.Any(x => x.BlogID == testForwardBlogIncludeContent.ID && x.Content.ContentPath == "testVideoContentPath" && x.Content.Type == ContentType.Video)); Assert.AreEqual(testForwardBlogIncludeContent.NewBlogXBlogs.Count, 1); Assert.IsNotNull(testForwardBlogIncludeContent.NewBlogXBlogs.First().NewBlog); Assert.IsNotNull(testForwardBlogIncludeContent.NewBlogXBlogs.First().BaseBlog); Assert.AreEqual(testForwardBlogIncludeContent.NewBlogXBlogs.First().NewBlog.Content, "testIncludeContentForwardBlog"); Assert.AreEqual(testForwardBlogIncludeContent.NewBlogXBlogs.First().BaseBlog.Content, "testIncludeContentBeForwardBlog"); Assert.AreEqual(testForwardBlogIncludeContent.NewBlogXBlogs.First().NewBlog.BlogXContents.Count, 1); Assert.AreEqual(testForwardBlogIncludeContent.NewBlogXBlogs.First().BaseBlog.BlogXContents.Count, 6); foreach (var newBlogContent in testForwardBlogIncludeContent.NewBlogXBlogs.First().NewBlog.BlogXContents) { Assert.AreEqual(newBlogContent.Blog.ID, testForwardBlogIncludeContent.ID); Assert.AreEqual(newBlogContent.Content.ContentPath, "testVideoContentPath"); Assert.AreEqual(newBlogContent.Content.ContentBinary, new byte[] { 1, 3, 5, 6, 99 }); } foreach (var baseBlogContent in testForwardBlogIncludeContent.NewBlogXBlogs.First().BaseBlog.BlogXContents) { Assert.AreEqual(baseBlogContent.Blog.ID, testBeForwardBlog.ID); Assert.AreEqual(baseBlogContent.Content.ContentPath, "testPhotoContentPath"); Assert.AreEqual(baseBlogContent.Content.ContentBinary, new byte[] { 12, 3, 4, 5, 7 }); } } }
/// <summary> /// 关注 /// </summary> /// <param name="followerId">关注者ID</param> /// <param name="followingId">被关注者ID</param> /// <returns></returns> public async Task<bool> FollowAsync(long followerId, long followingId) { GroupHandler groupHandler = new GroupHandler(_dbContext); PersonXPersonHandler pxpHandler = new PersonXPersonHandler(_dbContext); //1. 检查被关注人黑名单是否存在关注人,若已拉黑,则无法关注。 Group blackGroup = await groupHandler.Include(x => x.GroupMembers).SingleOrDefaultAsync(x => x.PersonID == followingId && x.Type == GroupType.BlackList); if(blackGroup != null && blackGroup.GroupMembers.Count > 0) { bool isInGroupMember = blackGroup.GroupMembers.Select(x => x.PersonID).Contains(followerId); if(isInGroupMember) { throw new DisplayableException("由于用户设置,你无法关注。"); } } //2. 检查关注人的关注名单是否已经关注了。 bool isFollow = await pxpHandler.Fetch(x => x.FollowerID == followerId && x.FollowingID == followingId).SingleOrDefaultAsync() != null; //3. 如果未关注则添加纪录。 if(!isFollow) { PersonXPerson pxp = new PersonXPerson() { FollowerID = followerId, FollowingID = followingId }; pxpHandler.Add(pxp); return await SaveChangesAsync() > 0; } return true; }
public async Task Test_01_AddCommentWithoutContent() { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { BlogHandler blogHandler = new BlogHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext); CommentHandler commentHandler = new CommentHandler(dbContext); Person sam = CreatePerson("TestSam", "TestSam", AllowablePersonForComment.All, true); Blog testBlog = await blogHandler.CreateBlogAsync(sam.ID, "TestCommentBlog", BlogInfoAccessInfo.MyselfOnly); Person shelly = CreatePerson("TestShelly", "TestShelly", AllowablePersonForComment.All, true); //1. normal test. Comment testComment = await commentHandler.AddCommentAsync(shelly.ID, testBlog.ID, "hello, i am a comment test"); Comment testNormalComment = await commentHandler.GetByIdAsync(testComment.ID); Assert.IsNotNull(testNormalComment); Assert.AreEqual(testNormalComment.PersonID, shelly.ID); Assert.AreEqual(testNormalComment.BlogID, testBlog.ID); Assert.AreEqual(testNormalComment.Content, "hello, i am a comment test"); //2. set error blog id and test it. bool isChecked = false; try { Comment testComment_1 = await commentHandler.AddCommentAsync(commentPerson.ID, 12313121, "hello, i am a comment test"); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "要评论的Blog不存在或者已经被删除"); } Assert.IsTrue(isChecked); //3. test black list comment. Group blackListGroup = new Group() { PersonID = sam.ID, Name = "TestBlackList", Type = GroupType.BlackList }; groupHandler.Add(blackListGroup); groupHandler.SaveChanges(); GroupMember GroupMemberByBlack = new GroupMember() { GroupID = blackListGroup.ID, PersonID = commentPerson.ID }; gmHandler.Add(GroupMemberByBlack); gmHandler.SaveChanges(); isChecked = false; try { Comment testComment_2 = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hello, i am a comment test 4 black list."); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "由于用户设置,你无法回复评论。"); } Assert.IsTrue(isChecked); //4. remove black list member. gmHandler.MarkAsDeleted(GroupMemberByBlack); gmHandler.SaveChanges(); } }
public async Task Test_04_FollowAsync() { using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { PersonHandler perHandler = new PersonHandler(dbContext); GroupHandler groupHandler = new GroupHandler(dbContext); GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext); //1. test normal. Person sam = CreatePerson("TestSam", "TestSam"); Person joan = CreatePerson("TestJoan", "TestJoan"); bool isSucceed = await perHandler.FollowAsync(sam.ID, joan.ID); sam = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == sam.ID); joan = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == joan.ID); Assert.IsTrue(isSucceed); Assert.AreEqual(sam.MyFollowingPersons.Count, 1); Assert.AreEqual(sam.MyFans.Count, 0); Assert.AreEqual(joan.MyFollowingPersons.Count, 0); Assert.AreEqual(joan.MyFans.Count, 1); //2. test one more time follow, and check it count was changed. isSucceed = await perHandler.FollowAsync(sam.ID, joan.ID); sam = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == sam.ID); joan = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == joan.ID); Assert.IsTrue(isSucceed); Assert.AreEqual(sam.MyFollowingPersons.Count, 1); Assert.AreEqual(sam.MyFollowingPersons.First().FollowingID, joan.ID); Assert.AreEqual(sam.MyFans.Count, 0); Assert.AreEqual(joan.MyFollowingPersons.Count, 0); Assert.AreEqual(joan.MyFans.Count, 1); Assert.AreEqual(joan.MyFans.First().FollowerID, sam.ID); //3. test black list. Person jay = CreatePerson("TestJay", "TestJay"); Person ken = CreatePerson("TestKen", "TestKen"); Group blackGroup = await groupHandler.CreateGroupAsync(ken.ID, "TestBGroup", GroupType.BlackList); GroupMember gm_sam = new GroupMember() { PersonID = jay.ID, GroupID = blackGroup.ID }; gmHandler.Add(gm_sam); gmHandler.SaveChanges(); bool isChecked = false; try { isSucceed = await perHandler.FollowAsync(jay.ID, ken.ID); } catch (Exception ex) { isChecked = true; Assert.AreEqual(ex.GetType(), typeof(DisplayableException)); Assert.AreEqual(ex.Message, "由于用户设置,你无法关注。"); } Assert.IsTrue(isChecked); jay = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == jay.ID); ken = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == ken.ID); Assert.AreEqual(jay.MyFollowingPersons.Count, 0); Assert.AreEqual(jay.MyFans.Count, 0); Assert.AreEqual(ken.MyFollowingPersons.Count, 0); Assert.AreEqual(ken.MyFans.Count, 0); } }
/// <summary> /// 添加一条评论 /// </summary> /// <param name="personId">评论者ID</param> /// <param name="blogId">评论的BlogID</param> /// <param name="content">评论内容</param> /// <param name="photoContentIds">评论的附件ID(仅限图片)</param> /// <param name="baseCommentId">被评论的CommentID</param> /// <returns></returns> public async Task<Comment> AddCommentAsync(long personId, long blogId, string content, List<long> photoContentIds = null, long? baseCommentId = null) { BlogHandler blogHandler = new BlogHandler(_dbContext); GroupHandler groupHandler = new GroupHandler(_dbContext); PersonHandler perHandler = new PersonHandler(_dbContext); //1. 检查要评论的Blog是否存在。 Blog beCommentBlog = await blogHandler.GetByIdAsync(blogId); //2. 如果为空或者被逻辑删除,则Exception。 if (beCommentBlog == null || beCommentBlog.IsDeleted) { throw new DisplayableException("要评论的Blog不存在或者已经被删除"); } //2.1 自己评论自己的Blog永远可以,所以只需要判断不同的PersonID。 if(beCommentBlog.PersonID != personId) { //3. 检查当前用户是否被该评论Blog的用户加入了黑名单,如果是则不能进行评论。 Group beCommentBlogPersonBlackList = await groupHandler.Include(x => x.GroupMembers).SingleOrDefaultAsync(x => x.PersonID == beCommentBlog.PersonID && x.Type == GroupType.BlackList); if (beCommentBlogPersonBlackList != null && beCommentBlogPersonBlackList.GroupMembers.Count > 0) { bool isBlocked = beCommentBlogPersonBlackList.GroupMembers.Select(x => x.PersonID).Contains(personId); if (isBlocked) { throw new DisplayableException("由于用户设置,你无法回复评论。"); } } //4. 检查评论Blog的用户消息设置,是否允许评论。 Person beCommentBlogPerson = await perHandler.GetByIdAsync(beCommentBlog.PersonID); if (beCommentBlogPerson != null) { //4.1 如果评论只允许关注的人评论,则判断Blog的用户的是否关注了当前用户。 if (beCommentBlogPerson.AllowablePersonForComment == AllowablePersonForComment.FollowerOnly) { //4.2 判断关注的人集合是否已经加载。 if (!_dbContext.Entry(beCommentBlogPerson).Collection(x => x.MyFollowingPersons).IsLoaded) { _dbContext.Entry(beCommentBlogPerson).Collection(x => x.MyFollowingPersons).Load(); } bool isFollow = beCommentBlogPerson.MyFollowingPersons.Select(x => x.FollowingID).Contains(personId); if (!isFollow) { throw new DisplayableException("由于用户设置,你无法回复评论。"); } } //4.3 如果评论只允许粉丝评论,则判断当前用户是否关注了该Blog用户。 else if (beCommentBlogPerson.AllowablePersonForComment == AllowablePersonForComment.FansOnly) { //4.4 判断粉丝集合是否已经加载。 if (!_dbContext.Entry(beCommentBlogPerson).Collection(x => x.MyFans).IsLoaded) { _dbContext.Entry(beCommentBlogPerson).Collection(x => x.MyFans).Load(); } bool isFans = beCommentBlogPerson.MyFans.Select(x => x.FollowerID).Contains(personId); if (!isFans) { throw new DisplayableException("由于用户设置,你无法回复评论。"); } } } //5. 检查评论Blog的用户消息设置,是否允许评论附带图片。 if(photoContentIds != null && photoContentIds.Count > 0) { if(!beCommentBlogPerson.AllowCommentAttachContent) { throw new DisplayableException("由于用户设置,你回复评论无法添加图片。"); } } } using(var dbTransaction = _dbContext.Database.BeginTransaction()) { try { //6. 建立Comment对象并保存。 Comment comment = new Comment() { PersonID = personId, BlogID = blogId, Content = content }; this.Add(comment); await SaveChangesAsync(); //7. 判断是否有图片,有则建立与Comment的关联。 if (photoContentIds != null && photoContentIds.Count > 0) { if(photoContentIds.Count > 6) { throw new DisplayableException("评论最多附件6张图片"); } ContentHandler contentHandler = new ContentHandler(_dbContext); //7.1 判断附件是否为图片。 List<Content> photoContents = await contentHandler.Fetch(x => photoContentIds.Contains(x.ID)).ToListAsync(); if(!photoContents.Any(x => x.Type == ContentType.Photo)) { throw new DisplayableException("评论只能附件图片"); } //7.2 建立Comment与Content的关联。 CommentXContentHandler cxcHandler = new CommentXContentHandler(_dbContext); foreach (var photoContentId in photoContentIds) { CommentXContent cxc = new CommentXContent() { CommentID = comment.ID, ContentID = photoContentId }; cxcHandler.Add(cxc); } await SaveChangesAsync(); } //8. 判断当前评论是否评论Blog里的其他评论,如果是则建立关联。 if(baseCommentId != null) { //8.1 判断被评论ID是否存在。 Comment baseComment = await GetByIdAsync(baseCommentId); if(baseComment == null) { throw new DisplayableException("此评论不存在或者已经被删除"); } //8.2 判断被评论的BlogID是否与当前评论的BlogID一致。 if(baseComment.BlogID != blogId) { throw new DisplayableException("此评论的BlogID与被评论的BlogID不一致"); } //8.3 建立关联。 CommentXCommentHandler cxcHandler = new CommentXCommentHandler(_dbContext); CommentXComment cxc = new CommentXComment() { BaseCommentID = (long)baseCommentId, NewCommentID = comment.ID }; cxcHandler.Add(cxc); await SaveChangesAsync(); } dbTransaction.Commit(); return comment; } catch (Exception) { dbTransaction.Rollback(); throw; } } }
/// <summary> /// 获取当前用户关注的人Blog /// </summary> /// <param name="personId">用户ID</param> /// <param name="groupId">组ID</param> /// <returns></returns> public async Task<List<Blog>> GetBlogsAsync(long personId, long? groupId = null, int pageIndex = 1, int pageSize = int.MaxValue) { PersonHandler perHandler = new PersonHandler(_dbContext); GroupHandler groupHandler = new GroupHandler(_dbContext); BlogAccessControlHandler acHandler = new BlogAccessControlHandler(_dbContext); //1. 获取当前用户Person实体,并加载正在关注列表。 Person per = await perHandler.Include(x => x.MyFollowingPersons).SingleOrDefaultAsync(x => x.ID == personId); if(per == null) { throw new DisplayableException("该用户不存在"); } //2. 获取组成员或者已关注的用户ID集合,若指定了GroupID,则获取组里成员ID集合,反则获取已关注的人ID集合。 List<long> perIds = new List<long>(); if(groupId.HasValue) { Group normalGroup = await groupHandler.Include(x => x.GroupMembers).SingleOrDefaultAsync(x => x.ID == groupId); if(normalGroup != null) { if(normalGroup.PersonID != personId) { throw new DisplayableException("该组不属于此用户"); } //2.1 获取指定Group的用户ID集合。 if(normalGroup.GroupMembers.Count > 0) { perIds = normalGroup.GroupMembers.Select(x => x.PersonID).ToList(); } } } else { //2.2 获取当前用户已关注的用户ID集合。 perIds = per.MyFollowingPersons.Select(x => x.FollowingID).ToList(); } //2.3 加上当前用户的ID,提供查询当前用户的Blog。 perIds.Add(personId); //3. 获取当前用户屏蔽名单Group。 Group shieldGroup = await groupHandler.Include(x => x.GroupMembers).SingleOrDefaultAsync(x => x.PersonID == personId && x.Type == GroupType.ShieldList); if (shieldGroup != null && shieldGroup.GroupMembers.Count > 0) { List<long> shieldListIds = shieldGroup.GroupMembers.Select(x => x.PersonID).ToList(); //3.1 过滤屏蔽名单上的用户,不加载屏蔽名单上用户的Blog。 perIds = perIds.Except(shieldListIds).ToList(); } //4. 获取过滤后的用户ID集合的Blog列表,以创建时间降序排序。 List<Blog> blogs = await Fetch(x => perIds.Contains(x.PersonID) && !x.IsDeleted).OrderByDescending(x => x.CreatedDate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync(); if(blogs.Count > 0) { //5.1 获取排除当前用户ID的BlogIDs集合。 List<long> blogListIds = blogs.Where(x => x.PersonID != personId).Select(x => x.ID).ToList(); //5.2 获取Blog列表的AccessControl(权限控制)列表,排除公开的权限控制。 List<BlogAccessControl> blogAccessControls = await acHandler.Fetch(x => blogListIds.Contains(x.BlogID) && x.AccessLevel != BlogInfoAccessInfo.All).ToListAsync(); foreach (var blogAccessControl in blogAccessControls) { Blog acBlog = blogs.Single(x => x.ID == blogAccessControl.BlogID); //5.3 如果权限控制为仅自己可见,且发表Blog的用户ID不等于当前用户ID,则过滤掉此Blog。 if (blogAccessControl.AccessLevel == BlogInfoAccessInfo.MyselfOnly) { blogs.Remove(acBlog); } //5.4 如果权限控制为群可见,判断此群的人员名单是否包含当前用户,没有则过滤此Blog。 else if (blogAccessControl.AccessLevel == BlogInfoAccessInfo.GroupOnly) { BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(_dbContext); BlogAccessControlXGroup acxGroup = await acxgHandler.Include(x => x.Group, x => x.Group.GroupMembers).SingleOrDefaultAsync(x => x.BlogAccessControlID == blogAccessControl.ID); if (acxGroup != null) { bool isInGroupMember = false; if (acxGroup.Group != null && acxGroup.Group.GroupMembers.Count > 0) { isInGroupMember = acxGroup.Group.GroupMembers.Select(x => x.PersonID).Any(x => x == personId); } if (!isInGroupMember) { blogs.Remove(acBlog); } } } //5.5 如果权限控制为朋友圈可见,判断当前用户是否与互相关注(Friends),如果不是则过滤此Blog。 else if (blogAccessControl.AccessLevel == BlogInfoAccessInfo.FriendOnly) { PersonXPersonHandler pxpHandler = new PersonXPersonHandler(_dbContext); bool isFriend = await pxpHandler.IsFriendAsync(personId, acBlog.PersonID); if (!isFriend) { blogs.Remove(acBlog); } } } } return blogs; }