lila/app/controllers/User.scala

118 lines
3.5 KiB
Scala
Raw Normal View History

2013-03-20 08:23:41 -06:00
package controllers
import lila.app._
import views._
import lila.security.Permission
2013-05-04 17:12:53 -06:00
import lila.user.{ Context, User UserModel, UserRepo }
2013-05-08 09:41:12 -06:00
import lila.user.tube.userTube
import lila.db.api.$find
2013-04-09 12:58:34 -06:00
import lila.common.LilaCookie
2013-03-20 08:23:41 -06:00
2013-04-10 04:17:58 -06:00
import play.api.mvc._, Results._
2013-03-20 08:23:41 -06:00
object User extends LilaController {
2013-05-08 09:41:12 -06:00
private def env = Env.user
private def gamePaginator = Env.game.paginator
2013-03-20 08:23:41 -06:00
private def forms = lila.user.DataForm
2013-05-08 09:41:12 -06:00
private lazy val makeUserInfo = mashup.UserInfo(
countUsers = () env.countEnabled,
bookmarkApi = Env.bookmark.api,
eloCalculator = Env.round.eloCalculator) _
def show(username: String) = showFilter(username, "all", 1)
def showFilter(username: String, filterName: String, page: Int) = Open { implicit ctx
Reasonable(page) {
OptionFuOk($find byId username) { userShow(_, filterName, page) }
2013-05-04 17:12:53 -06:00
}
}
2013-03-20 07:24:47 -06:00
2013-05-08 09:41:12 -06:00
private def userShow(u: UserModel, filterName: String, page: Int)(implicit ctx: Context) =
(u.enabled || isGranted(_.MarkEngine)).fold({
for {
spy isGranted(_.UserSpy) optionFu Env.security.userSpy(u.id)
info makeUserInfo(u, spy, ctx)
filters = mashup.GameFilterMenu(info, ctx.me, filterName)
pag (filters.query.fold(Env.bookmark.api.gamePaginatorByUser(u, page)) { query
gamePaginator.recentlyCreated(query, filters.cachedNb)(page)
})
} yield html.user.show(u, info, pag, filters)
}, fuccess(html.user.disabled(u)))
def list(page: Int) = Open { implicit ctx
Reasonable(page) {
onlineUsers zip env.paginator.elo(page) map {
case (users, pag) html.user.list(pag, users)
}
}
}
2013-03-20 08:23:41 -06:00
2013-05-08 09:41:12 -06:00
def online = Open { implicit ctx
onlineUsers map { html.user.online(_) }
}
2013-03-20 08:23:41 -06:00
2013-05-06 15:52:48 -06:00
def autocomplete = Open { implicit ctx
get("term", ctx.req).filter(""!=).fold(BadRequest("No search term provided").fuccess: Fu[Result]) { term
JsonOk(UserRepo usernamesLike term)
}
}
2013-03-20 08:23:41 -06:00
2013-05-08 09:41:12 -06:00
def getBio = Auth { ctx me Ok(me.bio).fuccess }
2013-03-20 08:23:41 -06:00
2013-05-08 09:41:12 -06:00
def setBio = AuthBody { ctx
me
implicit val req = ctx.body
JsonOk(forms.bio.bindFromRequest.fold(
f fulogwarn(f.errors.toString) inject ~me.bio,
bio UserRepo.setBio(me.id, bio) inject bio
) map { bio Map("bio" -> bio) })
}
2013-03-20 08:23:41 -06:00
2013-05-08 09:41:12 -06:00
def passwd = Auth { implicit ctx
me
Ok(html.user.passwd(me, forms.passwd)).fuccess
}
2013-03-20 08:23:41 -06:00
2013-05-08 09:41:12 -06:00
def passwdApply = AuthBody { implicit ctx
me
implicit val req = ctx.body
FormFuResult(forms.passwd) { err
html.user.passwd(me, err)
} { passwd
for {
ok UserRepo.checkPassword(me.id, passwd.oldPasswd)
_ ok ?? UserRepo.passwd(me.id, passwd.newPasswd1)
} yield ok.fold(
Redirect(routes.User show me.username),
BadRequest(html.user.passwd(me, forms.passwd))
)
}
}
2013-03-20 08:23:41 -06:00
2013-05-08 09:41:12 -06:00
def close = Auth { implicit ctx
me
Ok(html.user.close(me)).fuccess
}
2013-03-20 08:23:41 -06:00
2013-05-08 09:41:12 -06:00
def closeConfirm = Auth { ctx
me
implicit val req = ctx.req
(UserRepo disable me.id) >>
Env.team.api.quitAll(me.id) >>
(Env.security deleteUsername me.username) inject {
Redirect(routes.User show me.username) withCookies LilaCookie.newSession
}
}
2013-03-20 08:23:41 -06:00
2013-05-08 09:41:12 -06:00
def export(username: String) = TODO
// Open { implicit ctx ⇒
// OptionFuResult(UserRepo named username) { u ⇒
// Env.game.export(u).apply map { Redirect(_) }
2013-03-20 08:23:41 -06:00
// }
// }
2013-05-08 09:41:12 -06:00
private val onlineUsers: Fu[List[UserModel]] =
$find byIds env.usernameMemo.keys
2013-03-20 08:23:41 -06:00
}