From c9b236da67ba5ee4701c6ced919cafabc3db71eb Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Mon, 18 Dec 2023 15:11:40 -0500 Subject: [PATCH] Nullify `ChatBot` --- .../Components/Chat/ChatManager.cs | 3 +-- .../Controllers/ChatController.cs | 10 +++---- src/Tgstation.Server.Host/Models/ChatBot.cs | 26 +++++++++++++++---- .../Chat/Providers/TestDiscordProvider.cs | 1 + .../Chat/Providers/TestIrcProvider.cs | 1 + 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/Chat/ChatManager.cs b/src/Tgstation.Server.Host/Components/Chat/ChatManager.cs index b6000a7619..b0586d81a9 100644 --- a/src/Tgstation.Server.Host/Components/Chat/ChatManager.cs +++ b/src/Tgstation.Server.Host/Components/Chat/ChatManager.cs @@ -327,7 +327,7 @@ namespace Tgstation.Server.Host.Components.Chat if (originalChatBot != null) activeChatBots.Remove(originalChatBot); - activeChatBots.Add(new Models.ChatBot + activeChatBots.Add(new Models.ChatBot(newSettings.Channels) { Id = newSettings.Id, ConnectionString = newSettings.ConnectionString, @@ -335,7 +335,6 @@ namespace Tgstation.Server.Host.Components.Chat Name = newSettings.Name, ReconnectionInterval = newSettings.ReconnectionInterval, Provider = newSettings.Provider, - Channels = newSettings.Channels, }); } diff --git a/src/Tgstation.Server.Host/Controllers/ChatController.cs b/src/Tgstation.Server.Host/Controllers/ChatController.cs index 6b35f19d28..6bae07a444 100644 --- a/src/Tgstation.Server.Host/Controllers/ChatController.cs +++ b/src/Tgstation.Server.Host/Controllers/ChatController.cs @@ -126,12 +126,12 @@ namespace Tgstation.Server.Host.Controllers model.ReconnectionInterval ??= 1; // try to update das db first - var dbModel = new ChatBot + var newChannels = model.Channels?.Select(x => ConvertApiChatChannel(x, model.Provider!.Value)).ToList() ?? new List(); // important that this isn't null + var dbModel = new ChatBot(newChannels) { Name = model.Name, ConnectionString = model.ConnectionString, Enabled = model.Enabled, - Channels = model.Channels?.Select(x => ConvertApiChatChannel(x, model.Provider!.Value)).ToList() ?? new List(), // important that this isn't null InstanceId = Instance.Id!.Value, Provider = model.Provider, ReconnectionInterval = model.ReconnectionInterval, @@ -296,7 +296,7 @@ namespace Tgstation.Server.Host.Controllers if (current == default) return this.Gone(); - if ((model.Channels?.Count ?? current.Channels.Count) > (model.ChannelLimit ?? current.ChannelLimit!.Value)) + if ((model.Channels?.Count ?? current.Channels!.Count) > (model.ChannelLimit ?? current.ChannelLimit!.Value)) { // 400 or 409 depends on if the client sent both var errorMessage = new ErrorMessageResponse(ErrorCode.ChatBotMaxChannels); @@ -339,7 +339,7 @@ namespace Tgstation.Server.Host.Controllers var hasChannels = model.Channels != null; if (hasChannels || (model.Provider.HasValue && model.Provider != oldProvider)) { - DatabaseContext.ChatChannels.RemoveRange(current.Channels); + DatabaseContext.ChatChannels.RemoveRange(current.Channels!); if (hasChannels) { var dbChannels = model.Channels!.Select(x => ConvertApiChatChannel(x, model.Provider ?? current.Provider!.Value)).ToList(); @@ -347,7 +347,7 @@ namespace Tgstation.Server.Host.Controllers current.Channels = dbChannels; } else - current.Channels.Clear(); + current.Channels!.Clear(); } await DatabaseContext.Save(cancellationToken); diff --git a/src/Tgstation.Server.Host/Models/ChatBot.cs b/src/Tgstation.Server.Host/Models/ChatBot.cs index 8b5fe59f7d..fb31ff5032 100644 --- a/src/Tgstation.Server.Host/Models/ChatBot.cs +++ b/src/Tgstation.Server.Host/Models/ChatBot.cs @@ -1,11 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using Tgstation.Server.Api.Models.Response; -#nullable disable - namespace Tgstation.Server.Host.Models { /// @@ -25,17 +24,34 @@ namespace Tgstation.Server.Host.Models /// The parent . /// [Required] - public Instance Instance { get; set; } + public Instance? Instance { get; set; } /// /// See . /// public ICollection Channels { get; set; } + /// + /// Initializes a new instance of the class. + /// + public ChatBot() + : this(new List()) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The value of . + public ChatBot(ICollection channels) + { + Channels = channels ?? throw new ArgumentNullException(nameof(channels)); + } + /// public ChatBotResponse ToApi() => new ChatBotResponse { - Channels = Channels.Select(x => x.ToApi(Provider.Value)).ToList(), + Channels = Channels.Select(x => x.ToApi(this.Require(x => x.Provider))).ToList(), ConnectionString = ConnectionString, Enabled = Enabled, Provider = Provider, diff --git a/tests/Tgstation.Server.Host.Tests/Components/Chat/Providers/TestDiscordProvider.cs b/tests/Tgstation.Server.Host.Tests/Components/Chat/Providers/TestDiscordProvider.cs index b21b19f0ae..61d61a19a3 100644 --- a/tests/Tgstation.Server.Host.Tests/Components/Chat/Providers/TestDiscordProvider.cs +++ b/tests/Tgstation.Server.Host.Tests/Components/Chat/Providers/TestDiscordProvider.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Reflection; using System.Threading; using System.Threading.Tasks; diff --git a/tests/Tgstation.Server.Host.Tests/Components/Chat/Providers/TestIrcProvider.cs b/tests/Tgstation.Server.Host.Tests/Components/Chat/Providers/TestIrcProvider.cs index 9e8565955e..60021ea019 100644 --- a/tests/Tgstation.Server.Host.Tests/Components/Chat/Providers/TestIrcProvider.cs +++ b/tests/Tgstation.Server.Host.Tests/Components/Chat/Providers/TestIrcProvider.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Reflection; using System.Threading; using System.Threading.Tasks;