Add Tupperbox importing support

This commit is contained in:
Ske
2019-06-15 11:55:11 +02:00
parent 9be7514fb9
commit 1e1ef4495f
3 changed files with 198 additions and 103 deletions

View File

@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
@@ -27,19 +26,66 @@ namespace PluralKit.Bot.Commands
{
var response = await client.GetAsync(url);
if (!response.IsSuccessStatusCode) throw Errors.InvalidImportFile;
var str = await response.Content.ReadAsStringAsync();
var json = await response.Content.ReadAsStringAsync();
var data = TryDeserialize(str);
if (!data.HasValue || !data.Value.Valid) throw Errors.InvalidImportFile;
if (Context.SenderSystem != null && Context.SenderSystem.Hid != data.Value.Id)
var settings = new JsonSerializerSettings
{
// TODO: prompt "are you sure you want to import someone else's system?
MissingMemberHandling = MissingMemberHandling.Error
};
DataFileSystem data;
// TODO: can we clean up this mess?
try
{
data = JsonConvert.DeserializeObject<DataFileSystem>(json, settings);
}
catch (JsonException)
{
try
{
var tupperbox = JsonConvert.DeserializeObject<TupperboxProfile>(json, settings);
if (!tupperbox.Valid) throw Errors.InvalidImportFile;
var res = tupperbox.ToPluralKit();
if (res.HadGroups || res.HadMultibrackets || 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.HadMultibrackets)
issueStr += "\n- PluralKit does not support members with multiple proxy tags. Only the first pair will be imported.";
if (res.HadIndividualTags)
issueStr +=
"\n- PluralKit does not support per-member system tags. Since you had multiple members with distinct tags, tags will not be imported. You can set your system tag using the `pk;system tag <tag>` command later.";
var msg = await Context.Channel.SendMessageAsync($"{issueStr}\n\nDo you want to proceed with the import?");
if (!await Context.PromptYesNo(msg)) throw Errors.ImportCancelled;
}
data = res.System;
}
catch (JsonException)
{
throw Errors.InvalidImportFile;
}
}
if (!data.Valid) throw Errors.InvalidImportFile;
if (data.LinkedAccounts != null && !data.LinkedAccounts.Contains(Context.User.Id))
{
var msg = await Context.Channel.SendMessageAsync($"{Emojis.Warn} You seem to importing a system profile belonging to another account. Are you sure you want to proceed?");
if (!await Context.PromptYesNo(msg)) throw Errors.ImportCancelled;
}
// If passed system is null, it'll create a new one
// (and that's okay!)
var result = await DataFiles.ImportSystem(data.Value, Context.SenderSystem);
var result = await DataFiles.ImportSystem(data, Context.SenderSystem);
if (Context.SenderSystem == null)
{
@@ -72,7 +118,7 @@ namespace PluralKit.Bot.Commands
{
try
{
return JsonConvert.DeserializeObject<DataFileSystem>(json);
}
catch (JsonException e)
{