diff --git a/VoidCat/Program.cs b/VoidCat/Program.cs index 15cb3e3..0edc0a9 100644 --- a/VoidCat/Program.cs +++ b/VoidCat/Program.cs @@ -128,7 +128,6 @@ services.AddAuthorization((opt) => // void.cat services // services.AddTransient(); -services.AddVoidMigrations(); // file storage services.AddStorage(voidSettings); @@ -160,6 +159,9 @@ if (useRedis) services.AddTransient(); services.AddTransient(svc => svc.GetRequiredService()); services.AddTransient(svc => svc.GetRequiredService()); + + // redis specific migrations + services.AddTransient(); } else { diff --git a/VoidCat/Services/InMemory/InMemoryCache.cs b/VoidCat/Services/InMemory/InMemoryCache.cs index 6871f28..f92a9b3 100644 --- a/VoidCat/Services/InMemory/InMemoryCache.cs +++ b/VoidCat/Services/InMemory/InMemoryCache.cs @@ -16,7 +16,7 @@ public class InMemoryCache : ICache { return ValueTask.FromResult(_cache.Get(key)); } - + public ValueTask Set(string key, T value, TimeSpan? expire = null) { if (expire.HasValue) @@ -30,12 +30,12 @@ public class InMemoryCache : ICache return ValueTask.CompletedTask; } - + public ValueTask GetList(string key) { - return ValueTask.FromResult(_cache.Get(key)); + return ValueTask.FromResult(_cache.Get(key) ?? Array.Empty()); } - + public ValueTask AddToList(string key, string value) { var list = new HashSet(GetList(key).Result); @@ -55,6 +55,7 @@ public class InMemoryCache : ICache public ValueTask Delete(string key) { _cache.Remove(key); - return ValueTask.CompletedTask;; + return ValueTask.CompletedTask; + ; } -} +} \ No newline at end of file diff --git a/VoidCat/Services/Migrations/IMigration.cs b/VoidCat/Services/Migrations/IMigration.cs index 4368af2..a1cf6a7 100644 --- a/VoidCat/Services/Migrations/IMigration.cs +++ b/VoidCat/Services/Migrations/IMigration.cs @@ -3,12 +3,4 @@ public interface IMigration { ValueTask Migrate(); -} - -public static class Migrations -{ - public static void AddVoidMigrations(this IServiceCollection svc) - { - svc.AddTransient(); - } } \ No newline at end of file diff --git a/VoidCat/Services/Migrations/MetadataMigrator.cs b/VoidCat/Services/Migrations/MetadataMigrator.cs index 62b33e4..e9b9ae7 100644 --- a/VoidCat/Services/Migrations/MetadataMigrator.cs +++ b/VoidCat/Services/Migrations/MetadataMigrator.cs @@ -13,7 +13,7 @@ public abstract class MetadataMigrator : IMigration _settings = settings; _logger = logger; } - + public async ValueTask Migrate() { var newMeta = Path.Combine(_settings.DataDirectory, OldPath); @@ -21,7 +21,7 @@ public abstract class MetadataMigrator : IMigration { Directory.CreateDirectory(newMeta); } - + foreach (var fe in Directory.EnumerateFiles(_settings.DataDirectory)) { var filename = Path.GetFileNameWithoutExtension(fe); @@ -35,13 +35,13 @@ public abstract class MetadataMigrator : IMigration { var oldJson = await File.ReadAllTextAsync(fp); if (!ShouldMigrate(oldJson)) continue; - + var old = JsonConvert.DeserializeObject(oldJson); - if(old == null) continue; - + if (old == null) continue; + var newObj = MigrateModel(old); await File.WriteAllTextAsync(MapNewMeta(id), JsonConvert.SerializeObject(newObj)); - + // delete old metadata File.Delete(fp); } @@ -58,9 +58,10 @@ public abstract class MetadataMigrator : IMigration protected abstract bool ShouldMigrate(string json); protected abstract TNew MigrateModel(TOld old); - + private string MapOldMeta(Guid id) => Path.ChangeExtension(Path.Join(_settings.DataDirectory, OldPath, id.ToString()), ".json"); + private string MapNewMeta(Guid id) => Path.ChangeExtension(Path.Join(_settings.DataDirectory, NewPath, id.ToString()), ".json"); } \ No newline at end of file diff --git a/VoidCat/Services/Migrations/UserLookupKeyHashMigration.cs b/VoidCat/Services/Migrations/UserLookupKeyHashMigration.cs index 495242c..4eb5541 100644 --- a/VoidCat/Services/Migrations/UserLookupKeyHashMigration.cs +++ b/VoidCat/Services/Migrations/UserLookupKeyHashMigration.cs @@ -12,7 +12,7 @@ public class UserLookupKeyHashMigration : IMigration { _database = database; } - + public async ValueTask Migrate() { var users = await _database.SetMembersAsync("users"); @@ -41,4 +41,4 @@ public class UserLookupKeyHashMigration : IMigration public string Email { get; init; } } -} +} \ No newline at end of file diff --git a/VoidCat/Services/Users/UserStore.cs b/VoidCat/Services/Users/UserStore.cs index 6e0b010..ee71dd4 100644 --- a/VoidCat/Services/Users/UserStore.cs +++ b/VoidCat/Services/Users/UserStore.cs @@ -88,7 +88,7 @@ public class UserStore : IUserStore //retain flags var isEmailVerified = oldUser.Flags.HasFlag(VoidUserFlags.EmailVerified); - + // update only a few props oldUser.Avatar = newUser.Avatar; oldUser.Flags = newUser.Flags | (isEmailVerified ? VoidUserFlags.EmailVerified : 0);