2013-03-20 08:23:41 -06:00
|
|
|
package controllers
|
|
|
|
|
2013-09-11 04:38:16 -06:00
|
|
|
import play.api.mvc._, Results._
|
|
|
|
|
2014-01-13 14:11:17 -07:00
|
|
|
import lila.api.Context
|
2013-03-20 08:23:41 -06:00
|
|
|
import lila.app._
|
2014-06-28 03:58:38 -06:00
|
|
|
import lila.app.mashup.GameFilterMenu
|
2013-06-28 05:52:52 -06:00
|
|
|
import lila.common.LilaCookie
|
|
|
|
import lila.db.api.$find
|
2014-08-15 05:44:32 -06:00
|
|
|
import lila.game.{ GameRepo, Pov }
|
|
|
|
import lila.rating.PerfType
|
2013-03-20 08:23:41 -06:00
|
|
|
import lila.security.Permission
|
2013-05-08 09:41:12 -06:00
|
|
|
import lila.user.tube.userTube
|
2014-02-17 02:12:19 -07:00
|
|
|
import lila.user.{ User => UserModel, UserRepo }
|
2013-06-28 05:52:52 -06:00
|
|
|
import views._
|
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
|
2014-04-17 06:10:08 -06:00
|
|
|
private def relationApi = Env.relation.api
|
2013-05-08 09:41:12 -06:00
|
|
|
|
2014-08-15 05:44:32 -06:00
|
|
|
def tv(username: String) = Open { implicit ctx =>
|
|
|
|
OptionFuResult(UserRepo named username) { user =>
|
2014-12-26 14:18:32 -07:00
|
|
|
(GameRepo lastPlayedPlaying user) orElse
|
2014-12-23 17:34:13 -07:00
|
|
|
(GameRepo lastPlayed user) flatMap {
|
2014-12-20 07:01:19 -07:00
|
|
|
_.fold(fuccess(Redirect(routes.User.show(username)))) { pov =>
|
2014-08-15 05:44:32 -06:00
|
|
|
Round.watch(pov, userTv = user.some)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def show(username: String) = Open { implicit ctx =>
|
2014-03-16 14:57:15 -06:00
|
|
|
filter(username, none, 1)
|
2013-07-26 16:29:07 -06:00
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def showMini(username: String) = Open { implicit ctx =>
|
|
|
|
OptionFuResult(UserRepo named username) { user =>
|
2014-12-26 14:18:32 -07:00
|
|
|
GameRepo lastPlayedPlaying user zip
|
2014-04-17 06:10:08 -06:00
|
|
|
(ctx.userId ?? { relationApi.blocks(user.id, _) }) zip
|
2014-05-03 01:39:20 -06:00
|
|
|
(ctx.isAuth ?? { Env.pref.api.followable(user.id) }) zip
|
2014-04-22 17:28:19 -06:00
|
|
|
(ctx.userId ?? { relationApi.relation(_, user.id) }) map {
|
2014-12-20 07:01:19 -07:00
|
|
|
case (((pov, blocked), followable), relation) =>
|
|
|
|
Ok(html.user.mini(user, pov, blocked, followable, relation))
|
2014-05-14 14:01:22 -06:00
|
|
|
.withHeaders(CACHE_CONTROL -> "max-age=5")
|
2014-04-17 03:34:55 -06:00
|
|
|
}
|
2013-12-24 06:58:54 -07:00
|
|
|
}
|
2013-05-20 22:12:10 -06:00
|
|
|
}
|
2013-05-08 09:41:12 -06:00
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def showFilter(username: String, filterName: String, page: Int) = Open { implicit ctx =>
|
2014-03-16 14:57:15 -06:00
|
|
|
filter(username, filterName.some, page)
|
2013-05-20 22:12:10 -06:00
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def online = Open { implicit req =>
|
2014-04-23 12:04:52 -06:00
|
|
|
val max = 1000
|
2014-08-06 02:56:21 -06:00
|
|
|
UserRepo.byIdsSortRating(env.onlineUserIdMemo.keys, max) map { html.user.online(_, max) }
|
2013-07-26 04:50:29 -06:00
|
|
|
}
|
|
|
|
|
2014-04-22 17:22:57 -06:00
|
|
|
private def filter(
|
|
|
|
username: String,
|
|
|
|
filterOption: Option[String],
|
|
|
|
page: Int,
|
|
|
|
status: Results.Status = Results.Ok)(implicit ctx: Context) =
|
2013-05-08 09:41:12 -06:00
|
|
|
Reasonable(page) {
|
2014-02-17 02:12:19 -07:00
|
|
|
OptionFuResult(UserRepo named username) { u =>
|
2013-10-20 02:03:23 -06:00
|
|
|
(u.enabled || isGranted(_.UserSpy)).fold({
|
2014-06-28 03:58:38 -06:00
|
|
|
if (lila.common.HTTPRequest.isSynchronousHttp(ctx.req))
|
|
|
|
userShow(u, filterOption, page)
|
|
|
|
else
|
|
|
|
userGames(u, filterOption, page)
|
|
|
|
} map { status(_) },
|
2014-12-10 15:15:59 -07:00
|
|
|
fuccess(NotFound(html.user.disabled(u)))
|
|
|
|
)
|
2013-10-20 02:03:23 -06:00
|
|
|
}
|
2013-05-04 17:12:53 -06:00
|
|
|
}
|
2013-03-20 07:24:47 -06:00
|
|
|
|
2014-03-16 14:57:15 -06:00
|
|
|
private def userShow(u: UserModel, filterOption: Option[String], page: Int)(implicit ctx: Context) = for {
|
2013-10-20 02:03:23 -06:00
|
|
|
info ← Env.current.userInfo(u, ctx)
|
2014-12-26 09:59:14 -07:00
|
|
|
filters = GameFilterMenu(info, ctx.me, filterOption)
|
2014-11-17 18:54:16 -07:00
|
|
|
pag <- GameFilterMenu.paginatorOf(
|
|
|
|
user = u,
|
|
|
|
info = info.some,
|
|
|
|
filter = filters.current,
|
|
|
|
me = ctx.me,
|
|
|
|
page = page)
|
2014-04-17 06:10:08 -06:00
|
|
|
relation <- ctx.userId ?? { relationApi.relation(_, u.id) }
|
2014-04-22 17:22:57 -06:00
|
|
|
notes <- ctx.me ?? { me =>
|
|
|
|
relationApi friends me.id flatMap { env.noteApi.get(u, me, _) }
|
|
|
|
}
|
2014-05-03 01:39:20 -06:00
|
|
|
followable <- ctx.isAuth ?? { Env.pref.api followable u.id }
|
2014-05-26 06:49:43 -06:00
|
|
|
blocked <- ctx.userId ?? { relationApi.blocks(u.id, _) }
|
|
|
|
} yield html.user.show(u, info, pag, filters, relation, notes, followable, blocked)
|
2013-05-08 09:41:12 -06:00
|
|
|
|
2014-06-28 03:58:38 -06:00
|
|
|
private def userGames(u: UserModel, filterOption: Option[String], page: Int)(implicit ctx: Context) = {
|
2014-12-26 09:59:14 -07:00
|
|
|
import lila.app.mashup.GameFilter.{ All, Playing }
|
2014-12-30 03:37:42 -07:00
|
|
|
filterOption.fold({
|
|
|
|
Env.game.cached isPlayingSimul u.id map (_.fold(Playing, All).name)
|
|
|
|
})(fuccess) flatMap { filterName =>
|
|
|
|
GameFilterMenu.paginatorOf(
|
|
|
|
user = u,
|
|
|
|
info = none,
|
|
|
|
filter = GameFilterMenu.currentOf(GameFilterMenu.all, filterName),
|
|
|
|
me = ctx.me,
|
|
|
|
page = page
|
|
|
|
) map { html.user.games(u, _, filterName) }
|
2014-12-26 09:59:14 -07:00
|
|
|
}
|
2014-06-28 03:58:38 -06:00
|
|
|
}
|
|
|
|
|
2014-08-02 11:33:46 -06:00
|
|
|
def list = Open { implicit ctx =>
|
|
|
|
val nb = 10
|
|
|
|
for {
|
2014-08-03 14:03:17 -06:00
|
|
|
bullet ← env.cached topPerf PerfType.Bullet.key
|
|
|
|
blitz ← env.cached topPerf PerfType.Blitz.key
|
|
|
|
classical ← env.cached topPerf PerfType.Classical.key
|
2015-01-12 13:56:55 -07:00
|
|
|
correspondence ← env.cached topPerf PerfType.Correspondence.key
|
2014-08-03 14:03:17 -06:00
|
|
|
chess960 ← env.cached topPerf PerfType.Chess960.key
|
|
|
|
kingOfTheHill ← env.cached topPerf PerfType.KingOfTheHill.key
|
|
|
|
threeCheck ← env.cached topPerf PerfType.ThreeCheck.key
|
2014-12-22 19:34:49 -07:00
|
|
|
antichess <- env.cached topPerf PerfType.Antichess.key
|
2015-01-03 18:30:17 -07:00
|
|
|
atomic <- env.cached topPerf PerfType.Atomic.key
|
2014-08-02 11:33:46 -06:00
|
|
|
nbAllTime ← env.cached topNbGame nb map2 { (user: UserModel) =>
|
|
|
|
user -> user.count.game
|
|
|
|
}
|
|
|
|
nbWeek ← Env.game.cached activePlayerUidsWeek nb flatMap { pairs =>
|
2014-12-23 06:06:00 -07:00
|
|
|
UserRepo.byOrderedIds(pairs.map(_.userId)) map (_ zip pairs.map(_.nb))
|
2014-08-02 11:33:46 -06:00
|
|
|
}
|
|
|
|
tourneyWinners ← Env.tournament.winners scheduled nb
|
|
|
|
online ← env.cached topOnline 30
|
|
|
|
} yield html.user.list(
|
|
|
|
tourneyWinners = tourneyWinners,
|
|
|
|
online = online,
|
|
|
|
bullet = bullet,
|
|
|
|
blitz = blitz,
|
|
|
|
classical = classical,
|
2015-01-12 13:56:55 -07:00
|
|
|
correspondence = correspondence,
|
2014-08-02 11:33:46 -06:00
|
|
|
chess960 = chess960,
|
|
|
|
kingOfTheHill = kingOfTheHill,
|
|
|
|
threeCheck = threeCheck,
|
2014-12-22 19:34:49 -07:00
|
|
|
antichess = antichess,
|
2015-01-03 18:30:17 -07:00
|
|
|
atomic = atomic,
|
2014-08-02 11:33:46 -06:00
|
|
|
nbWeek = nbWeek,
|
|
|
|
nbAllTime = nbAllTime)
|
2013-05-08 09:41:12 -06:00
|
|
|
}
|
2013-03-20 08:23:41 -06:00
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def mod(username: String) = Secure(_.UserSpy) { implicit ctx =>
|
|
|
|
me => OptionFuOk(UserRepo named username) { user =>
|
2014-01-27 06:38:23 -07:00
|
|
|
Env.evaluation.evaluator find user zip
|
2014-01-13 14:11:17 -07:00
|
|
|
(Env.security userSpy user.id) map {
|
2014-02-17 02:12:19 -07:00
|
|
|
case (eval, spy) => html.user.mod(user, spy, eval)
|
2014-01-13 14:11:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def evaluate(username: String) = Secure(_.UserEvaluate) { implicit ctx =>
|
|
|
|
me => OptionFuResult(UserRepo named username) { user =>
|
2014-08-09 01:51:11 -06:00
|
|
|
Env.evaluation.evaluator.generate(user.id, true) inject Redirect(routes.User.show(username).url + "?mod")
|
2014-04-22 17:22:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def writeNote(username: String) = AuthBody { implicit ctx =>
|
|
|
|
me => OptionFuResult(UserRepo named username) { user =>
|
|
|
|
implicit val req = ctx.body
|
|
|
|
env.forms.note.bindFromRequest.fold(
|
|
|
|
err => filter(username, none, 1, Results.BadRequest),
|
|
|
|
text => env.noteApi.write(user, text, me) inject Redirect(routes.User.show(username).url + "?note")
|
|
|
|
)
|
2013-05-15 13:28:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def opponents(username: String) = Open { implicit ctx =>
|
|
|
|
OptionFuOk(UserRepo named username) { user =>
|
2014-05-03 01:39:20 -06:00
|
|
|
lila.game.BestOpponents(user.id, 50) flatMap { ops =>
|
2014-05-21 12:56:50 -06:00
|
|
|
ctx.isAuth.fold(
|
|
|
|
Env.pref.api.followables(ops map (_._1.id)),
|
|
|
|
fuccess(List.fill(50)(true))
|
|
|
|
) flatMap { followables =>
|
|
|
|
(ops zip followables).map {
|
|
|
|
case ((u, nb), followable) => ctx.userId ?? { myId =>
|
|
|
|
relationApi.relation(myId, u.id)
|
|
|
|
} map { lila.relation.Related(u, nb, followable, _) }
|
|
|
|
}.sequenceFu map { relateds =>
|
|
|
|
html.user.opponents(user, relateds)
|
|
|
|
}
|
2014-05-03 01:39:20 -06:00
|
|
|
}
|
2013-05-19 18:01:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def autocomplete = Open { implicit ctx =>
|
2014-06-01 17:01:39 -06:00
|
|
|
get("term", ctx.req).filter(_.nonEmpty).fold(BadRequest("No search term provided").fuccess: Fu[Result]) { term =>
|
2013-05-06 15:52:48 -06:00
|
|
|
JsonOk(UserRepo usernamesLike term)
|
|
|
|
}
|
|
|
|
}
|
2013-03-20 08:23:41 -06:00
|
|
|
}
|