aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-02-13 15:09:21 +0800
committercrupest <crupest@outlook.com>2021-02-13 15:09:21 +0800
commit01c3c5800f9b8a7b89d98b28ea748d496497c0b2 (patch)
treeb41f4a4eb95e0f1608be951d874f6911460e71e4
parentc3d0a5f88de0fbdf6bc584548832017087ab1248 (diff)
downloadtimeline-01c3c5800f9b8a7b89d98b28ea748d496497c0b2.tar.gz
timeline-01c3c5800f9b8a7b89d98b28ea748d496497c0b2.tar.bz2
timeline-01c3c5800f9b8a7b89d98b28ea748d496497c0b2.zip
feat: Post info add editable field.
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs20
-rw-r--r--BackEnd/Timeline/Controllers/TimelinePostController.cs19
-rw-r--r--BackEnd/Timeline/Models/Http/HttpTimelinePost.cs12
-rw-r--r--BackEnd/Timeline/Models/Mapper/TimelineMapper.cs37
4 files changed, 78 insertions, 10 deletions
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<HttpTimelinePost>($"timelines/{generator(1)}/posts", CreateTextPostRequest("a"));
+
+ post.Editable.Should().BeTrue();
+ }
+
+ {
+ using var client = await CreateClientAs(2);
+ var post2 = await client.TestGetAsync<HttpTimelinePost>($"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<HttpTimelinePost> Map(TimelinePostEntity post, string timelineName)
+ {
+ return _timelineMapper.MapToHttp(post, timelineName, Url, this.GetOptionalUserId(), UserHasAllTimelineManagementPermission);
+ }
+
+ private Task<List<HttpTimelinePost>> Map(List<TimelinePostEntity> post, string timelineName)
+ {
+ return _timelineMapper.MapToHttp(post, timelineName, Url, this.GetOptionalUserId(), UserHasAllTimelineManagementPermission);
+ }
+
/// <summary>
/// Get posts of a timeline.
/// </summary>
@@ -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<HttpTimelinePostDataDigest> dataList, bool deleted, DateTime time, HttpUser? author, string? color, DateTime lastUpdated)
+ public HttpTimelinePost(long id, List<HttpTimelinePostDataDigest> 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;
}
/// <summary>
@@ -52,5 +54,13 @@ namespace Timeline.Models.Http
/// Last updated time.
/// </summary>
public DateTime LastUpdated { get; set; } = default!;
+ /// <summary>
+ /// Timeline name.
+ /// </summary>
+ public string TimelineName { get; set; } = default!;
+ /// <summary>
+ /// True if you can edit this post.
+ /// </summary>
+ 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<HttpTimeline> MapToHttp(TimelineEntity entity, IUrlHelper urlHelper, long? userId)
@@ -64,7 +68,7 @@ namespace Timeline.Models.Mapper
}
- public async Task<HttpTimelinePost> MapToHttp(TimelinePostEntity entity, string timelineName, IUrlHelper urlHelper)
+ public async Task<HttpTimelinePost> 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<List<HttpTimelinePost>> MapToHttp(List<TimelinePostEntity> entities, string timelineName, IUrlHelper urlHelper)
+ public async Task<List<HttpTimelinePost>> MapToHttp(List<TimelinePostEntity> entities, string timelineName, IUrlHelper urlHelper, long? userId, bool isAdministrator)
{
var result = new List<HttpTimelinePost>();
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();
+ }
}
}