From d45f776470a7cece565a46655f286fcb06cb3a87 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 13 Feb 2021 15:09:21 +0800 Subject: feat: Post info add editable field. --- .../IntegratedTests/TimelinePostTest.cs | 20 ++++++++++++ .../Timeline/Controllers/TimelinePostController.cs | 19 ++++++++--- BackEnd/Timeline/Models/Http/HttpTimelinePost.cs | 12 ++++++- BackEnd/Timeline/Models/Mapper/TimelineMapper.cs | 37 +++++++++++++++++++--- 4 files changed, 78 insertions(+), 10 deletions(-) (limited to 'BackEnd') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs index c5ff507f..b91de6c2 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs @@ -566,5 +566,25 @@ namespace Timeline.Tests.IntegratedTests body.Should().Equal(imageData); } } + + [Theory] + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task Post_Editable(TimelineNameGenerator generator) + { + HttpTimelinePost post; + + { + using var client = await CreateClientAsUser(); + post = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a")); + + post.Editable.Should().BeTrue(); + } + + { + using var client = await CreateClientAs(2); + var post2 = await client.TestGetAsync($"timelines/{generator(1)}/posts/{post.Id}"); + post2.Editable.Should().BeFalse(); + } + } } } diff --git a/BackEnd/Timeline/Controllers/TimelinePostController.cs b/BackEnd/Timeline/Controllers/TimelinePostController.cs index 6904e28d..7e3d6900 100644 --- a/BackEnd/Timeline/Controllers/TimelinePostController.cs +++ b/BackEnd/Timeline/Controllers/TimelinePostController.cs @@ -12,6 +12,7 @@ using Timeline.Models.Http; using Timeline.Models.Mapper; using Timeline.Models.Validation; using Timeline.Services; +using Timeline.Entities; namespace Timeline.Controllers { @@ -43,6 +44,16 @@ namespace Timeline.Controllers private bool UserHasAllTimelineManagementPermission => this.UserHasPermission(UserPermission.AllTimelineManagement); + private Task Map(TimelinePostEntity post, string timelineName) + { + return _timelineMapper.MapToHttp(post, timelineName, Url, this.GetOptionalUserId(), UserHasAllTimelineManagementPermission); + } + + private Task> Map(List post, string timelineName) + { + return _timelineMapper.MapToHttp(post, timelineName, Url, this.GetOptionalUserId(), UserHasAllTimelineManagementPermission); + } + /// /// Get posts of a timeline. /// @@ -65,7 +76,7 @@ namespace Timeline.Controllers var posts = await _postService.GetPosts(timelineId, modifiedSince, includeDeleted ?? false); - var result = await _timelineMapper.MapToHttp(posts, timeline, Url); + var result = await Map(posts, timeline); return result; } @@ -89,7 +100,7 @@ namespace Timeline.Controllers } var post = await _postService.GetPost(timelineId, postId); - var result = await _timelineMapper.MapToHttp(post, timeline, Url); + var result = await Map(post, timeline); return result; } @@ -190,7 +201,7 @@ namespace Timeline.Controllers try { var post = await _postService.CreatePost(timelineId, userId, createRequest); - var result = await _timelineMapper.MapToHttp(post, timeline, Url); + var result = await Map(post, timeline); return result; } catch (TimelinePostCreateDataException e) @@ -222,7 +233,7 @@ namespace Timeline.Controllers } var entity = await _postService.PatchPost(timelineId, post, new TimelinePostPatchRequest { Time = body.Time, Color = body.Color }); - var result = await _timelineMapper.MapToHttp(entity, timeline, Url); + var result = await Map(entity, timeline); return Ok(result); } diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs index 26e1a92d..5e069821 100644 --- a/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs @@ -11,7 +11,7 @@ namespace Timeline.Models.Http { public HttpTimelinePost() { } - public HttpTimelinePost(long id, List dataList, bool deleted, DateTime time, HttpUser? author, string? color, DateTime lastUpdated) + public HttpTimelinePost(long id, List dataList, bool deleted, DateTime time, HttpUser? author, string? color, DateTime lastUpdated, string timelineName, bool editable) { Id = id; DataList = dataList; @@ -20,6 +20,8 @@ namespace Timeline.Models.Http Author = author; Color = color; LastUpdated = lastUpdated; + TimelineName = timelineName; + Editable = editable; } /// @@ -52,5 +54,13 @@ namespace Timeline.Models.Http /// Last updated time. /// public DateTime LastUpdated { get; set; } = default!; + /// + /// Timeline name. + /// + public string TimelineName { get; set; } = default!; + /// + /// True if you can edit this post. + /// + public bool Editable { get; set; } } } diff --git a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs index 5c46fa81..191e2f70 100644 --- a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs +++ b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs @@ -16,13 +16,17 @@ namespace Timeline.Models.Mapper private readonly UserMapper _userMapper; private readonly IHighlightTimelineService _highlightTimelineService; private readonly IBookmarkTimelineService _bookmarkTimelineService; + private readonly ITimelineService _timelineService; + private readonly ITimelinePostService _timelinePostService; - public TimelineMapper(DatabaseContext database, UserMapper userMapper, IHighlightTimelineService highlightTimelineService, IBookmarkTimelineService bookmarkTimelineService) + public TimelineMapper(DatabaseContext database, UserMapper userMapper, IHighlightTimelineService highlightTimelineService, IBookmarkTimelineService bookmarkTimelineService, ITimelineService timelineService, ITimelinePostService timelinePostService) { _database = database; _userMapper = userMapper; _highlightTimelineService = highlightTimelineService; _bookmarkTimelineService = bookmarkTimelineService; + _timelineService = timelineService; + _timelinePostService = timelinePostService; } public async Task MapToHttp(TimelineEntity entity, IUrlHelper urlHelper, long? userId) @@ -64,7 +68,7 @@ namespace Timeline.Models.Mapper } - public async Task MapToHttp(TimelinePostEntity entity, string timelineName, IUrlHelper urlHelper) + public async Task MapToHttp(TimelinePostEntity entity, string timelineName, IUrlHelper urlHelper, long? userId, bool isAdministrator) { _ = timelineName; @@ -79,6 +83,22 @@ namespace Timeline.Models.Mapper author = await _userMapper.MapToHttp(entity.Author, urlHelper); } + bool editable; + + if (userId is null) + { + editable = false; + } + else if (isAdministrator) + { + editable = true; + } + else + { + editable = await _timelinePostService.HasPostModifyPermission(entity.TimelineId, entity.LocalId, userId.Value); + } + + return new HttpTimelinePost( id: entity.LocalId, dataList: dataDigestList, @@ -86,18 +106,25 @@ namespace Timeline.Models.Mapper author: author, color: entity.Color, deleted: entity.Deleted, - lastUpdated: entity.LastUpdated + lastUpdated: entity.LastUpdated, + timelineName: timelineName, + editable: editable ); } - public async Task> MapToHttp(List entities, string timelineName, IUrlHelper urlHelper) + public async Task> MapToHttp(List entities, string timelineName, IUrlHelper urlHelper, long? userId, bool isAdministrator) { var result = new List(); foreach (var entity in entities) { - result.Add(await MapToHttp(entity, timelineName, urlHelper)); + result.Add(await MapToHttp(entity, timelineName, urlHelper, userId, isAdministrator)); } return result; } + + internal Task MapToHttp(TimelinePostEntity post, string timeline, IUrlHelper url) + { + throw new System.NotImplementedException(); + } } } -- cgit v1.2.3