diff --git a/SlackNet.AspNetCore/AspNetCoreExtensions.cs b/SlackNet.AspNetCore/AspNetCoreExtensions.cs index 92a0f14..5cd4923 100644 --- a/SlackNet.AspNetCore/AspNetCoreExtensions.cs +++ b/SlackNet.AspNetCore/AspNetCoreExtensions.cs @@ -14,7 +14,7 @@ public static IServiceCollection AddSlackNet(this IServiceCollection serviceColl configure(configuration); Default.RegisterServices((serviceType, createService) => serviceCollection.AddTransient(serviceType, c => createService(c.GetService))); serviceCollection.AddSingleton(); - serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.TryAddSingleton(); diff --git a/SlackNet.AspNetCore/ResolvedActionHandler.cs b/SlackNet.AspNetCore/ResolvedInteractiveMessageHandler.cs similarity index 66% rename from SlackNet.AspNetCore/ResolvedActionHandler.cs rename to SlackNet.AspNetCore/ResolvedInteractiveMessageHandler.cs index 926ad76..7d2e7e0 100644 --- a/SlackNet.AspNetCore/ResolvedActionHandler.cs +++ b/SlackNet.AspNetCore/ResolvedInteractiveMessageHandler.cs @@ -5,21 +5,21 @@ namespace SlackNet.AspNetCore { - abstract class ResolvedActionHandler : IActionHandler + abstract class ResolvedInteractiveMessageHandler : IInteractiveMessageHandler { - protected ResolvedActionHandler(string actionName) => ActionName = actionName; + protected ResolvedInteractiveMessageHandler(string actionName) => ActionName = actionName; public string ActionName { get; } public abstract Task Handle(InteractiveMessage message); } - class ResolvedActionHandler : ResolvedActionHandler - where T : IActionHandler + class ResolvedInteractiveMessageHandler : ResolvedInteractiveMessageHandler + where T : IInteractiveMessageHandler { private readonly IServiceProvider _serviceProvider; - public ResolvedActionHandler(IServiceProvider serviceProvider, string actionName) + public ResolvedInteractiveMessageHandler(IServiceProvider serviceProvider, string actionName) : base(actionName) { _serviceProvider = serviceProvider; diff --git a/SlackNet.AspNetCore/SlackActionsService.cs b/SlackNet.AspNetCore/SlackActionsService.cs deleted file mode 100644 index 1a509c7..0000000 --- a/SlackNet.AspNetCore/SlackActionsService.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using SlackNet.Interaction; - -namespace SlackNet.AspNetCore -{ - class SlackActionsService : ISlackActions - { - private readonly ISlackActions _actions = new SlackActions(); - - public SlackActionsService(IEnumerable handlers) - { - foreach (var handler in handlers) - _actions.SetHandler(handler.ActionName, handler); - } - - public Task Handle(InteractiveMessage request) => _actions.Handle(request); - public void SetHandler(string actionName, IActionHandler handler) => _actions.SetHandler(actionName, handler); - } -} \ No newline at end of file diff --git a/SlackNet.AspNetCore/SlackEventsMiddleware.cs b/SlackNet.AspNetCore/SlackEventsMiddleware.cs index 8008e05..ce112e3 100644 --- a/SlackNet.AspNetCore/SlackEventsMiddleware.cs +++ b/SlackNet.AspNetCore/SlackEventsMiddleware.cs @@ -15,7 +15,7 @@ class SlackEventsMiddleware private readonly RequestDelegate _next; private readonly SlackEndpointConfiguration _configuration; private readonly ISlackEvents _slackEvents; - private readonly ISlackActions _slackActions; + private readonly ISlackInteractiveMessages _slackInteractiveMessages; private readonly ISlackMessageActions _slackMessageActions; private readonly ISlackOptions _slackOptions; private readonly IDialogSubmissionHandler _dialogSubmissionHandler; @@ -25,7 +25,7 @@ public SlackEventsMiddleware( RequestDelegate next, SlackEndpointConfiguration configuration, ISlackEvents slackEvents, - ISlackActions slackActions, + ISlackInteractiveMessages slackInteractiveMessages, ISlackMessageActions slackMessageActions, ISlackOptions slackOptions, IDialogSubmissionHandler dialogSubmissionHandler, @@ -34,7 +34,7 @@ public SlackEventsMiddleware( _next = next; _configuration = configuration; _slackEvents = slackEvents; - _slackActions = slackActions; + _slackInteractiveMessages = slackInteractiveMessages; _slackMessageActions = slackMessageActions; _slackOptions = slackOptions; _dialogSubmissionHandler = dialogSubmissionHandler; @@ -103,7 +103,7 @@ private async Task HandleSlackAction(HttpContext context) private async Task HandleInteractiveMessage(HttpContext context, InteractiveMessage interactiveMessage) { - var response = await _slackActions.Handle(interactiveMessage).ConfigureAwait(false); + var response = await _slackInteractiveMessages.Handle(interactiveMessage).ConfigureAwait(false); var responseJson = response == null ? null : interactiveMessage.IsAppUnfurl ? Serialize(new AttachmentUpdateResponse(response)) diff --git a/SlackNet.AspNetCore/SlackInteractiveMessagesService.cs b/SlackNet.AspNetCore/SlackInteractiveMessagesService.cs new file mode 100644 index 0000000..f0dfdd1 --- /dev/null +++ b/SlackNet.AspNetCore/SlackInteractiveMessagesService.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using SlackNet.Interaction; + +namespace SlackNet.AspNetCore +{ + class SlackInteractiveMessagesService : ISlackInteractiveMessages + { + private readonly ISlackInteractiveMessages _interactiveMessages = new SlackInteractiveMessages(); + + public SlackInteractiveMessagesService(IEnumerable handlers) + { + foreach (var handler in handlers) + _interactiveMessages.SetHandler(handler.ActionName, handler); + } + + public Task Handle(InteractiveMessage request) => _interactiveMessages.Handle(request); + public void SetHandler(string actionName, IInteractiveMessageHandler handler) => _interactiveMessages.SetHandler(actionName, handler); + } +} \ No newline at end of file diff --git a/SlackNet.AspNetCore/SlackServiceConfiguration.cs b/SlackNet.AspNetCore/SlackServiceConfiguration.cs index c5ffec6..c2292cd 100644 --- a/SlackNet.AspNetCore/SlackServiceConfiguration.cs +++ b/SlackNet.AspNetCore/SlackServiceConfiguration.cs @@ -28,11 +28,11 @@ public SlackServiceConfiguration RegisterEventHandler() return this; } - public SlackServiceConfiguration RegisterActionHandler(string actionName) - where THandler : class, IActionHandler + public SlackServiceConfiguration RegisterInteractiveMessageHandler(string actionName) + where THandler : class, IInteractiveMessageHandler { _serviceCollection.AddTransient(); - _serviceCollection.AddSingleton(c => new ResolvedActionHandler(c, actionName)); + _serviceCollection.AddSingleton(c => new ResolvedInteractiveMessageHandler(c, actionName)); return this; } diff --git a/SlackNet.EventsExample/ColorSelector.cs b/SlackNet.EventsExample/ColorSelector.cs index e8e9cee..ad189dc 100644 --- a/SlackNet.EventsExample/ColorSelector.cs +++ b/SlackNet.EventsExample/ColorSelector.cs @@ -6,7 +6,7 @@ namespace SlackNet.EventsExample { - public class ColorSelector : IActionHandler, IOptionProvider + public class ColorSelector : IInteractiveMessageHandler, IOptionProvider { public static readonly string ActionName = "color_select"; diff --git a/SlackNet.EventsExample/Counter.cs b/SlackNet.EventsExample/Counter.cs index 4ec5012..6735545 100644 --- a/SlackNet.EventsExample/Counter.cs +++ b/SlackNet.EventsExample/Counter.cs @@ -5,7 +5,7 @@ namespace SlackNet.EventsExample { - public class Counter : IActionHandler + public class Counter : IInteractiveMessageHandler { public static readonly string ActionName = "add"; private static readonly Regex _counterPattern = new Regex("Counter: (\\d+)"); diff --git a/SlackNet.EventsExample/DialogDemo.cs b/SlackNet.EventsExample/DialogDemo.cs index 8f6dadc..eae5a31 100644 --- a/SlackNet.EventsExample/DialogDemo.cs +++ b/SlackNet.EventsExample/DialogDemo.cs @@ -6,7 +6,7 @@ namespace SlackNet.EventsExample { - public class DialogDemo : IDialogSubmissionHandler, IActionHandler + public class DialogDemo : IDialogSubmissionHandler, IInteractiveMessageHandler { internal const string EchoDialog = "echo-dialog"; internal const string ErrorDialog = "error-dialog"; diff --git a/SlackNet.EventsExample/Startup.cs b/SlackNet.EventsExample/Startup.cs index 0c33181..d7ff551 100644 --- a/SlackNet.EventsExample/Startup.cs +++ b/SlackNet.EventsExample/Startup.cs @@ -19,11 +19,11 @@ public void ConfigureServices(IServiceCollection services) services.AddSlackNet(c => c .UseApiToken(Configuration["Slack:ApiToken"]) .RegisterEventHandler() - .RegisterActionHandler(Counter.ActionName) - .RegisterActionHandler(ColorSelector.ActionName) + .RegisterInteractiveMessageHandler(Counter.ActionName) + .RegisterInteractiveMessageHandler(ColorSelector.ActionName) .RegisterOptionProvider(ColorSelector.ActionName) - .RegisterActionHandler(DialogDemo.EchoDialog) - .RegisterActionHandler(DialogDemo.ErrorDialog) + .RegisterInteractiveMessageHandler(DialogDemo.EchoDialog) + .RegisterInteractiveMessageHandler(DialogDemo.ErrorDialog) .RegisterDialogSubmissionHandler()); services.AddMvc(); } diff --git a/SlackNet/Default.cs b/SlackNet/Default.cs index 3d1efe5..b13bcee 100644 --- a/SlackNet/Default.cs +++ b/SlackNet/Default.cs @@ -41,7 +41,7 @@ private static JsonSerializerSettings SerializerSettings(ISlackTypeResolver slac public static ISlackEvents SlackEvents { get; } = new SlackEvents(); - public static ISlackActions SlackActions { get; } = new SlackActions(); + public static ISlackInteractiveMessages SlackInteractiveMessages { get; } = new SlackInteractiveMessages(); public static ISlackOptions SlackOptions { get; } = new SlackOptions(); @@ -53,7 +53,7 @@ public static void RegisterServices(Action, object registerService(typeof(ISlackTypeResolver), resolve => SlackTypeResolver(AssembliesContainingSlackTypes)); registerService(typeof(IWebSocketFactory), resolve => WebSocketFactory); registerService(typeof(ISlackEvents), resolve => SlackEvents); - registerService(typeof(ISlackActions), resolve => SlackActions); + registerService(typeof(ISlackInteractiveMessages), resolve => SlackInteractiveMessages); registerService(typeof(ISlackOptions), resolve => SlackOptions); } } diff --git a/SlackNet/Interaction/IActionHandler.cs b/SlackNet/Interaction/IInteractiveMessageHandler.cs similarity index 75% rename from SlackNet/Interaction/IActionHandler.cs rename to SlackNet/Interaction/IInteractiveMessageHandler.cs index 59d220d..fc00f81 100644 --- a/SlackNet/Interaction/IActionHandler.cs +++ b/SlackNet/Interaction/IInteractiveMessageHandler.cs @@ -2,7 +2,7 @@ namespace SlackNet.Interaction { - public interface IActionHandler + public interface IInteractiveMessageHandler { Task Handle(InteractiveMessage message); } diff --git a/SlackNet/Interaction/SlackActions.cs b/SlackNet/Interaction/SlackActions.cs deleted file mode 100644 index e49d7f6..0000000 --- a/SlackNet/Interaction/SlackActions.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace SlackNet.Interaction -{ - public interface ISlackActions - { - Task Handle(InteractiveMessage request); - void SetHandler(string actionName, IActionHandler handler); - } - - public class SlackActions : ISlackActions - { - private readonly Dictionary _handlers = new Dictionary(); - - public Task Handle(InteractiveMessage request) => - _handlers.TryGetValue(request.Actions[0].Name, out var handler) - ? handler.Handle(request) - : Task.FromResult(null); - - public void SetHandler(string actionName, IActionHandler handler) => _handlers[actionName] = handler; - } -} \ No newline at end of file diff --git a/SlackNet/Interaction/SlackInteractiveMessages.cs b/SlackNet/Interaction/SlackInteractiveMessages.cs new file mode 100644 index 0000000..d7fe849 --- /dev/null +++ b/SlackNet/Interaction/SlackInteractiveMessages.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace SlackNet.Interaction +{ + public interface ISlackInteractiveMessages + { + Task Handle(InteractiveMessage request); + void SetHandler(string actionName, IInteractiveMessageHandler handler); + } + + public class SlackInteractiveMessages : ISlackInteractiveMessages + { + private readonly Dictionary _handlers = new Dictionary(); + + public Task Handle(InteractiveMessage request) => + _handlers.TryGetValue(request.Actions[0].Name, out var handler) + ? handler.Handle(request) + : Task.FromResult(null); + + public void SetHandler(string actionName, IInteractiveMessageHandler handler) => _handlers[actionName] = handler; + } +} \ No newline at end of file