in src/main/scala/managehelpcontentpublisher/PathAndContent.scala [62:123]
private def pingGoogle(): Future[HttpResponse[String]] =
Future(Http(s"${config.sitemap.googlePingUrl}?sitemap=${config.sitemap.url}").asString)
/** Publishes the contents of the given Json string to representations of an Article and multiple Topics suitable to
* be rendered by a web layer.
*
* @param publishingOps
* Operations to read and write content to and from storage accessible to the web layer.
* @param jsonString
* A Json object holding all the data needed to publish an Article and its Topics.
* @return
* List of PathAndContents published.<br /> The meaning of Path depends on the implementation of publishingOps.
*/
def publishContents(publishingOps: PublishingOps)(jsonString: String): Either[Failure, Seq[PathAndContent]] = {
def publishArticle(article: Article): Either[Failure, PathAndContent] =
for {
content <- writeArticle(article)
result <- publishingOps.storeArticle(PathAndContent(article.path, content))
} yield result
def publishTopic(topic: Topic): Either[Failure, PathAndContent] =
for {
content <- writeTopic(topic)
result <- publishingOps.storeTopic(PathAndContent(topic.path, content))
} yield result
def publishTopics(topics: Seq[Topic]): Either[Failure, Seq[PathAndContent]] =
topics.map(publishTopic).sequence
def addToSitemap(article: Article): Either[Failure, Option[PathAndContent]] =
publishingOps.fetchSitemap() flatMap { oldSitemap =>
val articleUrl = new URI(s"${config.articleUrlPrefix}/${article.path}")
if (oldSitemap.contains(articleUrl)) {
Right(None)
} else {
val newSitemap = oldSitemap + articleUrl
publishingOps
.storeSitemap(newSitemap).map(_ =>
Some(PathAndContent(config.aws.sitemapFile, newSitemap.toSeq.sorted.mkString("\n")))
).tap(_ => pingGoogle())
}
}
for {
input <- readInput(jsonString)
newArticle = Article.fromInput(input.article)
oldArticleJson <- publishingOps.fetchArticleByPath(newArticle.path)
oldArticle <- oldArticleJson.map(readArticle).sequence
publishedArticle <- publishArticle(newArticle)
topics = Topic.fromInput(input)
publishedTopics <- publishTopics(topics)
publishedMoreTopics <- publishMoreTopics(publishingOps)(oldArticle, topics)
topicsArticleRemovedFrom <- removeFromTopics(publishingOps)(
newArticle,
ArticleTopic.topicsArticleRemovedFrom(newArticle, oldArticle)
)
updatedSitemap <- addToSitemap(newArticle)
} yield Seq(
publishedArticle
) ++ publishedTopics ++ publishedMoreTopics.toSeq ++ topicsArticleRemovedFrom ++ updatedSitemap.toSeq
}