From a41e20a0a3f32431355b649ee4ed16d138128378 Mon Sep 17 00:00:00 2001 From: Ske Date: Thu, 11 Jul 2019 22:46:18 +0200 Subject: [PATCH] Fix importing with no existing system --- PluralKit.Bot/Commands/ImportExportCommands.cs | 2 +- PluralKit.Core/DataFiles.cs | 8 +++++++- PluralKit.Core/Stores.cs | 4 +++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/PluralKit.Bot/Commands/ImportExportCommands.cs b/PluralKit.Bot/Commands/ImportExportCommands.cs index 69b31d6d..28123248 100644 --- a/PluralKit.Bot/Commands/ImportExportCommands.cs +++ b/PluralKit.Bot/Commands/ImportExportCommands.cs @@ -85,7 +85,7 @@ namespace PluralKit.Bot.Commands // If passed system is null, it'll create a new one // (and that's okay!) - var result = await DataFiles.ImportSystem(data, Context.SenderSystem); + var result = await DataFiles.ImportSystem(data, Context.SenderSystem, Context.User.Id); if (Context.SenderSystem == null) { diff --git a/PluralKit.Core/DataFiles.cs b/PluralKit.Core/DataFiles.cs index 1b314c6b..0e37342a 100644 --- a/PluralKit.Core/DataFiles.cs +++ b/PluralKit.Core/DataFiles.cs @@ -64,8 +64,11 @@ namespace PluralKit.Bot Timestamp = Formats.TimestampExportFormat.Format(sw.Timestamp) }; - public async Task ImportSystem(DataFileSystem data, PKSystem system) + public async Task ImportSystem(DataFileSystem data, PKSystem system, ulong accountId) { + // TODO: make atomic, somehow - we'd need to obtain one IDbConnection and reuse it + // which probably means refactoring SystemStore.Save and friends etc + var result = new ImportResult {AddedNames = new List(), ModifiedNames = new List()}; // If we don't already have a system to save to, create one @@ -78,6 +81,9 @@ namespace PluralKit.Bot if (data.AvatarUrl != null) system.AvatarUrl = data.AvatarUrl; if (data.TimeZone != null) system.UiTz = data.TimeZone ?? "UTC"; await _systems.Save(system); + + // Make sure to link the sender account, too + await _systems.Link(system, accountId); // Apply members // TODO: parallelize? diff --git a/PluralKit.Core/Stores.cs b/PluralKit.Core/Stores.cs index 084f1b6b..6ed9f466 100644 --- a/PluralKit.Core/Stores.cs +++ b/PluralKit.Core/Stores.cs @@ -25,8 +25,10 @@ namespace PluralKit { } public async Task Link(PKSystem system, ulong accountId) { + // We have "on conflict do nothing" since linking an account when it's already linked to the same system is idempotent + // This is used in import/export, although the pk;link command checks for this case beforehand using (var conn = _conn.Obtain()) - await conn.ExecuteAsync("insert into accounts (uid, system) values (@Id, @SystemId)", new { Id = accountId, SystemId = system.Id }); + await conn.ExecuteAsync("insert into accounts (uid, system) values (@Id, @SystemId) on conflict do nothing", new { Id = accountId, SystemId = system.Id }); } public async Task Unlink(PKSystem system, ulong accountId) {