chore: add csharpier to husky, format backend with csharpier

This commit is contained in:
sam 2024-10-02 00:28:07 +02:00
parent 5fab66444f
commit 7f971e8549
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
73 changed files with 2098 additions and 1048 deletions

View file

@ -19,12 +19,19 @@ public static class Users
var stopwatch = new Stopwatch();
stopwatch.Start();
var users = NetImporter.ReadFromFile<ImportUser>(filename).Output.Select(ConvertUser).ToList();
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());
Log.Information(
"Imported {Count} users in {Duration}",
users.Count,
stopwatch.ElapsedDuration()
);
}
private static User ConvertUser(ImportUser oldUser)
@ -43,40 +50,46 @@ public static class Users
Role = oldUser.ParseRole(),
Deleted = oldUser.Deleted,
DeletedAt = oldUser.DeletedAt?.ToInstant(),
DeletedBy = null
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
});
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
});
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
});
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
@ -90,41 +103,44 @@ public static class Users
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,
});
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,
});
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)
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()
});
user.Fields.Add(new Field { Name = field.Name, Entries = entries.ToArray() });
}
Log.Debug("Converted user {UserId}", oldUser.Id);
@ -161,14 +177,16 @@ public static class Users
bool Deleted,
OffsetDateTime? DeletedAt,
string? DeleteReason,
Dictionary<string, User.CustomPreference> CustomPreferences)
Dictionary<string, User.CustomPreference> CustomPreferences
)
{
public UserRole ParseRole() => Role switch
{
"USER" => UserRole.User,
"MODERATOR" => UserRole.Moderator,
"ADMIN" => UserRole.Admin,
_ => UserRole.User
};
public UserRole ParseRole() =>
Role switch
{
"USER" => UserRole.User,
"MODERATOR" => UserRole.Moderator,
"ADMIN" => UserRole.Admin,
_ => UserRole.User,
};
}
}
}

View file

@ -19,7 +19,10 @@ internal static class NetImporter
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Information)
.MinimumLevel.Override(
"Microsoft.EntityFrameworkCore.Database.Command",
LogEventLevel.Information
)
.WriteTo.Console()
.CreateLogger();
@ -47,16 +50,11 @@ internal static class NetImporter
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");
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 config = new Config { Database = new Config.DatabaseConfig { Url = connString } };
var db = new DatabaseContext(config, loggerFactory);
@ -70,14 +68,18 @@ internal static class NetImporter
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() }
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");
return JsonConvert.DeserializeObject<Input<T>>(data, Settings)
?? throw new Exception("Invalid input file");
}
}
internal record Input<T>(List<T> Output, List<string> Skipped);
internal record Input<T>(List<T> Output, List<string> Skipped);