Skip to content

Commit

Permalink
MessageAction backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Oxtoby committed May 16, 2020
1 parent 557bb17 commit c006800
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
13 changes: 13 additions & 0 deletions SlackNet.AspNetCore/MessageActionAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading.Tasks;
using SlackNet.Interaction;

namespace SlackNet.AspNetCore
{
class MessageActionAdapter : IMessageShortcutHandler
{
private readonly IMessageActionHandler _actionHandler;
public MessageActionAdapter(IMessageActionHandler actionHandler) => _actionHandler = actionHandler;

public Task Handle(MessageShortcut request) => _actionHandler.Handle(request);
}
}
24 changes: 24 additions & 0 deletions SlackNet.AspNetCore/SlackServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ public SlackServiceConfiguration RegisterMessageShortcutHandler(string callbackI
public SlackServiceConfiguration RegisterMessageShortcutHandler(string callbackId, Func<IServiceProvider, IMessageShortcutHandler> handlerFactory) =>
RegisterCompositeItem<IAsyncMessageShortcutHandler>(p => new SpecificMessageShortcutHandler(callbackId, new ResolvedMessageShortcutHandler(p, s => new MessageShortcutHandlerAsyncWrapper(handlerFactory(s)))));

[Obsolete("Use RegisterMessageShortcutHandler instead")]
public SlackServiceConfiguration RegisterMessageActionHandler<THandler>()
where THandler : class, IMessageActionHandler
{
_serviceCollection.TryAddScoped<THandler>();
return RegisterCompositeItem<IAsyncMessageShortcutHandler>(p => new ResolvedMessageShortcutHandler(p, s => new MessageShortcutHandlerAsyncWrapper(new MessageActionAdapter(s.GetRequiredService<THandler>()))));
}

[Obsolete("Use RegisterMessageShortcutHandler instead")]
public SlackServiceConfiguration RegisterMessageActionHandler(IMessageActionHandler handler) =>
RegisterCompositeItem<IAsyncMessageShortcutHandler>(c => new MessageShortcutHandlerAsyncWrapper(new MessageActionAdapter(handler)));

[Obsolete("Use RegisterMessageShortcutHandler instead")]
public SlackServiceConfiguration RegisterMessageActionHandler(Func<IServiceProvider, IMessageActionHandler> handlerFactory) =>
RegisterCompositeItem<IAsyncMessageShortcutHandler>(p => new ResolvedMessageShortcutHandler(p, s => new MessageShortcutHandlerAsyncWrapper(new MessageActionAdapter(handlerFactory(s)))));

[Obsolete("Use RegisterMessageShortcutHandler instead")]
public SlackServiceConfiguration RegisterMessageActionHandler<THandler>(string callbackId)
where THandler : class, IMessageActionHandler
{
_serviceCollection.TryAddScoped<THandler>();
return RegisterCompositeItem<IAsyncMessageShortcutHandler>(p => new SpecificMessageShortcutHandler(callbackId, new ResolvedMessageShortcutHandler(p, s => new MessageShortcutHandlerAsyncWrapper(new MessageActionAdapter(s.GetRequiredService<THandler>())))));
}

#endregion Message shortcuts

#region Global shortcuts
Expand Down
4 changes: 4 additions & 0 deletions SlackNet.Tests/ServiceConfigurationLintTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public void StandardRegistrationMethods()

ExpectReplaceMethod(publicMethods, nameof(SSC.ReplaceLegacyDialogSubmissionHandling), typeof(IDialogSubmissionHandler));
ExpectMethod(publicMethods, nameof(SSC.RegisterDialogSubmissionHandler), new[] { typeof(IDialogSubmissionHandler) }, new[] { typeof(string) });

// Backwards compatibility
ExpectRegistrationTriplet(publicMethods, nameof(SSC.RegisterMessageActionHandler), typeof(IMessageActionHandler), new Type[0], new Type[0]);
ExpectMethod(publicMethods, nameof(SSC.RegisterMessageActionHandler), new[] { typeof(IMessageActionHandler) }, new[] { typeof(string) });

// Experimental async API
ExpectReplaceMethod(publicMethods, nameof(SSC.ReplaceAsyncBlockActionHandling), typeof(IAsyncBlockActionHandler));
Expand Down
9 changes: 8 additions & 1 deletion SlackNet/Interaction/IMessageShortcutHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;

namespace SlackNet.Interaction
{
public interface IMessageShortcutHandler
{
Task Handle(MessageShortcut request);
}

[Obsolete("Use IMessageShortcutHandler instead")]
public interface IMessageActionHandler
{
Task Handle(MessageAction request);
}
}
10 changes: 7 additions & 3 deletions SlackNet/Interaction/MessageShortcut.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using SlackNet.Events;
using System;
using SlackNet.Events;

namespace SlackNet.Interaction
{
[SlackType("message_action")]
public class MessageShortcut : InteractionRequest
[Obsolete("Use MessageShortcut instead")]
public class MessageAction : InteractionRequest
{
public string CallbackId { get; set; }
public string TriggerId { get; set; }
public MessageEvent Message { get; set; }
}

[SlackType("message_action")]
public class MessageShortcut : MessageAction { }
}

0 comments on commit c006800

Please sign in to comment.