Skip to content

Commit

Permalink
Renaming IActionHandler to IInteractiveMessageHandler and ISlackActio…
Browse files Browse the repository at this point in the history
…ns to ISlackInteractiveMessages
  • Loading branch information
soxtoby committed May 26, 2019
1 parent 65a894e commit ec81700
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 66 deletions.
2 changes: 1 addition & 1 deletion SlackNet.AspNetCore/AspNetCoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISlackEvents, SlackEventsService>();
serviceCollection.AddSingleton<ISlackActions, SlackActionsService>();
serviceCollection.AddSingleton<ISlackInteractiveMessages, SlackInteractiveMessagesService>();
serviceCollection.AddSingleton<ISlackMessageActions, SlackMessageActionsService>();
serviceCollection.AddSingleton<ISlackOptions, SlackOptionsService>();
serviceCollection.TryAddSingleton<IDialogSubmissionHandler, NullDialogSubmissionHandler>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageResponse> Handle(InteractiveMessage message);
}

class ResolvedActionHandler<T> : ResolvedActionHandler
where T : IActionHandler
class ResolvedInteractiveMessageHandler<T> : ResolvedInteractiveMessageHandler
where T : IInteractiveMessageHandler
{
private readonly IServiceProvider _serviceProvider;

public ResolvedActionHandler(IServiceProvider serviceProvider, string actionName)
public ResolvedInteractiveMessageHandler(IServiceProvider serviceProvider, string actionName)
: base(actionName)
{
_serviceProvider = serviceProvider;
Expand Down
20 changes: 0 additions & 20 deletions SlackNet.AspNetCore/SlackActionsService.cs

This file was deleted.

8 changes: 4 additions & 4 deletions SlackNet.AspNetCore/SlackEventsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +25,7 @@ public SlackEventsMiddleware(
RequestDelegate next,
SlackEndpointConfiguration configuration,
ISlackEvents slackEvents,
ISlackActions slackActions,
ISlackInteractiveMessages slackInteractiveMessages,
ISlackMessageActions slackMessageActions,
ISlackOptions slackOptions,
IDialogSubmissionHandler dialogSubmissionHandler,
Expand All @@ -34,7 +34,7 @@ public SlackEventsMiddleware(
_next = next;
_configuration = configuration;
_slackEvents = slackEvents;
_slackActions = slackActions;
_slackInteractiveMessages = slackInteractiveMessages;
_slackMessageActions = slackMessageActions;
_slackOptions = slackOptions;
_dialogSubmissionHandler = dialogSubmissionHandler;
Expand Down Expand Up @@ -103,7 +103,7 @@ private async Task<HttpResponse> HandleSlackAction(HttpContext context)

private async Task<HttpResponse> 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))
Expand Down
20 changes: 20 additions & 0 deletions SlackNet.AspNetCore/SlackInteractiveMessagesService.cs
Original file line number Diff line number Diff line change
@@ -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<ResolvedInteractiveMessageHandler> handlers)
{
foreach (var handler in handlers)
_interactiveMessages.SetHandler(handler.ActionName, handler);
}

public Task<MessageResponse> Handle(InteractiveMessage request) => _interactiveMessages.Handle(request);
public void SetHandler(string actionName, IInteractiveMessageHandler handler) => _interactiveMessages.SetHandler(actionName, handler);
}
}
6 changes: 3 additions & 3 deletions SlackNet.AspNetCore/SlackServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public SlackServiceConfiguration RegisterEventHandler<TEvent, THandler>()
return this;
}

public SlackServiceConfiguration RegisterActionHandler<THandler>(string actionName)
where THandler : class, IActionHandler
public SlackServiceConfiguration RegisterInteractiveMessageHandler<THandler>(string actionName)
where THandler : class, IInteractiveMessageHandler
{
_serviceCollection.AddTransient<THandler>();
_serviceCollection.AddSingleton<ResolvedActionHandler>(c => new ResolvedActionHandler<THandler>(c, actionName));
_serviceCollection.AddSingleton<ResolvedInteractiveMessageHandler>(c => new ResolvedInteractiveMessageHandler<THandler>(c, actionName));
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion SlackNet.EventsExample/ColorSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace SlackNet.EventsExample
{
public class ColorSelector : IActionHandler, IOptionProvider
public class ColorSelector : IInteractiveMessageHandler, IOptionProvider
{
public static readonly string ActionName = "color_select";

Expand Down
2 changes: 1 addition & 1 deletion SlackNet.EventsExample/Counter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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+)");
Expand Down
2 changes: 1 addition & 1 deletion SlackNet.EventsExample/DialogDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
8 changes: 4 additions & 4 deletions SlackNet.EventsExample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddSlackNet(c => c
.UseApiToken(Configuration["Slack:ApiToken"])
.RegisterEventHandler<MessageEvent, MessageHandler>()
.RegisterActionHandler<Counter>(Counter.ActionName)
.RegisterActionHandler<ColorSelector>(ColorSelector.ActionName)
.RegisterInteractiveMessageHandler<Counter>(Counter.ActionName)
.RegisterInteractiveMessageHandler<ColorSelector>(ColorSelector.ActionName)
.RegisterOptionProvider<ColorSelector>(ColorSelector.ActionName)
.RegisterActionHandler<DialogDemo>(DialogDemo.EchoDialog)
.RegisterActionHandler<DialogDemo>(DialogDemo.ErrorDialog)
.RegisterInteractiveMessageHandler<DialogDemo>(DialogDemo.EchoDialog)
.RegisterInteractiveMessageHandler<DialogDemo>(DialogDemo.ErrorDialog)
.RegisterDialogSubmissionHandler<DialogDemo>());
services.AddMvc();
}
Expand Down
4 changes: 2 additions & 2 deletions SlackNet/Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -53,7 +53,7 @@ public static void RegisterServices(Action<Type, Func<Func<Type, object>, 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SlackNet.Interaction
{
public interface IActionHandler
public interface IInteractiveMessageHandler
{
Task<MessageResponse> Handle(InteractiveMessage message);
}
Expand Down
23 changes: 0 additions & 23 deletions SlackNet/Interaction/SlackActions.cs

This file was deleted.

23 changes: 23 additions & 0 deletions SlackNet/Interaction/SlackInteractiveMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SlackNet.Interaction
{
public interface ISlackInteractiveMessages
{
Task<MessageResponse> Handle(InteractiveMessage request);
void SetHandler(string actionName, IInteractiveMessageHandler handler);
}

public class SlackInteractiveMessages : ISlackInteractiveMessages
{
private readonly Dictionary<string, IInteractiveMessageHandler> _handlers = new Dictionary<string, IInteractiveMessageHandler>();

public Task<MessageResponse> Handle(InteractiveMessage request) =>
_handlers.TryGetValue(request.Actions[0].Name, out var handler)
? handler.Handle(request)
: Task.FromResult<MessageResponse>(null);

public void SetHandler(string actionName, IInteractiveMessageHandler handler) => _handlers[actionName] = handler;
}
}

0 comments on commit ec81700

Please sign in to comment.