2016-01-26 21:37:23 -07:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import play.api.data.Form
|
|
|
|
import play.api.i18n.Messages.Implicits._
|
2016-02-01 19:28:05 -07:00
|
|
|
import play.api.libs.json._
|
2016-01-26 21:37:23 -07:00
|
|
|
import play.api.mvc.{ Result, Results, Call, RequestHeader, Accepting }
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
2016-01-28 05:04:55 -07:00
|
|
|
import lila.api.{ Context, BodyContext }
|
2016-01-26 21:37:23 -07:00
|
|
|
import lila.app._
|
2016-01-30 03:27:41 -07:00
|
|
|
import lila.challenge.{ Challenge => ChallengeModel }
|
2016-01-26 21:37:23 -07:00
|
|
|
import lila.common.{ HTTPRequest, LilaCookie }
|
2016-01-30 03:27:41 -07:00
|
|
|
import views.html
|
2016-01-26 21:37:23 -07:00
|
|
|
|
2016-01-26 22:56:30 -07:00
|
|
|
object Challenge extends LilaController {
|
2016-01-26 21:37:23 -07:00
|
|
|
|
2016-01-27 08:01:23 -07:00
|
|
|
private def env = Env.challenge
|
2016-01-26 21:37:23 -07:00
|
|
|
|
|
|
|
private val PostRateLimit = new lila.memo.RateLimit(5, 1 minute)
|
|
|
|
|
2016-01-27 08:01:23 -07:00
|
|
|
def all = Auth { implicit ctx =>
|
|
|
|
me =>
|
2016-02-03 01:26:22 -07:00
|
|
|
env.api allFor me.id map { all =>
|
|
|
|
Ok(env.jsonView(all)) as JSON
|
|
|
|
}
|
2016-01-28 05:04:55 -07:00
|
|
|
}
|
|
|
|
|
2016-02-03 01:26:22 -07:00
|
|
|
def show(id: String) = Open { implicit ctx =>
|
|
|
|
showId(id)
|
|
|
|
}
|
2016-01-30 19:04:27 -07:00
|
|
|
|
2016-02-03 01:26:22 -07:00
|
|
|
protected[controllers] def showId(id: String)(implicit ctx: Context): Fu[Result] =
|
|
|
|
OptionFuResult(env.api byId id)(showChallenge)
|
|
|
|
|
|
|
|
protected[controllers] def showChallenge(c: ChallengeModel)(implicit ctx: Context): Fu[Result] =
|
|
|
|
env version c.id flatMap { version =>
|
|
|
|
val json = env.jsonView.show(c, version)
|
|
|
|
negotiate(
|
|
|
|
html = Ok(isMine(c).fold(
|
|
|
|
html.challenge.mine.apply _,
|
|
|
|
html.challenge.theirs.apply _
|
|
|
|
)(c, json)).fuccess,
|
|
|
|
api = _ => Ok(json).fuccess)
|
2016-01-28 05:04:55 -07:00
|
|
|
}
|
|
|
|
|
2016-01-30 03:27:41 -07:00
|
|
|
private def isMine(challenge: ChallengeModel)(implicit ctx: Context) = challenge.challenger match {
|
|
|
|
case Left(anon) => HTTPRequest sid ctx.req contains anon.secret
|
2016-01-28 05:04:55 -07:00
|
|
|
case Right(user) => ctx.userId contains user.id
|
2016-01-27 08:01:23 -07:00
|
|
|
}
|
2016-01-30 03:27:41 -07:00
|
|
|
|
2016-01-31 06:44:12 -07:00
|
|
|
private def isForMe(challenge: ChallengeModel)(implicit ctx: Context) =
|
|
|
|
challenge.destUserId.fold(true)(ctx.userId.contains)
|
|
|
|
|
|
|
|
def accept(id: String) = Open { implicit ctx =>
|
2016-02-03 01:26:22 -07:00
|
|
|
OptionFuResult(env.api byId id) { c =>
|
|
|
|
if (isForMe(c)) env.api.accept(c, ctx.me) map { game =>
|
|
|
|
Redirect(routes.Round.watcher(game.id, "white"))
|
|
|
|
}
|
2016-01-31 06:44:12 -07:00
|
|
|
else notFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def decline(id: String) = Auth { implicit ctx =>
|
|
|
|
me =>
|
2016-02-03 01:26:22 -07:00
|
|
|
OptionFuResult(env.api byId id) { c =>
|
|
|
|
if (isForMe(c)) env.api decline c
|
2016-01-31 06:44:12 -07:00
|
|
|
else notFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 03:27:41 -07:00
|
|
|
def cancel(id: String) = Open { implicit ctx =>
|
|
|
|
OptionFuResult(env.api byId id) { challenge =>
|
2016-01-31 22:40:31 -07:00
|
|
|
if (isMine(challenge)) env.api cancel challenge inject Redirect(routes.Lobby.home)
|
2016-01-30 03:27:41 -07:00
|
|
|
else notFound
|
|
|
|
}
|
|
|
|
}
|
2016-02-01 19:28:05 -07:00
|
|
|
|
|
|
|
def websocket(id: String, apiVersion: Int) = SocketOption[JsValue] { implicit ctx =>
|
|
|
|
env.api byId id flatMap {
|
|
|
|
_ ?? { c =>
|
|
|
|
get("sri") ?? { uid =>
|
|
|
|
env.socketHandler.join(id, uid, ctx.userId, isMine(c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 21:37:23 -07:00
|
|
|
}
|