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<TSingleSignOnToken>
18 : where TSingleSignOnToken : ISingleSignOnToken, new()
19 0 : {
20 0 : protected EventToHubProxy(ILogger logger, INotificationHub notificationHub, IAuthenticationTokenHelper<TSingleSignOnToken> authenticationTokenHelper)
21 : {
22 : Logger = logger;
23 : NotificationHub = notificationHub;
24 : AuthenticationTokenHelper = authenticationTokenHelper;
25 : }
26 :
27 : protected ILogger Logger { get; private set; }
28 :
29 : protected INotificationHub NotificationHub { get; private set; }
30 :
31 : protected IAuthenticationTokenHelper<TSingleSignOnToken> AuthenticationTokenHelper { get; private set; }
32 :
33 0 : protected virtual void HandleGenericEvent(IEvent<TSingleSignOnToken> message)
34 : {
35 : Type eventType = message.GetType();
36 : var notifyCallerEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyCallerEventAttribute)) as NotifyCallerEventAttribute;
37 : var notifyEveryoneEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyEveryoneEventAttribute)) as NotifyEveryoneEventAttribute;
38 : var notifyEveryoneExceptCallerEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyEveryoneExceptCallerEventAttribute)) as NotifyEveryoneExceptCallerEventAttribute;
39 :
40 : string userToken = (AuthenticationTokenHelper.GetAuthenticationToken().Token ?? string.Empty).Replace(".", string.Empty);
41 :
42 : if (notifyCallerEventAttribute != null)
43 : {
44 : NotificationHub.SendUserEvent(message, userToken);
45 : }
46 : if (notifyEveryoneEventAttribute != null)
47 : {
48 : NotificationHub.SendAllUsersEvent(message);
49 : }
50 : if (notifyEveryoneExceptCallerEventAttribute != null)
51 : {
52 : NotificationHub.SendExceptThisUserEvent(message, userToken);
53 : }
54 : }
55 : }
56 : }
|