in applications/app/controllers/ImageContentController.scala [95:168]
private def isSupported(c: ApiContent) = c.isImageContent
override def canRender(i: ItemResponse): Boolean = i.content.exists(isSupported)
private lazy val dateExtractor = """.+/(\d{4})/(\w{3})/(\d{2})/.+""".r
def getNextLightboxJson(path: String, tag: String, rawDirection: String): Action[AnyContent] =
Action.async { implicit request =>
val direction = Direction.forPathSegment(rawDirection)
/** unfortunately we have to infer the date from the path, because getting the real publication date would require
* another call to the content API…
*/
val date = (path match {
case dateExtractor(rawYear, rawMonth, rawDate) => {
(Try(rawYear.toInt).toOption, rawMonth, Try(rawDate.toInt).toOption) match {
case (Some(year), "jan", Some(day)) => Some(LocalDate.of(year, 1, day))
case (Some(year), "feb", Some(day)) => Some(LocalDate.of(year, 2, day))
case (Some(year), "mar", Some(day)) => Some(LocalDate.of(year, 3, day))
case (Some(year), "apr", Some(day)) => Some(LocalDate.of(year, 4, day))
case (Some(year), "may", Some(day)) => Some(LocalDate.of(year, 5, day))
case (Some(year), "jun", Some(day)) => Some(LocalDate.of(year, 6, day))
case (Some(year), "jul", Some(day)) => Some(LocalDate.of(year, 7, day))
case (Some(year), "aug", Some(day)) => Some(LocalDate.of(year, 8, day))
case (Some(year), "sep", Some(day)) => Some(LocalDate.of(year, 9, day))
case (Some(year), "oct", Some(day)) => Some(LocalDate.of(year, 10, day))
case (Some(year), "nov", Some(day)) => Some(LocalDate.of(year, 11, day))
case (Some(year), "dec", Some(day)) => Some(LocalDate.of(year, 12, day))
case _ => None
}
}
case _ => None
})
val instant = date.map(LocalDateTime.of(_, LocalTime.MIDNIGHT).toInstant(ZoneOffset.UTC))
val query =
SearchQuery().tag(tag).showTags("all").showElements("image").pageSize(contentApi.nextPreviousPageSize);
val capiQuery = FollowingSearchQuery(
direction match {
case Previous => query.fromDate(instant)
case Next => query.toDate(instant)
},
path,
direction,
)
contentApiClient.thriftClient.getResponse(capiQuery).map { response =>
val lightboxJson = response.results.flatMap(result =>
Content(result) match {
case content: ImageContent => Some(content.lightBox.javascriptConfig)
case _ => None
},
)
if (request.forceDCR) {
val timeDirection = direction match {
case Next => "past"
case Previous => "future"
}
Cached(CacheTime.Default)(
JsonComponent.fromWritable(
Json.obj(
"total" -> response.total,
"direction" -> timeDirection,
"date" -> date,
"images" -> JsArray(lightboxJson),
),
),
)
} else
Cached(CacheTime.Default)(JsonComponent.fromWritable(JsArray(lightboxJson)))
}
}