public static void Run(
            [QueueTrigger(MessageQueues.AvContent)] ContentEncodedMessage contentMessage,
            [DocumentDB(nameof(Content), nameof(AvContent), Id = "{documentId}")] AvContent avContent,
            [Queue(MessageQueues.DocumentUpdate)] out DocumentUpdatedMessage updatedMessage, TraceWriter log)
        {
            try
            {
                log.Info(contentMessage.ToString());

                FunctionExtensions.HasValueOrThrow(avContent?.Id, "avContent", $"Unable to find record with Id: {contentMessage?.DocumentId}");

                FunctionExtensions.HasValueOrThrow(contentMessage?.RemoteAssetUri, "RemoteAssetUri");

                avContent.RemoteAssetUri = contentMessage.RemoteAssetUri;

                updatedMessage = new DocumentUpdatedMessage(contentMessage.DocumentId, contentMessage.CollectionId, contentMessage.NotificationTags)
                {
                    Title   = $"New {avContent.ContentType}!",
                    Message = avContent.DisplayName
                };
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw;
            }
        }
Example #2
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;
            }
        }
Example #3
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;
            }
        }