paginate relays

This commit is contained in:
Thibault Duplessis 2015-06-27 20:33:37 +02:00
parent b294bb7a6a
commit 0425599aa5
6 changed files with 57 additions and 19 deletions

View file

@ -16,10 +16,8 @@ object Relay extends LilaController {
private def relayNotFound(implicit ctx: Context) = NotFound(html.relay.notFound())
val index = Open { implicit ctx =>
env.repo recentNonEmpty 50 flatMap { relays =>
env.contentApi byRelays relays map { contents =>
Ok(html.relay.home(relays, contents))
}
env.api paginator getInt("page").|(1) map { relays =>
Ok(html.relay.home(relays))
}
}

View file

@ -1,4 +1,4 @@
@(relays: List[lila.relay.Relay], contents: List[lila.relay.Content])(implicit ctx: Context)
@(relays: Paginator[lila.relay.Relay.WithContent])(implicit ctx: Context)
@side = {
<div class="side">
@ -10,10 +10,14 @@
</div>
</div>
}
@moreJs = {
@jsTag("vendor/jquery.infinitescroll.min.js")
}
@relay.layout(
title = "Watch Chess events",
side = side.some,
moreJs = moreJs,
openGraph = Map(
'type -> "website",
'title -> "Watch chess events",
@ -34,24 +38,30 @@ openGraph = Map(
<th>Status</th>
</tr>
</thead>
<tbody>
@relays.map { rel =>
<tr class="scheduled">
<td>@showDate(rel.date)</td>
<tbody class="infinitescroll">
@relays.nextPage.map { next =>
<tr><th class="pager none">
<a href="@routes.Relay.index?page=@next">Next</a>
</th></tr>
<tr></tr>
}
@relays.currentPageResults.map { rel =>
<tr class="paginated_element">
<td>@showDate(rel.relay.date)</td>
<td>
<a class="name" href="@routes.Relay.show(rel.id, rel.slug)">
<strong>@rel.baseName</strong> - <em>@rel.extName</em>
<a class="name" href="@routes.Relay.show(rel.relay.id, rel.relay.slug)">
<strong>@rel.relay.baseName</strong> - <em>@rel.relay.extName</em>
</a>
@contents.find(_ matches rel).flatMap(_.short).map { short =>
@rel.content.flatMap(_.short).map { short =>
<span class="short">@escape(short)</span>
}
</td>
<td>
@if(rel.finished) {
@if(rel.relay.finished) {
@trans.finished()
} else {
<a class="ongoing" href="@routes.Relay.show(rel.id, rel.slug)">
@rel.activeGames.size/@rel.games.size playing
<a class="ongoing" href="@routes.Relay.show(rel.relay.id, rel.relay.slug)">
@rel.relay.activeGames.size/@rel.relay.games.size playing
</a>
}
</td>

View file

@ -58,14 +58,16 @@ final class Env(
private val mainFics = system.actorOf(ficsProps, name = "fics")
val repo = new RelayRepo(db(CollectionRelay))
private val relayColl = db(CollectionRelay)
lazy val api = new RelayApi(mainFics, repo, tourneyMap)
val repo = new RelayRepo(relayColl)
lazy val jsonView = new JsonView
lazy val contentApi = new ContentApi(db(CollectionContent))
lazy val api = new RelayApi(relayColl, contentApi, mainFics, repo, tourneyMap)
private val importer = new Importer(
hub.actor.roundMap,
ImportMoveDelay,

View file

@ -50,6 +50,8 @@ object Relay {
def apply(relay: Relay): Mini = Mini(relay.id, relay.baseName, relay.slug)
}
case class WithContent(relay: Relay, content: Option[Content])
case class Game(
id: String, // lichess game ID
ficsId: Int,

View file

@ -2,10 +2,15 @@ package lila.relay
import akka.actor._
import akka.pattern.ask
import lila.common.paginator._
import lila.db.paginator.BSONAdapter
import lila.db.Types.Coll
import lila.hub.actorApi.map.Tell
import makeTimeout.veryLarge
final class RelayApi(
coll: Coll,
contentApi: ContentApi,
fics: ActorRef,
repo: RelayRepo,
relayMap: ActorRef) {
@ -28,4 +33,25 @@ final class RelayApi(
}
}
}
def paginator(page: Int): Fu[Paginator[Relay.WithContent]] = {
import reactivemongo.bson._
import BSONHandlers._
Paginator(
adapter = new BSONAdapter[Relay](
collection = coll,
selector = repo.selectNonEmpty,
projection = BSONDocument(),
sort = repo.sortRecent
) mapFutureList withContent,
currentPage = page,
maxPerPage = 30)
}
private def withContent(relays: Seq[Relay]): Fu[Seq[Relay.WithContent]] =
contentApi byRelays relays map { contents =>
relays map { r =>
Relay.WithContent(r, contents.find(_ matches r))
}
}
}

View file

@ -14,8 +14,8 @@ final class RelayRepo(coll: Coll) {
private def selectName(name: String) = BSONDocument("name" -> name)
private val selectStarted = BSONDocument("status" -> Relay.Status.Started.id)
private val selectRecent = BSONDocument("date" -> BSONDocument("$gt" -> DateTime.now.minusWeeks(2)))
private val selectNonEmpty = BSONDocument("games.0.id" -> BSONDocument("$exists" -> true))
private val sortRecent = BSONDocument("date" -> -1)
private[relay] val selectNonEmpty = BSONDocument("games.0.id" -> BSONDocument("$exists" -> true))
private[relay] val sortRecent = BSONDocument("date" -> -1)
def byId(id: String): Fu[Option[Relay]] = coll.find(selectId(id)).one[Relay]