Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="cdmdotnet Limited">
4 : // // Copyright cdmdotnet Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using cdmdotnet.Logging;
11 : using Cqrs.Authentication;
12 : using Cqrs.Events;
13 : using Cqrs.WebApi.SignalR.Hubs;
14 :
15 : namespace Cqrs.WebApi.Events.Handlers
16 : {
17 : public abstract class EventToHubProxy<TAuthenticationToken>
18 0 : {
19 0 : protected EventToHubProxy(ILogger logger, INotificationHub notificationHub, IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper)
20 : {
21 : Logger = logger;
22 : NotificationHub = notificationHub;
23 : AuthenticationTokenHelper = authenticationTokenHelper;
24 : }
25 :
26 : protected ILogger Logger { get; private set; }
27 :
28 : protected INotificationHub NotificationHub { get; private set; }
29 :
30 : protected IAuthenticationTokenHelper<TAuthenticationToken> AuthenticationTokenHelper { get; private set; }
31 :
32 0 : protected virtual void HandleGenericEvent(IEvent<TAuthenticationToken> message)
33 : {
34 : Type eventType = message.GetType();
35 : var notifyCallerEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyCallerEventAttribute)) as NotifyCallerEventAttribute;
36 : var notifyEveryoneEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyEveryoneEventAttribute)) as NotifyEveryoneEventAttribute;
37 : var notifyEveryoneExceptCallerEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyEveryoneExceptCallerEventAttribute)) as NotifyEveryoneExceptCallerEventAttribute;
38 :
39 : string userToken = ((object)AuthenticationTokenHelper.GetAuthenticationToken() ?? string.Empty).ToString().Replace(".", string.Empty);
40 :
41 : if (notifyCallerEventAttribute != null)
42 : {
43 : NotificationHub.SendUserEvent(message, userToken);
44 : }
45 : if (notifyEveryoneEventAttribute != null)
46 : {
47 : NotificationHub.SendAllUsersEvent(message);
48 : }
49 : if (notifyEveryoneExceptCallerEventAttribute != null)
50 : {
51 : NotificationHub.SendExceptThisUserEvent(message, userToken);
52 : }
53 : }
54 : }
55 : }
|