diff --git a/Iceshrimp.Backend/Core/Configuration/Config.cs b/Iceshrimp.Backend/Core/Configuration/Config.cs index b6ebc80f..2dc1e566 100644 --- a/Iceshrimp.Backend/Core/Configuration/Config.cs +++ b/Iceshrimp.Backend/Core/Configuration/Config.cs @@ -139,10 +139,11 @@ public sealed class Config MaxUploadSizeBytes = suffix switch { - null => num, + null => num, 'k' or 'K' => num * 1024, 'm' or 'M' => num * 1024 * 1024, 'g' or 'G' => num * 1024 * 1024 * 1024, + _ => throw new Exception("Unsupported suffix, use one of: [K]ilobytes, [M]egabytes, [G]igabytes") }; } @@ -169,10 +170,11 @@ public sealed class Config MaxCacheSizeBytes = suffix switch { - null => num, + null => num, 'k' or 'K' => num * 1024, 'm' or 'M' => num * 1024 * 1024, 'g' or 'G' => num * 1024 * 1024 * 1024, + _ => throw new Exception("Unsupported suffix, use one of: [K]ilobytes, [M]egabytes, [G]igabytes") }; } @@ -229,10 +231,11 @@ public sealed class Config MaxFileSizeBytes = suffix switch { - null => num, + null => num, 'k' or 'K' => num * 1024, 'm' or 'M' => num * 1024 * 1024, 'g' or 'G' => num * 1024 * 1024 * 1024, + _ => throw new Exception("Unsupported suffix, use one of: [K]ilobytes, [M]egabytes, [G]igabytes") }; } diff --git a/Iceshrimp.Backend/Core/Extensions/QueryableExtensions.cs b/Iceshrimp.Backend/Core/Extensions/QueryableExtensions.cs index 7f68690f..1959c714 100644 --- a/Iceshrimp.Backend/Core/Extensions/QueryableExtensions.cs +++ b/Iceshrimp.Backend/Core/Extensions/QueryableExtensions.cs @@ -29,22 +29,22 @@ public static class QueryableExtensions if (pq is { SinceId: not null, MinId: not null }) throw GracefulException.BadRequest("Can't use sinceId and minId params simultaneously"); + // @formatter:off query = pq switch { - { SinceId: not null, MaxId: not null } => query - .Where(p => p.Id.IsGreaterThan(pq.SinceId) && - p.Id.IsLessThan(pq.MaxId)) - .OrderByDescending(p => p.Id), - { MinId: not null, MaxId: not null } => query - .Where(p => p.Id.IsGreaterThan(pq.MinId) && - p.Id.IsLessThan(pq.MaxId)) - .OrderBy(p => p.Id), - { SinceId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.SinceId)) - .OrderByDescending(p => p.Id), - { MinId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId)).OrderBy(p => p.Id), - { MaxId: not null } => query.Where(p => p.Id.IsLessThan(pq.MaxId)).OrderByDescending(p => p.Id), - _ => query.OrderByDescending(p => p.Id) + { SinceId: not null, MaxId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.SinceId) && p.Id.IsLessThan(pq.MaxId)) + .OrderByDescending(p => p.Id), + { MinId: not null, MaxId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId) && p.Id.IsLessThan(pq.MaxId)) + .OrderBy(p => p.Id), + { SinceId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.SinceId)) + .OrderByDescending(p => p.Id), + { MinId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId)) + .OrderBy(p => p.Id), + { MaxId: not null } => query.Where(p => p.Id.IsLessThan(pq.MaxId)) + .OrderByDescending(p => p.Id), + _ => query.OrderByDescending(p => p.Id) }; + // @formatter:on return query.Skip(pq.Offset ?? 0).Take(Math.Min(pq.Limit ?? defaultLimit, maxLimit)); } @@ -63,24 +63,22 @@ public static class QueryableExtensions if (pq is { SinceId: not null, MinId: not null }) throw GracefulException.BadRequest("Can't use sinceId and minId params simultaneously"); + // @formatter:off query = pq switch { - { SinceId: not null, MaxId: not null } => query - .Where(predicate.Compose(id => id.IsGreaterThan(pq.SinceId) && - id.IsLessThan(pq.MaxId))) - .OrderByDescending(predicate), - { MinId: not null, MaxId: not null } => query - .Where(predicate.Compose(id => id.IsGreaterThan(pq.MinId) && - id.IsLessThan(pq.MaxId))) - .OrderBy(predicate), - { SinceId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.SinceId))) - .OrderByDescending(predicate), - { MinId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.MinId))) - .OrderBy(predicate), - { MaxId: not null } => query.Where(predicate.Compose(id => id.IsLessThan(pq.MaxId))) - .OrderByDescending(predicate), - _ => query.OrderByDescending(predicate) + { SinceId: not null, MaxId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.SinceId) && id.IsLessThan(pq.MaxId))) + .OrderByDescending(predicate), + { MinId: not null, MaxId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.MinId) && id.IsLessThan(pq.MaxId))) + .OrderBy(predicate), + { SinceId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.SinceId))) + .OrderByDescending(predicate), + { MinId: not null } => query.Where(predicate.Compose(id => id.IsGreaterThan(pq.MinId))) + .OrderBy(predicate), + { MaxId: not null } => query.Where(predicate.Compose(id => id.IsLessThan(pq.MaxId))) + .OrderByDescending(predicate), + _ => query.OrderByDescending(predicate) }; + // @formatter:on return query.Skip(pq.Offset ?? 0).Take(Math.Min(pq.Limit ?? defaultLimit, maxLimit)); } @@ -124,17 +122,22 @@ public static class QueryableExtensions maxId = res; } + // @formatter:off query = pq switch { { SinceId: not null, MaxId: not null } => query.Where(predicate.Compose(id => id > sinceId && id < maxId)) .OrderByDescending(predicate), - { MinId: not null, MaxId: not null } => query.Where(predicate.Compose(id => id > minId && id < maxId)) - .OrderBy(predicate), - { SinceId: not null } => query.Where(predicate.Compose(id => id > sinceId)).OrderByDescending(predicate), - { MinId: not null } => query.Where(predicate.Compose(id => id > minId)).OrderBy(predicate), - { MaxId: not null } => query.Where(predicate.Compose(id => id < maxId)).OrderByDescending(predicate), - _ => query.OrderByDescending(predicate) + { MinId: not null, MaxId: not null } => query.Where(predicate.Compose(id => id > minId && id < maxId)) + .OrderBy(predicate), + { SinceId: not null } => query.Where(predicate.Compose(id => id > sinceId)) + .OrderByDescending(predicate), + { MinId: not null } => query.Where(predicate.Compose(id => id > minId)) + .OrderBy(predicate), + { MaxId: not null } => query.Where(predicate.Compose(id => id < maxId)) + .OrderByDescending(predicate), + _ => query.OrderByDescending(predicate) }; + // @formatter:on return query.Skip(pq.Offset ?? 0).Take(Math.Min(pq.Limit ?? defaultLimit, maxLimit)); } @@ -149,16 +152,18 @@ public static class QueryableExtensions if (pq.Limit is < 1) throw GracefulException.BadRequest("Limit cannot be less than 1"); + // @formatter:off query = pq switch { - { MinId: not null, MaxId: not null } => query - .Where(p => p.Id.IsGreaterThan(pq.MinId) && - p.Id.IsLessThan(pq.MaxId)) - .OrderBy(p => p.Id), - { MinId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId)).OrderBy(p => p.Id), - { MaxId: not null } => query.Where(p => p.Id.IsLessThan(pq.MaxId)).OrderByDescending(p => p.Id), - _ => query.OrderByDescending(p => p.Id) + { MinId: not null, MaxId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId) && p.Id.IsLessThan(pq.MaxId)) + .OrderBy(p => p.Id), + { MinId: not null } => query.Where(p => p.Id.IsGreaterThan(pq.MinId)) + .OrderBy(p => p.Id), + { MaxId: not null } => query.Where(p => p.Id.IsLessThan(pq.MaxId)) + .OrderByDescending(p => p.Id), + _ => query.OrderByDescending(p => p.Id) }; + // @formatter:on return query.Take(Math.Min(pq.Limit ?? defaultLimit, maxLimit)); } diff --git a/Iceshrimp.Backend/Core/Federation/WebFinger/WebFingerService.cs b/Iceshrimp.Backend/Core/Federation/WebFinger/WebFingerService.cs index cb9d9604..beee9f17 100644 --- a/Iceshrimp.Backend/Core/Federation/WebFinger/WebFingerService.cs +++ b/Iceshrimp.Backend/Core/Federation/WebFinger/WebFingerService.cs @@ -66,15 +66,16 @@ public class WebFingerService( else if (query.StartsWith('@')) { proto = "https"; - var split = query.Split('@'); + + // @formatter:off domain = split.Length switch { < 2 or > 3 => throw new GracefulException(HttpStatusCode.BadRequest, $"Invalid query: {query}"), - 2 => throw new GracefulException(HttpStatusCode.BadRequest, - $"Can't run WebFinger for local user: {query}"), - _ => split[2] + 2 => throw new GracefulException(HttpStatusCode.BadRequest, $"Can't run WebFinger for local user: {query}"), + _ => split[2] }; + // @formatter:on } else { diff --git a/Iceshrimp.Backend/Core/Queues/PreDeliverQueue.cs b/Iceshrimp.Backend/Core/Queues/PreDeliverQueue.cs index 4c28996b..75553c82 100644 --- a/Iceshrimp.Backend/Core/Queues/PreDeliverQueue.cs +++ b/Iceshrimp.Backend/Core/Queues/PreDeliverQueue.cs @@ -153,14 +153,15 @@ file static class QueryableExtensions this IQueryable query, Enums.FederationMode mode, DatabaseContext db ) { + // @formatter:off Expression> expr = mode switch { - Enums.FederationMode.BlockList => u => - u.Host == null || !db.BlockedInstances.Any(p => u.Host == p.Host || u.Host.EndsWith("." + p.Host)), - Enums.FederationMode.AllowList => u => - u.Host == null || db.AllowedInstances.Any(p => u.Host == p.Host || u.Host.EndsWith("." + p.Host)), + Enums.FederationMode.BlockList => u => u.Host == null || !db.BlockedInstances.Any(p => u.Host == p.Host || u.Host.EndsWith("." + p.Host)), + Enums.FederationMode.AllowList => u => u.Host == null || db.AllowedInstances.Any(p => u.Host == p.Host || u.Host.EndsWith("." + p.Host)), + _ => throw new ArgumentOutOfRangeException(nameof(mode), mode, null) }; + // @formatter:on return query.Where(expr); }