mirror of
https://git.v0l.io/Kieran/void.cat.git
synced 2025-04-01 01:08:05 +02:00
Fix startup when not using redis
This commit is contained in:
parent
9112f77018
commit
ef5b00b16b
@ -128,7 +128,6 @@ services.AddAuthorization((opt) =>
|
||||
// void.cat services
|
||||
//
|
||||
services.AddTransient<RazorPartialToStringRenderer>();
|
||||
services.AddVoidMigrations();
|
||||
|
||||
// file storage
|
||||
services.AddStorage(voidSettings);
|
||||
@ -160,6 +159,9 @@ if (useRedis)
|
||||
services.AddTransient<RedisStatsController>();
|
||||
services.AddTransient<IStatsCollector>(svc => svc.GetRequiredService<RedisStatsController>());
|
||||
services.AddTransient<IStatsReporter>(svc => svc.GetRequiredService<RedisStatsController>());
|
||||
|
||||
// redis specific migrations
|
||||
services.AddTransient<IMigration, UserLookupKeyHashMigration>();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ public class InMemoryCache : ICache
|
||||
{
|
||||
return ValueTask.FromResult(_cache.Get<T?>(key));
|
||||
}
|
||||
|
||||
|
||||
public ValueTask Set<T>(string key, T value, TimeSpan? expire = null)
|
||||
{
|
||||
if (expire.HasValue)
|
||||
@ -30,12 +30,12 @@ public class InMemoryCache : ICache
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
public ValueTask<string[]> GetList(string key)
|
||||
{
|
||||
return ValueTask.FromResult(_cache.Get<string[]>(key));
|
||||
return ValueTask.FromResult(_cache.Get<string[]>(key) ?? Array.Empty<string>());
|
||||
}
|
||||
|
||||
|
||||
public ValueTask AddToList(string key, string value)
|
||||
{
|
||||
var list = new HashSet<string>(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;
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
@ -3,12 +3,4 @@
|
||||
public interface IMigration
|
||||
{
|
||||
ValueTask Migrate();
|
||||
}
|
||||
|
||||
public static class Migrations
|
||||
{
|
||||
public static void AddVoidMigrations(this IServiceCollection svc)
|
||||
{
|
||||
svc.AddTransient<IMigration, UserLookupKeyHashMigration>();
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@ public abstract class MetadataMigrator<TOld, TNew> : IMigration
|
||||
_settings = settings;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public async ValueTask Migrate()
|
||||
{
|
||||
var newMeta = Path.Combine(_settings.DataDirectory, OldPath);
|
||||
@ -21,7 +21,7 @@ public abstract class MetadataMigrator<TOld, TNew> : 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<TOld, TNew> : IMigration
|
||||
{
|
||||
var oldJson = await File.ReadAllTextAsync(fp);
|
||||
if (!ShouldMigrate(oldJson)) continue;
|
||||
|
||||
|
||||
var old = JsonConvert.DeserializeObject<TOld>(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<TOld, TNew> : 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");
|
||||
}
|
@ -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; }
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user