From 4ea535d93753826ec900879560d876cec4d58c38 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 10 Feb 2021 02:03:06 +0800 Subject: ... --- BackEnd/Timeline/Entities/DatabaseContext.cs | 1 + .../Timeline/Entities/TimelinePostDataEntity.cs | 31 ++++++++++++++++++++++ BackEnd/Timeline/Entities/TimelinePostEntity.cs | 12 +++------ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 BackEnd/Timeline/Entities/TimelinePostDataEntity.cs (limited to 'BackEnd/Timeline/Entities') diff --git a/BackEnd/Timeline/Entities/DatabaseContext.cs b/BackEnd/Timeline/Entities/DatabaseContext.cs index 513cdc95..a0b59d1f 100644 --- a/BackEnd/Timeline/Entities/DatabaseContext.cs +++ b/BackEnd/Timeline/Entities/DatabaseContext.cs @@ -28,6 +28,7 @@ namespace Timeline.Entities public DbSet UserPermission { get; set; } = default!; public DbSet Timelines { get; set; } = default!; public DbSet TimelinePosts { get; set; } = default!; + public DbSet TimelinePostData { get; set; } = default!; public DbSet TimelineMembers { get; set; } = default!; public DbSet HighlightTimelines { get; set; } = default!; public DbSet BookmarkTimelines { get; set; } = default!; diff --git a/BackEnd/Timeline/Entities/TimelinePostDataEntity.cs b/BackEnd/Timeline/Entities/TimelinePostDataEntity.cs new file mode 100644 index 00000000..6ae8fd24 --- /dev/null +++ b/BackEnd/Timeline/Entities/TimelinePostDataEntity.cs @@ -0,0 +1,31 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace Timeline.Entities +{ + [Table("timeline_post_data")] + public class TimelinePostDataEntity + { + [Column("id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public long Id { get; set; } + + [Column("post")] + public long PostId { get; set; } + + [ForeignKey(nameof(PostId))] + public TimelinePostEntity Timeline { get; set; } = default!; + + [Column("index")] + public long Index { get; set; } + + [Column("kind")] + public string Kind { get; set; } = default!; + + [Column("data_tag")] + public string DataTag { get; set; } = default!; + + [Column("last_updated")] + public DateTime LastUpdated { get; set; } + } +} diff --git a/BackEnd/Timeline/Entities/TimelinePostEntity.cs b/BackEnd/Timeline/Entities/TimelinePostEntity.cs index 39b11a5b..c65ef929 100644 --- a/BackEnd/Timeline/Entities/TimelinePostEntity.cs +++ b/BackEnd/Timeline/Entities/TimelinePostEntity.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; @@ -25,14 +26,7 @@ namespace Timeline.Entities [ForeignKey(nameof(AuthorId))] public UserEntity? Author { get; set; } = default!; - [Column("content_type"), Required] - public string ContentType { get; set; } = default!; - - [Column("content")] - public string? Content { get; set; } - - [Column("extra_content")] - public string? ExtraContent { get; set; } + public bool Deleted { get; set; } [Column("color")] public string? Color { get; set; } @@ -42,5 +36,7 @@ namespace Timeline.Entities [Column("last_updated")] public DateTime LastUpdated { get; set; } + + public List DataList { get; set; } = default!; } } -- cgit v1.2.3 From d1317bd9fe08a933a13df88ba692343cde549123 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 11 Feb 2021 20:10:56 +0800 Subject: ... --- .../Timeline/Entities/TimelinePostDataEntity.cs | 3 +- BackEnd/Timeline/Services/DataManager.cs | 19 +- .../Services/TimelinePostCreateDataException.cs | 2 +- .../Services/TimelinePostDataNotExistException.cs | 10 + BackEnd/Timeline/Services/TimelinePostService.cs | 225 ++++++++++----------- 5 files changed, 130 insertions(+), 129 deletions(-) (limited to 'BackEnd/Timeline/Entities') diff --git a/BackEnd/Timeline/Entities/TimelinePostDataEntity.cs b/BackEnd/Timeline/Entities/TimelinePostDataEntity.cs index 6ae8fd24..9bc5d3e8 100644 --- a/BackEnd/Timeline/Entities/TimelinePostDataEntity.cs +++ b/BackEnd/Timeline/Entities/TimelinePostDataEntity.cs @@ -10,11 +10,12 @@ namespace Timeline.Entities [Column("id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long Id { get; set; } + [Required] [Column("post")] public long PostId { get; set; } [ForeignKey(nameof(PostId))] - public TimelinePostEntity Timeline { get; set; } = default!; + public TimelinePostEntity Post { get; set; } = default!; [Column("index")] public long Index { get; set; } diff --git a/BackEnd/Timeline/Services/DataManager.cs b/BackEnd/Timeline/Services/DataManager.cs index f24bb59b..b697630c 100644 --- a/BackEnd/Timeline/Services/DataManager.cs +++ b/BackEnd/Timeline/Services/DataManager.cs @@ -22,20 +22,22 @@ namespace Timeline.Services /// increases its ref count and returns a tag to the entry. /// /// The data. Can't be null. + /// If true save database change. Otherwise it does not save database change. /// The tag of the created entry. /// Thrown when is null. - public Task RetainEntry(byte[] data); + public Task RetainEntry(byte[] data, bool saveDatabaseChange = true); /// /// Decrease the the ref count of the entry. /// Remove it if ref count is zero. /// /// The tag of the entry. + /// If true save database change. Otherwise it does not save database change. /// Thrown when is null. /// /// It's no-op if entry with tag does not exist. /// - public Task FreeEntry(string tag); + public Task FreeEntry(string tag, bool saveDatabaseChange = true); /// /// Retrieve the entry with given tag. If not exist, returns null. @@ -57,7 +59,7 @@ namespace Timeline.Services _eTagGenerator = eTagGenerator; } - public async Task RetainEntry(byte[] data) + public async Task RetainEntry(byte[] data, bool saveDatabaseChange = true) { if (data == null) throw new ArgumentNullException(nameof(data)); @@ -80,11 +82,14 @@ namespace Timeline.Services { entity.Ref += 1; } - await _database.SaveChangesAsync(); + + if (saveDatabaseChange) + await _database.SaveChangesAsync(); + return tag; } - public async Task FreeEntry(string tag) + public async Task FreeEntry(string tag, bool saveDatabaseChange) { if (tag == null) throw new ArgumentNullException(nameof(tag)); @@ -101,7 +106,9 @@ namespace Timeline.Services { entity.Ref -= 1; } - await _database.SaveChangesAsync(); + + if (saveDatabaseChange) + await _database.SaveChangesAsync(); } } diff --git a/BackEnd/Timeline/Services/TimelinePostCreateDataException.cs b/BackEnd/Timeline/Services/TimelinePostCreateDataException.cs index fd1e6664..10a09de7 100644 --- a/BackEnd/Timeline/Services/TimelinePostCreateDataException.cs +++ b/BackEnd/Timeline/Services/TimelinePostCreateDataException.cs @@ -6,7 +6,7 @@ namespace Timeline.Services public TimelinePostCreateDataException() { } public TimelinePostCreateDataException(string message) : base(message) { } public TimelinePostCreateDataException(string message, System.Exception inner) : base(message, inner) { } - public TimelinePostCreateDataException(long index, string? message, System.Exception? inner = null) : base(message, inner) { Index = index; } + public TimelinePostCreateDataException(long index, string? message, System.Exception? inner = null) : base($"Data at index {index} is invalid.{(message is null ? "" : " " + message)}", inner) { Index = index; } protected TimelinePostCreateDataException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } diff --git a/BackEnd/Timeline/Services/TimelinePostDataNotExistException.cs b/BackEnd/Timeline/Services/TimelinePostDataNotExistException.cs index eac7a771..c70f5d9c 100644 --- a/BackEnd/Timeline/Services/TimelinePostDataNotExistException.cs +++ b/BackEnd/Timeline/Services/TimelinePostDataNotExistException.cs @@ -8,8 +8,18 @@ namespace Timeline.Services public TimelinePostDataNotExistException() : this(null, null) { } public TimelinePostDataNotExistException(string? message) : this(message, null) { } public TimelinePostDataNotExistException(string? message, Exception? inner) : base(message, inner) { } + public TimelinePostDataNotExistException(long timelineId, long postId, long dataIndex, string? message = null, Exception? inner = null) : base(message, inner) + { + TimelineId = timelineId; + PostId = postId; + DataIndex = dataIndex; + } protected TimelinePostDataNotExistException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public long TimelineId { get; set; } + public long PostId { get; set; } + public long DataIndex { get; set; } } } diff --git a/BackEnd/Timeline/Services/TimelinePostService.cs b/BackEnd/Timeline/Services/TimelinePostService.cs index cea702a1..8afd0770 100644 --- a/BackEnd/Timeline/Services/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/TimelinePostService.cs @@ -4,6 +4,7 @@ using SixLabors.ImageSharp; using System; using System.Collections.Generic; using System.Linq; +using System.Text; using System.Threading.Tasks; using Timeline.Entities; using Timeline.Helpers; @@ -62,11 +63,11 @@ namespace Timeline.Services /// /// The id of the timeline of the post. /// The id of the post. - /// If true, return the entity even if it is deleted. + /// If true, return the entity even if it is deleted. /// The post. /// Thrown when timeline does not exist. /// Thrown when post of does not exist or has been deleted. - Task GetPost(long timelineId, long postId, bool includeDelete = false); + Task GetPost(long timelineId, long postId, bool includeDeleted = false); /// /// Get the data digest of a post. @@ -201,7 +202,7 @@ namespace Timeline.Services if (!includeDeleted) { - query = query.Where(p => p.Content != null); + query = query.Where(p => !p.Deleted); } if (modifiedSince.HasValue) @@ -214,7 +215,7 @@ namespace Timeline.Services return await query.ToListAsync(); } - public async Task GetPost(long timelineId, long postId, bool includeDelete = false) + public async Task GetPost(long timelineId, long postId, bool includeDeleted = false) { await CheckTimelineExistence(timelineId); @@ -225,7 +226,7 @@ namespace Timeline.Services throw new TimelinePostNotExistException(timelineId, postId, false); } - if (!includeDelete && post.Content is null) + if (!includeDeleted && post.Deleted) { throw new TimelinePostNotExistException(timelineId, postId, true); } @@ -233,99 +234,46 @@ namespace Timeline.Services return post; } - public async Task GetPostDataETag(long timelineId, long postId) + public async Task GetPostDataDigest(long timelineId, long postId, long dataIndex) { await CheckTimelineExistence(timelineId); - var postEntity = await _database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).SingleOrDefaultAsync(); + var postEntity = await _database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).Select(p => new { p.Id, p.Deleted }).SingleOrDefaultAsync(); - if (postEntity == null) + if (postEntity is null) throw new TimelinePostNotExistException(timelineId, postId, false); - if (postEntity.Content == null) + if (postEntity.Deleted) throw new TimelinePostNotExistException(timelineId, postId, true); - if (postEntity.ContentType != TimelinePostDataKind.Image) - throw new TimelinePostNoDataException(ExceptionGetDataNonImagePost); + var dataEntity = await _database.TimelinePostData.Where(d => d.PostId == postEntity.Id && d.Index == dataIndex).SingleOrDefaultAsync(); - var tag = postEntity.Content; + if (dataEntity is null) + throw new TimelinePostDataNotExistException(timelineId, postId, dataIndex); - return tag; + return new CacheableDataDigest(dataEntity.DataTag, dataEntity.LastUpdated); } - public async Task GetPostData(long timelineId, long postId) + public async Task GetPostData(long timelineId, long postId, long dataIndex) { await CheckTimelineExistence(timelineId); - var postEntity = await _database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).SingleOrDefaultAsync(); + var postEntity = await _database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).Select(p => new { p.Id, p.Deleted }).SingleOrDefaultAsync(); - if (postEntity == null) + if (postEntity is null) throw new TimelinePostNotExistException(timelineId, postId, false); - if (postEntity.Content == null) + if (postEntity.Deleted) throw new TimelinePostNotExistException(timelineId, postId, true); - if (postEntity.ContentType != TimelinePostDataKind.Image) - throw new TimelinePostNoDataException(ExceptionGetDataNonImagePost); + var dataEntity = await _database.TimelinePostData.Where(d => d.PostId == postEntity.Id && d.Index == dataIndex).SingleOrDefaultAsync(); - var tag = postEntity.Content; + if (dataEntity is null) + throw new TimelinePostDataNotExistException(timelineId, postId, dataIndex); - byte[] data; + var data = await _dataManager.GetEntryAndCheck(dataEntity.DataTag, $"Timeline {timelineId}, post {postId}, data {dataIndex} requires this data."); - try - { - data = await _dataManager.GetEntry(tag); - } - catch (InvalidOperationException e) - { - throw new DatabaseCorruptedException(ExceptionGetDataDataEntryNotExist, e); - } - - if (postEntity.ExtraContent == null) - { - _logger.LogWarning(LogGetDataNoFormat); - var format = Image.DetectFormat(data); - postEntity.ExtraContent = format.DefaultMimeType; - await _database.SaveChangesAsync(); - } - - return new TimelinePostData - { - Data = data, - Type = postEntity.ExtraContent, - ETag = tag, - LastModified = postEntity.LastUpdated - }; - } - - private async Task SaveContent(TimelinePostEntity entity, TimelinePostCreateRequestData content) - { - switch (content) - { - case TimelinePostCreateRequestTextData c: - entity.ContentType = c.Kind; - entity.Content = c.Data; - break; - case TimelinePostCreateRequestImageData c: - var imageFormat = await _imageValidator.Validate(c.Data); - var imageFormatText = imageFormat.DefaultMimeType; - - var tag = await _dataManager.RetainEntry(c.Data); - - entity.ContentType = content.Kind; - entity.Content = tag; - entity.ExtraContent = imageFormatText; - break; - default: - throw new ArgumentException("Unknown content type.", nameof(content)); - }; - } - - private async Task CleanContent(TimelinePostEntity entity) - { - if (entity.Content is not null && entity.ContentType == TimelinePostDataKind.Image) - await _dataManager.FreeEntry(entity.Content); - entity.Content = null; + return new ByteData(data, dataEntity.Kind); } public async Task CreatePost(long timelineId, long authorId, TimelinePostCreateRequest request) @@ -333,15 +281,55 @@ namespace Timeline.Services if (request is null) throw new ArgumentNullException(nameof(request)); - - if (request.Content is null) - throw new ArgumentException("Content is null.", nameof(request)); - { if (!_colorValidator.Validate(request.Color, out var message)) throw new ArgumentException("Color is not valid.", nameof(request)); } + if (request.DataList is null) + throw new ArgumentException("Data list can't be null.", nameof(request)); + + if (request.DataList.Count == 0) + throw new ArgumentException("Data list can't be empty.", nameof(request)); + + if (request.DataList.Count > 100) + throw new ArgumentException("Data list count can't be bigger than 100.", nameof(request)); + + for (int index = 0; index < request.DataList.Count; index++) + { + var data = request.DataList[index]; + + switch (data.ContentType) + { + case MimeTypes.ImageGif: + case MimeTypes.ImageJpeg: + case MimeTypes.ImagePng: + case MimeTypes.ImageWebp: + try + { + await _imageValidator.Validate(data.Data, data.ContentType); + } + catch (ImageException e) + { + throw new TimelinePostCreateDataException(index, "Image validation failed.", e); + } + break; + case MimeTypes.TextPlain: + case MimeTypes.TextMarkdown: + try + { + new UTF8Encoding(false, true).GetString(data.Data); + } + catch (DecoderFallbackException e) + { + throw new TimelinePostCreateDataException(index, "Text is not a valid utf-8 sequence.", e); + } + break; + default: + throw new TimelinePostCreateDataException(index, "Unsupported content type."); + } + } + request.Time = request.Time?.MyToUtc(); await CheckTimelineExistence(timelineId); @@ -361,13 +349,29 @@ namespace Timeline.Services Color = request.Color }; - await SaveContent(postEntity, request.Content); - var timelineEntity = await _database.Timelines.Where(t => t.Id == timelineId).SingleAsync(); timelineEntity.CurrentPostLocalId += 1; postEntity.LocalId = timelineEntity.CurrentPostLocalId; - _database.TimelinePosts.Add(postEntity); + await _database.SaveChangesAsync(); + + List dataTags = new List(); + + for (int index = 0; index < request.DataList.Count; index++) + { + var data = request.DataList[index]; + + var tag = await _dataManager.RetainEntry(data.Data, false); + + _database.TimelinePostData.Add(new TimelinePostDataEntity + { + DataTag = tag, + Kind = data.ContentType, + Index = index, + PostId = postEntity.Id, + LastUpdated = currentTime, + }); + } await _database.SaveChangesAsync(); @@ -392,12 +396,10 @@ namespace Timeline.Services var entity = await _database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).SingleOrDefaultAsync(); - await using var transaction = await _database.Database.BeginTransactionAsync(); - if (entity is null) throw new TimelinePostNotExistException(timelineId, postId, false); - if (entity.Content is null) + if (entity.Deleted) throw new TimelinePostNotExistException(timelineId, postId, true); if (request.Time.HasValue) @@ -406,18 +408,10 @@ namespace Timeline.Services if (request.Color is not null) entity.Color = request.Color; - if (request.Content is not null) - { - await CleanContent(entity); - await SaveContent(entity, request.Content); - } - entity.LastUpdated = _clock.GetCurrentTime(); await _database.SaveChangesAsync(); - await transaction.CommitAsync(); - return entity; } @@ -430,15 +424,23 @@ namespace Timeline.Services if (entity == null) throw new TimelinePostNotExistException(timelineId, postId, false); - if (entity.Content == null) + if (entity.Deleted) throw new TimelinePostNotExistException(timelineId, postId, true); await using var transaction = await _database.Database.BeginTransactionAsync(); - await CleanContent(entity); - + entity.Deleted = true; entity.LastUpdated = _clock.GetCurrentTime(); + var dataEntities = await _database.TimelinePostData.Where(d => d.PostId == entity.Id).ToListAsync(); + + foreach (var dataEntity in dataEntities) + { + await _dataManager.FreeEntry(dataEntity.DataTag, false); + } + + _database.TimelinePostData.RemoveRange(dataEntities); + await _database.SaveChangesAsync(); await transaction.CommitAsync(); @@ -446,30 +448,11 @@ 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(); - - foreach (var post in posts) - { - if (post.Content != null) - { - if (post.ContentType == TimelinePostDataKind.Image) - { - dataTags.Add(post.Content); - } - post.Content = null; - } - post.LastUpdated = now; - } - - await _database.SaveChangesAsync(); + var postEntities = await _database.TimelinePosts.Where(p => p.AuthorId == userId).Select(p => new { p.TimelineId, p.LocalId }).ToListAsync(); - foreach (var dataTag in dataTags) + foreach (var postEntity in postEntities) { - await _dataManager.FreeEntry(dataTag); + await this.DeletePost(postEntity.TimelineId, postEntity.LocalId); } } @@ -479,9 +462,9 @@ namespace Timeline.Services var timelineEntity = await _database.Timelines.Where(t => t.Id == timelineId).Select(t => new { t.OwnerId }).SingleAsync(); - var postEntity = await _database.TimelinePosts.Where(p => p.Id == postId).Select(p => new { p.Content, p.AuthorId }).SingleOrDefaultAsync(); + var postEntity = await _database.TimelinePosts.Where(p => p.Id == postId).Select(p => new { p.Deleted, p.AuthorId }).SingleOrDefaultAsync(); - if (postEntity == null) + if (postEntity is null) { if (throwOnPostNotExist) throw new TimelinePostNotExistException(timelineId, postId, false); @@ -489,7 +472,7 @@ namespace Timeline.Services return true; } - if (postEntity.Content == null && throwOnPostNotExist) + if (postEntity.Deleted && throwOnPostNotExist) { throw new TimelinePostNotExistException(timelineId, postId, true); } -- cgit v1.2.3 From b5376f71157f68f06aa04bde79389f9ab291d84a Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 11 Feb 2021 22:21:12 +0800 Subject: ... --- .../IntegratedTests/TimelinePostTest.cs | 338 ++++----------------- .../IntegratedTests/UserAvatarTest.cs | 17 +- .../Timeline/Controllers/UserAvatarController.cs | 4 +- BackEnd/Timeline/Entities/TimelinePostEntity.cs | 2 + .../Timeline/Formatters/ByteDataInputFormatter.cs | 6 + BackEnd/Timeline/Models/Http/HttpTimelinePost.cs | 5 + .../Models/Http/HttpTimelinePostCreateRequest.cs | 2 + BackEnd/Timeline/Models/Mapper/TimelineMapper.cs | 2 + BackEnd/Timeline/Services/TimelinePostService.cs | 4 +- 9 files changed, 87 insertions(+), 293 deletions(-) (limited to 'BackEnd/Timeline/Entities') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs index 4caff416..bd79ae18 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs @@ -1,44 +1,43 @@ using FluentAssertions; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Formats.Png; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Text; using System.Threading.Tasks; -using Timeline.Entities; using Timeline.Models; using Timeline.Models.Http; -using Timeline.Tests.Helpers; using Xunit; +using Xunit.Abstractions; namespace Timeline.Tests.IntegratedTests { - public static class TimelineHelper + public class TimelinePostTest : BaseTimelineTest { - public static HttpTimelinePostContent TextPostContent(string text) + private static HttpTimelinePostCreateRequest CreateTextPostRequest(string text, DateTime? time = null, string? color = null) { - return new HttpTimelinePostContent("text", text, null, null); - } - - public static HttpTimelinePostCreateRequest TextPostCreateRequest(string text, DateTime? time = null) - { - return new HttpTimelinePostCreateRequest + return new HttpTimelinePostCreateRequest() { - Content = new HttpTimelinePostCreateRequestContent + Time = time, + Color = color, + DataList = new List() { - Type = "text", - Text = text - }, - Time = time + new HttpTimelinePostCreateRequestData() + { + ContentType = MimeTypes.TextPlain, + Data = Convert.ToBase64String(Encoding.UTF8.GetBytes(text)) + } + } }; } - } - public class TimelinePostTest : BaseTimelineTest - { + private readonly ITestOutputHelper _outputHelper; + + public TimelinePostTest(ITestOutputHelper outputHelper) + { + _outputHelper = outputHelper; + } + [Theory] [MemberData(nameof(TimelineNameGeneratorTestData))] public async Task GetPostsAndVisibility_Should_Work(TimelineNameGenerator generator) @@ -102,15 +101,13 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsUser(); - var postContentList = new List { "a", "b", "c", "d" }; var posts = new List(); - foreach (var content in postContentList) + for (int i = 0; i < 4; i++) { - var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", - new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Text = content, Type = TimelinePostDataKind.Text } }); + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a")); posts.Add(post); - await Task.Delay(1000); + await Task.Delay(TimeSpan.FromSeconds(1)); } await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}"); @@ -118,64 +115,57 @@ namespace Timeline.Tests.IntegratedTests { var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture) }"); body.Should().HaveCount(2) - .And.Subject.Select(p => p.Content!.Text).Should().Equal("b", "d"); + .And.Subject.Select(p => p.Id).Should().Equal(posts[1].Id, posts[3].Id); } } [Theory] [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task Post_ModifiedSince_And_IncludeDeleted(TimelineNameGenerator generator) + public async Task PostList_IncludeDeleted(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); - var postContentList = new List { "a", "b", "c", "d" }; var posts = new List(); - foreach (var (content, index) in postContentList.Select((v, i) => (v, i))) + for (int i = 0; i < 4; i++) { - var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", - new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Text = content, Type = TimelinePostDataKind.Text } }); - posts.Add(post); - await Task.Delay(1000); + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a")); + posts.Add(body); } - await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}"); - + foreach (var id in new long[] { posts[0].Id, posts[2].Id }) { + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{id}"); + } - posts = await client.TestGetAsync>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture)}&includeDeleted=true"); - posts.Should().HaveCount(3); - posts.Select(p => p.Deleted).Should().Equal(false, true, false); - posts.Select(p => p.Content == null).Should().Equal(false, true, false); + { + posts = await client.TestGetAsync>($"timelines/{generator(1)}/posts?includeDeleted=true"); + posts.Should().HaveCount(4); + posts.Select(p => p.Deleted).Should().Equal(true, false, true, false); } } [Theory] [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task PostList_IncludeDeleted(TimelineNameGenerator generator) + public async Task Post_ModifiedSince_And_IncludeDeleted(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); - var postContentList = new List { "a", "b", "c", "d" }; var posts = new List(); - foreach (var content in postContentList) + for (int i = 0; i < 4; i++) { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", - new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Text = content, Type = TimelinePostDataKind.Text } }); - posts.Add(body); + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a")); + posts.Add(post); + await Task.Delay(TimeSpan.FromSeconds(1)); } - foreach (var id in new long[] { posts[0].Id, posts[2].Id }) - { - await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{id}"); - } + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}"); { - posts = await client.TestGetAsync>($"timelines/{generator(1)}/posts?includeDeleted=true"); - posts.Should().HaveCount(4); - posts.Select(p => p.Deleted).Should().Equal(true, false, true, false); - posts.Select(p => p.Content == null).Should().Equal(true, false, true, false); + posts = await client.TestGetAsync>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture)}&includeDeleted=true"); + posts.Should().HaveCount(3); + posts.Select(p => p.Deleted).Should().Equal(false, true, false); } } @@ -190,25 +180,25 @@ namespace Timeline.Tests.IntegratedTests using (var client = await CreateDefaultClient()) { // no auth should get 401 - await client.TestPostAssertUnauthorizedAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + await client.TestPostAssertUnauthorizedAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa")); } using (var client = await CreateClientAsUser()) { // post self's - await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa")); // post other not as a member should get 403 - await client.TestPostAssertForbiddenAsync($"timelines/{generator(0)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + await client.TestPostAssertForbiddenAsync($"timelines/{generator(0)}/posts", CreateTextPostRequest("aaa")); } using (var client = await CreateClientAsAdministrator()) { // post as admin - await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa")); } using (var client = await CreateClientAs(2)) { // post as member - await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa")); } } @@ -219,7 +209,7 @@ namespace Timeline.Tests.IntegratedTests async Task CreatePost(int userNumber) { using var client = await CreateClientAs(userNumber); - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa")); return body.Id; } @@ -265,57 +255,6 @@ namespace Timeline.Tests.IntegratedTests } } - [Theory] - [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task TextPost_Should_Work(TimelineNameGenerator generator) - { - using var client = await CreateClientAsUser(); - - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().BeEmpty(); - } - - const string mockContent = "aaa"; - HttpTimelinePost createRes; - { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest(mockContent)); - body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent)); - body.Author.Should().BeEquivalentTo(await client.GetUserAsync("user1")); - body.Deleted.Should().BeFalse(); - createRes = body; - } - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().BeEquivalentTo(createRes); - } - const string mockContent2 = "bbb"; - var mockTime2 = DateTime.UtcNow.AddDays(-1); - HttpTimelinePost createRes2; - { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest(mockContent2, mockTime2)); - body.Should().NotBeNull(); - body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent2)); - body.Author.Should().BeEquivalentTo(await client.GetUserAsync("user1")); - body.Time.Should().BeCloseTo(mockTime2, 1000); - body.Deleted.Should().BeFalse(); - createRes2 = body; - } - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().BeEquivalentTo(createRes, createRes2); - } - { - await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{createRes.Id}"); - await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/{createRes.Id}"); - await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/30000"); - } - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().BeEquivalentTo(createRes2); - } - } - [Theory] [MemberData(nameof(TimelineNameGeneratorTestData))] public async Task GetPost_Should_Ordered(TimelineNameGenerator generator) @@ -324,7 +263,7 @@ namespace Timeline.Tests.IntegratedTests async Task CreatePost(DateTime time) { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa", time)); + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa", time)); return body.Id; } @@ -339,166 +278,19 @@ namespace Timeline.Tests.IntegratedTests } } - [Theory] - [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task CreatePost_InvalidModel(TimelineNameGenerator generator) - { - using var client = await CreateClientAsUser(); - var postUrl = $"timelines/{generator(1)}/posts"; - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = null! }); - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = null! } }); - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "hahaha" } }); - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "text", Text = null } }); - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "image", Data = null } }); - // image not base64 - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "image", Data = "!!!" } }); - // image base64 not image - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "image", Data = Convert.ToBase64String(new byte[] { 0x01, 0x02, 0x03 }) } }); - } - - [Theory] - [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task ImagePost_ShouldWork(TimelineNameGenerator generator) - { - var imageData = ImageHelper.CreatePngWithSize(100, 200); - - long postId; - string postImageUrl; - - void AssertPostContent(HttpTimelinePostContent content) - { - content.Type.Should().Be(TimelinePostDataKind.Image); - content.Url.Should().EndWith($"timelines/{generator(1)}/posts/{postId}/data"); - content.Text.Should().Be(null); - } - - using var client = await CreateClientAsUser(); - - { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", - new HttpTimelinePostCreateRequest - { - Content = new HttpTimelinePostCreateRequestContent - { - Type = TimelinePostDataKind.Image, - Data = Convert.ToBase64String(imageData) - } - }); - postId = body.Id; - postImageUrl = body.Content!.Url!; - AssertPostContent(body.Content); - } - - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().HaveCount(1); - var post = body[0]; - post.Id.Should().Be(postId); - AssertPostContent(post.Content!); - } - - { - var res = await client.GetAsync($"timelines/{generator(1)}/posts/{postId}/data"); - res.Content.Headers.ContentType!.MediaType.Should().Be("image/png"); - var data = await res.Content.ReadAsByteArrayAsync(); - var image = Image.Load(data, out var format); - image.Width.Should().Be(100); - image.Height.Should().Be(200); - format.Name.Should().Be(PngFormat.Instance.Name); - } - - await CacheTestHelper.TestCache(client, $"timelines/{generator(1)}/posts/{postId}/data"); - await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}"); - await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/{postId}"); - - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().BeEmpty(); - } - - { - using var scope = TestApp.Host.Services.CreateScope(); - var database = scope.ServiceProvider.GetRequiredService(); - var count = await database.Data.CountAsync(); - count.Should().Be(0); - } - } - - [Theory] - [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task ImagePost_400(TimelineNameGenerator generator) - { - using var client = await CreateClientAsUser(); - - await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/11234/data", errorCode: ErrorCodes.TimelineController.PostNotExist); - - long postId; - { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); - postId = body.Id; - } - - await client.TestGetAssertErrorAsync($"timelines/{generator(1)}/posts/{postId}/data", errorCode: ErrorCodes.TimelineController.PostNoData); - } - - [Theory] - [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task PostDataETag(TimelineNameGenerator generator) - { - using var client = await CreateClientAsUser(); - - long id; - string etag; - - { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest - { - Content = new HttpTimelinePostCreateRequestContent - { - Type = TimelinePostDataKind.Image, - Data = Convert.ToBase64String(ImageHelper.CreatePngWithSize(100, 50)) - } - }); - body.Content!.ETag.Should().NotBeNullOrEmpty(); - - id = body.Id; - etag = body.Content.ETag!; - } - - { - var res = await client.GetAsync($"timelines/{generator(1)}/posts/{id}/data"); - res.StatusCode.Should().Be(200); - res.Headers.ETag.Should().NotBeNull(); - res.Headers.ETag!.ToString().Should().Be(etag); - } - } - [Theory] [MemberData(nameof(TimelineNameGeneratorTestData))] public async Task Color(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); - HttpTimelinePostCreateRequestContent CreateRequestContent() => new() - { - Type = "text", - Text = "aaa" - }; - - await client.TestPostAssertInvalidModelAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest - { - Content = CreateRequestContent(), - Color = "#1" - }); + await client.TestPostAssertInvalidModelAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a", color: "aa")); long id; { - var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest - { - Content = CreateRequestContent(), - Color = "#aabbcc" - }); + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", + CreateTextPostRequest("a", color: "#aabbcc")); post.Color.Should().Be("#aabbcc"); id = post.Id; } @@ -515,18 +307,10 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsUser(); - HttpTimelinePostCreateRequestContent CreateRequestContent() => new() - { - Type = "text", - Text = "aaa" - }; await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/1"); - var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest - { - Content = CreateRequestContent(), - }); + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a")); var post2 = await client.TestGetAsync($"timelines/{generator(1)}/posts/{post.Id}"); post2.Should().BeEquivalentTo(post); @@ -542,14 +326,8 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsUser(); - var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest - { - Content = new() - { - Type = "text", - Text = "aaa" - } - }); + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", + CreateTextPostRequest("a")); var date = new DateTime(2000, 10, 1); diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs index 893a5d28..708120b1 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs @@ -11,8 +11,8 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; +using Timeline.Models; using Timeline.Models.Http; -using Timeline.Services; using Timeline.Tests.Helpers; using Xunit; @@ -23,11 +23,10 @@ namespace Timeline.Tests.IntegratedTests [Fact] public async Task Test() { - Avatar mockAvatar = new Avatar - { - Data = ImageHelper.CreatePngWithSize(100, 100), - Type = PngFormat.Instance.DefaultMimeType - }; + ByteData mockAvatar = new ByteData( + ImageHelper.CreatePngWithSize(100, 100), + PngFormat.Instance.DefaultMimeType + ); using (var client = await CreateClientAsUser()) { @@ -106,7 +105,7 @@ namespace Timeline.Tests.IntegratedTests } { - await client.TestPutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.Type); + await client.TestPutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.ContentType); await TestAvatar("user1", mockAvatar.Data); } @@ -137,7 +136,7 @@ namespace Timeline.Tests.IntegratedTests // Authorization check. using (var client = await CreateClientAsAdministrator()) { - await client.TestPutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.Type); + await client.TestPutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.ContentType); await client.TestDeleteAsync("users/user1/avatar"); await client.TestPutByteArrayAssertErrorAsync("users/usernotexist/avatar", new[] { (byte)0x00 }, "image/png", errorCode: ErrorCodes.UserCommon.NotExist); await client.TestDeleteAssertErrorAsync("users/usernotexist/avatar", errorCode: ErrorCodes.UserCommon.NotExist); @@ -175,4 +174,4 @@ namespace Timeline.Tests.IntegratedTests } } } -} \ No newline at end of file +} diff --git a/BackEnd/Timeline/Controllers/UserAvatarController.cs b/BackEnd/Timeline/Controllers/UserAvatarController.cs index 8ac2d21a..180d1f9b 100644 --- a/BackEnd/Timeline/Controllers/UserAvatarController.cs +++ b/BackEnd/Timeline/Controllers/UserAvatarController.cs @@ -102,12 +102,12 @@ namespace Timeline.Controllers try { - var etag = await _service.SetAvatar(id, body); + var digest = await _service.SetAvatar(id, body); _logger.LogInformation(Log.Format(LogPutSuccess, ("Username", username), ("Mime Type", Request.ContentType))); - Response.Headers.Append("ETag", new EntityTagHeaderValue($"\"{etag}\"").ToString()); + Response.Headers.Append("ETag", new EntityTagHeaderValue($"\"{digest.ETag}\"").ToString()); return Ok(); } diff --git a/BackEnd/Timeline/Entities/TimelinePostEntity.cs b/BackEnd/Timeline/Entities/TimelinePostEntity.cs index c65ef929..1f0270cb 100644 --- a/BackEnd/Timeline/Entities/TimelinePostEntity.cs +++ b/BackEnd/Timeline/Entities/TimelinePostEntity.cs @@ -37,6 +37,8 @@ namespace Timeline.Entities [Column("last_updated")] public DateTime LastUpdated { get; set; } +#pragma warning disable CA2227 public List DataList { get; set; } = default!; +#pragma warning restore CA2227 } } diff --git a/BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs b/BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs index 2451ead6..49f8221a 100644 --- a/BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs +++ b/BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs @@ -44,6 +44,12 @@ namespace Timeline.Formatters var logger = context.HttpContext.RequestServices.GetRequiredService>(); + if (request.ContentType is null) + { + logger.LogInformation("Failed to read body as bytes. Content-Type is not set."); + return await InputFormatterResult.FailureAsync(); + } + if (contentLength == null) { logger.LogInformation("Failed to read body as bytes. Content-Length is not set."); diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs index 165c92da..26e1a92d 100644 --- a/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs @@ -26,7 +26,12 @@ namespace Timeline.Models.Http /// Post id. /// public long Id { get; set; } + /// + /// The data list. + /// +#pragma warning disable CA2227 public List DataList { get; set; } = default!; +#pragma warning restore CA2227 /// /// True if post is deleted. /// diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs index 07d823ad..2a973c72 100644 --- a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs @@ -13,7 +13,9 @@ namespace Timeline.Models.Http [Required] [MinLength(1)] [MaxLength(100)] +#pragma warning disable CA2227 public List DataList { get; set; } = default!; +#pragma warning restore CA2227 /// /// Time of the post. If not set, current time will be used. diff --git a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs index 33ee9593..1f10c123 100644 --- a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs +++ b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs @@ -66,6 +66,8 @@ namespace Timeline.Models.Mapper public async Task MapToHttp(TimelinePostEntity entity, string timelineName, IUrlHelper urlHelper) { + _ = timelineName; + await _database.Entry(entity).Collection(p => p.DataList).LoadAsync(); await _database.Entry(entity).Reference(e => e.Author).LoadAsync(); diff --git a/BackEnd/Timeline/Services/TimelinePostService.cs b/BackEnd/Timeline/Services/TimelinePostService.cs index 8afd0770..62bc43cc 100644 --- a/BackEnd/Timeline/Services/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/TimelinePostService.cs @@ -1,6 +1,5 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; -using SixLabors.ImageSharp; using System; using System.Collections.Generic; using System.Linq; @@ -12,7 +11,6 @@ using Timeline.Helpers.Cache; using Timeline.Models; using Timeline.Models.Validation; using Timeline.Services.Exceptions; -using static Timeline.Resources.Services.TimelineService; namespace Timeline.Services { @@ -37,7 +35,9 @@ namespace Timeline.Services /// If not set, current time is used. public DateTime? Time { get; set; } +#pragma warning disable CA2227 public List DataList { get; set; } = new List(); +#pragma warning restore CA2227 } public class TimelinePostPatchRequest -- cgit v1.2.3 From 85ac5c4513bd464d29696bc99fcda45ac228d1a2 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 12 Feb 2021 18:31:14 +0800 Subject: feat: Revert timeline post entity change. --- BackEnd/Timeline/Entities/TimelinePostEntity.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'BackEnd/Timeline/Entities') diff --git a/BackEnd/Timeline/Entities/TimelinePostEntity.cs b/BackEnd/Timeline/Entities/TimelinePostEntity.cs index 1f0270cb..317d43fc 100644 --- a/BackEnd/Timeline/Entities/TimelinePostEntity.cs +++ b/BackEnd/Timeline/Entities/TimelinePostEntity.cs @@ -26,6 +26,16 @@ namespace Timeline.Entities [ForeignKey(nameof(AuthorId))] public UserEntity? Author { get; set; } = default!; + [Column("content_type")] + public string? ContentType { get; set; } + + [Column("content")] + public string? Content { get; set; } + + [Column("extra_content")] + public string? ExtraContent { get; set; } + + [Column("deleted")] public bool Deleted { get; set; } [Column("color")] -- cgit v1.2.3 From 1aec1eb3f0822e17793d43e040efdf127ea8e561 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 12 Feb 2021 22:10:56 +0800 Subject: feat: Add databse custom migration service. --- BackEnd/Timeline/Entities/DatabaseContext.cs | 2 + BackEnd/Timeline/Entities/MigrationEntity.cs | 15 +++++ BackEnd/Timeline/Entities/TimelinePostEntity.cs | 3 + BackEnd/Timeline/Program.cs | 19 +++--- .../Services/Migration/CustomMigrationManager.cs | 55 +++++++++++++++++ .../Services/Migration/ICustomMigration.cs | 11 ++++ .../MigationServiceCollectionExtensions.cs | 14 +++++ .../TimelinePostContentToDataMigration.cs | 69 ++++++++++++++++++++++ BackEnd/Timeline/Startup.cs | 3 + 9 files changed, 181 insertions(+), 10 deletions(-) create mode 100644 BackEnd/Timeline/Entities/MigrationEntity.cs create mode 100644 BackEnd/Timeline/Services/Migration/CustomMigrationManager.cs create mode 100644 BackEnd/Timeline/Services/Migration/ICustomMigration.cs create mode 100644 BackEnd/Timeline/Services/Migration/MigationServiceCollectionExtensions.cs create mode 100644 BackEnd/Timeline/Services/Migration/TimelinePostContentToDataMigration.cs (limited to 'BackEnd/Timeline/Entities') diff --git a/BackEnd/Timeline/Entities/DatabaseContext.cs b/BackEnd/Timeline/Entities/DatabaseContext.cs index a0b59d1f..8ccdabb5 100644 --- a/BackEnd/Timeline/Entities/DatabaseContext.cs +++ b/BackEnd/Timeline/Entities/DatabaseContext.cs @@ -35,5 +35,7 @@ namespace Timeline.Entities public DbSet JwtToken { get; set; } = default!; public DbSet Data { get; set; } = default!; + + public DbSet Migrations { get; set; } = default!; } } diff --git a/BackEnd/Timeline/Entities/MigrationEntity.cs b/BackEnd/Timeline/Entities/MigrationEntity.cs new file mode 100644 index 00000000..0472f24e --- /dev/null +++ b/BackEnd/Timeline/Entities/MigrationEntity.cs @@ -0,0 +1,15 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace Timeline.Entities +{ + [Table("migrations")] + public class MigrationEntity + { + [Column("id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public long Id { get; set; } + + [Column("name"), Required] + public string Name { get; set; } = default!; + } +} diff --git a/BackEnd/Timeline/Entities/TimelinePostEntity.cs b/BackEnd/Timeline/Entities/TimelinePostEntity.cs index 317d43fc..451f56cd 100644 --- a/BackEnd/Timeline/Entities/TimelinePostEntity.cs +++ b/BackEnd/Timeline/Entities/TimelinePostEntity.cs @@ -26,12 +26,15 @@ namespace Timeline.Entities [ForeignKey(nameof(AuthorId))] public UserEntity? Author { get; set; } = default!; + [Obsolete("Use post data instead.")] [Column("content_type")] public string? ContentType { get; set; } + [Obsolete("Use post data instead.")] [Column("content")] public string? Content { get; set; } + [Obsolete("Use post data instead.")] [Column("extra_content")] public string? ExtraContent { get; set; } diff --git a/BackEnd/Timeline/Program.cs b/BackEnd/Timeline/Program.cs index 75bf6154..0f75908f 100644 --- a/BackEnd/Timeline/Program.cs +++ b/BackEnd/Timeline/Program.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Hosting; using System.Resources; using Timeline.Entities; using Timeline.Services; +using Timeline.Services.Migration; [assembly: NeutralResourcesLanguage("en")] @@ -17,18 +18,16 @@ namespace Timeline { var host = CreateWebHostBuilder(args).Build(); - var env = host.Services.GetRequiredService(); + using (var scope = host.Services.CreateScope()) + { + var databaseBackupService = scope.ServiceProvider.GetRequiredService(); + databaseBackupService.BackupNow(); - var databaseBackupService = host.Services.GetRequiredService(); - databaseBackupService.BackupNow(); + var databaseContext = scope.ServiceProvider.GetRequiredService(); + databaseContext.Database.Migrate(); - if (env.IsProduction()) - { - using (var scope = host.Services.CreateScope()) - { - var databaseContext = scope.ServiceProvider.GetRequiredService(); - databaseContext.Database.Migrate(); - } + var customMigrationManager = scope.ServiceProvider.GetRequiredService(); + customMigrationManager.Migrate(); } host.Run(); diff --git a/BackEnd/Timeline/Services/Migration/CustomMigrationManager.cs b/BackEnd/Timeline/Services/Migration/CustomMigrationManager.cs new file mode 100644 index 00000000..ba86e10b --- /dev/null +++ b/BackEnd/Timeline/Services/Migration/CustomMigrationManager.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Timeline.Entities; + +namespace Timeline.Services.Migration +{ + public interface ICustomMigrationManager + { + Task Migrate(); + } + + public class CustomMigrationManager : ICustomMigrationManager + { + private IEnumerable _migrations; + private DatabaseContext _database; + + private ILogger _logger; + + public CustomMigrationManager(IEnumerable migrations, DatabaseContext database, ILogger logger) + { + _migrations = migrations; + _database = database; + _logger = logger; + } + + public async Task Migrate() + { + foreach (var migration in _migrations) + { + var name = migration.GetName(); + var did = await _database.Migrations.AnyAsync(m => m.Name == name); + + _logger.LogInformation("Found custom migration '{0}'. Did: {1}.", name, did); + + if (!did) + { + _logger.LogInformation("Begin custom migration '{0}'.", name); + + await using var transaction = await _database.Database.BeginTransactionAsync(); + + await migration.Execute(_database); + + _database.Migrations.Add(new MigrationEntity { Name = name }); + await _database.SaveChangesAsync(); + + await transaction.CommitAsync(); + + _logger.LogInformation("End custom migration '{0}'.", name); + } + } + } + } +} diff --git a/BackEnd/Timeline/Services/Migration/ICustomMigration.cs b/BackEnd/Timeline/Services/Migration/ICustomMigration.cs new file mode 100644 index 00000000..1f47df1e --- /dev/null +++ b/BackEnd/Timeline/Services/Migration/ICustomMigration.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks; +using Timeline.Entities; + +namespace Timeline.Services.Migration +{ + public interface ICustomMigration + { + string GetName(); + Task Execute(DatabaseContext database); + } +} diff --git a/BackEnd/Timeline/Services/Migration/MigationServiceCollectionExtensions.cs b/BackEnd/Timeline/Services/Migration/MigationServiceCollectionExtensions.cs new file mode 100644 index 00000000..0e6f6c0a --- /dev/null +++ b/BackEnd/Timeline/Services/Migration/MigationServiceCollectionExtensions.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace Timeline.Services.Migration +{ + public static class MigrationServiceCollectionExtensions + { + public static IServiceCollection AddCustomMigration(this IServiceCollection services) + { + services.AddScoped(); + services.AddScoped(); + return services; + } + } +} \ No newline at end of file diff --git a/BackEnd/Timeline/Services/Migration/TimelinePostContentToDataMigration.cs b/BackEnd/Timeline/Services/Migration/TimelinePostContentToDataMigration.cs new file mode 100644 index 00000000..de2e2183 --- /dev/null +++ b/BackEnd/Timeline/Services/Migration/TimelinePostContentToDataMigration.cs @@ -0,0 +1,69 @@ +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using SixLabors.ImageSharp; +using Timeline.Entities; +using Timeline.Models; + +namespace Timeline.Services.Migration +{ + public class TimelinePostContentToDataMigration : ICustomMigration + { + private readonly IDataManager _dataManager; + + public TimelinePostContentToDataMigration(IDataManager dataManager) + { + _dataManager = dataManager; + } + + public string GetName() => "TimelinePostContentToData"; + + public async Task Execute(DatabaseContext database) + { +#pragma warning disable CS0618 + var postEntities = await database.TimelinePosts.ToListAsync(); + + foreach (var postEntity in postEntities) + { + if (postEntity.Content is null) + { + postEntity.Deleted = true; + } + else + { + if (postEntity.ContentType == "text") + { + var tag = await _dataManager.RetainEntry(Encoding.UTF8.GetBytes(postEntity.Content), false); + database.TimelinePostData.Add(new TimelinePostDataEntity + { + DataTag = tag, + Kind = MimeTypes.TextPlain, + Index = 0, + PostId = postEntity.Id, + LastUpdated = postEntity.LastUpdated + }); + } + else + { + var data = await _dataManager.GetEntryAndCheck(postEntity.Content, "Old image content does not have corresponding data with the tag."); + var format = Image.DetectFormat(data); + database.TimelinePostData.Add(new TimelinePostDataEntity + { + DataTag = postEntity.Content, + Kind = format.DefaultMimeType, + Index = 0, + PostId = postEntity.Id, + LastUpdated = postEntity.LastUpdated + }); + } + } + postEntity.Content = null; + postEntity.ContentType = null; + postEntity.ExtraContent = null; + } + + await database.SaveChangesAsync(); +#pragma warning restore CS0618 + } + } +} diff --git a/BackEnd/Timeline/Startup.cs b/BackEnd/Timeline/Startup.cs index 5951dc80..26ba3bfc 100644 --- a/BackEnd/Timeline/Startup.cs +++ b/BackEnd/Timeline/Startup.cs @@ -21,6 +21,7 @@ using Timeline.Models.Converters; using Timeline.Models.Mapper; using Timeline.Routes; using Timeline.Services; +using Timeline.Services.Migration; using Timeline.Swagger; namespace Timeline @@ -86,7 +87,9 @@ namespace Timeline services.TryAddSingleton(); services.AddSingleton(); + services.AddSingleton(); + services.AddCustomMigration(); services.AddAutoMapper(GetType().Assembly); services.AddMappers(); -- cgit v1.2.3