lila/app/controllers/Relay.scala

85 lines
2.4 KiB
Scala
Raw Normal View History

2017-09-20 19:26:15 -06:00
package controllers
import play.api.mvc._
2017-09-30 17:00:51 -06:00
import play.api.libs.json.JsValue
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
def index = Open { implicit ctx =>
2017-10-06 18:56:06 -06:00
env.api.all(ctx.me) map { sel =>
2017-09-20 19:26:15 -06:00
Ok(html.relay.index(sel))
}
}
2017-10-06 18:08:32 -06:00
def form = Secure(_.Beta) { implicit ctx => me =>
2017-09-20 19:26:15 -06:00
NoLame {
Ok(html.relay.create(env.forms.create)).fuccess
}
}
2017-10-06 18:08:32 -06:00
def create = SecureBody(_.Beta) { 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 =>
2017-10-01 16:56:57 -06:00
OptionFuResult(env.api.byIdAndOwner(RelayModel.Id(id), me)) { relay =>
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 =>
2017-10-01 16:56:57 -06:00
OptionFuResult(env.api.byIdAndOwner(RelayModel.Id(id), me)) { relay =>
implicit val req = ctx.body
env.forms.edit(relay).bindFromRequest.fold(
err => BadRequest(html.relay.edit(relay, err)).fuccess,
data => env.api.update(data update relay) inject Redirect(showRoute(relay))
)
}
}
2017-09-22 23:43:19 -06:00
def show(slug: String, id: String) = Open { implicit ctx =>
OptionFuResult(env.api byId RelayModel.Id(id)) { relay =>
2017-10-01 16:56:57 -06:00
if (relay.slug != slug) Redirect(showRoute(relay)).fuccess
2017-09-28 15:11:25 -06:00
else Env.study.api byIdWithChapter relay.studyId flatMap {
_ ?? { oldSc =>
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)
} yield Ok(html.relay.show(relay, sc.study, data, chat, sVersion))
}
}
2017-09-20 19:26:15 -06:00
}
}
2017-09-30 17:00:51 -06:00
def websocket(id: String, apiVersion: Int) = SocketOption[JsValue] { implicit ctx =>
get("sri") ?? { uid =>
env.api byId RelayModel.Id(id) flatMap {
_ ?? { 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)
2017-09-20 19:26:15 -06:00
}