From 57b95cf3c53c0248355ffa46214224cdba4bf79b Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 13 Feb 2021 15:46:02 +0800 Subject: feat: Add timeline manageable. --- BackEnd/Timeline/Models/Http/HttpTimeline.cs | 4 +++- BackEnd/Timeline/Models/Mapper/TimelineMapper.cs | 22 +++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'BackEnd/Timeline/Models') diff --git a/BackEnd/Timeline/Models/Http/HttpTimeline.cs b/BackEnd/Timeline/Models/Http/HttpTimeline.cs index 87ebf0bb..8a865f96 100644 --- a/BackEnd/Timeline/Models/Http/HttpTimeline.cs +++ b/BackEnd/Timeline/Models/Http/HttpTimeline.cs @@ -10,7 +10,7 @@ namespace Timeline.Models.Http { public HttpTimeline() { } - public HttpTimeline(string uniqueId, string title, string name, DateTime nameLastModifed, string description, HttpUser owner, TimelineVisibility visibility, List members, string? color, DateTime createTime, DateTime lastModified, bool isHighlight, bool isBookmark, HttpTimelineLinks links) + public HttpTimeline(string uniqueId, string title, string name, DateTime nameLastModifed, string description, HttpUser owner, TimelineVisibility visibility, List members, string? color, DateTime createTime, DateTime lastModified, bool isHighlight, bool isBookmark, bool manageable, HttpTimelineLinks links) { UniqueId = uniqueId; Title = title; @@ -25,6 +25,7 @@ namespace Timeline.Models.Http LastModified = lastModified; IsHighlight = isHighlight; IsBookmark = isBookmark; + Manageable = manageable; _links = links; } @@ -78,6 +79,7 @@ namespace Timeline.Models.Http public bool IsHighlight { get; set; } public bool IsBookmark { get; set; } + public bool Manageable { get; set; } #pragma warning disable CA1707 // Identifiers should not contain underscores /// diff --git a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs index 191e2f70..8dfd7b8d 100644 --- a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs +++ b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs @@ -29,13 +29,28 @@ namespace Timeline.Models.Mapper _timelinePostService = timelinePostService; } - public async Task MapToHttp(TimelineEntity entity, IUrlHelper urlHelper, long? userId) + public async Task MapToHttp(TimelineEntity entity, IUrlHelper urlHelper, long? userId, bool isAdministrator) { await _database.Entry(entity).Reference(e => e.Owner).LoadAsync(); await _database.Entry(entity).Collection(e => e.Members).Query().Include(m => m.User).LoadAsync(); var timelineName = entity.Name is null ? "@" + entity.Owner.Username : entity.Name; + bool manageable; + + if (userId is null) + { + manageable = false; + } + else if (isAdministrator) + { + manageable = true; + } + else + { + manageable = await _timelineService.HasManagePermission(entity.Id, userId.Value); + } + return new HttpTimeline( uniqueId: entity.UniqueId, title: string.IsNullOrEmpty(entity.Title) ? timelineName : entity.Title, @@ -50,6 +65,7 @@ namespace Timeline.Models.Mapper lastModified: entity.LastModified, isHighlight: await _highlightTimelineService.IsHighlightTimeline(entity.Id), isBookmark: userId is not null && await _bookmarkTimelineService.IsBookmark(userId.Value, entity.Id, false, false), + manageable: manageable, links: new HttpTimelineLinks( self: urlHelper.ActionLink(nameof(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { timeline = timelineName }), posts: urlHelper.ActionLink(nameof(TimelinePostController.List), nameof(TimelinePostController)[0..^nameof(Controller).Length], new { timeline = timelineName }) @@ -57,12 +73,12 @@ namespace Timeline.Models.Mapper ); } - public async Task> MapToHttp(List entities, IUrlHelper urlHelper, long? userId) + public async Task> MapToHttp(List entities, IUrlHelper urlHelper, long? userId, bool isAdministrator) { var result = new List(); foreach (var entity in entities) { - result.Add(await MapToHttp(entity, urlHelper, userId)); + result.Add(await MapToHttp(entity, urlHelper, userId, isAdministrator)); } return result; } -- cgit v1.2.3