diff options
author | crupest <crupest@outlook.com> | 2020-08-11 18:22:43 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-11 18:22:43 +0800 |
commit | 2de24db92e892c6e3f18d9f5f05e8f3b7aba5c9e (patch) | |
tree | 56115decfb5e7e7e96c1d84161d7e7c920ce8cff /Timeline/Services/TimelineService.cs | |
parent | 06e79c99bbc810f16058b35f1c88c23148bf8e57 (diff) | |
parent | 12b5ecddb47e50c9f4597553a795c68b09acaddb (diff) | |
download | timeline-2de24db92e892c6e3f18d9f5f05e8f3b7aba5c9e.tar.gz timeline-2de24db92e892c6e3f18d9f5f05e8f3b7aba5c9e.tar.bz2 timeline-2de24db92e892c6e3f18d9f5f05e8f3b7aba5c9e.zip |
Merge pull request #146 from crupest/fix-141
Fix #141 .
Diffstat (limited to 'Timeline/Services/TimelineService.cs')
-rw-r--r-- | Timeline/Services/TimelineService.cs | 51 |
1 files changed, 46 insertions, 5 deletions
diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index eafb0088..0070fe3e 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -220,6 +220,12 @@ namespace Timeline.Services Task DeletePost(string timelineName, long postId);
/// <summary>
+ /// Delete all posts of the given user. Used when delete a user.
+ /// </summary>
+ /// <param name="userId">The id of the user.</param>
+ Task DeleteAllPostsOfUser(long userId);
+
+ /// <summary>
/// Change member of timeline.
/// </summary>
/// <param name="timelineName">The name of the timeline.</param>
@@ -413,9 +419,7 @@ namespace Timeline.Services private async Task<TimelinePost> MapTimelinePostFromEntity(TimelinePostEntity entity, string timelineName)
{
-
-
- var author = await _userService.GetUserById(entity.AuthorId);
+ User? author = entity.AuthorId.HasValue ? await _userService.GetUserById(entity.AuthorId.Value) : null;
ITimelinePostContent? content = null;
@@ -561,11 +565,13 @@ namespace Timeline.Services public async Task<List<TimelinePost>> GetPosts(string timelineName, DateTime? modifiedSince = null, bool includeDeleted = false)
{
+ modifiedSince = modifiedSince?.MyToUtc();
+
if (timelineName == null)
throw new ArgumentNullException(nameof(timelineName));
var timelineId = await FindTimelineId(timelineName);
- var query = _database.TimelinePosts.OrderBy(p => p.Time).Where(p => p.TimelineId == timelineId);
+ IQueryable<TimelinePostEntity> query = _database.TimelinePosts.Where(p => p.TimelineId == timelineId);
if (!includeDeleted)
{
@@ -574,9 +580,11 @@ namespace Timeline.Services if (modifiedSince.HasValue)
{
- query = query.Where(p => p.LastUpdated >= modifiedSince);
+ query = query.Include(p => p.Author).Where(p => p.LastUpdated >= modifiedSince || (p.Author != null && p.Author.UsernameChangeTime >= modifiedSince));
}
+ query = query.OrderBy(p => p.Time);
+
var postEntities = await query.ToListAsync();
var posts = new List<TimelinePost>();
@@ -659,6 +667,8 @@ namespace Timeline.Services public async Task<TimelinePost> CreateTextPost(string timelineName, long authorId, string text, DateTime? time)
{
+ time = time?.MyToUtc();
+
if (timelineName == null)
throw new ArgumentNullException(nameof(timelineName));
if (text == null)
@@ -700,6 +710,8 @@ namespace Timeline.Services public async Task<TimelinePost> CreateImagePost(string timelineName, long authorId, byte[] data, DateTime? time)
{
+ time = time?.MyToUtc();
+
if (timelineName == null)
throw new ArgumentNullException(nameof(timelineName));
if (data == null)
@@ -778,6 +790,35 @@ namespace Timeline.Services }
}
+ public async Task DeleteAllPostsOfUser(long userId)
+ {
+ var posts = await _database.TimelinePosts.Where(p => p.AuthorId == userId).ToListAsync();
+
+ var now = _clock.GetCurrentTime();
+
+ var dataTags = new List<string>();
+
+ foreach (var post in posts)
+ {
+ if (post.Content != null)
+ {
+ if (post.ContentType == TimelinePostContentTypes.Image)
+ {
+ dataTags.Add(post.Content);
+ }
+ post.Content = null;
+ }
+ post.LastUpdated = now;
+ }
+
+ await _database.SaveChangesAsync();
+
+ foreach (var dataTag in dataTags)
+ {
+ await _dataManager.FreeEntry(dataTag);
+ }
+ }
+
public async Task ChangeProperty(string timelineName, TimelineChangePropertyRequest newProperties)
{
if (timelineName == null)
|