Example #1
0
        /// <summary>
        /// Submits a notification to all matching registered WebHooks. To match, the <see cref="WebHook"/> must be registered by the
        /// current <see cref="ControllerBase.User"/> and have a filter that matches one or more of the actions provided for the notification.
        /// </summary>
        /// <param name="controller">The <see cref="ControllerBase"/> instance.</param>
        /// <param name="notifications">The set of notifications to include in the WebHook.</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 async Task <int> NotifyAsync(this ControllerBase controller, IEnumerable <NotificationDictionary> notifications, Func <WebHook, string, bool> predicate)
        {
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }
            if (notifications == null)
            {
                throw new ArgumentNullException(nameof(notifications));
            }
            if (!notifications.Any())
            {
                return(0);
            }


            // Get the User ID from the User principal
            IWebHookUser user   = controller.HttpContext.RequestServices.GetUser();
            string       userId = await user.GetUserIdAsync(controller.User);

            // Send a notification to registered WebHooks with matching filters
            IWebHookManager manager = controller.HttpContext.RequestServices.GetManager();

            return(await manager.NotifyAsync(userId, notifications, predicate));
        }
        /// <summary>
        /// Gets the user ID for this request.
        /// </summary>
        private async Task <string> GetUserId()
        {
            try
            {
                string id = await _user.GetUserIdAsync(User);

                return(id);
            }
            catch (Exception ex)
            {
                HttpResponseMessage error = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message, ex);
                throw new HttpResponseException(error);
            }
        }
        /// <summary>
        /// Gets the user ID for this request.
        /// </summary>
        protected virtual async Task <string> GetUserId()
        {
            try
            {
                IPrincipal user = new ClaimsPrincipal(WindowsIdentity.GetCurrent());
                string     id   = await _user.GetUserIdAsync(user);

                return(id);
            }
            catch (Exception ex)
            {
                HttpResponseMessage error = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message, ex);
                throw new HttpResponseException(error);
            }
        }
        /// <inheritdoc />
        public virtual async Task <IEnumerable <WebHook> > GetWebHooksAsync(IPrincipal user, Func <string, WebHook, Task> predicate)
        {
            string userId = await _userManager.GetUserIdAsync(user);

            IEnumerable <WebHook> webHooks = await _store.GetAllWebHooksAsync(userId);

            await ApplyServiceSideFilterPredicate(userId, webHooks, predicate);

            return(webHooks);
        }
        /// <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="notifications">The set of notifications to include in the WebHook.</param>
        /// <returns>The number of <see cref="WebHook"/> instances that were selected and subsequently notified about the actions.</returns>
        public static async Task <int> NotifyAsync(this Controller controller, params NotificationDictionary[] notifications)
        {
            if (controller == null)
            {
                throw new ArgumentNullException("controller");
            }
            if (notifications == null)
            {
                throw new ArgumentNullException("notifications");
            }
            if (notifications.Length == 0)
            {
                return(0);
            }

            // Get the User ID from the User principal
            IWebHookUser user   = DependencyResolver.Current.GetUser();
            string       userId = await user.GetUserIdAsync(controller.User);

            // Send a notification to registered WebHooks with matching filters
            IWebHookManager manager = DependencyResolver.Current.GetManager();

            return(await manager.NotifyAsync(userId, notifications));
        }
Example #6
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="notifications">The set of notifications to include in the WebHook.</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 async Task <int> NotifyAsync(this Controller controller, IEnumerable <NotificationDictionary> notifications, Func <WebHook, string, bool> predicate)
        {
            if (controller == null)
            {
                throw new ArgumentNullException("controller");
            }
            if (notifications == null)
            {
                throw new ArgumentNullException("notifications");
            }
            if (!notifications.Any())
            {
                return(0);
            }

            // Get the User ID from the User principal
            IWebHookUser user   = DependencyResolver.Current.GetUser();
            string       userId = await user.GetUserIdAsync(controller.User);

            // Send a notification to registered WebHooks with matching filters
            IWebHookManager manager = DependencyResolver.Current.GetManager();

            return(await manager.NotifyAsync(userId, notifications, predicate));
        }