2019-09-29 19:40:13 +00:00
|
|
|
using System;
|
2019-06-14 20:48:19 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
using Dapper;
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
namespace PluralKit.Core
|
2019-06-14 20:48:19 +00:00
|
|
|
{
|
|
|
|
public class DataFileService
|
|
|
|
{
|
2020-08-29 11:46:27 +00:00
|
|
|
private readonly IDatabase _db;
|
|
|
|
private readonly ModelRepository _repo;
|
|
|
|
private readonly ILogger _logger;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
public DataFileService(IDatabase db, ModelRepository repo, ILogger logger)
|
2019-06-14 20:48:19 +00:00
|
|
|
{
|
2020-06-11 19:11:50 +00:00
|
|
|
_db = db;
|
2020-08-29 11:46:27 +00:00
|
|
|
_repo = repo;
|
2021-08-26 01:43:31 +00:00
|
|
|
_logger = logger;
|
2020-06-11 19:11:50 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
public async Task<JObject> ExportSystem(PKSystem system)
|
2019-06-14 20:48:19 +00:00
|
|
|
{
|
2020-08-29 11:46:27 +00:00
|
|
|
await using var conn = await _db.Obtain();
|
2020-10-09 14:24:15 +00:00
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
var o = new JObject();
|
|
|
|
|
|
|
|
o.Add("version", 1);
|
|
|
|
o.Add("id", system.Hid);
|
|
|
|
o.Add("name", system.Name);
|
|
|
|
o.Add("description", system.Description);
|
|
|
|
o.Add("tag", system.Tag);
|
|
|
|
o.Add("avatar_url", system.AvatarUrl);
|
|
|
|
o.Add("timezone", system.UiTz);
|
|
|
|
o.Add("created", system.Created.FormatExport());
|
|
|
|
o.Add("accounts", new JArray((await _repo.GetSystemAccounts(conn, system.Id)).ToList()));
|
2021-08-27 15:03:47 +00:00
|
|
|
o.Add("members", new JArray((await _repo.GetSystemMembers(conn, system.Id).ToListAsync()).Select(m => m.ToJson(LookupContext.ByOwner))));
|
|
|
|
|
2021-08-26 01:43:31 +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)
|
2019-10-20 07:16:57 +00:00
|
|
|
{
|
2021-08-26 01:43:31 +00:00
|
|
|
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-09-29 19:40:13 +00:00
|
|
|
}
|
2021-08-26 01:43:31 +00:00
|
|
|
o.Add("switches", switches);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
return o;
|
2019-06-15 09:55:11 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
public async Task<ImportResultNew> ImportSystem(ulong userId, PKSystem? system, JObject importFile, Func<string, Task> confirmFunc)
|
2019-06-15 09:55:11 +00:00
|
|
|
{
|
2021-08-26 01:43:31 +00:00
|
|
|
await using var conn = await _db.Obtain();
|
|
|
|
await using var tx = await conn.BeginTransactionAsync();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
return await BulkImporter.PerformImport(conn, tx, _repo, _logger, userId, system, importFile, confirmFunc);
|
2019-06-15 09:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-26 01:43:31 +00:00
|
|
|
}
|