From 969065724d153f1ce7a1b4c045ea4777331a2327 Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 20 Sep 2020 23:32:57 +0200 Subject: [PATCH] Fix Tupperbox importing (again) --- PluralKit.Bot/Commands/ImportExport.cs | 87 +++++++++++++++----------- 1 file changed, 49 insertions(+), 38 deletions(-) diff --git a/PluralKit.Bot/Commands/ImportExport.cs b/PluralKit.Bot/Commands/ImportExport.cs index d0be47ab..0e08eb63 100644 --- a/PluralKit.Bot/Commands/ImportExport.cs +++ b/PluralKit.Bot/Commands/ImportExport.cs @@ -9,6 +9,8 @@ using Newtonsoft.Json; using DSharpPlus.Exceptions; using DSharpPlus.Entities; +using Newtonsoft.Json.Linq; + using PluralKit.Core; namespace PluralKit.Bot @@ -16,6 +18,12 @@ namespace PluralKit.Bot public class ImportExport { private readonly DataFileService _dataFiles; + private readonly JsonSerializerSettings _settings = new JsonSerializerSettings + { + // Otherwise it'll mess up/reformat the ISO strings for ???some??? reason >.> + DateParseHandling = DateParseHandling.None + }; + public ImportExport(DataFileService dataFiles) { _dataFiles = dataFiles; @@ -41,51 +49,22 @@ namespace PluralKit.Bot throw Errors.InvalidImportFile; } - if (!response.IsSuccessStatusCode) throw Errors.InvalidImportFile; - var json = await response.Content.ReadAsStringAsync(); - - var settings = new JsonSerializerSettings(); + if (!response.IsSuccessStatusCode) + throw Errors.InvalidImportFile; DataFileSystem data; - - // TODO: can we clean up this mess? try { - data = JsonConvert.DeserializeObject(json, settings); + var json = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync(), _settings); + data = await LoadSystem(ctx, json); } catch (JsonException) { - try - { - var tupperbox = JsonConvert.DeserializeObject(json, settings); - if (!tupperbox.Valid) throw Errors.InvalidImportFile; - - var res = tupperbox.ToPluralKit(); - if (res.HadGroups || res.HadIndividualTags) - { - var issueStr = - $"{Emojis.Warn} The following potential issues were detected converting your Tupperbox input file:"; - if (res.HadGroups) - issueStr += - "\n- PluralKit does not support member groups. Members will be imported without groups."; - if (res.HadIndividualTags) - issueStr += - "\n- PluralKit does not support per-member system tags. Since you had multiple members with distinct tags, those tags will be applied to the members' *display names*/nicknames instead."; - - var msg = $"{issueStr}\n\nDo you want to proceed with the import?"; - if (!await ctx.PromptYesNo(msg)) throw Errors.ImportCancelled; - } - - data = res.System; - } - catch (JsonException) - { - throw Errors.InvalidImportFile; - } + throw Errors.InvalidImportFile; } - - - if (!data.Valid) throw Errors.InvalidImportFile; + + if (!data.Valid) + throw Errors.InvalidImportFile; if (data.LinkedAccounts != null && !data.LinkedAccounts.Contains(ctx.Author.Id)) { @@ -111,7 +90,39 @@ namespace PluralKit.Bot } }); } - + + private async Task LoadSystem(Context ctx, JObject json) + { + if (json.ContainsKey("tuppers")) + return await ImportFromTupperbox(ctx, json); + + return json.ToObject(); + } + + private async Task ImportFromTupperbox(Context ctx, JObject json) + { + var tupperbox = json.ToObject(); + if (!tupperbox.Valid) + throw Errors.InvalidImportFile; + + var res = tupperbox.ToPluralKit(); + if (res.HadGroups || res.HadIndividualTags) + { + var issueStr = + $"{Emojis.Warn} The following potential issues were detected converting your Tupperbox input file:"; + if (res.HadGroups) + issueStr += "\n- PluralKit does not support member groups. Members will be imported without groups."; + if (res.HadIndividualTags) + issueStr += "\n- PluralKit does not support per-member system tags. Since you had multiple members with distinct tags, those tags will be applied to the members' *display names*/nicknames instead."; + + var msg = $"{issueStr}\n\nDo you want to proceed with the import?"; + if (!await ctx.PromptYesNo(msg)) + throw Errors.ImportCancelled; + } + + return res.System; + } + public async Task Export(Context ctx) { ctx.CheckSystem();