lila/app/controllers/Relay.scala

110 lines
3.7 KiB
Scala
Raw Normal View History

2017-09-20 19:26:15 -06:00
package controllers
2017-09-30 17:00:51 -06:00
import play.api.libs.json.JsValue
import play.api.mvc._
2017-09-20 19:26:15 -06:00
import lila.api.Context
import lila.app._
import lila.relay.{ Relay => RelayModel }
import views._
2019-12-04 21:32:03 -07:00
final class Relay(
env: Env,
2019-12-05 14:51:18 -07:00
studyC: => Study
2019-12-04 21:32:03 -07:00
) extends LilaController(env) {
2017-09-20 19:26:15 -06:00
2017-10-27 09:56:26 -06:00
def index(page: Int) = Open { implicit ctx =>
Reasonable(page) {
for {
2019-12-04 21:32:03 -07:00
fresh <- (page == 1).??(env.relay.api.fresh(ctx.me) map some)
pager <- env.relay.pager.finished(ctx.me, page)
2017-10-27 09:56:26 -06:00
} yield Ok(html.relay.index(fresh, pager, routes.Relay.index()))
2017-09-20 19:26:15 -06:00
}
}
2017-10-22 17:55:30 -06:00
def form = Auth { implicit ctx => me =>
2017-09-20 19:26:15 -06:00
NoLame {
2019-12-04 21:32:03 -07:00
Ok(html.relay.form.create(env.relay.forms.create)).fuccess
2017-09-20 19:26:15 -06:00
}
}
2017-10-22 17:55:30 -06:00
def create = AuthBody { implicit ctx => me =>
2017-09-20 19:26:15 -06:00
implicit val req = ctx.body
2019-12-04 21:32:03 -07:00
env.relay.forms.create.bindFromRequest.fold(
2019-04-11 02:09:14 -06:00
err => BadRequest(html.relay.form.create(err)).fuccess,
2019-12-04 21:32:03 -07:00
setup => env.relay.api.create(setup, me) map { relay =>
2017-10-01 16:56:57 -06:00
Redirect(showRoute(relay))
2017-09-20 19:26:15 -06:00
}
)
}
2017-10-06 18:08:32 -06:00
def edit(slug: String, id: String) = Auth { implicit ctx => me =>
2019-12-04 21:32:03 -07:00
OptionFuResult(env.relay.api.byIdAndContributor(id, me)) { relay =>
Ok(html.relay.form.edit(relay, env.relay.forms.edit(relay))).fuccess
2017-10-01 16:56:57 -06:00
}
}
2017-10-06 18:08:32 -06:00
def update(slug: String, id: String) = AuthBody { implicit ctx => me =>
2019-12-04 21:32:03 -07:00
OptionFuResult(env.relay.api.byIdAndContributor(id, me)) { relay =>
2017-10-01 16:56:57 -06:00
implicit val req = ctx.body
2019-12-04 21:32:03 -07:00
env.relay.forms.edit(relay).bindFromRequest.fold(
2019-04-11 02:09:14 -06:00
err => BadRequest(html.relay.form.edit(relay, err)).fuccess,
2019-12-04 21:32:03 -07:00
data => env.relay.api.update(relay) { data.update(_, me) } map { r =>
2018-09-06 11:48:25 -06:00
Redirect(showRoute(r))
}
2017-10-01 16:56:57 -06:00
)
}
}
2019-11-14 17:13:41 -07:00
def reset(slug: String, id: String) = Auth { implicit ctx => me =>
2019-12-04 21:32:03 -07:00
OptionFuResult(env.relay.api.byIdAndContributor(id, me)) { relay =>
env.relay.api.reset(relay, me) inject Redirect(showRoute(relay))
2019-11-14 17:13:41 -07:00
}
}
2017-09-22 23:43:19 -06:00
def show(slug: String, id: String) = Open { implicit ctx =>
2018-03-27 18:11:49 -06:00
pageHit
WithRelay(slug, id) { relay =>
val sc =
2019-12-04 16:39:16 -07:00
if (relay.sync.ongoing) env.study.chapterRepo relaysAndTagsByStudyId relay.studyId flatMap { chapters =>
chapters.find(_.looksAlive) orElse chapters.headOption match {
2019-12-04 16:39:16 -07:00
case Some(chapter) => env.study.api.byIdWithChapter(relay.studyId, chapter.id)
case None => env.study.api byIdWithChapter relay.studyId
}
}
2019-12-04 16:39:16 -07:00
else env.study.api byIdWithChapter relay.studyId
sc flatMap { _ ?? { doShow(relay, _) } }
}
}
def chapter(slug: String, id: String, chapterId: String) = Open { implicit ctx =>
WithRelay(slug, id) { relay =>
2019-12-04 16:39:16 -07:00
env.study.api.byIdWithChapter(relay.studyId, chapterId) flatMap {
_ ?? { doShow(relay, _) }
2017-09-28 15:11:25 -06:00
}
2017-09-20 19:26:15 -06:00
}
}
2017-09-30 17:00:51 -06:00
private def WithRelay(slug: String, id: String)(f: RelayModel => Fu[Result])(implicit ctx: Context): Fu[Result] =
2019-12-04 21:32:03 -07:00
OptionFuResult(env.relay.api byId id) { relay =>
if (relay.slug != slug) Redirect(showRoute(relay)).fuccess
else f(relay)
}
private def doShow(relay: RelayModel, oldSc: lila.study.Study.WithChapter)(implicit ctx: Context): Fu[Result] =
2019-12-04 21:32:03 -07:00
studyC.CanViewResult(oldSc.study) {
for {
2019-12-04 21:32:03 -07:00
(sc, studyData) <- studyC.getJsonData(oldSc)
data = env.relay.jsonView.makeData(relay, studyData)
chat <- studyC.chatOf(sc.study)
2019-12-04 16:39:16 -07:00
sVersion <- env.study.version(sc.study.id)
2019-12-04 21:32:03 -07:00
streams <- studyC.streamsOf(sc.study)
} yield Ok(html.relay.show(relay, sc.study, data, chat, sVersion, streams))
}
2017-10-01 16:56:57 -06:00
private def showRoute(r: RelayModel) = routes.Relay.show(r.slug, r.id.value)
private implicit def makeRelayId(id: String): RelayModel.Id = RelayModel.Id(id)
private implicit def makeChapterId(id: String): lila.study.Chapter.Id = lila.study.Chapter.Id(id)
2017-09-20 19:26:15 -06:00
}