示例#1
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, Routes.Post, Route = Routes.PublishContent)] DocumentUpdatedMessage updateMessage,
            [NotificationHub(ConnectionStringSetting = EnvironmentVariables.AzureWebJobsNotificationHubsConnectionString, /*Platform = NotificationPlatform.Apns,*/ TagExpression = "{NotificationTags}")] IAsyncCollector <Notification> notification,
            TraceWriter log)
        {
            log.Info(updateMessage?.ToString());

            UserStore userStore = null;

            var userId = Thread.CurrentPrincipal.GetClaimsIdentity()?.UniqueIdentifier();

            if (!string.IsNullOrEmpty(userId))
            {
                log.Info($"User is authenticated and has userId: {userId}");

                userStore = await DocumentClient.GetUserStore(userId, log);
            }


            if (!userStore?.UserRole.CanWrite() ?? false)
            {
                log.Info("Not authenticated");

                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            try
            {
                FunctionExtensions.HasValueOrThrow(updateMessage?.CollectionId, DocumentUpdatedMessage.CollectionIdKey);

                var template = PushTemplate.FromMessage(updateMessage);

                await notification.AddAsync(new TemplateNotification (template.GetProperties()));

                throw new HttpResponseException(HttpStatusCode.Accepted);
            }
            catch (HttpResponseException response)
            {
                return(response.Response);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw;
            }
        }
示例#2
0
        public static async Task Run(
            [QueueTrigger(MessageQueues.DocumentUpdate, Connection = EnvironmentVariables.AzureWebJobsStorage)] DocumentUpdatedMessage updateMessage,
            [NotificationHub(ConnectionStringSetting = EnvironmentVariables.AzureWebJobsNotificationHubsConnectionString, TagExpression = "{NotificationTags}")] IAsyncCollector <Notification> notification,
            TraceWriter log)
        {
            try
            {
                log.Info(updateMessage?.ToString());

                FunctionExtensions.HasValueOrThrow(updateMessage?.CollectionId, DocumentUpdatedMessage.CollectionIdKey);

                var template = PushTemplate.FromMessage(updateMessage);

                await notification.AddAsync(new TemplateNotification (template.GetProperties()));
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw;
            }
        }