mirror of
https://git.v0l.io/Kieran/void.cat.git
synced 2025-04-08 12:38:00 +02:00
Migrate email mapping keys
This commit is contained in:
parent
11b94c3ff9
commit
000d7bac92
@ -9,5 +9,6 @@ public static class Migrations
|
||||
{
|
||||
public static void AddVoidMigrations(this IServiceCollection svc)
|
||||
{
|
||||
svc.AddTransient<IMigration, UserLookupKeyHashMigration>();
|
||||
}
|
||||
}
|
44
VoidCat/Services/Migrations/UserLookupKeyHashMigration.cs
Normal file
44
VoidCat/Services/Migrations/UserLookupKeyHashMigration.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
using VoidCat.Model;
|
||||
|
||||
namespace VoidCat.Services.Migrations;
|
||||
|
||||
public class UserLookupKeyHashMigration : IMigration
|
||||
{
|
||||
private readonly IDatabase _database;
|
||||
|
||||
public UserLookupKeyHashMigration(IDatabase database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public async ValueTask Migrate()
|
||||
{
|
||||
var users = await _database.SetMembersAsync("users");
|
||||
foreach (var userId in users)
|
||||
{
|
||||
if (!Guid.TryParse(userId, out var gid)) continue;
|
||||
|
||||
var userJson = await _database.StringGetAsync($"user:{gid}");
|
||||
var user = JsonConvert.DeserializeObject<UserEmail>(userJson);
|
||||
if (user == default) continue;
|
||||
|
||||
if (await _database.KeyExistsAsync(MapOld(user.Email)))
|
||||
{
|
||||
await _database.KeyDeleteAsync(MapOld(user.Email));
|
||||
await _database.StringSetAsync(MapNew(user.Email), $"\"{userId}\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static RedisKey MapOld(string email) => $"user:email:{email}";
|
||||
private static RedisKey MapNew(string email) => $"user:email:{email.Hash("md5")}";
|
||||
|
||||
internal class UserEmail
|
||||
{
|
||||
public UserEmail(string email) => Email = email;
|
||||
|
||||
public string Email { get; init; }
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user