2014-05-31 11:01:43 -06:00
|
|
|
package controllers
|
|
|
|
|
2014-06-29 04:51:53 -06:00
|
|
|
import io.prismic.Document
|
2019-11-25 07:50:47 -07:00
|
|
|
import play.api.mvc._
|
2014-05-31 11:01:43 -06:00
|
|
|
|
2019-11-25 07:50:47 -07:00
|
|
|
import lila.api.Context
|
2014-05-31 11:01:43 -06:00
|
|
|
import lila.app._
|
2019-11-25 07:50:47 -07:00
|
|
|
import lila.blog.BlogApi
|
2019-12-04 16:39:16 -07:00
|
|
|
import lila.common.config.MaxPerPage
|
2014-05-31 11:01:43 -06:00
|
|
|
|
2019-12-04 16:39:16 -07:00
|
|
|
final class Blog(
|
|
|
|
env: Env,
|
|
|
|
prismicC: Prismic
|
|
|
|
)(implicit ws: play.api.libs.ws.WSClient) extends LilaController(env) {
|
2014-05-31 11:01:43 -06:00
|
|
|
|
2019-12-04 16:39:16 -07:00
|
|
|
import prismicC._
|
2014-05-31 13:03:04 -06:00
|
|
|
|
2019-12-04 16:39:16 -07:00
|
|
|
private def blogApi = env.blog.api
|
2014-06-29 04:51:53 -06:00
|
|
|
|
2019-11-25 07:50:47 -07:00
|
|
|
def index(page: Int, ref: Option[String]) = WithPrismic { implicit ctx => implicit prismic =>
|
2018-03-27 18:11:49 -06:00
|
|
|
pageHit
|
2019-12-04 16:39:16 -07:00
|
|
|
blogApi.recent(prismic, page, MaxPerPage(12)) flatMap {
|
2019-11-25 07:50:47 -07:00
|
|
|
case Some(response) => fuccess(Ok(views.html.blog.index(response)))
|
|
|
|
case _ => notFound
|
2014-05-31 13:03:04 -06:00
|
|
|
}
|
2014-05-31 11:01:43 -06:00
|
|
|
}
|
|
|
|
|
2019-11-25 07:50:47 -07:00
|
|
|
def show(id: String, slug: String, ref: Option[String]) = WithPrismic { implicit ctx => implicit prismic =>
|
2018-03-27 18:11:49 -06:00
|
|
|
pageHit
|
2019-11-25 07:50:47 -07:00
|
|
|
blogApi.one(prismic, id) flatMap { maybeDocument =>
|
|
|
|
checkSlug(maybeDocument, slug) {
|
|
|
|
case Left(newSlug) => MovedPermanently(routes.Blog.show(id, newSlug, ref).url)
|
|
|
|
case Right(doc) => Ok(views.html.blog.show(doc))
|
2014-05-31 11:01:43 -06:00
|
|
|
}
|
2019-11-25 07:50:47 -07:00
|
|
|
} recoverWith {
|
|
|
|
case e: RuntimeException if e.getMessage contains "Not Found" => notFound
|
2014-05-31 13:03:04 -06:00
|
|
|
}
|
2014-05-31 11:01:43 -06:00
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:16 -07:00
|
|
|
def preview(token: String) = WithPrismic { _ => implicit prismic =>
|
2019-11-25 07:50:47 -07:00
|
|
|
prismic.api.previewSession(token, prismic.linkResolver, routes.Lobby.home.url) map { redirectUrl =>
|
|
|
|
Redirect(redirectUrl)
|
|
|
|
.withCookies(Cookie(io.prismic.Prismic.previewCookie, token, path = "/", maxAge = Some(30 * 60 * 1000), httpOnly = false))
|
2018-11-16 02:49:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 08:31:09 -06:00
|
|
|
def atom = Action.async { implicit req =>
|
2018-11-16 02:49:47 -07:00
|
|
|
blogApi context req flatMap { implicit prismic =>
|
2019-12-04 16:39:16 -07:00
|
|
|
blogApi.recent(prismic.api, none, 1, MaxPerPage(50)) map {
|
2018-03-14 08:31:09 -06:00
|
|
|
_ ?? { docs =>
|
2019-04-22 03:42:25 -06:00
|
|
|
Ok(views.html.blog.atom(docs)) as XML
|
2018-03-14 08:31:09 -06:00
|
|
|
}
|
2014-07-09 17:56:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 07:50:47 -07:00
|
|
|
def all = WithPrismic { implicit ctx => implicit prismic =>
|
|
|
|
blogApi.byYear(prismic, lila.blog.thisYear) map { posts =>
|
2019-11-25 08:53:20 -07:00
|
|
|
Ok(views.html.blog.index.byYear(lila.blog.thisYear, posts))
|
2019-11-25 07:50:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def year(year: Int) = WithPrismic { implicit ctx => implicit prismic =>
|
|
|
|
if (lila.blog.allYears contains year)
|
|
|
|
blogApi.byYear(prismic, year) map { posts =>
|
2019-11-25 08:53:20 -07:00
|
|
|
Ok(views.html.blog.index.byYear(year, posts))
|
2019-11-25 07:50:47 -07:00
|
|
|
}
|
|
|
|
else notFound
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:16 -07:00
|
|
|
def discuss(id: String) = WithPrismic { _ => implicit prismic =>
|
2018-03-10 21:27:44 -07:00
|
|
|
val categSlug = "general-chess-discussion"
|
|
|
|
val topicSlug = s"blog-$id"
|
|
|
|
val redirect = Redirect(routes.ForumTopic.show(categSlug, topicSlug))
|
2019-12-04 16:39:16 -07:00
|
|
|
env.forum.topicRepo.existsByTree(categSlug, topicSlug) flatMap {
|
2018-03-10 21:27:44 -07:00
|
|
|
case true => fuccess(redirect)
|
2019-11-25 07:50:47 -07:00
|
|
|
case _ => blogApi.one(prismic.api, none, id) flatMap {
|
|
|
|
_ ?? { doc =>
|
2019-12-04 16:39:16 -07:00
|
|
|
env.forum.categRepo.bySlug(categSlug) flatMap {
|
2019-11-25 07:50:47 -07:00
|
|
|
_ ?? { categ =>
|
2019-12-04 16:39:16 -07:00
|
|
|
env.forum.topicApi.makeBlogDiscuss(
|
2019-11-25 07:50:47 -07:00
|
|
|
categ = categ,
|
|
|
|
slug = topicSlug,
|
|
|
|
name = doc.getText("blog.title") | "New blog post",
|
2019-12-04 16:39:16 -07:00
|
|
|
url = s"${env.net.baseUrl}${routes.Blog.show(doc.id, doc.slug)}"
|
2019-11-25 07:50:47 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
} inject redirect
|
2018-03-10 21:27:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 07:50:47 -07:00
|
|
|
private def WithPrismic(f: Context => BlogApi.Context => Fu[Result]): Action[Unit] = Open { ctx =>
|
|
|
|
blogApi context ctx.req flatMap { prismic =>
|
|
|
|
f(ctx)(prismic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 11:01:43 -06:00
|
|
|
// -- Helper: Check if the slug is valid and redirect to the most recent version id needed
|
2014-06-01 17:01:39 -06:00
|
|
|
private def checkSlug(document: Option[Document], slug: String)(callback: Either[String, Document] => Result)(implicit ctx: lila.api.Context) =
|
2014-05-31 11:01:43 -06:00
|
|
|
document.collect {
|
2017-02-14 08:34:07 -07:00
|
|
|
case document if document.slug == slug => fuccess(callback(Right(document)))
|
2014-05-31 11:01:43 -06:00
|
|
|
case document if document.slugs.contains(slug) => fuccess(callback(Left(document.slug)))
|
|
|
|
} getOrElse notFound
|
|
|
|
}
|