lila/app/controllers/Relay.scala

190 lines
6.2 KiB
Scala
Raw Normal View History

2017-09-20 19:26:15 -06:00
package controllers
2019-12-08 10:35:26 -07:00
import com.github.ghik.silencer.silent
import play.api.mvc._
2019-12-29 10:57:22 -07:00
import play.api.data.Form
2019-12-29 12:40:14 -07:00
import play.api.libs.json._
2017-09-20 19:26:15 -06:00
import lila.api.Context
import lila.app._
2019-12-29 10:57:22 -07:00
import lila.relay.{ Relay => RelayModel, RelayForm }
import lila.user.{ User => UserModel }
2017-09-20 19:26:15 -06:00
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
}
}
2019-12-08 10:35:26 -07:00
def form = Auth { implicit ctx => _ =>
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
}
}
2019-12-29 13:57:21 -07:00
def create = AuthOrScopedBody(_.Study.Write)(
auth = implicit ctx =>
me =>
env.relay.forms.create
.bindFromRequest()(ctx.body)
.fold(
err => BadRequest(html.relay.form.create(err)).fuccess,
setup =>
env.relay.api.create(setup, me) map { relay =>
Redirect(showRoute(relay))
}
),
scoped = req =>
me =>
env.relay.forms.create
.bindFromRequest()(req)
.fold(
err => BadRequest(apiFormError(err)).fuccess,
setup =>
env.relay.api.create(setup, me) map { relay =>
Ok(asJson(relay)) as JSON
}
)
)
2017-09-20 19:26:15 -06:00
2019-12-08 10:35:26 -07:00
def edit(@silent 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
}
}
2019-12-29 10:57:22 -07:00
def update(@silent slug: String, id: String) = AuthOrScopedBody(_.Study.Write)(
auth = implicit ctx =>
me =>
doUpdate(id, me)(ctx.body) flatMap {
case None => notFound
case Some(res) =>
res
.fold(
{ case (old, err) => BadRequest(html.relay.form.edit(old, err)) },
relay => Redirect(showRoute(relay))
)
.fuccess
},
scoped = req =>
me =>
2019-12-29 12:40:14 -07:00
doUpdate(id, me)(req) map {
case None => NotFound(jsonError("No such broadcast"))
2019-12-29 10:57:22 -07:00
case Some(res) =>
res.fold(
2019-12-29 12:40:14 -07:00
{ case (_, err) => BadRequest(apiFormError(err)) },
relay => Ok(asJson(relay)) as JSON
2019-12-29 10:57:22 -07:00
)
}
)
private def doUpdate(id: String, me: UserModel)(
implicit req: Request[_]
): Fu[Option[Either[(RelayModel, Form[RelayForm.Data]), RelayModel]]] =
env.relay.api.byIdAndContributor(id, me) flatMap {
_ ?? { relay =>
env.relay.forms
.edit(relay)
.bindFromRequest
.fold(
err => fuccess(Left(relay -> err)),
data => env.relay.api.update(relay) { data.update(_, me) } dmap Right.apply
) dmap some
}
2017-10-01 16:56:57 -06:00
}
2019-12-08 10:35:26 -07:00
def reset(@silent 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
}
}
2019-12-29 12:40:14 -07:00
def show(slug: String, id: String) = OpenOrScoped(_.Study.Read)(
open = implicit ctx => {
pageHit
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, _) } }
}
},
scoped = _ =>
me =>
env.relay.api.byIdAndContributor(id, me) map {
case None => NotFound(jsonError("No such broadcast"))
case Some(relay) => Ok(asJson(relay)) as JSON
}
)
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
def cloneRelay(@silent slug: String, id: String) = Auth { implicit ctx => me =>
OptionFuResult(env.relay.api.byIdAndContributor(id, me)) { relay =>
env.relay.api.cloneRelay(relay, me) map { newRelay =>
Redirect(routes.Relay.edit(newRelay.slug, newRelay.id.value))
}
}
}
def push(@silent slug: String, id: String) = ScopedBody(parse.tolerantText)(Seq(_.Study.Write)) {
req => me =>
env.relay.api.byIdAndContributor(id, me) flatMap {
case None => notFoundJson()
case Some(relay) => env.relay.push(relay, req.body) inject jsonOkResult
}
}
2019-12-29 12:40:14 -07:00
private def asJson(relay: RelayModel) = Json.obj(
2019-12-29 13:45:30 -07:00
"broadcast" -> env.relay.jsonView.apiShow(relay),
"url" -> s"${env.net.baseUrl}${showRoute(relay)}"
2019-12-29 12:40:14 -07:00
)
2019-12-13 07:30:20 -07: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)
}
2019-12-13 07:30:20 -07:00
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)
2019-12-13 07:30:20 -07:00
chat <- studyC.chatOf(sc.study)
2019-12-04 16:39:16 -07:00
sVersion <- env.study.version(sc.study.id)
2019-12-13 07:30:20 -07:00
streams <- studyC.streamsOf(sc.study)
} yield EnableSharedArrayBuffer(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)
2019-12-13 07:30:20 -07:00
implicit private def makeRelayId(id: String): RelayModel.Id = RelayModel.Id(id)
implicit private def makeChapterId(id: String): lila.study.Chapter.Id = lila.study.Chapter.Id(id)
2017-09-20 19:26:15 -06:00
}