aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Models
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-02-13 21:24:33 +0800
committercrupest <crupest@outlook.com>2021-02-13 21:24:33 +0800
commit50dbdde46eed6f2ff4c9691eea4414c1712af8e5 (patch)
treeaa0ee1272aa3e27462289fce0b2d5466d5049f96 /BackEnd/Timeline/Models
parentc8bd19aacf9059f740df4f6fa9890127c20c1f6d (diff)
parent6a1495dc98f5ae5f89de58ed0ff0b500fc71ff5a (diff)
downloadtimeline-50dbdde46eed6f2ff4c9691eea4414c1712af8e5.tar.gz
timeline-50dbdde46eed6f2ff4c9691eea4414c1712af8e5.tar.bz2
timeline-50dbdde46eed6f2ff4c9691eea4414c1712af8e5.zip
Merge branch 'master' into frontend
Diffstat (limited to 'BackEnd/Timeline/Models')
-rw-r--r--BackEnd/Timeline/Models/Http/HttpTimeline.cs6
-rw-r--r--BackEnd/Timeline/Models/Http/HttpTimelinePost.cs12
-rw-r--r--BackEnd/Timeline/Models/Mapper/TimelineMapper.cs70
3 files changed, 78 insertions, 10 deletions
diff --git a/BackEnd/Timeline/Models/Http/HttpTimeline.cs b/BackEnd/Timeline/Models/Http/HttpTimeline.cs
index 87ebf0bb..e3e46bd5 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<HttpUser> 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<HttpUser> members, string? color, DateTime createTime, DateTime lastModified, bool isHighlight, bool isBookmark, bool manageable, bool postable, HttpTimelineLinks links)
{
UniqueId = uniqueId;
Title = title;
@@ -25,6 +25,8 @@ namespace Timeline.Models.Http
LastModified = lastModified;
IsHighlight = isHighlight;
IsBookmark = isBookmark;
+ Manageable = manageable;
+ Postable = postable;
_links = links;
}
@@ -78,6 +80,8 @@ namespace Timeline.Models.Http
public bool IsHighlight { get; set; }
public bool IsBookmark { get; set; }
+ public bool Manageable { get; set; }
+ public bool Postable { get; set; }
#pragma warning disable CA1707 // Identifiers should not contain underscores
/// <summary>
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..e4304311 100644
--- a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs
+++ b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs
@@ -16,22 +16,51 @@ 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)
+ public async Task<HttpTimeline> 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);
+ }
+
+ bool postable;
+ if (userId is null)
+ {
+ postable = false;
+ }
+ else
+ {
+ postable = await _timelineService.IsMemberOf(entity.Id, userId.Value);
+ }
+
return new HttpTimeline(
uniqueId: entity.UniqueId,
title: string.IsNullOrEmpty(entity.Title) ? timelineName : entity.Title,
@@ -46,6 +75,8 @@ 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,
+ postable: postable,
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 })
@@ -53,18 +84,18 @@ namespace Timeline.Models.Mapper
);
}
- public async Task<List<HttpTimeline>> MapToHttp(List<TimelineEntity> entities, IUrlHelper urlHelper, long? userId)
+ public async Task<List<HttpTimeline>> MapToHttp(List<TimelineEntity> entities, IUrlHelper urlHelper, long? userId, bool isAdministrator)
{
var result = new List<HttpTimeline>();
foreach (var entity in entities)
{
- result.Add(await MapToHttp(entity, urlHelper, userId));
+ result.Add(await MapToHttp(entity, urlHelper, userId, isAdministrator));
}
return result;
}
- 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 +110,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 +133,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();
+ }
}
}