Пример #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WebHookWorkItem"/> class.
 /// </summary>
 public WebHookWorkItem(IWebHookNotification notification, IWebHook webHook)
 {
     Notification = notification ?? throw new ArgumentNullException(nameof(notification));
     WebHook      = webHook ?? throw new ArgumentNullException(nameof(webHook));
     Id           = Guid.NewGuid();
     Timestamp    = DateTime.UtcNow;
 }
Пример #2
0
        public async Task NotifyAsync(IWebHookNotification notification, CancellationToken cancellationToken = default)
        {
            if (notification == null)
            {
                throw new ArgumentNullException(nameof(notification));
            }

            await _webHooksQueue.QueueWebHookAsync(notification);
        }
Пример #3
0
        public Task NotifyAsync(IWebHookNotification notification, CancellationToken cancellationToken = default)
        {
            if (notification == null)
            {
                throw new ArgumentNullException(nameof(notification));
            }

            _webHooksQueue.QueueWebHook(notification);
            return(Task.CompletedTask);
        }
Пример #4
0
        /// <inheritdoc />
        protected override async Task <Guid> LogAsync(IWebHookNotification notification, IReadOnlyList <IWebHook> webHooks, CancellationToken cancellationToken)
        {
            var notif = new WebHookNotification
            {
                Payload   = notification.Payload,
                TriggerId = notification.TriggerId,
                Count     = webHooks.Count
            };
            await _context.SaveAsync(notif);

            return(notif.Id);
        }
Пример #5
0
        /// <summary>
        /// Apply any filter that could not be done in the query, such as parameters matching on payload
        /// </summary>
        /// <param name="webHook"></param>
        /// <param name="notification"></param>
        /// <returns></returns>
        protected virtual bool FiltersMatch(WebHook webHook, IWebHookNotification notification)
        {
            if (webHook.Filters == null || webHook.Filters.Count == 0)
            {
                return(true);
            }

            return(webHook.Filters.Any(f => f.Parameters == null ||
                                       f.Parameters.Count == 0 ||
                                       f.Parameters.All(kvp => notification.Payload.ContainsKey(kvp.Key) &&
                                                        WebHookFilterMatches(notification.Payload[kvp.Key], kvp.Value))));
        }
Пример #6
0
        /// <inheritdoc />
        protected override async Task <Guid> LogAsync(IWebHookNotification notification, IReadOnlyList <IWebHook> webHooks, CancellationToken cancellationToken)
        {
            var notif = new Registrations.EFStorage.WebHookNotification
            {
                Payload   = notification.Payload,
                TriggerId = notification.TriggerId,
                Count     = webHooks.Count
            };

            _context.Add(notif);

            await _context.SaveChangesAsync();

            return(notif.Id);
        }
Пример #7
0
        /// <inheritdoc />
        public async Task <IReadOnlyList <IWebHook> > GetApplicableWebHooksAsync(IWebHookNotification notification,
                                                                                 CancellationToken cancellationToken = default)
        {
            if (notification == null)
            {
                throw new ArgumentNullException(nameof(notification));
            }

            var filterDefinition = Builders <WebHook> .Filter.Where(w => !w.IsPaused && w.Filters.Count == 0 || w.Filters.Any(f => f.Trigger == notification.TriggerId));

            var webHooks = await _context.Collection.Find(filterDefinition).ToListAsync(cancellationToken);

            foreach (var webHook in webHooks)
            {
                webHook.Secret = _secretProtector.Unprotect(webHook.ProtectedSecret);
            }
            return(webHooks);
        }
Пример #8
0
        /// <inheritdoc />
        public async Task <IReadOnlyList <IWebHook> > GetApplicableWebHooksAsync(IWebHookNotification notification, CancellationToken cancellationToken = default)
        {
            if (notification == null)
            {
                throw new ArgumentNullException(nameof(notification));
            }

            var webHooks = await FilterQuery(_context.WebHooks.AsNoTracking()
                                             .Where(w => !w.IsPaused)
                                             .Include(w => w.Filters), notification)
                           .ToListAsync(cancellationToken);

            foreach (var webHook in webHooks)
            {
                webHook.Secret = _secretProtector.Unprotect(webHook.ProtectedSecret);
            }
            return(webHooks);
        }
Пример #9
0
        /// <inheritdoc />
        public bool Matches(IWebHook webHook, IWebHookNotification notification)
        {
            if (webHook == null)
            {
                throw new ArgumentNullException(nameof(webHook));
            }

            if (notification == null)
            {
                throw new ArgumentNullException(nameof(notification));
            }

            if (webHook.Filters == null || webHook.Filters.Count == 0)
            {
                return(true);
            }

            return(webHook.Filters
                   .Where(f => IsTriggerMatching(f.Trigger, notification.TriggerId))
                   .OrderBy(f => f.Parameters == null ? 0 : f.Parameters.Count)
                   .Any(f => f.Parameters == null || f.Parameters.Count == 0 || f.Parameters.All(kvp => IsPayloadMatchingParameter(kvp.Key, kvp.Value, notification.Payload))));
        }
Пример #10
0
 /// <summary>
 /// Apply the SQL filter matching the current notification
 /// </summary>
 /// <param name="query"></param>
 /// <param name="notification"></param>
 /// <returns></returns>
 protected virtual IQueryable <WebHook> FilterQuery(IQueryable <WebHook> query, IWebHookNotification notification)
 {
     return(query.Where(w => w.Filters == null || w.Filters.Count == 0 || w.Filters.Any(f => f.Trigger == notification.TriggerId)));
 }
Пример #11
0
        public void GetWorkItems_FilterSingleNotification(IEnumerable <WebHook> webHooks, IWebHookNotification notification)
        {
            // Act
            IEnumerable <WebHookWorkItem> actual = WebHookManager.GetWorkItems(webHooks.ToArray(), new[] { notification });

            // Assert
            Assert.Equal(webHooks.Count(), actual.Count());
            foreach (WebHookWorkItem workItem in actual)
            {
                Assert.Same(workItem.Notifications.Single(), notification);
            }
        }
Пример #12
0
        /// <summary>
        /// Submits a notification to all matching registered WebHooks. To match, the <see cref="WebHook"/> must be registered by the
        /// current <see cref="Controller.User"/> and have a filter that matches one or more of the actions provided for the notification.
        /// </summary>
        /// <param name="controller">The MVC <see cref="Controller"/> instance.</param>
        /// <param name="action">The action describing the notification.</param>
        /// <param name="data">Optional additional data to include in the WebHook request.</param>
        /// <param name="predicate">A function to test each <see cref="WebHook"/> to see whether it fulfills the condition. The
        /// predicate is passed the <see cref="WebHook"/> and the user who registered it. If the predicate returns <c>true</c> then
        /// the <see cref="WebHook"/> is included; otherwise it is not.</param>
        /// <returns>The number of <see cref="WebHook"/> instances that were selected and subsequently notified about the actions.</returns>
        public static Task <int> NotifyAsync(this Controller controller, string action, object data, Func <WebHook, string, bool> predicate)
        {
            var notifications = new IWebHookNotification[] { new WebHookDataNotification(action, data) };

            return(NotifyAsync(controller, notifications, predicate));
        }
Пример #13
0
 /// <inheritdoc />
 Task IWebHookService.NotifyAsync(IWebHookNotification notification, CancellationToken cancellationToken)
 => PublishAsync(notification, cancellationToken);