lila/app/controllers/Simul.scala

169 lines
4.6 KiB
Scala
Raw Normal View History

2015-03-30 16:40:26 -06:00
package controllers
import play.api.libs.json._
import play.api.mvc._
import lila.api.Context
import lila.app._
import lila.chat.Chat
2015-03-30 16:40:26 -06:00
import lila.common.HTTPRequest
import lila.simul.{ Simul => Sim }
import views._
2019-12-04 21:46:58 -07:00
final class Simul(
env: Env,
2019-12-05 14:51:18 -07:00
apiC: => Team
2019-12-04 21:46:58 -07:00
) extends LilaController(env) {
2015-03-30 16:40:26 -06:00
2019-07-08 11:59:39 -06:00
private def forms = lila.simul.SimulForm
2015-03-30 16:40:26 -06:00
2019-01-15 21:06:49 -07:00
private def simulNotFound(implicit ctx: Context) = NotFound(html.simul.bits.notFound())
2015-03-30 16:40:26 -06:00
val home = Open { implicit ctx =>
2018-03-27 18:11:49 -06:00
pageHit
2019-10-17 09:28:51 -06:00
fetchSimuls flatMap {
case created ~ started ~ finished =>
Ok(html.simul.home(created, started, finished)).fuccess
}
}
val apiList = Action.async {
fetchSimuls flatMap {
case created ~ started ~ finished =>
2019-12-04 21:46:58 -07:00
env.simul.jsonView.apiAll(created, started, finished) map { json =>
2019-10-17 09:28:51 -06:00
Ok(json) as JSON
}
2015-04-03 07:33:31 -06:00
}
}
val homeReload = Open { implicit ctx =>
fetchSimuls map {
case ((created, started), finished) =>
Ok(html.simul.homeInner(created, started, finished))
}
}
private def fetchSimuls =
2019-12-04 21:46:58 -07:00
env.simul.allCreated.get zip env.simul.repo.allStarted zip env.simul.repo.allFinished(30)
2015-04-03 07:33:31 -06:00
def show(id: String) = Open { implicit ctx =>
2019-12-04 21:46:58 -07:00
env.simul.repo find id flatMap {
2015-04-03 07:33:31 -06:00
_.fold(simulNotFound.fuccess) { sim =>
2017-01-26 05:19:27 -07:00
for {
2019-12-04 16:39:16 -07:00
team <- sim.team ?? env.team.api.team
2019-12-04 21:46:58 -07:00
version <- env.simul.version(sim.id)
json <- env.simul.jsonView(sim, team.map { t =>
2019-07-08 11:59:39 -06:00
lila.simul.SimulTeam(t.id, t.name, ctx.userId exists {
2019-12-04 16:39:16 -07:00
env.team.api.syncBelongsTo(t.id, _)
2019-07-08 11:59:39 -06:00
})
})
2019-12-04 16:39:16 -07:00
chat <- canHaveChat ?? env.chat.api.userChat.cached.findMine(Chat.Id(sim.id), ctx.me).map(some)
_ <- chat ?? { c => env.user.lightUserApi.preloadMany(c.chat.userIds) }
stream <- env.streamer.liveStreamApi one sim.hostId
2019-07-08 11:59:39 -06:00
} yield html.simul.show(sim, version, json, chat, stream, team)
2015-04-03 07:33:31 -06:00
}
2015-06-19 09:36:31 -06:00
} map NoCache
2015-04-03 07:33:31 -06:00
}
2019-05-02 20:42:45 -06:00
private[controllers] def canHaveChat(implicit ctx: Context): Boolean =
!ctx.kid && // no public chats for kids
ctx.me.fold(true) { // anon can see public chats
2019-12-04 16:39:16 -07:00
env.chat.panic.allowed
2019-05-02 20:42:45 -06:00
}
2017-10-28 15:40:52 -06:00
2019-10-16 05:07:28 -06:00
def hostPing(simulId: String) = Open { implicit ctx =>
AsHost(simulId) { simul =>
2019-12-04 16:39:16 -07:00
env.simul.cleaner hostPing simul
2019-10-16 05:07:28 -06:00
jsonOkResult
}
}
2015-04-03 07:33:31 -06:00
def start(simulId: String) = Open { implicit ctx =>
AsHost(simulId) { simul =>
2019-12-04 21:46:58 -07:00
env.simul.api start simul.id
jsonOkResult
2015-04-03 07:33:31 -06:00
}
}
def abort(simulId: String) = Open { implicit ctx =>
AsHost(simulId) { simul =>
2019-12-04 21:46:58 -07:00
env.simul.api abort simul.id
jsonOkResult
}
}
2015-04-03 07:33:31 -06:00
def accept(simulId: String, userId: String) = Open { implicit ctx =>
AsHost(simulId) { simul =>
2019-12-04 21:46:58 -07:00
env.simul.api.accept(simul.id, userId, true)
jsonOkResult
2015-04-03 07:33:31 -06:00
}
}
def reject(simulId: String, userId: String) = Open { implicit ctx =>
AsHost(simulId) { simul =>
2019-12-04 21:46:58 -07:00
env.simul.api.accept(simul.id, userId, false)
jsonOkResult
}
}
def setText(simulId: String) = OpenBody { implicit ctx =>
AsHost(simulId) { simul =>
implicit val req = ctx.body
2019-07-08 11:59:39 -06:00
forms.setText.bindFromRequest.fold(
2019-12-08 11:12:00 -07:00
_ => BadRequest,
text => {
2019-12-04 21:46:58 -07:00
env.simul.api.setText(simul.id, text)
jsonOkResult
}
)
2015-04-03 07:33:31 -06:00
}
2015-03-30 16:40:26 -06:00
}
2017-01-26 05:19:27 -07:00
def form = Auth { implicit ctx => me =>
2018-04-17 17:15:58 -06:00
NoLameOrBot {
2019-12-05 14:51:18 -07:00
apiC.teamsIBelongTo(me) map { teams =>
2019-07-08 11:59:39 -06:00
Ok(html.simul.form(forms.create, teams))
}
2017-01-26 05:19:27 -07:00
}
2015-03-30 16:40:26 -06:00
}
2015-04-03 07:33:31 -06:00
2017-01-26 05:19:27 -07:00
def create = AuthBody { implicit ctx => implicit me =>
2018-04-17 17:15:58 -06:00
NoLameOrBot {
2017-01-26 05:19:27 -07:00
implicit val req = ctx.body
2019-07-08 11:59:39 -06:00
forms.create.bindFromRequest.fold(
2019-12-05 14:51:18 -07:00
err => apiC.teamsIBelongTo(me) map { teams =>
2019-07-08 11:59:39 -06:00
BadRequest(html.simul.form(err, teams))
},
2019-12-04 21:46:58 -07:00
setup => env.simul.api.create(setup, me) map { simul =>
2017-01-26 05:19:27 -07:00
Redirect(routes.Simul.show(simul.id))
}
)
2017-01-26 05:19:27 -07:00
}
2015-04-03 15:04:59 -06:00
}
2017-01-26 05:19:27 -07:00
def join(id: String, variant: String) = Auth { implicit ctx => implicit me =>
2018-04-17 17:15:58 -06:00
NoLameOrBot {
2015-04-07 17:37:42 -06:00
fuccess {
2019-12-04 21:46:58 -07:00
env.simul.api.addApplicant(id, me, variant)
2015-04-07 17:37:42 -06:00
if (HTTPRequest isXhr ctx.req) Ok(Json.obj("ok" -> true)) as JSON
else Redirect(routes.Simul.show(id))
}
2017-01-26 05:19:27 -07:00
}
}
def withdraw(id: String) = Auth { implicit ctx => me =>
fuccess {
2019-12-04 21:46:58 -07:00
env.simul.api.removeApplicant(id, me)
2017-01-26 05:19:27 -07:00
if (HTTPRequest isXhr ctx.req) Ok(Json.obj("ok" -> true)) as JSON
else Redirect(routes.Simul.show(id))
}
2015-04-03 15:04:59 -06:00
}
2015-04-03 07:33:31 -06:00
private def AsHost(simulId: Sim.ID)(f: Sim => Result)(implicit ctx: Context): Fu[Result] =
2019-12-04 21:46:58 -07:00
env.simul.repo.find(simulId) flatMap {
2015-04-03 07:33:31 -06:00
case None => notFound
case Some(simul) if ctx.userId.exists(simul.hostId ==) => fuccess(f(simul))
case _ => fuccess(Unauthorized)
}
2015-03-30 16:40:26 -06:00
}