diff --git a/PluralKit.Bot/Commands/ImportExport.cs b/PluralKit.Bot/Commands/ImportExport.cs index 3f3927e1..3981899f 100644 --- a/PluralKit.Bot/Commands/ImportExport.cs +++ b/PluralKit.Bot/Commands/ImportExport.cs @@ -100,7 +100,7 @@ public class ImportExport var json = await ctx.BusyIndicator(async () => { // Make the actual data file - var data = await _dataFiles.ExportSystem(ctx.System, ctx.Config.UiTz); + var data = await _dataFiles.ExportSystem(ctx.System); return JsonConvert.SerializeObject(data, Formatting.None); }); diff --git a/PluralKit.Core/Database/Repository/ModelRepository.Config.cs b/PluralKit.Core/Database/Repository/ModelRepository.Config.cs index 980d3214..05bb3541 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.Config.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.Config.cs @@ -4,13 +4,13 @@ namespace PluralKit.Core; public partial class ModelRepository { - public Task GetSystemConfig(SystemId system) - => _db.QueryFirst(new Query("config").Where("system", system)); + public Task GetSystemConfig(SystemId system, IPKConnection conn = null) + => _db.QueryFirst(conn, new Query("config").Where("system", system)); - public async Task UpdateSystemConfig(SystemId system, SystemConfigPatch patch) + public async Task UpdateSystemConfig(SystemId system, SystemConfigPatch patch, IPKConnection conn = null) { var query = patch.Apply(new Query("config").Where("system", system)); - var config = await _db.QueryFirst(query, "returning *"); + var config = await _db.QueryFirst(conn, query, "returning *"); _ = _dispatch.Dispatch(system, new UpdateDispatchData { diff --git a/PluralKit.Core/Database/Repository/ModelRepository.System.cs b/PluralKit.Core/Database/Repository/ModelRepository.System.cs index fa484db1..d15b6d04 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.System.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.System.cs @@ -80,7 +80,12 @@ public partial class ModelRepository var system = await _db.QueryFirst(conn, query, "returning *"); _logger.Information("Created {SystemId}", system.Id); - await _db.Execute(conn => conn.QueryAsync("insert into config (system) values (@system)", new { system = system.Id })); + var (q, pms) = ("insert into config (system) values (@system)", new { system = system.Id }); + + if (conn == null) + await _db.Execute(conn => conn.QueryAsync(q, pms)); + else + await conn.QueryAsync(q, pms); // no dispatch call here - system was just created, we don't have a webhook URL return system; diff --git a/PluralKit.Core/Services/DataFileService.cs b/PluralKit.Core/Services/DataFileService.cs index ae9588ae..9b5a6365 100644 --- a/PluralKit.Core/Services/DataFileService.cs +++ b/PluralKit.Core/Services/DataFileService.cs @@ -21,7 +21,7 @@ public class DataFileService _dispatch = dispatch; } - public async Task ExportSystem(PKSystem system, string timezone) + public async Task ExportSystem(PKSystem system) { await using var conn = await _db.Obtain(); @@ -30,7 +30,9 @@ public class DataFileService o.Merge(system.ToJson(LookupContext.ByOwner)); - o.Add("timezone", timezone); + var config = await _repo.GetSystemConfig(system.Id); + o.Add("config", config.ToJson()); + o.Add("accounts", new JArray((await _repo.GetSystemAccounts(system.Id)).ToList())); o.Add("members", new JArray((await _repo.GetSystemMembers(system.Id).ToListAsync()).Select(m => diff --git a/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs b/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs index 3bfdfc02..47b46a55 100644 --- a/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs +++ b/PluralKit.Core/Utils/BulkImporter/BulkImporter.cs @@ -61,7 +61,7 @@ public partial class BulkImporter: IAsyncDisposable importer._system = system; } - importer._cfg = await repo.GetSystemConfig(system.Id); + importer._cfg = await repo.GetSystemConfig(system.Id, conn); // Fetch all members in the system and log their names and hids var members = await conn.QueryAsync("select id, hid, name from members where system = @System", diff --git a/PluralKit.Core/Utils/BulkImporter/PluralKitImport.cs b/PluralKit.Core/Utils/BulkImporter/PluralKitImport.cs index bfac7e06..6428d93d 100644 --- a/PluralKit.Core/Utils/BulkImporter/PluralKitImport.cs +++ b/PluralKit.Core/Utils/BulkImporter/PluralKitImport.cs @@ -30,6 +30,20 @@ public partial class BulkImporter await _repo.UpdateSystem(_system.Id, patch, _conn); + var configPatch = new SystemConfigPatch(); + + if (importFile.ContainsKey("config")) + configPatch = SystemConfigPatch.FromJson(importFile.Value("config")); + + if (importFile.ContainsKey("timezone")) + configPatch.UiTz = importFile.Value("timezone"); + + configPatch.AssertIsValid(); + if (configPatch.Errors.Count > 0) + throw new ImportException($"Field config.{patch.Errors[0].Key} in export file is invalid."); + + await _repo.UpdateSystemConfig(_system.Id, configPatch, _conn); + var members = importFile.Value("members"); var groups = importFile.Value("groups"); var switches = importFile.Value("switches");