public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) { var isAuthorized = await Authenticator.AuthenticateRequestForScopeAndRole(req, "CMS.Articles.Edit", "Articles.Write", log); if (!isAuthorized) { return(new UnauthorizedResult()); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); log.LogInformation($"Publish article called with data: {requestBody}"); Article articleToPublish = JsonConvert.DeserializeObject <Article>(requestBody); if (articleToPublish == null) { log.LogError($"Invalid article format: {requestBody}"); return(new BadRequestObjectResult("Invalid article format.")); } if (string.IsNullOrEmpty(articleToPublish.Id)) { log.LogError($"PublishArticle called without article ID."); return(new BadRequestObjectResult("Article ID is required.")); } articleToPublish.PublicationDate = DateTime.Now; articleToPublish.IsPublished = true; try { await CmsDb.UpdateArticleAsync(articleToPublish.Id, articleToPublish); } catch (Exception ex) { log.LogError($"Error publishing article: {ex.Message}"); return(new InternalServerErrorResult()); } log.LogInformation($"Successfully published article with id {articleToPublish.Id}"); return(new OkObjectResult(articleToPublish)); }
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; } }