Fix Tupperbox importing (again)

This commit is contained in:
Ske 2020-09-20 23:32:57 +02:00
parent 4f6c98db48
commit 969065724d

View File

@ -9,6 +9,8 @@ using Newtonsoft.Json;
using DSharpPlus.Exceptions; using DSharpPlus.Exceptions;
using DSharpPlus.Entities; using DSharpPlus.Entities;
using Newtonsoft.Json.Linq;
using PluralKit.Core; using PluralKit.Core;
namespace PluralKit.Bot namespace PluralKit.Bot
@ -16,6 +18,12 @@ namespace PluralKit.Bot
public class ImportExport public class ImportExport
{ {
private readonly DataFileService _dataFiles; 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) public ImportExport(DataFileService dataFiles)
{ {
_dataFiles = dataFiles; _dataFiles = dataFiles;
@ -41,51 +49,22 @@ namespace PluralKit.Bot
throw Errors.InvalidImportFile; throw Errors.InvalidImportFile;
} }
if (!response.IsSuccessStatusCode) throw Errors.InvalidImportFile; if (!response.IsSuccessStatusCode)
var json = await response.Content.ReadAsStringAsync(); throw Errors.InvalidImportFile;
var settings = new JsonSerializerSettings();
DataFileSystem data; DataFileSystem data;
// TODO: can we clean up this mess?
try try
{ {
data = JsonConvert.DeserializeObject<DataFileSystem>(json, settings); var json = JsonConvert.DeserializeObject<JObject>(await response.Content.ReadAsStringAsync(), _settings);
data = await LoadSystem(ctx, json);
} }
catch (JsonException) catch (JsonException)
{ {
try throw Errors.InvalidImportFile;
{
var tupperbox = JsonConvert.DeserializeObject<TupperboxProfile>(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;
}
} }
if (!data.Valid)
if (!data.Valid) throw Errors.InvalidImportFile; throw Errors.InvalidImportFile;
if (data.LinkedAccounts != null && !data.LinkedAccounts.Contains(ctx.Author.Id)) if (data.LinkedAccounts != null && !data.LinkedAccounts.Contains(ctx.Author.Id))
{ {
@ -111,7 +90,39 @@ namespace PluralKit.Bot
} }
}); });
} }
private async Task<DataFileSystem> LoadSystem(Context ctx, JObject json)
{
if (json.ContainsKey("tuppers"))
return await ImportFromTupperbox(ctx, json);
return json.ToObject<DataFileSystem>();
}
private async Task<DataFileSystem> ImportFromTupperbox(Context ctx, JObject json)
{
var tupperbox = json.ToObject<TupperboxProfile>();
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) public async Task Export(Context ctx)
{ {
ctx.CheckSystem(); ctx.CheckSystem();