Skip to content

Commit

Permalink
feat: get a proper count of next items
Browse files Browse the repository at this point in the history
  • Loading branch information
mxdvl committed Jul 5, 2024
1 parent 6a51eca commit ff6f635
Showing 1 changed file with 42 additions and 16 deletions.
58 changes: 42 additions & 16 deletions applications/app/controllers/ImageContentController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import com.gu.contentapi.client.model.Direction.Next
import com.gu.contentapi.client.model.Direction.Previous
import java.time.LocalDate
import java.time.Instant
import java.time.temporal.TemporalAmount
import java.time.LocalTime
import org.joda.time.DateTime
import java.time.Duration
import scala.util.Try
import java.time.LocalDateTime
import java.time.ZoneOffset

class ImageContentController(
val contentApiClient: ContentApiClient,
Expand Down Expand Up @@ -98,32 +100,51 @@ class ImageContentController(
override def canRender(i: ItemResponse): Boolean = i.content.exists(isSupported)

private val ONE_HUNDRED_YEARS = 3600 * 24 * 365 * 100
private val dateExtractor = "/(\\d{4})/(\\w{3})/(\\d{2})/".r
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 maybeDate = path match {
case dateExtractor(year, month, date) => Some(( year.toInt, month.toInt, date.toInt))
case dateExtractor(rawYear, rawMonth, rawDate) => {
(Try(rawYear.toInt).toOption, rawMonth, Try(rawDate.toInt).toOption) match {
case (Some(year), "jan", Some(date)) => Some(LocalDate.of(year, 1, date))
case (Some(year), "feb", Some(date)) => Some(LocalDate.of(year, 2, date))
case (Some(year), "mar", Some(date)) => Some(LocalDate.of(year, 3, date))
case (Some(year), "apr", Some(date)) => Some(LocalDate.of(year, 4, date))
case (Some(year), "may", Some(date)) => Some(LocalDate.of(year, 5, date))
case (Some(year), "jun", Some(date)) => Some(LocalDate.of(year, 6, date))
case (Some(year), "jul", Some(date)) => Some(LocalDate.of(year, 7, date))
case (Some(year), "aug", Some(date)) => Some(LocalDate.of(year, 8, date))
case (Some(year), "sep", Some(date)) => Some(LocalDate.of(year, 9, date))
case (Some(year), "oct", Some(date)) => Some(LocalDate.of(year, 10, date))
case (Some(year), "nov", Some(date)) => Some(LocalDate.of(year, 11, date))
case (Some(year), "dec", Some(date)) => Some(LocalDate.of(year, 12, date))
case _ => None
}
}
case _ => None
}
println(maybeDate)

val now = Instant.now().minusSeconds(3600 * 24 * 10); // todo: make it the publication date…
val query = SearchQuery().tag(tag).showTags("all").showElements("image").pageSize(contentApi.nextPreviousPageSize);
val query =
SearchQuery().tag(tag).showTags("all").showElements("image").pageSize(contentApi.nextPreviousPageSize);

val capiQuery = FollowingSearchQuery(
direction match {
case Previous => query.fromDate(now)
case Next => query.toDate(now)
(direction, maybeDate) match {
case (Previous, Some(date)) =>
query.fromDate(LocalDateTime.of(date, LocalTime.MIDNIGHT).toInstant(ZoneOffset.UTC))
case (Next, Some(date)) => query.toDate(LocalDateTime.of(date, LocalTime.MIDNIGHT).toInstant(ZoneOffset.UTC))
case _ => query
},
path,
direction,
)

println(capiQuery.parameters.toString())

contentApiClient.thriftClient.getResponse(capiQuery).map { response =>
val lightboxJson = response.results.flatMap(result =>
Content(result) match {
Expand All @@ -132,17 +153,22 @@ class ImageContentController(
},
)

if (request.forceDCR)
if (request.forceDCR) {
val timeDirection = direction match {
case Next => "past"
case Previous => "future"
}

Cached(CacheTime.Default)(
JsonComponent.fromWritable(
Json.obj(
"total" -> response.total,
"direction" -> response.pages,
"direction" -> timeDirection,
"images" -> JsArray(lightboxJson),
),
),
)
else
} else
Cached(CacheTime.Default)(JsonComponent.fromWritable(JsArray(lightboxJson)))

}
Expand Down

0 comments on commit ff6f635

Please sign in to comment.