tgstation-server 6.19.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ChatAuthority.cs
Go to the documentation of this file.
1using System;
3using System.Linq;
5
9
17
19{
22 {
30 : base(instanceManager, databaseContext, logger)
31 {
32 }
33
41 {
42 if (model.ReconnectionInterval == 0)
43 throw new InvalidOperationException("RecconnectionInterval cannot be zero!");
44
45 if (model.Name != null && String.IsNullOrWhiteSpace(model.Name))
46 return BadRequest<ChatBot>(ErrorCode.ChatBotWhitespaceName);
47
48 if (model.ConnectionString != null && String.IsNullOrWhiteSpace(model.ConnectionString))
49 return BadRequest<ChatBot>(ErrorCode.ChatBotWhitespaceConnectionString);
50
51 var defaultMaxChannels = (ulong)Math.Max(ChatBot.DefaultChannelLimit, model.Channels?.Count ?? 0);
52 if (defaultMaxChannels > UInt16.MaxValue)
53 return BadRequest<ChatBot>(ErrorCode.ChatBotMaxChannels);
54
55 if (forCreation)
57
58 return null;
59 }
60
64 string name,
65 string connectionString,
67 long instanceId,
70 bool enabled,
71 CancellationToken cancellationToken)
72 {
74 ArgumentNullException.ThrowIfNull(name);
76
77 return new(
78 () => Flag(ChatBotRights.Create),
79 async () =>
80 {
81 var model = new ChatBot
82 {
83 Name = name,
84 ConnectionString = connectionString,
86 InstanceId = instanceId,
87 Provider = provider,
88 ReconnectionInterval = reconnectionInterval,
89 ChannelLimit = channelLimit,
90 Channels = initialChannels.ToList(),
91 };
92
94 if (earlyOut != null)
95 return earlyOut;
96
99 .Where(instance => instance.Id == instanceId)
100 .Select(instance => new
101 {
102 ChatBotLimit = instance.ChatBotLimit!.Value,
103 TotalChatBots = instance.ChatSettings!.Count,
104 })
105 .FirstOrDefaultAsync(cancellationToken);
106
107 if (query == null)
108 return Gone<ChatBot>();
109
110 if (query.TotalChatBots >= query.ChatBotLimit)
111 return Conflict<ChatBot>(ErrorCode.ChatBotMax);
112
113 model.Enabled ??= false;
115
117
118 await DatabaseContext.Save(cancellationToken);
119 return await WithComponentInstance(
120 async instance =>
121 {
122 try
123 {
124 // try to create it
125 await instance.Chat.ChangeSettings(model, cancellationToken);
126
127 if (model.Channels.Count > 0)
128 await instance.Chat.ChangeChannels(model.Require(x => x.Id), model.Channels, cancellationToken);
129 }
130 catch (Exception ex)
131 {
132 Logger.LogError(ex, "Failed to complete chat bot {id} initialization after addition, removing...", model.Id);
133
134 // undo the add
136
137 // DCTx2: Operations must always run
138 await DatabaseContext.Save(default);
139 await instance.Chat.DeleteConnection(model.Require(x => x.Id), default);
140 throw;
141 }
142
144 },
145 instanceId);
146 },
147 instanceId);
148 }
149 }
150}
RequirementsGated< AuthorityResponse< ChatBot > > Create(IEnumerable< Models.ChatChannel > initialChannels, string name, string connectionString, ChatProvider provider, long instanceId, uint? reconnectionInterval, ushort? channelLimit, bool enabled, CancellationToken cancellationToken)
Create a new ChatBot.A RequirementsGated<TResult> AuthorityResponse<TResult> for the created ChatBot.
ChatAuthority(IInstanceManager instanceManager, IDatabaseContext databaseContext, ILogger< ChatAuthority > logger)
Initializes a new instance of the ChatAuthority class.
static ? AuthorityResponse< ChatBot > StandardModelChecks(ChatBot model, bool forCreation)
Perform some basic validation of a given model .
ILogger< AuthorityBase > Logger
Gets the ILogger for the AuthorityBase.
AuthorityBase for IAuthoritys that need to access IInstanceCores.
readonly IInstanceManager instanceManager
The IInstanceManager for the ComponentInterfacingAuthorityBase.
Evaluates a set of IAuthorizationRequirements to be checked before executing a response.
Backend abstract implementation of IDatabaseContext.
DbSet< Instance > Instances
The Instances in the DatabaseContext.
Task Save(CancellationToken cancellationToken)
Saves changes made to the IDatabaseContext.A Task representing the running operation.
DbSet< ChatBot > ChatBots
The ChatBots in the DatabaseContext.
const ushort DefaultChannelLimit
Default for Api.Models.Internal.ChatBotSettings.ChannelLimit.
Definition ChatBot.cs:16
IAuthority for manipulating chat bots.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12
ChatProvider
Represents a chat service provider.
ChatBotRights
Rights for chat bots.
@ List
User may list files if the Models.Instance allows it.
HttpSuccessResponse
Indicates the type of HTTP status code a successful AuthorityResponse<TResult> should generate.
@ Enabled
The OAuth Gateway is enabled.