lila/app/controllers/Relay.scala

111 lines
3.6 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._
object Relay extends LilaController {
private val env = Env.relay
2017-10-27 09:56:26 -06:00
def index(page: Int) = Open { implicit ctx =>
Reasonable(page) {
for {
fresh <- (page == 1).??(env.api.fresh(ctx.me) map some)
pager <- env.pager.finished(ctx.me, page)
} 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 {
Ok(html.relay.create(env.forms.create)).fuccess
}
}
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
env.forms.create.bindFromRequest.fold(
err => BadRequest(html.relay.create(err)).fuccess,
setup => env.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 =>
OptionFuResult(env.api.byIdAndContributor(id, me)) { relay =>
2017-10-01 16:56:57 -06:00
Ok(html.relay.edit(relay, env.forms.edit(relay))).fuccess
}
}
2017-10-06 18:08:32 -06:00
def update(slug: String, id: String) = AuthBody { implicit ctx => me =>
OptionFuResult(env.api.byIdAndContributor(id, me)) { relay =>
2017-10-01 16:56:57 -06:00
implicit val req = ctx.body
env.forms.edit(relay).bindFromRequest.fold(
err => BadRequest(html.relay.edit(relay, err)).fuccess,
2017-10-17 14:53:10 -06:00
data => env.api.update(relay) { data.update(_, me) } map { r => Redirect(showRoute(r)) }
2017-10-01 16:56:57 -06:00
)
}
}
2017-09-22 23:43:19 -06:00
def show(slug: String, id: String) = Open { implicit ctx =>
WithRelay(slug, id) { relay =>
val sc =
if (relay.sync.ongoing) Env.study.chapterRepo relaysAndTagsByStudyId relay.studyId flatMap { chapters =>
chapters.find(_.looksAlive) orElse chapters.headOption match {
case Some(chapter) => Env.study.api.byIdWithChapter(relay.studyId, chapter.id)
case None => Env.study.api byIdWithChapter relay.studyId
}
}
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 =>
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] =
OptionFuResult(env.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] = for {
(sc, studyData) <- Study.getJsonData(oldSc)
data = lila.relay.JsonView.makeData(relay, studyData)
chat <- Study.chatOf(sc.study)
sVersion <- Env.study.version(sc.study.id)
2018-01-06 07:51:04 -07:00
streams <- Study.streamsOf(sc.study)
} yield Ok(html.relay.show(relay, sc.study, data, chat, sVersion, streams))
2017-09-30 17:00:51 -06:00
def websocket(id: String, apiVersion: Int) = SocketOption[JsValue] { implicit ctx =>
get("sri") ?? { uid =>
env.api byId id flatMap {
2017-09-30 17:00:51 -06:00
_ ?? { relay =>
env.socketHandler.join(
relayId = relay.id,
uid = lila.socket.Socket.Uid(uid),
user = ctx.me
)
}
}
}
}
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
}