feat: add .net user importer
This commit is contained in:
parent
41e620ad03
commit
412d720abc
10 changed files with 318 additions and 6 deletions
174
migrators/NetImporter/ImportUser.cs
Normal file
174
migrators/NetImporter/ImportUser.cs
Normal file
|
@ -0,0 +1,174 @@
|
|||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Foxnouns.Backend.Database;
|
||||
using Foxnouns.Backend.Database.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NodaTime;
|
||||
using NodaTime.Extensions;
|
||||
using Serilog;
|
||||
|
||||
namespace NetImporter;
|
||||
|
||||
public static class Users
|
||||
{
|
||||
public static async Task ImportUsers(string filename)
|
||||
{
|
||||
await using var db = await NetImporter.GetContextAsync();
|
||||
await db.Database.ExecuteSqlRawAsync("SELECT 1");
|
||||
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
var users = NetImporter.ReadFromFile<ImportUser>(filename).Output.Select(ConvertUser).ToList();
|
||||
db.AddRange(users);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
stopwatch.Stop();
|
||||
Log.Information("Imported {Count} users in {Duration}", users.Count, stopwatch.ElapsedDuration());
|
||||
}
|
||||
|
||||
private static User ConvertUser(ImportUser oldUser)
|
||||
{
|
||||
var user = new User
|
||||
{
|
||||
Id = oldUser.Id,
|
||||
Username = oldUser.Username,
|
||||
DisplayName = oldUser.DisplayName,
|
||||
Bio = oldUser.Bio,
|
||||
MemberTitle = oldUser.MemberTitle,
|
||||
LastActive = oldUser.LastActive.ToInstant(),
|
||||
Avatar = oldUser.AvatarHash,
|
||||
Links = oldUser.Links ?? [],
|
||||
|
||||
Role = oldUser.ParseRole(),
|
||||
Deleted = oldUser.Deleted,
|
||||
DeletedAt = oldUser.DeletedAt?.ToInstant(),
|
||||
DeletedBy = null
|
||||
};
|
||||
|
||||
if (oldUser is { DiscordId: not null, DiscordUsername: not null })
|
||||
{
|
||||
user.AuthMethods.Add(new AuthMethod
|
||||
{
|
||||
Id = SnowflakeGenerator.Instance.GenerateSnowflake(),
|
||||
AuthType = AuthType.Discord,
|
||||
RemoteId = oldUser.DiscordId,
|
||||
RemoteUsername = oldUser.DiscordUsername
|
||||
});
|
||||
}
|
||||
|
||||
if (oldUser is { TumblrId: not null, TumblrUsername: not null })
|
||||
{
|
||||
user.AuthMethods.Add(new AuthMethod
|
||||
{
|
||||
Id = SnowflakeGenerator.Instance.GenerateSnowflake(),
|
||||
AuthType = AuthType.Tumblr,
|
||||
RemoteId = oldUser.TumblrId,
|
||||
RemoteUsername = oldUser.TumblrUsername
|
||||
});
|
||||
}
|
||||
|
||||
if (oldUser is { GoogleId: not null, GoogleUsername: not null })
|
||||
{
|
||||
user.AuthMethods.Add(new AuthMethod
|
||||
{
|
||||
Id = SnowflakeGenerator.Instance.GenerateSnowflake(),
|
||||
AuthType = AuthType.Google,
|
||||
RemoteId = oldUser.GoogleId,
|
||||
RemoteUsername = oldUser.GoogleUsername
|
||||
});
|
||||
}
|
||||
|
||||
// Convert all custom preference UUIDs to snowflakes
|
||||
var prefMapping = new Dictionary<string, Snowflake>();
|
||||
foreach (var (key, value) in oldUser.CustomPreferences)
|
||||
{
|
||||
var newKey = SnowflakeGenerator.Instance.GenerateSnowflake();
|
||||
prefMapping[key] = newKey;
|
||||
user.CustomPreferences[newKey] = value;
|
||||
}
|
||||
|
||||
foreach (var name in oldUser.Names ?? [])
|
||||
{
|
||||
user.Names.Add(new FieldEntry
|
||||
{
|
||||
Value = name.Value,
|
||||
Status = prefMapping.TryGetValue(name.Status, out var newStatus) ? newStatus.ToString() : name.Status,
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var pronoun in oldUser.Pronouns ?? [])
|
||||
{
|
||||
user.Pronouns.Add(new Pronoun
|
||||
{
|
||||
Value = pronoun.Value,
|
||||
DisplayText = pronoun.DisplayText,
|
||||
Status = prefMapping.TryGetValue(pronoun.Status, out var newStatus)
|
||||
? newStatus.ToString()
|
||||
: pronoun.Status,
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var field in oldUser.Fields ?? [])
|
||||
{
|
||||
var entries = field.Entries.Select(entry => new FieldEntry
|
||||
{
|
||||
Value = entry.Value,
|
||||
Status = prefMapping.TryGetValue(entry.Status, out var newStatus)
|
||||
? newStatus.ToString()
|
||||
: entry.Status,
|
||||
})
|
||||
.ToList();
|
||||
|
||||
user.Fields.Add(new Field
|
||||
{
|
||||
Name = field.Name,
|
||||
Entries = entries.ToArray()
|
||||
});
|
||||
}
|
||||
|
||||
Log.Debug("Converted user {UserId}", oldUser.Id);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")]
|
||||
private record ImportUser(
|
||||
Snowflake Id,
|
||||
string Sid,
|
||||
string Username,
|
||||
string? DisplayName,
|
||||
string? Bio,
|
||||
string? MemberTitle,
|
||||
OffsetDateTime LastActive,
|
||||
string? AvatarHash,
|
||||
string[]? Links,
|
||||
FieldEntry[]? Names,
|
||||
Pronoun[]? Pronouns,
|
||||
Field[]? Fields,
|
||||
string? DiscordId,
|
||||
string? DiscordUsername,
|
||||
string? FediverseId,
|
||||
string? FediverseUsername,
|
||||
long? FediverseAppId,
|
||||
string? TumblrId,
|
||||
string? TumblrUsername,
|
||||
string? GoogleId,
|
||||
string? GoogleUsername,
|
||||
bool MemberListHidden,
|
||||
string? Timezone,
|
||||
string Role,
|
||||
bool Deleted,
|
||||
OffsetDateTime? DeletedAt,
|
||||
string? DeleteReason,
|
||||
Dictionary<string, User.CustomPreference> CustomPreferences)
|
||||
{
|
||||
public UserRole ParseRole() => Role switch
|
||||
{
|
||||
"USER" => UserRole.User,
|
||||
"MODERATOR" => UserRole.Moderator,
|
||||
"ADMIN" => UserRole.Admin,
|
||||
_ => UserRole.User
|
||||
};
|
||||
}
|
||||
}
|
83
migrators/NetImporter/NetImporter.cs
Normal file
83
migrators/NetImporter/NetImporter.cs
Normal file
|
@ -0,0 +1,83 @@
|
|||
using Foxnouns.Backend;
|
||||
using Foxnouns.Backend.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using NodaTime;
|
||||
using NodaTime.Serialization.JsonNet;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
|
||||
namespace NetImporter;
|
||||
|
||||
internal static class NetImporter
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.Enrich.FromLogContext()
|
||||
.MinimumLevel.Debug()
|
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||||
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Information)
|
||||
.WriteTo.Console()
|
||||
.CreateLogger();
|
||||
|
||||
switch (args.Length)
|
||||
{
|
||||
case < 2:
|
||||
Console.WriteLine("Not enough arguments. Usage: <type> <file>");
|
||||
return;
|
||||
case > 2:
|
||||
Console.WriteLine("Too many arguments. Usage: <type> <file>");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (args[0].ToLowerInvariant())
|
||||
{
|
||||
case "users":
|
||||
await Users.ImportUsers(args[1]);
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("Invalid type. Valid types are: users");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
internal static async Task<DatabaseContext> GetContextAsync()
|
||||
{
|
||||
var connString = Environment.GetEnvironmentVariable("DATABASE");
|
||||
if (connString == null) throw new Exception("$DATABASE not set, must be an ADO.NET connection string");
|
||||
|
||||
var loggerFactory = new LoggerFactory().AddSerilog(Log.Logger);
|
||||
var config = new Config
|
||||
{
|
||||
Database = new Config.DatabaseConfig
|
||||
{
|
||||
Url = connString
|
||||
}
|
||||
};
|
||||
|
||||
var db = new DatabaseContext(config, loggerFactory);
|
||||
|
||||
if ((await db.Database.GetPendingMigrationsAsync()).Any())
|
||||
{
|
||||
Log.Fatal("Database needs to be migrated first");
|
||||
}
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() }
|
||||
}.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
|
||||
|
||||
internal static Input<T> ReadFromFile<T>(string path)
|
||||
{
|
||||
var data = File.ReadAllText(path);
|
||||
return JsonConvert.DeserializeObject<Input<T>>(data, Settings) ?? throw new Exception("Invalid input file");
|
||||
}
|
||||
}
|
||||
|
||||
internal record Input<T>(List<T> Output, List<string> Skipped);
|
25
migrators/NetImporter/NetImporter.csproj
Normal file
25
migrators/NetImporter/NetImporter.csproj
Normal file
|
@ -0,0 +1,25 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Foxnouns.Backend\Foxnouns.Backend.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NodaTime.Serialization.JsonNet" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="8.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Add table
Add a link
Reference in a new issue