2014-05-31 13:03:04 -06:00
|
|
|
package lila.blog
|
|
|
|
|
|
|
|
import io.prismic._
|
2018-11-16 02:49:47 -07:00
|
|
|
import play.api.mvc.RequestHeader
|
2020-08-07 08:22:26 -06:00
|
|
|
import play.api.libs.ws.StandaloneWSClient
|
2014-05-31 13:03:04 -06:00
|
|
|
|
2019-12-01 19:04:35 -07:00
|
|
|
import lila.common.config.MaxPerPage
|
2018-03-14 08:31:09 -06:00
|
|
|
import lila.common.paginator._
|
2021-11-19 00:42:40 -07:00
|
|
|
import scala.util.Try
|
2018-03-14 08:31:09 -06:00
|
|
|
|
2017-01-26 15:22:12 -07:00
|
|
|
final class BlogApi(
|
2019-12-19 20:34:38 -07:00
|
|
|
config: BlogConfig
|
2020-08-07 08:22:26 -06:00
|
|
|
)(implicit ec: scala.concurrent.ExecutionContext, ws: StandaloneWSClient) {
|
2014-05-31 13:03:04 -06:00
|
|
|
|
2021-09-18 07:22:59 -06:00
|
|
|
import BlogApi.looksLikePrismicId
|
|
|
|
|
2019-12-19 20:34:38 -07:00
|
|
|
private def collection = config.collection
|
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
def recent(
|
|
|
|
api: Api,
|
|
|
|
page: Int,
|
|
|
|
maxPerPage: MaxPerPage,
|
|
|
|
ref: Option[String]
|
2021-11-19 00:42:40 -07:00
|
|
|
): Fu[Option[Paginator[Document]]] = Try {
|
2019-12-13 07:30:20 -07:00
|
|
|
api
|
|
|
|
.forms(collection)
|
|
|
|
.ref(ref | api.master.ref)
|
2015-09-01 03:47:29 -06:00
|
|
|
.orderings(s"[my.$collection.date desc]")
|
2019-12-13 07:30:20 -07:00
|
|
|
.pageSize(maxPerPage.value)
|
|
|
|
.page(page)
|
|
|
|
.submit()
|
2020-08-16 07:24:06 -06:00
|
|
|
.fold(_ => none, some)
|
2019-12-19 20:34:38 -07:00
|
|
|
.dmap2 { PrismicPaginator(_, page, maxPerPage) }
|
2021-11-19 00:42:40 -07:00
|
|
|
} recover { case _: NoSuchElementException =>
|
|
|
|
fuccess(none)
|
|
|
|
} get
|
2019-12-21 12:34:42 -07:00
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
def recent(
|
|
|
|
prismic: BlogApi.Context,
|
|
|
|
page: Int,
|
2020-02-24 17:29:15 -07:00
|
|
|
maxPerPage: MaxPerPage
|
2019-12-13 07:30:20 -07:00
|
|
|
): Fu[Option[Paginator[Document]]] =
|
2020-02-24 17:29:15 -07:00
|
|
|
recent(prismic.api, page, maxPerPage, prismic.ref.some)
|
2014-05-31 13:03:04 -06:00
|
|
|
|
2018-11-16 02:49:47 -07:00
|
|
|
def one(api: Api, ref: Option[String], id: String): Fu[Option[Document]] =
|
2021-09-18 07:22:59 -06:00
|
|
|
looksLikePrismicId(id) ?? api
|
2019-12-13 07:30:20 -07:00
|
|
|
.forms(collection)
|
2015-09-01 03:47:29 -06:00
|
|
|
.query(s"""[[:d = at(document.id, "$id")]]""")
|
2019-12-13 07:30:20 -07:00
|
|
|
.ref(ref | api.master.ref)
|
2021-06-03 01:24:15 -06:00
|
|
|
.submit()
|
|
|
|
.map(_.results.headOption)
|
2014-05-31 13:03:04 -06:00
|
|
|
|
2018-11-16 02:49:47 -07:00
|
|
|
def one(prismic: BlogApi.Context, id: String): Fu[Option[Document]] = one(prismic.api, prismic.ref.some, id)
|
|
|
|
|
2019-11-25 07:50:47 -07:00
|
|
|
def byYear(prismic: BlogApi.Context, year: Int): Fu[List[MiniPost]] = {
|
2019-12-13 07:30:20 -07:00
|
|
|
prismic.api
|
|
|
|
.forms(collection)
|
|
|
|
.ref(prismic.ref)
|
2019-11-25 07:50:47 -07:00
|
|
|
.query(s"[[date.year(my.$collection.date, $year)]]")
|
|
|
|
.orderings(s"[my.$collection.date desc]")
|
|
|
|
.pageSize(100) // prismic max
|
2019-12-13 07:30:20 -07:00
|
|
|
.submit()
|
|
|
|
.fold(_ => Nil, _.results flatMap MiniPost.fromDocument(collection, "wide"))
|
2019-11-25 07:50:47 -07:00
|
|
|
}
|
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
def context(
|
|
|
|
req: RequestHeader
|
|
|
|
)(implicit linkResolver: (Api, Option[String]) => DocumentLinkResolver): Fu[BlogApi.Context] = {
|
2014-05-31 13:03:04 -06:00
|
|
|
prismicApi map { api =>
|
2018-11-16 02:49:47 -07:00
|
|
|
val ref = resolveRef(api) {
|
2019-12-13 07:30:20 -07:00
|
|
|
req.cookies
|
|
|
|
.get(Prismic.previewCookie)
|
|
|
|
.map(_.value)
|
2018-11-16 02:49:47 -07:00
|
|
|
.orElse(req.queryString get "ref" flatMap (_.headOption) filter (_.nonEmpty))
|
|
|
|
}
|
2018-11-16 02:55:49 -07:00
|
|
|
BlogApi.Context(api, ref | api.master.ref, linkResolver(api, ref))
|
2016-03-23 21:51:51 -06:00
|
|
|
}
|
2018-11-16 02:49:47 -07:00
|
|
|
}
|
2016-03-23 21:51:51 -06:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def masterContext(implicit
|
|
|
|
linkResolver: (Api, Option[String]) => DocumentLinkResolver
|
2020-01-01 08:29:19 -07:00
|
|
|
): Fu[BlogApi.Context] = {
|
|
|
|
prismicApi map { api =>
|
|
|
|
BlogApi.Context(api, api.master.ref, linkResolver(api, none))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 09:42:23 -06:00
|
|
|
def all(page: Int = 1)(implicit prismic: BlogApi.Context): Fu[List[Document]] =
|
|
|
|
recent(prismic.api, page, MaxPerPage(50), none) flatMap { res =>
|
|
|
|
val docs = res.??(_.currentPageResults).toList
|
|
|
|
(docs.nonEmpty ?? all(page + 1)) map (docs ::: _)
|
|
|
|
}
|
|
|
|
|
2016-03-23 21:51:51 -06:00
|
|
|
private def resolveRef(api: Api)(ref: Option[String]) =
|
2018-11-16 02:49:47 -07:00
|
|
|
ref.map(_.trim).filterNot(_.isEmpty) map { reqRef =>
|
2016-03-23 21:51:51 -06:00
|
|
|
api.refs.values.collectFirst {
|
|
|
|
case r if r.label == reqRef => r.ref
|
2018-11-16 02:49:47 -07:00
|
|
|
} getOrElse reqRef
|
2014-05-31 13:03:04 -06:00
|
|
|
}
|
|
|
|
|
2021-11-19 00:42:40 -07:00
|
|
|
def prismicApi = (new Prismic).get(config.apiUrl)
|
2014-05-31 13:03:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
object BlogApi {
|
|
|
|
|
2018-11-16 04:27:28 -07:00
|
|
|
def extract(body: Fragment.StructuredText): String =
|
2014-05-31 14:50:04 -06:00
|
|
|
body.blocks
|
|
|
|
.takeWhile(_.isInstanceOf[Fragment.StructuredText.Block.Paragraph])
|
2019-12-13 07:30:20 -07:00
|
|
|
.take(2)
|
|
|
|
.map {
|
2018-11-16 04:27:28 -07:00
|
|
|
case Fragment.StructuredText.Block.Paragraph(text, _, _) => s"<p>$text</p>"
|
2019-12-13 07:30:20 -07:00
|
|
|
case _ => ""
|
|
|
|
}
|
|
|
|
.mkString
|
2014-05-31 14:50:04 -06:00
|
|
|
|
2014-05-31 13:03:04 -06:00
|
|
|
case class Context(api: Api, ref: String, linkResolver: DocumentLinkResolver) {
|
|
|
|
def maybeRef = Option(ref).filterNot(_ == api.master.ref)
|
|
|
|
}
|
2021-09-18 07:22:59 -06:00
|
|
|
|
|
|
|
private val idRegex = """^[\w-]{16}$""".r
|
|
|
|
|
|
|
|
def looksLikePrismicId(id: String) = idRegex.matches(id)
|
2014-05-31 13:03:04 -06:00
|
|
|
}
|