diff --git a/src/Tgstation.Server.Host/Controllers/ChatController.cs b/src/Tgstation.Server.Host/Controllers/ChatController.cs index fc411b460a..6b35f19d28 100644 --- a/src/Tgstation.Server.Host/Controllers/ChatController.cs +++ b/src/Tgstation.Server.Host/Controllers/ChatController.cs @@ -141,7 +141,7 @@ namespace Tgstation.Server.Host.Controllers DatabaseContext.ChatBots.Add(dbModel); await DatabaseContext.Save(cancellationToken); - return await WithComponentInstance( + return await WithComponentInstanceNullable( async instance => { try @@ -180,7 +180,7 @@ namespace Tgstation.Server.Host.Controllers [TgsAuthorize(ChatBotRights.Delete)] [ProducesResponseType(204)] public async ValueTask Delete(long id, CancellationToken cancellationToken) - => await WithComponentInstance( + => await WithComponentInstanceNullable( async instance => { await Task.WhenAll( @@ -352,7 +352,7 @@ namespace Tgstation.Server.Host.Controllers await DatabaseContext.Save(cancellationToken); - earlyOut = await WithComponentInstance( + earlyOut = await WithComponentInstanceNullable( async instance => { var chat = instance.Chat; diff --git a/src/Tgstation.Server.Host/Controllers/ComponentInterfacingController.cs b/src/Tgstation.Server.Host/Controllers/ComponentInterfacingController.cs index c830a45d17..108390890f 100644 --- a/src/Tgstation.Server.Host/Controllers/ComponentInterfacingController.cs +++ b/src/Tgstation.Server.Host/Controllers/ComponentInterfacingController.cs @@ -10,11 +10,10 @@ using Tgstation.Server.Api.Models; using Tgstation.Server.Api.Models.Response; using Tgstation.Server.Host.Components; using Tgstation.Server.Host.Database; +using Tgstation.Server.Host.Models; using Tgstation.Server.Host.Security; using Tgstation.Server.Host.Utils; -#nullable disable - namespace Tgstation.Server.Host.Controllers { /// @@ -65,18 +64,18 @@ namespace Tgstation.Server.Host.Controllers } /// - protected override async ValueTask ValidateRequest(CancellationToken cancellationToken) + protected override async ValueTask ValidateRequest(CancellationToken cancellationToken) { if (!useInstanceRequestHeader) return null; - if (!ApiHeaders.InstanceId.HasValue) + if (!ApiHeaders!.InstanceId.HasValue) return BadRequest(new ErrorMessageResponse(ErrorCode.InstanceHeaderRequired)); if (AuthenticationContext.InstancePermissionSet == null) return Forbid(); - if (ValidateInstanceOnlineStatus(Instance)) + if (ValidateInstanceOnlineStatus(Instance!)) await DatabaseContext.Save(cancellationToken); using var instanceReferenceCheck = instanceManager.GetInstanceReference(Instance); @@ -99,7 +98,7 @@ namespace Tgstation.Server.Host.Controllers using (var instanceReferenceCheck = instanceManager.GetInstanceReference(metadata)) online = instanceReferenceCheck != null; - if (metadata.Online.Value == online) + if (metadata.Require(x => x.Online) == online) return false; const string OfflineWord = "offline"; @@ -122,11 +121,11 @@ namespace Tgstation.Server.Host.Controllers /// The to grab. If , will be used. /// A resulting in the that should be returned. /// The context of should be as small as possible so as to avoid race conditions. This function can return a if the requested instance was offline. - protected async ValueTask WithComponentInstance(Func> action, Models.Instance instance = null) + protected async ValueTask WithComponentInstanceNullable(Func> action, Models.Instance? instance = null) { ArgumentNullException.ThrowIfNull(action); - instance ??= Instance; + instance ??= Instance ?? throw new InvalidOperationException("ComponentInterfacingController has no Instance!"); using var instanceReference = instanceManager.GetInstanceReference(instance); using (LogContext.PushProperty(SerilogContextHelper.InstanceReferenceContextProperty, instanceReference.Uid)) @@ -136,5 +135,15 @@ namespace Tgstation.Server.Host.Controllers return await action(instanceReference); } } + + /// + /// Run a given with the relevant . + /// + /// A accepting the and returning a with the . + /// The to grab. If , will be used. + /// A resulting in the that should be returned. + /// The context of should be as small as possible so as to avoid race conditions. This function can return a if the requested instance was offline. + protected async ValueTask WithComponentInstance(Func> action, Models.Instance? instance = null) + => (await WithComponentInstanceNullable(async core => await action(core), instance))!; } }