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) _
|
|
|
|
|
2013-05-20 22:12:10 -06:00
|
|
|
def show(username: String) = Open { implicit ctx ⇒
|
|
|
|
isXhr.fold(mini(username), filter(username, "all", 1))
|
|
|
|
}
|
2013-05-08 09:41:12 -06:00
|
|
|
|
|
|
|
def showFilter(username: String, filterName: String, page: Int) = Open { implicit ctx ⇒
|
2013-05-20 22:12:10 -06:00
|
|
|
filter(username, filterName, page)
|
|
|
|
}
|
|
|
|
|
|
|
|
private def filter(username: String, filterName: String, page: Int)(implicit ctx: Context) =
|
2013-05-08 09:41:12 -06:00
|
|
|
Reasonable(page) {
|
2013-05-14 21:59:04 -06:00
|
|
|
OptionFuOk(UserRepo named 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) =
|
2013-05-15 13:28:46 -06:00
|
|
|
(u.enabled || isGranted(_.UserSpy)).fold({
|
2013-05-08 09:41:12 -06:00
|
|
|
for {
|
2013-05-15 13:28:46 -06:00
|
|
|
info ← makeUserInfo(u, ctx)
|
2013-05-08 09:41:12 -06:00
|
|
|
filters = mashup.GameFilterMenu(info, ctx.me, filterName)
|
|
|
|
pag ← (filters.query.fold(Env.bookmark.api.gamePaginatorByUser(u, page)) { query ⇒
|
|
|
|
gamePaginator.recentlyCreated(query, filters.cachedNb)(page)
|
|
|
|
})
|
2013-05-23 10:59:46 -06:00
|
|
|
nbFollowing ← Env.relation.api nbFollowing u.id
|
|
|
|
nbFollowers ← Env.relation.api nbFollowers u.id
|
|
|
|
} yield html.user.show(u, info, pag, nbFollowing, nbFollowers, filters)
|
2013-05-08 09:41:12 -06:00
|
|
|
}, fuccess(html.user.disabled(u)))
|
|
|
|
|
2013-05-20 22:12:10 -06:00
|
|
|
private def mini(username: String)(implicit ctx: Context) =
|
|
|
|
OptionOk(UserRepo named username) { user ⇒
|
2013-05-22 06:49:09 -06:00
|
|
|
Thread sleep 200
|
2013-05-20 22:12:10 -06:00
|
|
|
html.user.mini(user)
|
|
|
|
}
|
|
|
|
|
2013-05-08 09:41:12 -06:00
|
|
|
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-15 13:28:46 -06:00
|
|
|
def mod(username: String) = Secure(_.UserSpy) { implicit ctx ⇒
|
|
|
|
me ⇒ OptionFuOk(UserRepo named username) { user ⇒
|
|
|
|
Env.security userSpy user.id map { spy ⇒
|
|
|
|
html.user.mod(user, spy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-19 18:01:20 -06:00
|
|
|
def opponents(username: String) = Open { implicit ctx ⇒
|
|
|
|
OptionFuOk(UserRepo named username) { user ⇒
|
|
|
|
mashup.UserInfo.bestOpponents(user.id, 50) map { ops ⇒
|
|
|
|
html.user.opponents(user, ops)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 ⇒
|
2013-05-08 10:15:55 -06:00
|
|
|
fuccess(html.user.passwd(me, err))
|
2013-05-08 09:41:12 -06:00
|
|
|
} { 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
|
2013-05-15 13:28:46 -06:00
|
|
|
(UserRepo disable me.id) >>
|
|
|
|
Env.team.api.quitAll(me.id) >>
|
2013-05-15 19:53:16 -06:00
|
|
|
(Env.security disconnect me.id) inject {
|
2013-05-15 13:28:46 -06:00
|
|
|
Redirect(routes.User show me.username) withCookies LilaCookie.newSession
|
|
|
|
}
|
2013-05-08 09:41:12 -06:00
|
|
|
}
|
2013-03-20 08:23:41 -06:00
|
|
|
|
2013-05-10 03:57:18 -06:00
|
|
|
def export(username: String) = Open { implicit ctx ⇒
|
|
|
|
OptionFuResult(UserRepo named username) { u ⇒
|
|
|
|
Env.game export u map { Redirect(_) }
|
|
|
|
}
|
|
|
|
}
|
2013-03-20 08:23:41 -06:00
|
|
|
|
2013-05-19 08:27:32 -06:00
|
|
|
private def onlineUsers: Fu[List[UserModel]] = $find byIds env.onlineUserIdMemo.keys
|
2013-03-20 08:23:41 -06:00
|
|
|
}
|