lila/app/controllers/Account.scala

128 lines
3.7 KiB
Scala
Raw Normal View History

2013-10-20 07:46:29 -06:00
package controllers
import play.api.mvc._, Results._
import lila.app._
import lila.common.LilaCookie
import lila.db.api.$find
import lila.security.Permission
import lila.user.tube.userTube
2014-02-17 02:12:19 -07:00
import lila.user.{ User => UserModel, UserRepo }
2013-10-20 07:46:29 -06:00
import views._
object Account extends LilaController {
private def env = Env.user
private def forms = lila.user.DataForm
2014-02-17 02:12:19 -07:00
def profile = Auth { implicit ctx =>
me =>
2013-10-20 11:30:34 -06:00
Ok(html.account.profile(me, forms profileOf me)).fuccess
2013-10-20 07:46:29 -06:00
}
2014-02-17 02:12:19 -07:00
def profileApply = AuthBody { implicit ctx =>
me =>
2013-12-27 15:12:20 -07:00
implicit val req: Request[_] = ctx.body
2014-02-17 02:12:19 -07:00
FormFuResult(forms.profile) { err =>
2013-10-20 07:46:29 -06:00
fuccess(html.account.profile(me, err))
2014-02-17 02:12:19 -07:00
} { profile =>
2013-10-20 07:46:29 -06:00
UserRepo.setProfile(me.id, profile) inject Redirect(routes.User show me.username)
}
}
2014-07-21 14:46:52 -06:00
def info = Auth { implicit ctx =>
me =>
negotiate(
html = notFound,
api = _ => lila.game.GameRepo nowPlaying me map { povs =>
Ok {
import play.api.libs.json._
Env.user.jsonView(me, extended = true) ++ Json.obj(
"nowPlaying" -> JsArray(povs map { pov =>
Json.obj(
"id" -> pov.fullId,
2014-12-16 04:11:36 -07:00
"fen" -> (chess.format.Forsyth exportBoard pov.game.toChess.board),
2014-12-17 12:22:16 -07:00
"color" -> pov.color.name,
2014-12-16 04:11:36 -07:00
"lastMove" -> ~pov.game.castleLastMoveTime.lastMoveString,
2014-08-29 04:18:44 -06:00
"variant" -> pov.game.variant.key,
"speed" -> pov.game.speed.key,
2014-08-30 02:23:21 -06:00
"perf" -> lila.game.PerfPicker.key(pov.game),
2014-08-29 04:18:44 -06:00
"rated" -> pov.game.rated,
"opponent" -> Json.obj(
"id" -> pov.opponent.userId,
"username" -> lila.game.Namer.playerString(pov.opponent, withRating = false)(Env.user.lightUser),
2014-11-30 14:31:07 -07:00
"rating" -> pov.opponent.rating),
2014-12-03 12:22:52 -07:00
"isMyTurn" -> pov.isMyTurn,
"secondsLeft" -> pov.remainingSeconds)
})
)
}
}
2014-07-21 14:46:52 -06:00
)
}
2014-12-10 15:07:43 -07:00
def passwd = Auth { implicit ctx =>
me =>
Ok(html.account.passwd(me, forms.passwd)).fuccess
}
2014-02-17 02:12:19 -07:00
def passwdApply = AuthBody { implicit ctx =>
me =>
2013-10-20 07:46:29 -06:00
implicit val req = ctx.body
2014-02-17 02:12:19 -07:00
FormFuResult(forms.passwd) { err =>
2013-10-20 07:46:29 -06:00
fuccess(html.account.passwd(me, err))
2014-12-10 15:07:43 -07:00
} { data =>
2013-10-20 07:46:29 -06:00
for {
2014-12-10 15:07:43 -07:00
ok UserRepo.checkPassword(me.id, data.oldPasswd)
_ ok ?? UserRepo.passwd(me.id, data.newPasswd1)
} yield {
val content = html.account.passwd(me, forms.passwd.fill(data), ok.some)
ok.fold(Ok(content), BadRequest(content))
}
}
}
2014-12-14 17:32:18 -07:00
private def emailForm(id: String) = UserRepo email id map { email =>
forms.email.fill(forms.Email(~email, ""))
}
2014-12-10 15:07:43 -07:00
def email = Auth { implicit ctx =>
me =>
2014-12-14 17:32:18 -07:00
emailForm(me.id) map { form =>
Ok(html.account.email(me, form))
}
2014-12-10 15:07:43 -07:00
}
def emailApply = AuthBody { implicit ctx =>
me =>
implicit val req = ctx.body
FormFuResult(forms.email) { err =>
fuccess(html.account.email(me, err))
} { data =>
for {
ok UserRepo.checkPassword(me.id, data.passwd)
_ ok ?? UserRepo.email(me.id, data.email)
2014-12-14 17:32:18 -07:00
form <- emailForm(me.id)
2014-12-10 15:07:43 -07:00
} yield {
2014-12-14 17:32:18 -07:00
val content = html.account.email(me, form, ok.some)
2014-12-10 15:07:43 -07:00
ok.fold(Ok(content), BadRequest(content))
}
2013-10-20 07:46:29 -06:00
}
}
2014-02-17 02:12:19 -07:00
def close = Auth { implicit ctx =>
me =>
2013-10-20 07:46:29 -06:00
Ok(html.account.close(me)).fuccess
}
2014-02-17 02:12:19 -07:00
def closeConfirm = Auth { ctx =>
me =>
2013-10-20 07:46:29 -06:00
implicit val req = ctx.req
(UserRepo disable me.id) >>
Env.team.api.quitAll(me.id) >>
(Env.security disconnect me.id) inject {
Redirect(routes.User show me.username) withCookies LilaCookie.newSession
}
}
}