2021-08-26 01:43:31 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
2019-06-14 20:48:19 +00:00
|
|
|
using NodaTime;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
2019-07-18 15:13:42 +00:00
|
|
|
using Serilog;
|
2019-06-14 20:48:19 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public class DataFileService
|
|
|
|
{
|
|
|
|
private readonly IDatabase _db;
|
|
|
|
private readonly DispatchService _dispatch;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly ModelRepository _repo;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public DataFileService(IDatabase db, ModelRepository repo, ILogger logger, DispatchService dispatch)
|
|
|
|
{
|
|
|
|
_db = db;
|
|
|
|
_repo = repo;
|
|
|
|
_logger = logger;
|
|
|
|
_dispatch = dispatch;
|
|
|
|
}
|
2020-10-09 14:24:15 +00:00
|
|
|
|
2021-11-30 22:04:42 +00:00
|
|
|
public async Task<JObject> ExportSystem(PKSystem system)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
|
|
|
await using var conn = await _db.Obtain();
|
2021-10-31 00:45:59 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var o = new JObject();
|
|
|
|
o.Add("version", 1);
|
2021-10-31 00:45:59 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
o.Merge(system.ToJson(LookupContext.ByOwner));
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-30 22:04:42 +00:00
|
|
|
var config = await _repo.GetSystemConfig(system.Id);
|
|
|
|
o.Add("config", config.ToJson());
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
o.Add("accounts", new JArray((await _repo.GetSystemAccounts(system.Id)).ToList()));
|
|
|
|
o.Add("members",
|
|
|
|
new JArray((await _repo.GetSystemMembers(system.Id).ToListAsync()).Select(m =>
|
|
|
|
m.ToJson(LookupContext.ByOwner))));
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var groups = await _repo.GetSystemGroups(system.Id).ToListAsync();
|
|
|
|
var j_groups = groups.Select(x => x.ToJson(LookupContext.ByOwner, needsMembersArray: true)).ToList();
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (groups.Count > 0)
|
|
|
|
{
|
|
|
|
var q = await _repo.GetGroupMemberInfo(groups.Select(x => x.Id));
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
foreach (var row in q)
|
|
|
|
((JArray)j_groups.Find(x => x.Value<string>("id") == row.Group)["members"]).Add(row.Member);
|
|
|
|
}
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
o.Add("groups", new JArray(j_groups));
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var switches = new JArray();
|
|
|
|
var switchList = await _repo.GetPeriodFronters(conn, system.Id, null,
|
|
|
|
Instant.FromDateTimeUtc(DateTime.MinValue.ToUniversalTime()), SystemClock.Instance.GetCurrentInstant());
|
|
|
|
foreach (var sw in switchList)
|
|
|
|
{
|
|
|
|
var s = new JObject();
|
|
|
|
s.Add("timestamp", sw.TimespanStart.FormatExport());
|
|
|
|
s.Add("members", new JArray(sw.Members.Select(m => m.Hid)));
|
|
|
|
switches.Add(s);
|
2019-06-15 09:55:11 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
o.Add("switches", switches);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<ImportResultNew> ImportSystem(ulong userId, PKSystem? system, JObject importFile,
|
|
|
|
Func<string, Task> confirmFunc)
|
|
|
|
{
|
|
|
|
await using var conn = await _db.Obtain();
|
|
|
|
await using var tx = await conn.BeginTransactionAsync();
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return await BulkImporter.PerformImport(conn, tx, _repo, _logger, _dispatch, userId, system, importFile,
|
|
|
|
confirmFunc);
|
2021-09-22 01:42:41 +00:00
|
|
|
}
|
2021-08-26 01:43:31 +00:00
|
|
|
}
|