lila/modules/studySearch/src/main/Env.scala

56 lines
1.7 KiB
Scala
Raw Normal View History

2016-07-25 03:11:39 -06:00
package lila.studySearch
import akka.actor._
2019-12-03 21:40:01 -07:00
import com.softwaremill.macwire._
2016-07-25 14:34:15 -06:00
import scala.concurrent.duration._
2016-07-25 03:11:39 -06:00
import lila.common.Bus
2019-12-03 21:40:01 -07:00
import lila.common.paginator._
import lila.hub.actorApi.study.RemoveStudy
import lila.hub.LateMultiThrottler
2016-07-25 03:11:39 -06:00
import lila.search._
2016-07-25 12:14:43 -06:00
import lila.study.Study
import lila.user.User
2016-07-25 03:11:39 -06:00
final class Env(
2019-12-03 21:40:01 -07:00
studyRepo: lila.study.StudyRepo,
chapterRepo: lila.study.ChapterRepo,
pager: lila.study.StudyPager,
makeClient: Index => ESClient
2020-06-24 03:37:18 -06:00
)(implicit
ec: scala.concurrent.ExecutionContext,
system: ActorSystem,
mat: akka.stream.Materializer
) {
2016-07-25 03:11:39 -06:00
2019-12-05 10:42:46 -07:00
private val client = makeClient(Index("study"))
2016-07-25 03:11:39 -06:00
2020-05-03 17:50:10 -06:00
private val indexThrottler = LateMultiThrottler(executionTimeout = 3.seconds.some, logger = logger)
2016-07-25 14:34:15 -06:00
2019-12-03 21:40:01 -07:00
val api: StudySearchApi = wire[StudySearchApi]
2016-07-25 03:11:39 -06:00
2016-07-25 12:14:43 -06:00
def apply(me: Option[User])(text: String, page: Int) =
Paginator[Study.WithChaptersAndLiked](
adapter = new AdapterLike[Study] {
2019-12-13 07:30:20 -07:00
def query = Query(text, me.map(_.id))
def nbResults = api count query
2017-08-23 17:56:39 -06:00
def slice(offset: Int, length: Int) = api.search(query, From(offset), Size(length))
2019-12-08 18:43:50 -07:00
} mapFutureList pager.withChaptersAndLiking(me),
2016-07-25 12:14:43 -06:00
currentPage = page,
2019-12-08 18:43:50 -07:00
maxPerPage = pager.maxPerPage
)
2016-07-25 03:11:39 -06:00
2020-05-05 22:11:15 -06:00
def cli =
new lila.common.Cli {
def process = {
case "study" :: "search" :: "reset" :: Nil => api.reset("reset") inject "done"
case "study" :: "search" :: "index" :: since :: Nil => api.reset(since) inject "done"
}
2016-07-25 03:11:39 -06:00
}
2019-11-29 17:07:51 -07:00
Bus.subscribeFun("study") {
case lila.study.actorApi.SaveStudy(study) => api.store(study).unit
case RemoveStudy(id, _) => client.deleteById(Id(id)).unit
}
2016-07-25 03:11:39 -06:00
}