run dotnet format

This commit is contained in:
spiral
2021-08-27 11:03:47 -04:00
parent 05989242f9
commit ac2671452d
278 changed files with 1913 additions and 1808 deletions

View File

@@ -12,10 +12,10 @@ using Serilog;
namespace PluralKit.Core
{
public partial class BulkImporter : IAsyncDisposable
public partial class BulkImporter: IAsyncDisposable
{
private ILogger _logger { get; init; }
private ModelRepository _repo { get; init; }
private ModelRepository _repo { get; init; }
private PKSystem _system { get; set; }
private IPKConnection _conn { get; init; }
@@ -41,16 +41,17 @@ namespace PluralKit.Core
_confirmFunc = confirmFunc,
};
if (system == null) {
if (system == null)
{
system = await repo.CreateSystem(conn, null, tx);
await repo.AddAccount(conn, system.Id, userId);
importer._result.CreatedSystem = system.Hid;
importer._system = system;
}
// 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});
new { System = system.Id });
foreach (var m in members)
{
importer._existingMemberHids[m.Hid] = m.Id;
@@ -105,11 +106,12 @@ namespace PluralKit.Core
{
await _tx.RollbackAsync();
}
catch (InvalidOperationException) {}
catch (InvalidOperationException) { }
}
private class ImportException : Exception {
public ImportException(string Message) : base(Message) {}
private class ImportException: Exception
{
public ImportException(string Message) : base(Message) { }
}
}

View File

@@ -33,9 +33,10 @@ namespace PluralKit.Core
var members = importFile.Value<JArray>("members");
var switches = importFile.Value<JArray>("switches");
var newMembers = members.Count(m => {
var (found, _) = TryGetExistingMember(m.Value<string>("id"), m.Value<string>("name"));
var newMembers = members.Count(m =>
{
var (found, _) = TryGetExistingMember(m.Value<string>("id"), m.Value<string>("name"));
return found == null;
});
await AssertLimitNotReached(newMembers);
@@ -43,7 +44,7 @@ namespace PluralKit.Core
foreach (JObject member in members)
await ImportMember(member);
if (switches.Any(sw => sw.Value<JArray>("members").Any(m => !_knownIdentifiers.ContainsKey((string) m))))
if (switches.Any(sw => sw.Value<JArray>("members").Any(m => !_knownIdentifiers.ContainsKey((string)m))))
throw new ImportException("One or more switches include members that haven't been imported.");
await ImportSwitches(switches);
@@ -99,9 +100,9 @@ namespace PluralKit.Core
private async Task ImportSwitches(JArray switches)
{
var existingSwitches = (await _conn.QueryAsync<PKSwitch>("select * from switches where system = @System", new {System = _system.Id})).ToList();
var existingSwitches = (await _conn.QueryAsync<PKSwitch>("select * from switches where system = @System", new { System = _system.Id })).ToList();
var existingTimestamps = existingSwitches.Select(sw => sw.Timestamp).ToImmutableHashSet();
var lastSwitchId = existingSwitches.Count != 0 ? existingSwitches.Select(sw => sw.Id).Max() : (SwitchId?) null;
var lastSwitchId = existingSwitches.Count != 0 ? existingSwitches.Select(sw => sw.Id).Max() : (SwitchId?)null;
if (switches.Count > 10000)
throw new ImportException($"Too many switches present in import file.");
@@ -115,10 +116,10 @@ namespace PluralKit.Core
var timestampString = sw.Value<string>("timestamp");
var timestamp = DateTimeFormats.TimestampExportFormat.Parse(timestampString);
if (!timestamp.Success) throw new ImportException($"Switch timestamp {timestampString} is not an valid timestamp.");
// Don't import duplicate switches
if (existingTimestamps.Contains(timestamp.Value)) continue;
// Otherwise, write to importer
await importer.StartRowAsync();
await importer.WriteAsync(_system.Id.Value, NpgsqlDbType.Integer);
@@ -127,7 +128,7 @@ namespace PluralKit.Core
var members = sw.Value<JArray>("members");
if (members.Count > Limits.MaxSwitchMemberCount)
throw new ImportException($"Switch with timestamp {timestampString} contains too many members ({members.Count} > 100).");
// Note that we've imported a switch with this timestamp
importedSwitches[timestamp.Value] = sw.Value<JArray>("members");
}
@@ -135,13 +136,13 @@ namespace PluralKit.Core
// Commit the import
await importer.CompleteAsync();
}
// Now, fetch all the switches we just added (so, now we get their IDs too)
// IDs are sequential, so any ID in this system, with a switch ID > the last max, will be one we just added
var justAddedSwitches = await _conn.QueryAsync<PKSwitch>(
"select * from switches where system = @System and id > @LastSwitchId",
new {System = _system.Id, LastSwitchId = lastSwitchId?.Value ?? -1});
new { System = _system.Id, LastSwitchId = lastSwitchId?.Value ?? -1 });
// Lastly, import the switch members
await using (var importer = _conn.BeginBinaryImport("copy switch_members (switch, member) from stdin (format binary)"))
{
@@ -149,13 +150,13 @@ namespace PluralKit.Core
{
if (!importedSwitches.TryGetValue(justAddedSwitch.Timestamp, out var switchMembers))
throw new Exception($"Found 'just-added' switch (by ID) with timestamp {justAddedSwitch.Timestamp}, but this did not correspond to a timestamp we just added a switch entry of! :/");
// We still assume timestamps are unique and non-duplicate, so:
foreach (var memberIdentifier in switchMembers)
{
if (!_knownIdentifiers.TryGetValue((string) memberIdentifier, out var memberId))
if (!_knownIdentifiers.TryGetValue((string)memberIdentifier, out var memberId))
throw new Exception($"Attempted to import switch with member identifier {memberIdentifier} but could not find an entry in the id map for this! :/");
await importer.StartRowAsync();
await importer.WriteAsync(justAddedSwitch.Id.Value, NpgsqlDbType.Integer);
await importer.WriteAsync(memberId.Value, NpgsqlDbType.Integer);

View File

@@ -49,7 +49,7 @@ namespace PluralKit.Core
var hasGroup = tupper.ContainsKey("group_id") && tupper["group_id"].Type != JTokenType.Null;
var multipleTags = false;
var name = tupper.Value<string>("name");
var patch = new MemberPatch();
@@ -61,8 +61,8 @@ namespace PluralKit.Core
if (brackets.Count % 2 != 0)
throw new ImportException($"Field 'brackets' in tupper {name} is invalid.");
var tags = new List<ProxyTag>();
for (var i = 0; i < brackets.Count / 2; i++)
tags.Add(new ProxyTag((string) brackets[i * 2], (string) brackets[i * 2 + 1]));
for (var i = 0; i < brackets.Count / 2; i++)
tags.Add(new ProxyTag((string)brackets[i * 2], (string)brackets[i * 2 + 1]));
patch.ProxyTags = tags.ToArray();
}
// todo: && if is new member
@@ -97,7 +97,7 @@ namespace PluralKit.Core
}
else
_result.Modified++;
_logger.Debug("Importing member with identifier {FileId} to system {System} (is creating new member? {IsCreatingNewMember})",
name, _system.Id, isNewMember);
@@ -113,9 +113,9 @@ namespace PluralKit.Core
{
throw new ImportException($"Field {e.Message} in tupper {name} is invalid.");
}
await _repo.UpdateMember(_conn, memberId, patch, _tx);
return (lastSetTag, multipleTags, hasGroup);
}
}

View File

@@ -1,12 +1,13 @@
using NodaTime;
using NodaTime;
using NodaTime.Text;
namespace PluralKit.Core {
namespace PluralKit.Core
{
public static class DateTimeFormats
{
public static IPattern<Instant> TimestampExportFormat = InstantPattern.ExtendedIso;
public static IPattern<LocalDate> DateExportFormat = LocalDatePattern.CreateWithInvariantCulture("yyyy-MM-dd");
// We create a composite pattern that only shows the two most significant things
// eg. if we have something with nonzero day component, we show <x>d <x>h, but if it's
// a smaller duration we may only bother with showing <x>h <x>m or <x>m <x>s
@@ -17,7 +18,7 @@ namespace PluralKit.Core {
{DurationPattern.CreateWithInvariantCulture("H'h' m'm'"), d => d.Hours > 0},
{DurationPattern.CreateWithInvariantCulture("D'd' h'h'"), d => d.Days > 0}
}.Build();
public static IPattern<LocalDateTime> LocalDateTimeFormat = LocalDateTimePattern.CreateWithInvariantCulture("yyyy-MM-dd HH:mm:ss");
public static IPattern<ZonedDateTime> ZonedDateTimeFormat = ZonedDateTimePattern.CreateWithInvariantCulture("yyyy-MM-dd HH:mm:ss x", DateTimeZoneProviders.Tzdb);

View File

@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Text.RegularExpressions;
using NodaTime;
@@ -11,7 +11,7 @@ namespace PluralKit.Core
public static Duration? ParsePeriod(string str)
{
Duration d = Duration.Zero;
foreach (Match match in Regex.Matches(str, "(\\d{1,6})(\\w)"))
{
var amount = int.Parse(match.Groups[1].Value);
@@ -34,7 +34,7 @@ namespace PluralKit.Core
// NodaTime can't parse constructs like "1st" and "2nd" so we quietly replace those away
// Gotta make sure to do the regex otherwise we'll catch things like the "st" in "August" too
str = Regex.Replace(str, "(\\d+)(st|nd|rd|th)", "$1");
var patterns = new[]
{
"MMM d yyyy", // Jan 1 2019
@@ -72,12 +72,12 @@ namespace PluralKit.Core
public static ZonedDateTime? ParseDateTime(string str, bool nudgeToPast = false, DateTimeZone zone = null)
{
if (zone == null) zone = DateTimeZone.Utc;
// Find the current timestamp in the given zone, find the (naive) midnight timestamp, then put that into the same zone (and make it naive again)
// Should yield a <current *local @ zone* date> 12:00:00 AM.
var now = SystemClock.Instance.GetCurrentInstant().InZone(zone).LocalDateTime;
var midnight = now.Date.AtMidnight();
// First we try to parse the string as a relative time using the period parser
var relResult = ParsePeriod(str);
if (relResult != null)
@@ -119,7 +119,7 @@ namespace PluralKit.Core
"MM dd", // 01 01
"MM/dd" // 01-01
};
// First, we try all the timestamps that only have a time
foreach (var timePattern in timePatterns)
{
@@ -130,17 +130,17 @@ namespace PluralKit.Core
// If we have a successful match and we need a time in the past, we try to shove a future-time a date before
// Example: "4:30 pm" at 3:30 pm likely refers to 4:30 pm the previous day
var val = result.Value;
// If we need to nudge, we just subtract a day. This only occurs when we're parsing specifically *just time*, so
// we know we won't nudge it by more than a day since we use today's midnight timestamp as a date template.
// Since this is a naive datetime, this ensures we're actually moving by one calendar day even if
// DST changes occur, since they'll be resolved later wrt. the right side of the boundary
if (val > now && nudgeToPast) val = val.PlusDays(-1);
return val.InZoneLeniently(zone);
}
}
// Then we try specific date+time combinations, both date first and time first, with and without commas
foreach (var timePattern in timePatterns)
{
@@ -158,7 +158,7 @@ namespace PluralKit.Core
}
}
}
// Finally, just date patterns, still using midnight as the template
foreach (var datePattern in datePatterns)
{

View File

@@ -1,5 +1,7 @@
namespace PluralKit.Core {
public static class Emojis {
namespace PluralKit.Core
{
public static class Emojis
{
public static readonly string Warn = "\u26A0";
public static readonly string Success = "\u2705";
public static readonly string Error = "\u274C";

View File

@@ -23,7 +23,7 @@ namespace PluralKit.Core
return default;
}
var entry = new HandlerEntry {Predicate = predicate, Handler = Handler};
var entry = new HandlerEntry { Predicate = predicate, Handler = Handler };
_handlers[Interlocked.Increment(ref _seq)] = entry;
// Wait for either the event task or the timeout task

View File

@@ -1,4 +1,4 @@
using System.IO;
using System.IO;
using Microsoft.Extensions.Configuration;
@@ -7,20 +7,21 @@ using Newtonsoft.Json;
using NodaTime;
using NodaTime.Serialization.JsonNet;
namespace PluralKit.Core {
namespace PluralKit.Core
{
public static class InitUtils
{
public static void InitStatic()
{
Database.InitStatic();
}
public static IConfigurationBuilder BuildConfiguration(string[] args) => new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("pluralkit.conf", true)
.AddEnvironmentVariables()
.AddCommandLine(args);
public static JsonSerializerSettings BuildSerializerSettings() => new JsonSerializerSettings().BuildSerializerSettings();
public static JsonSerializerSettings BuildSerializerSettings(this JsonSerializerSettings settings)

View File

@@ -1,4 +1,5 @@
namespace PluralKit.Core {
namespace PluralKit.Core
{
public static class Limits
{
public static readonly int MaxProxyNameLength = 80;
@@ -6,7 +7,7 @@ namespace PluralKit.Core {
public static readonly int MaxSystemNameLength = 100;
public static readonly int MaxSystemTagLength = MaxProxyNameLength - 1;
public static readonly int MaxMemberCount = 1000;
public static int MaxMembersWarnThreshold (int memberLimit) => memberLimit - 50;
public static int MaxMembersWarnThreshold(int memberLimit) => memberLimit - 50;
public static readonly int MaxGroupCount = 250;
public static readonly int MaxDescriptionLength = 1000;
public static readonly int MaxProxyTagLength = 100;

View File

@@ -10,7 +10,7 @@ namespace PluralKit.Core
try
{
uri = new Uri(input);
if (!uri.IsAbsoluteUri || (uri.Scheme != "http" && uri.Scheme != "https"))
if (!uri.IsAbsoluteUri || (uri.Scheme != "http" && uri.Scheme != "https"))
return false;
}
catch (UriFormatException)

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
@@ -20,7 +20,7 @@ namespace PluralKit.Core
if (str != null) return str.Length > length;
return false;
}
public static string ExtractCountryFlag(string flag)
{
if (flag.Length != 4) return null;
@@ -30,14 +30,14 @@ namespace PluralKit.Core
var cp2 = char.ConvertToUtf32(flag, 2);
if (cp1 < 0x1F1E6 || cp1 > 0x1F1FF) return null;
if (cp2 < 0x1F1E6 || cp2 > 0x1F1FF) return null;
return $"{(char) (cp1 - 0x1F1E6 + 'A')}{(char) (cp2 - 0x1F1E6 + 'A')}";
return $"{(char)(cp1 - 0x1F1E6 + 'A')}{(char)(cp2 - 0x1F1E6 + 'A')}";
}
catch (ArgumentException)
{
return null;
}
}
public static string NullIfEmpty(this string input)
{
if (input == null) return null;
@@ -70,7 +70,7 @@ namespace PluralKit.Core
foreach (var s in input)
{
var limit = characterLimitByPage.Invoke(output.Count);
// Would adding this string put us over the limit?
// (note: don't roll over if the buffer's already empty; this means an individual section is above the character limit. todo: truncate, then?)
if (buf.Length > 0 && buf.Length + s.Length > limit)
@@ -82,12 +82,12 @@ namespace PluralKit.Core
buf.Append(s);
}
// We most likely have something left over, so add that in too
if (buf.Length > 0)
output.Add(buf.ToString());
return output;
}
}
}
}

View File

@@ -2,24 +2,35 @@ using System;
using System.Threading;
using System.Threading.Tasks;
namespace PluralKit.Core {
public static class TaskUtils {
public static async Task CatchException(this Task task, Action<Exception> handler) {
try {
namespace PluralKit.Core
{
public static class TaskUtils
{
public static async Task CatchException(this Task task, Action<Exception> handler)
{
try
{
await task;
} catch (Exception e) {
}
catch (Exception e)
{
handler(e);
}
}
public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan? timeout) {
public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan? timeout)
{
// https://stackoverflow.com/a/22078975
using (var timeoutCancellationTokenSource = new CancellationTokenSource()) {
using (var timeoutCancellationTokenSource = new CancellationTokenSource())
{
var completedTask = await Task.WhenAny(task, Task.Delay(timeout ?? TimeSpan.FromMilliseconds(-1), timeoutCancellationTokenSource.Token));
if (completedTask == task) {
if (completedTask == task)
{
timeoutCancellationTokenSource.Cancel();
return await task; // Very important in order to propagate exceptions
} else {
}
else
{
throw new TimeoutException();
}
}