diff --git a/src/Tgstation.Server.Api/Rights/ChatSettingsRights.cs b/src/Tgstation.Server.Api/Rights/ChatSettingsRights.cs
index e8803311ac..c4336d76dd 100644
--- a/src/Tgstation.Server.Api/Rights/ChatSettingsRights.cs
+++ b/src/Tgstation.Server.Api/Rights/ChatSettingsRights.cs
@@ -39,6 +39,14 @@ namespace Tgstation.Server.Api.Rights
///
/// User can change
///
- WriteName = 32
+ WriteName = 32,
+ ///
+ /// User can create new
+ ///
+ Create = 64,
+ ///
+ /// User can delete
+ ///
+ Delete = 128
}
}
diff --git a/src/Tgstation.Server.Host/Controllers/ChatController.cs b/src/Tgstation.Server.Host/Controllers/ChatController.cs
new file mode 100644
index 0000000000..22f1d75cb4
--- /dev/null
+++ b/src/Tgstation.Server.Host/Controllers/ChatController.cs
@@ -0,0 +1,97 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Mvc;
+using Tgstation.Server.Api.Models;
+using Tgstation.Server.Api.Rights;
+using Tgstation.Server.Host.Components;
+using Tgstation.Server.Host.Models;
+using Tgstation.Server.Host.Security;
+
+namespace Tgstation.Server.Host.Controllers
+{
+ ///
+ /// for managing
+ ///
+ [TgsAuthorize]
+ public sealed class ChatController : ModelController
+ {
+ ///
+ /// The for the
+ ///
+ readonly IInstanceManager instanceManager;
+
+ ///
+ /// Construct a
+ ///
+ /// The for the
+ /// The for the
+ /// The value of
+ public ChatController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, IInstanceManager instanceManager) : base(databaseContext, authenticationContextFactory)
+ {
+ this.instanceManager = instanceManager ?? throw new ArgumentNullException(nameof(instanceManager));
+ }
+
+ ///
+ [TgsAuthorize(ChatSettingsRights.Create)]
+ public override async Task Create([FromBody] Api.Models.ChatSettings model, CancellationToken cancellationToken)
+ {
+ if (model == null)
+ throw new ArgumentNullException(nameof(model));
+
+ if (String.IsNullOrWhiteSpace(model.Name))
+ return BadRequest(new { message = "Name cannot be null or whitespace!" });
+
+ if (String.IsNullOrWhiteSpace(model.ConnectionString))
+ return BadRequest(new { message = "ConnectionString cannot be null or whitespace!" });
+
+ //try to update das db first
+ var dbModel = new Models.ChatSettings
+ {
+ Name = model.Name,
+ ConnectionString = model.ConnectionString,
+ Enabled = model.Enabled,
+ Channels = model.Channels?.Select(x => new Models.ChatChannel
+ {
+ DiscordChannelId = x.DiscordChannelId,
+ IrcChannel = x.IrcChannel,
+ IsAdminChannel = x.IsAdminChannel,
+ IsWatchdogChannel = x.IsWatchdogChannel
+ }).ToList() ?? new List(),
+ InstanceId = Instance.Id,
+ Provider = model.Provider,
+ };
+ DatabaseContext.ChatSettings.Add(dbModel);
+ DatabaseContext.ChatChannels.AddRange(dbModel.Channels);
+ await DatabaseContext.Save(cancellationToken).ConfigureAwait(false);
+
+ try
+ {
+ try
+ {
+ //try to create it
+ var instance = instanceManager.GetInstance(Instance);
+ await instance.Chat.ChangeSettings(dbModel, cancellationToken).ConfigureAwait(false);
+
+ if (dbModel.Channels.Count > 0)
+ await instance.Chat.ChangeChannels(dbModel.Id, dbModel.Channels, cancellationToken).ConfigureAwait(false);
+ }
+ catch
+ {
+ //undo the add
+ DatabaseContext.ChatSettings.Remove(dbModel);
+ DatabaseContext.ChatChannels.RemoveRange(dbModel.Channels);
+ await DatabaseContext.Save(default).ConfigureAwait(false);
+ throw;
+ }
+ }
+ catch (InvalidOperationException e)
+ {
+ return BadRequest(new { message = e.Message });
+ }
+ return Json(dbModel);
+ }
+ }
+}
diff --git a/src/Tgstation.Server.Host/Controllers/DreamDaemonController.cs b/src/Tgstation.Server.Host/Controllers/DreamDaemonController.cs
index 168d3a46da..c1a4a6b673 100644
--- a/src/Tgstation.Server.Host/Controllers/DreamDaemonController.cs
+++ b/src/Tgstation.Server.Host/Controllers/DreamDaemonController.cs
@@ -18,10 +18,10 @@ using Tgstation.Server.Host.Security;
namespace Tgstation.Server.Host.Controllers
{
///
- /// for managing
+ /// for managing the
///
[Route("/" + nameof(DreamDaemon))]
- public sealed class DreamDaemonController : ModelController
+ public sealed class DreamDaemonController : ModelController
{
///
/// The for the
diff --git a/src/Tgstation.Server.Host/Models/DatabaseContext.cs b/src/Tgstation.Server.Host/Models/DatabaseContext.cs
index 554118c0f4..ff966eba6f 100644
--- a/src/Tgstation.Server.Host/Models/DatabaseContext.cs
+++ b/src/Tgstation.Server.Host/Models/DatabaseContext.cs
@@ -52,9 +52,7 @@ namespace Tgstation.Server.Host.Models
///
public DbSet InstanceUsers { get; set; }
- ///
- /// The s in the
- ///
+ ///
public DbSet ChatChannels { get; set; }
///
diff --git a/src/Tgstation.Server.Host/Models/IDatabaseContext.cs b/src/Tgstation.Server.Host/Models/IDatabaseContext.cs
index e08826dcc9..9e8d906905 100644
--- a/src/Tgstation.Server.Host/Models/IDatabaseContext.cs
+++ b/src/Tgstation.Server.Host/Models/IDatabaseContext.cs
@@ -49,6 +49,11 @@ namespace Tgstation.Server.Host.Models
///
DbSet ChatSettings { get; set; }
+ ///
+ /// The in the
+ ///
+ DbSet ChatChannels { get; set; }
+
///
/// The in the
///