From e02e1e120693d466679cae2b602085f57b60971f Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 11 Aug 2020 01:16:56 +0800 Subject: Make author column of post nullable. --- Timeline/Services/TimelineService.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'Timeline/Services/TimelineService.cs') diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index eafb0088..ad410d15 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -413,9 +413,7 @@ namespace Timeline.Services private async Task 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; -- cgit v1.2.3 From c3369742b6e68714d0a7df46a99a0798eb2d6940 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 11 Aug 2020 01:48:57 +0800 Subject: Handle post deletion on user deletion correctly. --- Timeline.Tests/IntegratedTests/TokenTest.cs | 4 +- Timeline/Controllers/UserController.cs | 6 ++- Timeline/Services/TimelineService.cs | 35 +++++++++++++++ Timeline/Services/UserDeleteService.cs | 69 +++++++++++++++++++++++++++++ Timeline/Services/UserService.cs | 44 ------------------ Timeline/Startup.cs | 1 + 6 files changed, 111 insertions(+), 48 deletions(-) create mode 100644 Timeline/Services/UserDeleteService.cs (limited to 'Timeline/Services/TimelineService.cs') diff --git a/Timeline.Tests/IntegratedTests/TokenTest.cs b/Timeline.Tests/IntegratedTests/TokenTest.cs index d1c31606..480d66cd 100644 --- a/Timeline.Tests/IntegratedTests/TokenTest.cs +++ b/Timeline.Tests/IntegratedTests/TokenTest.cs @@ -119,9 +119,9 @@ namespace Timeline.Tests.IntegratedTests using var client = await CreateDefaultClient(); var token = (await CreateUserTokenAsync(client, "user1", "user1pw")).Token; - using (var scope = TestApp.Host.Services.CreateScope()) // UserService is scoped. + using (var scope = TestApp.Host.Services.CreateScope()) // UserDeleteService is scoped. { - var userService = scope.ServiceProvider.GetRequiredService(); + var userService = scope.ServiceProvider.GetRequiredService(); await userService.DeleteUser("user1"); } diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index c8c1e610..3986bb5b 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -22,12 +22,14 @@ namespace Timeline.Controllers { private readonly ILogger _logger; private readonly IUserService _userService; + private readonly IUserDeleteService _userDeleteService; private readonly IMapper _mapper; - public UserController(ILogger logger, IUserService userService, IMapper mapper) + public UserController(ILogger logger, IUserService userService, IUserDeleteService userDeleteService, IMapper mapper) { _logger = logger; _userService = userService; + _userDeleteService = userDeleteService; _mapper = mapper; } @@ -102,7 +104,7 @@ namespace Timeline.Controllers [HttpDelete("users/{username}"), AdminAuthorize] public async Task> Delete([FromRoute][Username] string username) { - var delete = await _userService.DeleteUser(username); + var delete = await _userDeleteService.DeleteUser(username); if (delete) return Ok(CommonDeleteResponse.Delete()); else diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index ad410d15..283938fb 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -219,6 +219,12 @@ namespace Timeline.Services /// Task DeletePost(string timelineName, long postId); + /// + /// Delete all posts of the given user. Used when delete a user. + /// + /// The id of the user. + Task DeleteAllPostsOfUser(long userId); + /// /// Change member of timeline. /// @@ -776,6 +782,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(); + + 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) diff --git a/Timeline/Services/UserDeleteService.cs b/Timeline/Services/UserDeleteService.cs new file mode 100644 index 00000000..845de573 --- /dev/null +++ b/Timeline/Services/UserDeleteService.cs @@ -0,0 +1,69 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading.Tasks; +using Timeline.Entities; +using Timeline.Helpers; +using Timeline.Models.Validation; +using static Timeline.Resources.Services.UserService; + +namespace Timeline.Services +{ + public interface IUserDeleteService + { + /// + /// Delete a user of given username. + /// + /// Username of the user to delete. Can't be null. + /// True if user is deleted, false if user not exist. + /// Thrown if is null. + /// Thrown when is of bad format. + Task DeleteUser(string username); + } + + public class UserDeleteService : IUserDeleteService + { + private readonly ILogger _logger; + + private readonly DatabaseContext _databaseContext; + + private readonly ITimelineService _timelineService; + + private readonly UsernameValidator _usernameValidator = new UsernameValidator(); + + public UserDeleteService(ILogger logger, DatabaseContext databaseContext, ITimelineService timelineService) + { + _logger = logger; + _databaseContext = databaseContext; + _timelineService = timelineService; + } + + public async Task DeleteUser(string username) + { + if (username == null) + throw new ArgumentNullException(nameof(username)); + + if (!_usernameValidator.Validate(username, out var message)) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ExceptionUsernameBadFormat, message), nameof(username)); + } + + var user = await _databaseContext.Users.Where(u => u.Username == username).SingleOrDefaultAsync(); + if (user == null) + return false; + + await _timelineService.DeleteAllPostsOfUser(user.Id); + + _databaseContext.Users.Remove(user); + + await _databaseContext.SaveChangesAsync(); + _logger.LogInformation(Log.Format(LogDatabaseRemove, ("Id", user.Id), ("Username", user.Username))); + + return true; + } + + } +} diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index 4e56c86a..c186c170 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -126,22 +126,6 @@ namespace Timeline.Services /// Task ModifyUser(string username, User? info); - /// - /// Delete a user of given id. - /// - /// Id of the user to delete. - /// True if user is deleted, false if user not exist. - Task DeleteUser(long id); - - /// - /// Delete a user of given username. - /// - /// Username of the user to delete. Can't be null. - /// True if user is deleted, false if user not exist. - /// Thrown if is null. - /// Thrown when is of bad format. - Task DeleteUser(string username); - /// /// Try to change a user's password with old password. /// @@ -428,34 +412,6 @@ namespace Timeline.Services return CreateUserFromEntity(entity); } - public async Task DeleteUser(long id) - { - var user = await _databaseContext.Users.Where(u => u.Id == id).SingleOrDefaultAsync(); - if (user == null) - return false; - - _databaseContext.Users.Remove(user); - await _databaseContext.SaveChangesAsync(); - _logger.LogInformation(Log.Format(LogDatabaseRemove, ("Id", id), ("Username", user.Username))); - return true; - } - - public async Task DeleteUser(string username) - { - if (username == null) - throw new ArgumentNullException(nameof(username)); - CheckUsernameFormat(username, nameof(username)); - - var user = await _databaseContext.Users.Where(u => u.Username == username).SingleOrDefaultAsync(); - if (user == null) - return false; - - _databaseContext.Users.Remove(user); - await _databaseContext.SaveChangesAsync(); - _logger.LogInformation(Log.Format(LogDatabaseRemove, ("Id", user.Id), ("Username", username))); - return true; - } - public async Task ChangePassword(long id, string oldPassword, string newPassword) { if (oldPassword == null) diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 84f0e8ba..7a813ac7 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -71,6 +71,7 @@ namespace Timeline services.AddTransient(); services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); -- cgit v1.2.3 From 7df5e63e8b2eaa5b4f83ee72a32eeedd480cde47 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 11 Aug 2020 17:59:44 +0800 Subject: Post list modified since now consider username change. And make all datetime utc. --- Timeline.Tests/Helpers/TestClock.cs | 4 +- Timeline.Tests/IntegratedTests/TimelineTest.cs | 4 +- Timeline/Entities/DatabaseContext.cs | 2 + Timeline/Entities/UtcDateAnnotation.cs | 44 +++++++++++++++++++ Timeline/Helpers/DateTimeExtensions.cs | 14 ++++++ .../Models/Converters/JsonDateTimeConverter.cs | 7 +-- Timeline/Models/Converters/MyDateTimeConverter.cs | 51 ++++++++++++++++++++++ Timeline/Services/Clock.cs | 2 +- Timeline/Services/TimelineService.cs | 12 ++++- Timeline/Services/UserTokenManager.cs | 3 ++ Timeline/Startup.cs | 3 ++ 11 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 Timeline/Entities/UtcDateAnnotation.cs create mode 100644 Timeline/Helpers/DateTimeExtensions.cs create mode 100644 Timeline/Models/Converters/MyDateTimeConverter.cs (limited to 'Timeline/Services/TimelineService.cs') diff --git a/Timeline.Tests/Helpers/TestClock.cs b/Timeline.Tests/Helpers/TestClock.cs index 0cbf236d..ed2d65a6 100644 --- a/Timeline.Tests/Helpers/TestClock.cs +++ b/Timeline.Tests/Helpers/TestClock.cs @@ -12,7 +12,7 @@ namespace Timeline.Tests.Helpers public DateTime GetCurrentTime() { - return _currentTime ?? DateTime.Now; + return _currentTime ?? DateTime.UtcNow; } public void SetCurrentTime(DateTime? mockTime) @@ -22,7 +22,7 @@ namespace Timeline.Tests.Helpers public DateTime SetMockCurrentTime() { - var time = new DateTime(2000, 1, 1, 1, 1, 1); + var time = new DateTime(3000, 1, 1, 1, 1, 1, DateTimeKind.Utc); _currentTime = time; return time; } diff --git a/Timeline.Tests/IntegratedTests/TimelineTest.cs b/Timeline.Tests/IntegratedTests/TimelineTest.cs index 49672f29..16b3c7e4 100644 --- a/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -952,7 +952,7 @@ namespace Timeline.Tests.IntegratedTests .Which.Should().NotBeNull().And.BeEquivalentTo(createRes); } const string mockContent2 = "bbb"; - var mockTime2 = DateTime.Now.AddDays(-1); + var mockTime2 = DateTime.UtcNow.AddDays(-1); TimelinePostInfo createRes2; { var res = await client.PostAsJsonAsync(generator(1, "posts"), @@ -1009,7 +1009,7 @@ namespace Timeline.Tests.IntegratedTests .Which.Id; } - var now = DateTime.Now; + var now = DateTime.UtcNow; var id0 = await CreatePost(now.AddDays(1)); var id1 = await CreatePost(now.AddDays(-1)); var id2 = await CreatePost(now); diff --git a/Timeline/Entities/DatabaseContext.cs b/Timeline/Entities/DatabaseContext.cs index ba2566bd..ecadd703 100644 --- a/Timeline/Entities/DatabaseContext.cs +++ b/Timeline/Entities/DatabaseContext.cs @@ -19,6 +19,8 @@ namespace Timeline.Entities modelBuilder.Entity().Property(e => e.LastModified).HasDefaultValueSql("datetime('now', 'utc')"); modelBuilder.Entity().HasIndex(e => e.Tag).IsUnique(); modelBuilder.Entity().Property(e => e.UniqueId).HasDefaultValueSql("lower(hex(randomblob(16)))"); + + modelBuilder.ApplyUtcDateTimeConverter(); } public DbSet Users { get; set; } = default!; diff --git a/Timeline/Entities/UtcDateAnnotation.cs b/Timeline/Entities/UtcDateAnnotation.cs new file mode 100644 index 00000000..6600e701 --- /dev/null +++ b/Timeline/Entities/UtcDateAnnotation.cs @@ -0,0 +1,44 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using System; + +namespace Timeline.Entities +{ + // Copied from https://github.com/dotnet/efcore/issues/4711#issuecomment-589842988 + public static class UtcDateAnnotation + { + private const string IsUtcAnnotation = "IsUtc"; + private static readonly ValueConverter UtcConverter = + new ValueConverter(v => v, v => DateTime.SpecifyKind(v, DateTimeKind.Utc)); + + public static PropertyBuilder IsUtc(this PropertyBuilder builder, bool isUtc = true) => + builder.HasAnnotation(IsUtcAnnotation, isUtc); + + public static bool IsUtc(this IMutableProperty property) => + ((bool?)property.FindAnnotation(IsUtcAnnotation)?.Value) ?? true; + + /// + /// Make sure this is called after configuring all your entities. + /// + public static void ApplyUtcDateTimeConverter(this ModelBuilder builder) + { + foreach (var entityType in builder.Model.GetEntityTypes()) + { + foreach (var property in entityType.GetProperties()) + { + if (!property.IsUtc()) + { + continue; + } + + if (property.ClrType == typeof(DateTime)) + { + property.SetValueConverter(UtcConverter); + } + } + } + } + } +} diff --git a/Timeline/Helpers/DateTimeExtensions.cs b/Timeline/Helpers/DateTimeExtensions.cs new file mode 100644 index 00000000..374f3bc9 --- /dev/null +++ b/Timeline/Helpers/DateTimeExtensions.cs @@ -0,0 +1,14 @@ +using System; + +namespace Timeline.Helpers +{ + public static class DateTimeExtensions + { + public static DateTime MyToUtc(this DateTime dateTime) + { + if (dateTime.Kind == DateTimeKind.Utc) return dateTime; + if (dateTime.Kind == DateTimeKind.Local) return dateTime.ToUniversalTime(); + return DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); + } + } +} diff --git a/Timeline/Models/Converters/JsonDateTimeConverter.cs b/Timeline/Models/Converters/JsonDateTimeConverter.cs index ef129a01..865b6251 100644 --- a/Timeline/Models/Converters/JsonDateTimeConverter.cs +++ b/Timeline/Models/Converters/JsonDateTimeConverter.cs @@ -3,7 +3,8 @@ using System.Diagnostics; using System.Globalization; using System.Text.Json; using System.Text.Json.Serialization; - +using Timeline.Helpers; + namespace Timeline.Models.Converters { public class JsonDateTimeConverter : JsonConverter @@ -11,12 +12,12 @@ namespace Timeline.Models.Converters public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { Debug.Assert(typeToConvert == typeof(DateTime)); - return DateTime.Parse(reader.GetString(), CultureInfo.InvariantCulture); + return DateTime.Parse(reader.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); } public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { - writer.WriteStringValue(value.ToUniversalTime().ToString("s", CultureInfo.InvariantCulture) + "Z"); + writer.WriteStringValue(value.MyToUtc().ToString("s", CultureInfo.InvariantCulture) + "Z"); } } } diff --git a/Timeline/Models/Converters/MyDateTimeConverter.cs b/Timeline/Models/Converters/MyDateTimeConverter.cs new file mode 100644 index 00000000..f125cd5c --- /dev/null +++ b/Timeline/Models/Converters/MyDateTimeConverter.cs @@ -0,0 +1,51 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace Timeline.Models.Converters +{ + public class MyDateTimeConverter : TypeConverter + { + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) + { + return base.CanConvertTo(context, destinationType); + } + + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + { + if (value is string text) + { + text = text.Trim(); + if (text.Length == 0) + { + return DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); + } + + return DateTime.Parse(text, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); + } + + return base.ConvertFrom(context, culture, value); + } + + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + if (destinationType == typeof(string) && value is DateTime) + { + DateTime dt = (DateTime)value; + if (dt == DateTime.MinValue) + { + return string.Empty; + } + + return dt.ToString("s", CultureInfo.InvariantCulture) + "Z"; + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } +} diff --git a/Timeline/Services/Clock.cs b/Timeline/Services/Clock.cs index 040f9304..4395edcd 100644 --- a/Timeline/Services/Clock.cs +++ b/Timeline/Services/Clock.cs @@ -23,7 +23,7 @@ namespace Timeline.Services public DateTime GetCurrentTime() { - return DateTime.Now; + return DateTime.UtcNow; } } } diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index 283938fb..0070fe3e 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -565,11 +565,13 @@ namespace Timeline.Services public async Task> 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 query = _database.TimelinePosts.Where(p => p.TimelineId == timelineId); if (!includeDeleted) { @@ -578,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(); @@ -663,6 +667,8 @@ namespace Timeline.Services public async Task CreateTextPost(string timelineName, long authorId, string text, DateTime? time) { + time = time?.MyToUtc(); + if (timelineName == null) throw new ArgumentNullException(nameof(timelineName)); if (text == null) @@ -704,6 +710,8 @@ namespace Timeline.Services public async Task CreateImagePost(string timelineName, long authorId, byte[] data, DateTime? time) { + time = time?.MyToUtc(); + if (timelineName == null) throw new ArgumentNullException(nameof(timelineName)); if (data == null) diff --git a/Timeline/Services/UserTokenManager.cs b/Timeline/Services/UserTokenManager.cs index a016ff96..813dae67 100644 --- a/Timeline/Services/UserTokenManager.cs +++ b/Timeline/Services/UserTokenManager.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using System; using System.Threading.Tasks; +using Timeline.Helpers; using Timeline.Models; using Timeline.Services.Exceptions; @@ -57,6 +58,8 @@ namespace Timeline.Services public async Task CreateToken(string username, string password, DateTime? expireAt = null) { + expireAt = expireAt?.MyToUtc(); + if (username == null) throw new ArgumentNullException(nameof(username)); if (password == null) diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 7a813ac7..be2377b9 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using System; +using System.ComponentModel; using System.Text.Json.Serialization; using Timeline.Auth; using Timeline.Configs; @@ -40,6 +41,8 @@ namespace Timeline // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + TypeDescriptor.AddAttributes(typeof(DateTime), new TypeConverterAttribute(typeof(MyDateTimeConverter))); + services.AddControllers(setup => { setup.InputFormatters.Add(new StringInputFormatter()); -- cgit v1.2.3