feat: upgrade to .NET 6, refactor everything
This commit is contained in:
@@ -1,27 +1,25 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
namespace PluralKit.Core;
|
||||
|
||||
namespace PluralKit.Core
|
||||
public static class BuildInfoService
|
||||
{
|
||||
public static class BuildInfoService
|
||||
public static string Version { get; private set; }
|
||||
public static string FullVersion { get; private set; }
|
||||
|
||||
public static async Task LoadVersion()
|
||||
{
|
||||
public static string Version { get; private set; }
|
||||
public static string FullVersion { get; private set; }
|
||||
|
||||
public static async Task LoadVersion()
|
||||
using (var stream = typeof(BuildInfoService).Assembly.GetManifestResourceStream("version"))
|
||||
{
|
||||
using (var stream = typeof(BuildInfoService).Assembly.GetManifestResourceStream("version"))
|
||||
{
|
||||
// if this happens, something broke
|
||||
if (stream == null) FullVersion = "(unknown version) ";
|
||||
else using (var reader = new StreamReader(stream)) FullVersion = await reader.ReadToEndAsync();
|
||||
}
|
||||
|
||||
// cheap hack to remove newline
|
||||
FullVersion = FullVersion.Remove(FullVersion.Length - 1);
|
||||
|
||||
// show only short commit hash to users
|
||||
Version = FullVersion.Remove(7);
|
||||
// if this happens, something broke
|
||||
if (stream == null) FullVersion = "(unknown version) ";
|
||||
else
|
||||
using (var reader = new StreamReader(stream))
|
||||
FullVersion = await reader.ReadToEndAsync();
|
||||
}
|
||||
|
||||
// cheap hack to remove newline
|
||||
FullVersion = FullVersion.Remove(FullVersion.Length - 1);
|
||||
|
||||
// show only short commit hash to users
|
||||
Version = FullVersion.Remove(7);
|
||||
}
|
||||
}
|
@@ -1,81 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Dapper;
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
using NodaTime;
|
||||
|
||||
using Serilog;
|
||||
|
||||
namespace PluralKit.Core
|
||||
namespace PluralKit.Core;
|
||||
|
||||
public class DataFileService
|
||||
{
|
||||
public class DataFileService
|
||||
private readonly IDatabase _db;
|
||||
private readonly DispatchService _dispatch;
|
||||
private readonly ILogger _logger;
|
||||
private readonly ModelRepository _repo;
|
||||
|
||||
public DataFileService(IDatabase db, ModelRepository repo, ILogger logger, DispatchService dispatch)
|
||||
{
|
||||
private readonly IDatabase _db;
|
||||
private readonly ModelRepository _repo;
|
||||
private readonly ILogger _logger;
|
||||
private readonly DispatchService _dispatch;
|
||||
_db = db;
|
||||
_repo = repo;
|
||||
_logger = logger;
|
||||
_dispatch = dispatch;
|
||||
}
|
||||
|
||||
public DataFileService(IDatabase db, ModelRepository repo, ILogger logger, DispatchService dispatch)
|
||||
public async Task<JObject> ExportSystem(PKSystem system)
|
||||
{
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
var o = new JObject();
|
||||
o.Add("version", 1);
|
||||
|
||||
o.Merge(system.ToJson(LookupContext.ByOwner));
|
||||
|
||||
o.Add("timezone", system.UiTz);
|
||||
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))));
|
||||
|
||||
var groups = await _repo.GetSystemGroups(system.Id).ToListAsync();
|
||||
var j_groups = groups.Select(x => x.ToJson(LookupContext.ByOwner, needsMembersArray: true)).ToList();
|
||||
|
||||
if (groups.Count > 0)
|
||||
{
|
||||
_db = db;
|
||||
_repo = repo;
|
||||
_logger = logger;
|
||||
_dispatch = dispatch;
|
||||
var q = await _repo.GetGroupMemberInfo(groups.Select(x => x.Id));
|
||||
|
||||
foreach (var row in q)
|
||||
((JArray)j_groups.Find(x => x.Value<string>("id") == row.Group)["members"]).Add(row.Member);
|
||||
}
|
||||
|
||||
public async Task<JObject> ExportSystem(PKSystem system)
|
||||
o.Add("groups", new JArray(j_groups));
|
||||
|
||||
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)
|
||||
{
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
var o = new JObject();
|
||||
o.Add("version", 1);
|
||||
|
||||
o.Merge(system.ToJson(LookupContext.ByOwner));
|
||||
|
||||
o.Add("timezone", system.UiTz);
|
||||
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))));
|
||||
|
||||
var groups = (await _repo.GetSystemGroups(system.Id).ToListAsync());
|
||||
var j_groups = groups.Select(x => x.ToJson(LookupContext.ByOwner, needsMembersArray: true)).ToList();
|
||||
|
||||
if (groups.Count > 0)
|
||||
{
|
||||
var q = await _repo.GetGroupMemberInfo(groups.Select(x => x.Id));
|
||||
|
||||
foreach (var row in q)
|
||||
((JArray)j_groups.Find(x => x.Value<string>("id") == row.Group)["members"]).Add(row.Member);
|
||||
}
|
||||
|
||||
o.Add("groups", new JArray(j_groups));
|
||||
|
||||
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);
|
||||
}
|
||||
o.Add("switches", switches);
|
||||
|
||||
return o;
|
||||
var s = new JObject();
|
||||
s.Add("timestamp", sw.TimespanStart.FormatExport());
|
||||
s.Add("members", new JArray(sw.Members.Select(m => m.Hid)));
|
||||
switches.Add(s);
|
||||
}
|
||||
|
||||
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();
|
||||
o.Add("switches", switches);
|
||||
|
||||
return await BulkImporter.PerformImport(conn, tx, _repo, _logger, _dispatch, userId, system, importFile, confirmFunc);
|
||||
}
|
||||
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();
|
||||
|
||||
return await BulkImporter.PerformImport(conn, tx, _repo, _logger, _dispatch, userId, system, importFile,
|
||||
confirmFunc);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user