From 04d78e33481783f95efdc657208b099c8de208ac Mon Sep 17 00:00:00 2001 From: spiral Date: Fri, 26 Nov 2021 21:14:09 -0500 Subject: [PATCH] refactor: remove some unused code --- .../Gateway/Payloads/GatewayStatusUpdate.cs | 2 +- Myriad/Types/Activity.cs | 33 +++---- Myriad/Types/Permissions.cs | 6 -- PluralKit.Bot/Bot.cs | 4 +- PluralKit.Core/Database/Utils/QueryBuilder.cs | 88 ------------------- .../Database/Utils/UpdateQueryBuilder.cs | 41 --------- PluralKit.Core/Models/ModelTypes/Partial.cs | 8 -- PluralKit.Core/Utils/TaskUtils.cs | 39 -------- 8 files changed, 17 insertions(+), 204 deletions(-) delete mode 100644 Myriad/Types/Permissions.cs delete mode 100644 PluralKit.Core/Database/Utils/QueryBuilder.cs delete mode 100644 PluralKit.Core/Database/Utils/UpdateQueryBuilder.cs delete mode 100644 PluralKit.Core/Utils/TaskUtils.cs diff --git a/Myriad/Gateway/Payloads/GatewayStatusUpdate.cs b/Myriad/Gateway/Payloads/GatewayStatusUpdate.cs index 89892db7..51ab979c 100644 --- a/Myriad/Gateway/Payloads/GatewayStatusUpdate.cs +++ b/Myriad/Gateway/Payloads/GatewayStatusUpdate.cs @@ -18,7 +18,7 @@ public record GatewayStatusUpdate } public ulong? Since { get; init; } - public ActivityPartial[]? Activities { get; init; } + public Activity[]? Activities { get; init; } public UserStatus Status { get; init; } public bool Afk { get; init; } } \ No newline at end of file diff --git a/Myriad/Types/Activity.cs b/Myriad/Types/Activity.cs index e5977c8c..ce91c688 100644 --- a/Myriad/Types/Activity.cs +++ b/Myriad/Types/Activity.cs @@ -1,22 +1,17 @@ -namespace Myriad.Types +namespace Myriad.Types; + +public record Activity { - public record Activity: ActivityPartial - { - } + public string Name { get; init; } + public ActivityType Type { get; init; } + public string? Url { get; init; } +} - public record ActivityPartial - { - public string Name { get; init; } - public ActivityType Type { get; init; } - public string? Url { get; init; } - } - - public enum ActivityType - { - Game = 0, - Streaming = 1, - Listening = 2, - Custom = 4, - Competing = 5 - } +public enum ActivityType +{ + Game = 0, + Streaming = 1, + Listening = 2, + Custom = 4, + Competing = 5 } \ No newline at end of file diff --git a/Myriad/Types/Permissions.cs b/Myriad/Types/Permissions.cs deleted file mode 100644 index c0f602a7..00000000 --- a/Myriad/Types/Permissions.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Myriad.Types -{ - public static class Permissions - { - } -} \ No newline at end of file diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index f631bc3c..b0a35571 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -122,7 +122,7 @@ public class Bot { Activities = new[] { - new ActivityPartial {Name = "Restarting... (please wait)", Type = ActivityType.Game} + new Activity {Name = "Restarting... (please wait)", Type = ActivityType.Game} }, Status = GatewayStatusUpdate.UserStatus.Idle }))); @@ -256,7 +256,7 @@ public class Bot { Activities = new[] { - new ActivityPartial + new Activity { Name = $"pk;help | in {totalGuilds:N0} servers | shard #{shard.ShardId}", Type = ActivityType.Game, diff --git a/PluralKit.Core/Database/Utils/QueryBuilder.cs b/PluralKit.Core/Database/Utils/QueryBuilder.cs deleted file mode 100644 index b612da4d..00000000 --- a/PluralKit.Core/Database/Utils/QueryBuilder.cs +++ /dev/null @@ -1,88 +0,0 @@ -#nullable enable -using System; -using System.Text; - -namespace PluralKit.Core -{ - public class QueryBuilder - { - private readonly string? _conflictField; - private readonly string? _condition; - private readonly StringBuilder _insertFragment = new StringBuilder(); - private readonly StringBuilder _valuesFragment = new StringBuilder(); - private readonly StringBuilder _updateFragment = new StringBuilder(); - private bool _firstInsert = true; - private bool _firstUpdate = true; - public QueryType Type { get; } - public string Table { get; } - - private QueryBuilder(QueryType type, string table, string? conflictField, string? condition) - { - Type = type; - Table = table; - _conflictField = conflictField; - _condition = condition; - } - - public static QueryBuilder Insert(string table) => new QueryBuilder(QueryType.Insert, table, null, null); - public static QueryBuilder Update(string table, string condition) => new QueryBuilder(QueryType.Update, table, null, condition); - public static QueryBuilder Upsert(string table, string conflictField) => new QueryBuilder(QueryType.Upsert, table, conflictField, null); - - public QueryBuilder Constant(string fieldName, string paramName) - { - if (_firstInsert) _firstInsert = false; - else - { - _insertFragment.Append(", "); - _valuesFragment.Append(", "); - } - - _insertFragment.Append(fieldName); - _valuesFragment.Append(paramName); - return this; - } - - public QueryBuilder Variable(string fieldName, string paramName) - { - Constant(fieldName, paramName); - - if (_firstUpdate) _firstUpdate = false; - else _updateFragment.Append(", "); - - _updateFragment.Append(fieldName); - _updateFragment.Append(" = "); - _updateFragment.Append(paramName); - return this; - } - - public string Build(string? suffix = null) - { - if (_firstInsert) - throw new ArgumentException("No fields have been added to the query."); - - StringBuilder query = new StringBuilder(Type switch - { - QueryType.Insert => $"insert into {Table} ({_insertFragment}) values ({_valuesFragment})", - QueryType.Upsert => $"insert into {Table} ({_insertFragment}) values ({_valuesFragment}) on conflict ({_conflictField}) do update set {_updateFragment}", - QueryType.Update => $"update {Table} set {_updateFragment}", - _ => throw new ArgumentOutOfRangeException($"Unknown query type {Type}") - }); - - if (Type == QueryType.Update && _condition != null) - query.Append($" where {_condition}"); - - if (!string.IsNullOrEmpty(suffix)) - query.Append($" {suffix}"); - query.Append(";"); - - return query.ToString(); - } - - public enum QueryType - { - Insert, - Update, - Upsert - } - } -} \ No newline at end of file diff --git a/PluralKit.Core/Database/Utils/UpdateQueryBuilder.cs b/PluralKit.Core/Database/Utils/UpdateQueryBuilder.cs deleted file mode 100644 index fd5698e6..00000000 --- a/PluralKit.Core/Database/Utils/UpdateQueryBuilder.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Dapper; - -namespace PluralKit.Core; - -public class UpdateQueryBuilder -{ - private readonly DynamicParameters _params = new(); - private readonly QueryBuilder _qb; - - private UpdateQueryBuilder(QueryBuilder qb) - { - _qb = qb; - } - - public static UpdateQueryBuilder Insert(string table) => new(QueryBuilder.Insert(table)); - - public static UpdateQueryBuilder Update(string table, string condition) => - new(QueryBuilder.Update(table, condition)); - - public static UpdateQueryBuilder Upsert(string table, string conflictField) => - new(QueryBuilder.Upsert(table, conflictField)); - - public UpdateQueryBuilder WithConstant(string name, T value) - { - _params.Add(name, value); - _qb.Constant(name, $"@{name}"); - return this; - } - - public UpdateQueryBuilder With(string columnName, T value) - { - _params.Add(columnName, value); - _qb.Variable(columnName, $"@{columnName}"); - return this; - } - - public UpdateQueryBuilder With(string columnName, Partial partialValue) => - partialValue.IsPresent ? With(columnName, partialValue.Value) : this; - - public (string Query, DynamicParameters Parameters) Build(string suffix = "") => (_qb.Build(suffix), _params); -} \ No newline at end of file diff --git a/PluralKit.Core/Models/ModelTypes/Partial.cs b/PluralKit.Core/Models/ModelTypes/Partial.cs index e8ae6f89..d4c201d4 100644 --- a/PluralKit.Core/Models/ModelTypes/Partial.cs +++ b/PluralKit.Core/Models/ModelTypes/Partial.cs @@ -75,12 +75,4 @@ public static class PartialExt public static Partial Map(this Partial pt, Func fn) => pt.IsPresent ? Partial.Present(fn.Invoke(pt.Value)) : Partial.Absent; - - public static void Apply(this Partial pt, DynamicParameters bag, QueryBuilder qb, string fieldName) - { - if (!pt.IsPresent) return; - - bag.Add(fieldName, pt.Value); - qb.Variable(fieldName, $"@{fieldName}"); - } } \ No newline at end of file diff --git a/PluralKit.Core/Utils/TaskUtils.cs b/PluralKit.Core/Utils/TaskUtils.cs deleted file mode 100644 index e7ea9c92..00000000 --- a/PluralKit.Core/Utils/TaskUtils.cs +++ /dev/null @@ -1,39 +0,0 @@ -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 handler) - { - try - { - await task; - } - catch (Exception e) - { - handler(e); - } - } - - public static async Task TimeoutAfter(this Task task, TimeSpan? timeout) - { - // https://stackoverflow.com/a/22078975 - using (var timeoutCancellationTokenSource = new CancellationTokenSource()) - { - var completedTask = await Task.WhenAny(task, Task.Delay(timeout ?? TimeSpan.FromMilliseconds(-1), timeoutCancellationTokenSource.Token)); - if (completedTask == task) - { - timeoutCancellationTokenSource.Cancel(); - return await task; // Very important in order to propagate exceptions - } - else - { - throw new TimeoutException(); - } - } - } - } -} \ No newline at end of file