2021-11-27 02:10:56 +00:00
|
|
|
using Dapper;
|
2021-08-26 01:43:31 +00:00
|
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
2021-08-26 01:43:31 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public partial class BulkImporter: IAsyncDisposable
|
|
|
|
{
|
|
|
|
private readonly Dictionary<string, GroupId> _existingGroupHids = new();
|
|
|
|
private readonly Dictionary<string, GroupId> _existingGroupNames = new();
|
2021-08-26 01:43:31 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly Dictionary<string, MemberId> _existingMemberHids = new();
|
|
|
|
private readonly Dictionary<string, MemberId> _existingMemberNames = new();
|
|
|
|
private readonly Dictionary<string, GroupId> _knownGroupIdentifiers = new();
|
|
|
|
private readonly Dictionary<string, MemberId> _knownMemberIdentifiers = new();
|
2021-08-26 01:43:31 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly ImportResultNew _result = new();
|
|
|
|
private ILogger _logger { get; init; }
|
|
|
|
private ModelRepository _repo { get; init; }
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private PKSystem _system { get; set; }
|
2021-11-30 02:35:21 +00:00
|
|
|
private SystemConfig _cfg { get; set; }
|
2021-11-27 02:10:56 +00:00
|
|
|
private IPKConnection _conn { get; init; }
|
|
|
|
private IPKTransaction _tx { get; init; }
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private Func<string, Task> _confirmFunc { get; init; }
|
2021-08-26 01:43:31 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async ValueTask DisposeAsync()
|
|
|
|
{
|
|
|
|
// try rolling back the transaction
|
|
|
|
// this will throw if the transaction was committed, but that's fine
|
|
|
|
// so we just catch InvalidOperationException
|
|
|
|
try
|
2021-08-26 01:43:31 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
await _tx.RollbackAsync();
|
2021-08-26 01:43:31 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
catch (InvalidOperationException) { }
|
|
|
|
}
|
2021-08-26 01:43:31 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
internal static async Task<ImportResultNew> PerformImport(IPKConnection conn, IPKTransaction tx,
|
|
|
|
ModelRepository repo, ILogger logger, DispatchService dispatch, ulong userId,
|
|
|
|
PKSystem? system, JObject importFile, Func<string, Task> confirmFunc)
|
|
|
|
{
|
|
|
|
await using var importer = new BulkImporter
|
|
|
|
{
|
|
|
|
_logger = logger,
|
|
|
|
_repo = repo,
|
|
|
|
_system = system,
|
|
|
|
_conn = conn,
|
|
|
|
_tx = tx,
|
|
|
|
_confirmFunc = confirmFunc
|
|
|
|
};
|
|
|
|
|
|
|
|
if (system == null)
|
2021-08-26 01:43:31 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
system = await repo.CreateSystem(null, importer._conn);
|
|
|
|
await repo.AddAccount(system.Id, userId, importer._conn);
|
|
|
|
importer._result.CreatedSystem = system.Hid;
|
|
|
|
importer._system = system;
|
2021-08-26 01:43:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 02:35:21 +00:00
|
|
|
importer._cfg = await repo.GetSystemConfig(system.Id);
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Fetch all members in the system and log their names and hids
|
|
|
|
var members = await conn.QueryAsync<PKMember>("select id, hid, name from members where system = @System",
|
|
|
|
new { System = system.Id });
|
|
|
|
foreach (var m in members)
|
2021-09-22 01:42:41 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
importer._existingMemberHids[m.Hid] = m.Id;
|
|
|
|
importer._existingMemberNames[m.Name] = m.Id;
|
2021-09-22 01:42:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// same as above for groups
|
|
|
|
var groups = await conn.QueryAsync<PKGroup>("select id, hid, name from groups where system = @System",
|
|
|
|
new { System = system.Id });
|
|
|
|
foreach (var g in groups)
|
2021-08-26 01:43:31 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
importer._existingGroupHids[g.Hid] = g.Id;
|
|
|
|
importer._existingGroupNames[g.Name] = g.Id;
|
2021-08-26 01:43:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
try
|
2021-09-22 01:42:41 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
if (importFile.ContainsKey("tuppers"))
|
|
|
|
await importer.ImportTupperbox(importFile);
|
|
|
|
else if (importFile.ContainsKey("switches"))
|
|
|
|
await importer.ImportPluralKit(importFile);
|
|
|
|
else
|
|
|
|
throw new ImportException("File type is unknown.");
|
|
|
|
importer._result.Success = true;
|
|
|
|
await tx.CommitAsync();
|
|
|
|
|
|
|
|
_ = dispatch.Dispatch(system.Id, new UpdateDispatchData { Event = DispatchEvent.SUCCESSFUL_IMPORT });
|
2021-09-22 01:42:41 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
catch (ImportException e)
|
2021-08-26 01:43:31 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
importer._result.Success = false;
|
|
|
|
importer._result.Message = e.Message;
|
2021-08-26 01:43:31 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
catch (ArgumentNullException)
|
2021-08-27 15:03:47 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
importer._result.Success = false;
|
2021-08-26 01:43:31 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
return importer._result;
|
2021-08-26 01:43:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private (MemberId?, bool) TryGetExistingMember(string hid, string name)
|
2021-08-26 01:43:31 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
if (_existingMemberHids.TryGetValue(hid, out var byHid)) return (byHid, true);
|
|
|
|
if (_existingMemberNames.TryGetValue(name, out var byName)) return (byName, false);
|
|
|
|
return (null, false);
|
2021-08-26 01:43:31 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
private (GroupId?, bool) TryGetExistingGroup(string hid, string name)
|
|
|
|
{
|
|
|
|
if (_existingGroupHids.TryGetValue(hid, out var byHid)) return (byHid, true);
|
|
|
|
if (_existingGroupNames.TryGetValue(name, out var byName)) return (byName, false);
|
|
|
|
return (null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task AssertMemberLimitNotReached(int newMembers)
|
|
|
|
{
|
2021-11-30 02:35:21 +00:00
|
|
|
var memberLimit = _cfg.MemberLimitOverride ?? Limits.MaxMemberCount;
|
2021-11-27 02:10:56 +00:00
|
|
|
var existingMembers = await _repo.GetSystemMemberCount(_system.Id);
|
|
|
|
if (existingMembers + newMembers > memberLimit)
|
|
|
|
throw new ImportException($"Import would exceed the maximum number of members ({memberLimit}).");
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task AssertGroupLimitNotReached(int newGroups)
|
|
|
|
{
|
2021-11-30 02:35:21 +00:00
|
|
|
var limit = _cfg.GroupLimitOverride ?? Limits.MaxGroupCount;
|
2021-11-27 02:10:56 +00:00
|
|
|
var existing = await _repo.GetSystemGroupCount(_system.Id);
|
|
|
|
if (existing + newGroups > limit)
|
|
|
|
throw new ImportException($"Import would exceed the maximum number of groups ({limit}).");
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ImportException: Exception
|
|
|
|
{
|
|
|
|
public ImportException(string Message) : base(Message) { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public record ImportResultNew
|
|
|
|
{
|
|
|
|
public int Added = 0;
|
|
|
|
public string? CreatedSystem;
|
|
|
|
public string? Message;
|
|
|
|
public int Modified = 0;
|
|
|
|
public bool Success;
|
2021-08-26 01:43:31 +00:00
|
|
|
}
|