public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); log.LogInformation($"Add comment to article called with data: {requestBody}"); Comment userComment = JsonConvert.DeserializeObject <Comment>(requestBody); if (userComment == null) { log.LogError($"Invalid comment format: {requestBody}"); return(new BadRequestObjectResult("Invalid comment format.")); } if (string.IsNullOrEmpty(userComment.ParentArticleId)) { log.LogError($"AddCommentToArticle called without parent article ID."); return(new BadRequestObjectResult("Parent Article ID is required.")); } var articleForComment = await CmsDb.GetArticleAsync(userComment.ParentArticleId); if (articleForComment == null) { log.LogError($"AddCommentToArticle called, but could not find article with id {userComment.ParentArticleId}"); return(new NotFoundResult()); } userComment.Id = Guid.NewGuid().ToString(); userComment.IsPublished = false; userComment.CreationDate = DateTime.Now; var connectionString = Environment.GetEnvironmentVariable("ServiceBusConnectionString"); var contentModerationQueueName = Environment.GetEnvironmentVariable("ContentModerationQueueName"); await using (var client = new ServiceBusClient(connectionString)) { var sender = client.CreateSender(contentModerationQueueName); var message = new ServiceBusMessage(JsonConvert.SerializeObject(userComment)); await sender.SendMessageAsync(message); log.LogInformation($"Sent new comment with id {userComment.Id} to moderation queue."); } return(new OkObjectResult(userComment)); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { var isAuthorized = await Authenticator.AuthenticateRequestForScopeAndRole(req, "CMS.Articles.Read", "Articles.Read", log); if (!isAuthorized) { return(new UnauthorizedResult()); } string id = req.Query["id"]; if (string.IsNullOrEmpty(id)) { log.LogError("No id provided to GetArticleById Function."); return(new BadRequestObjectResult("GetArticleById requires an id.")); } log.LogInformation($"Getting articles with id {id}"); Article article; try { article = await CmsDb.GetArticleAsync(id); } catch (Exception ex) { log.LogError($"Error loading article with id {id}: {ex.Message}"); return(new InternalServerErrorResult()); } log.LogInformation($"Successfully retrieved article with id: {id}"); return(new OkObjectResult(article)); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { string id = req.Query["id"]; if (string.IsNullOrEmpty(id)) { log.LogError("No id provided to GetPublishedArticleById Function."); return(new BadRequestObjectResult("GetPublishedArticleById requires an id.")); } log.LogInformation($"Getting published articles with id {id}"); Article article; try { article = await CmsDb.GetArticleAsync(id); } catch (Exception ex) { log.LogError($"Error loading article with id {id}: {ex.Message}"); return(new InternalServerErrorResult()); } log.LogInformation($"Successfully retrieved article with id: {id}"); if (!article.IsPublished) { log.LogError($"Article {id} exists, but is not published."); return(new NotFoundResult()); } return(new OkObjectResult(article)); }
public async Task Run([ServiceBusTrigger("contentmoderation", Connection = "ServiceBusConnectionString")] string myQueueItem, ILogger log) { log.LogInformation($"ModerateComment function processed: {myQueueItem}"); var comment = JsonConvert.DeserializeObject <Comment>(myQueueItem); if (comment == null) { log.LogError($"Could not deserialize comment: {myQueueItem}"); return; } var commentText = comment.Content; commentText = commentText.Replace(Environment.NewLine, " "); var commentTextBytes = Encoding.UTF8.GetBytes(commentText); var commentTextStream = new MemoryStream(commentTextBytes); var subscriptionKey = Environment.GetEnvironmentVariable("ContentModeratorSubscriptionKey"); var contentModeratorClient = new ContentModeratorClient(new ApiKeyServiceClientCredentials(subscriptionKey)); contentModeratorClient.Endpoint = Environment.GetEnvironmentVariable("ContentModeratorEndpoint"); using (contentModeratorClient) { var result = await contentModeratorClient.TextModeration.ScreenTextAsync( "text/plain", commentTextStream, language : "eng", autocorrect : false, pII : true, listId : null, classify : true); if (result.Classification.ReviewRecommended ?? false) { log.LogInformation($"Content was NOT approved for publication: {commentText}"); return; } else { log.LogInformation($"Publishing comment {comment.Id} on article {comment.ParentArticleId}"); } } var article = await CmsDb.GetArticleAsync(comment.ParentArticleId); if (article == null) { log.LogError($"Could not find article with id {comment.ParentArticleId}"); return; } comment.IsPublished = true; comment.PublicationDate = DateTime.Now; List <Comment> comments; if (article.Comments == null) { comments = new List <Comment>(); } else { comments = article.Comments.ToList(); } comments.Add(comment); article.Comments = comments; try { await CmsDb.UpdateArticleAsync(article.Id, article); } catch (Exception ex) { log.LogError($"Error updating article {article.Id} with comment: {ex.Message}"); return; } }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { var isAuthorized = await Authenticator.AuthenticateRequestForScopeAndRole(req, "CMS.Articles.Edit", "Articles.Write", log); if (!isAuthorized) { return(new UnauthorizedResult()); } var articleId = req.Query["articleId"]; if (string.IsNullOrEmpty(articleId)) { log.LogError("GetImageUploadSasToken called without article ID"); return(new BadRequestObjectResult("articleId required")); } var fileName = req.Query["fileName"]; if (string.IsNullOrEmpty(articleId)) { log.LogError("GetImageUploadSasToken called without file name"); return(new BadRequestObjectResult("fileName required")); } var article = await CmsDb.GetArticleAsync(articleId); if (article == null) { log.LogError($"GetImageUploadSasToken called with unknown article ID: {articleId}"); return(new NotFoundObjectResult($"Article ID {articleId} not found.")); } log.LogInformation($"Getting SAS token for image upload {fileName} to article {articleId}."); var storageAccountName = Environment.GetEnvironmentVariable("ImageStorageAccountName"); var storageAccountKey = Environment.GetEnvironmentVariable("ImageStorageAccountKey"); var imagesContainerName = Environment.GetEnvironmentVariable("ImageStorageBlobContainerName"); var filePath = $"{articleId}/{fileName}"; var blobEndpoint = $"https://{storageAccountName}.blob.core.windows.net/{imagesContainerName}/{filePath}"; var storageCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey); var blobClient = new BlobClient(new Uri(blobEndpoint), storageCredential); var sasBuilder = new BlobSasBuilder(BlobSasPermissions.Write | BlobSasPermissions.Create, DateTimeOffset.UtcNow.AddMinutes(30)); sasBuilder.BlobContainerName = blobClient.BlobContainerName; sasBuilder.BlobName = filePath; sasBuilder.Resource = "b"; sasBuilder.StartsOn = DateTimeOffset.UtcNow.AddMinutes(-5); var blobUri = blobClient.GenerateSasUri(sasBuilder); log.LogInformation($"Successfully created SAS token for image upload {fileName} to article {articleId}"); return(new OkObjectResult(blobUri.ToString())); }