Nullify ChatBot

This commit is contained in:
Jordan Dominion
2023-12-18 15:11:40 -05:00
parent 960f2e4561
commit c9b236da67
5 changed files with 29 additions and 12 deletions
@@ -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,
});
}
@@ -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<Models.ChatChannel>(); // 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<Models.ChatChannel>(), // 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);
+21 -5
View File
@@ -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
{
/// <inheritdoc cref="Api.Models.Internal.ChatBotSettings" />
@@ -25,17 +24,34 @@ namespace Tgstation.Server.Host.Models
/// The parent <see cref="Models.Instance"/>.
/// </summary>
[Required]
public Instance Instance { get; set; }
public Instance? Instance { get; set; }
/// <summary>
/// See <see cref="Api.Models.Internal.ChatBotApiBase.Channels"/>.
/// </summary>
public ICollection<ChatChannel> Channels { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="ChatBot"/> class.
/// </summary>
public ChatBot()
: this(new List<ChatChannel>())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ChatBot"/> class.
/// </summary>
/// <param name="channels">The value of <see cref="Channels"/>.</param>
public ChatBot(ICollection<ChatChannel> channels)
{
Channels = channels ?? throw new ArgumentNullException(nameof(channels));
}
/// <inheritdoc />
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,
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;