2013-03-20 08:23:41 -06:00
|
|
|
package controllers
|
|
|
|
|
2015-05-19 09:59:12 -06:00
|
|
|
import play.api.libs.json.Json
|
2013-09-11 04:38:16 -06:00
|
|
|
import play.api.mvc._, Results._
|
|
|
|
|
2015-09-03 03:37:25 -06:00
|
|
|
import lila.api.{ Context, BodyContext }
|
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
|
2015-04-25 23:44:35 -06:00
|
|
|
import lila.evaluation.{ PlayerAggregateAssessment }
|
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
|
2015-09-03 03:37:25 -06:00
|
|
|
private def userGameSearch = Env.gameSearch.userGameSearch
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 03:37:25 -06:00
|
|
|
def show(username: String) = OpenBody { 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
|
2015-12-09 02:31:39 -07:00
|
|
|
Env.donation.isDonor(user.id) zip
|
2016-01-23 14:33:41 -07:00
|
|
|
(ctx.userId ?? { relationApi.fetchBlocks(user.id, _) }) zip
|
2015-05-29 21:31:59 -06:00
|
|
|
(ctx.userId ?? { Env.game.crosstableApi(user.id, _) }) zip
|
2014-05-03 01:39:20 -06:00
|
|
|
(ctx.isAuth ?? { Env.pref.api.followable(user.id) }) zip
|
2016-01-23 14:33:41 -07:00
|
|
|
(ctx.userId ?? { relationApi.fetchRelation(_, user.id) }) map {
|
2015-09-13 03:03:01 -06:00
|
|
|
case (((((pov, donor), blocked), crosstable), followable), relation) =>
|
|
|
|
Ok(html.user.mini(user, pov, blocked, followable, relation, crosstable, donor))
|
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
|
|
|
|
2015-09-03 03:37:25 -06:00
|
|
|
def showFilter(username: String, filterName: String, page: Int) = OpenBody { 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 =>
|
2016-02-26 18:20:27 -07:00
|
|
|
val max = 50
|
2015-06-04 17:53:14 -06:00
|
|
|
def get(nb: Int) = UserRepo.byIdsSortRating(env.onlineUserIdMemo.keys, nb)
|
2015-06-04 15:37:31 -06:00
|
|
|
negotiate(
|
2016-02-26 18:20:27 -07:00
|
|
|
html = notFound,
|
|
|
|
api = _ => env.cached top50Online true map { list =>
|
|
|
|
Ok(Json.toJson(list.take(getInt("nb").fold(10)(_ min max)).map(env.jsonView(_))))
|
2015-06-04 17:53:14 -06:00
|
|
|
}
|
2015-06-04 15:37:31 -06:00
|
|
|
)
|
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,
|
2015-09-16 09:32:02 -06:00
|
|
|
status: Results.Status = Results.Ok)(implicit ctx: BodyContext[_]) =
|
2013-05-08 09:41:12 -06:00
|
|
|
Reasonable(page) {
|
2014-02-17 02:12:19 -07:00
|
|
|
OptionFuResult(UserRepo named username) { u =>
|
2015-05-19 09:59:12 -06:00
|
|
|
if (u.enabled || isGranted(_.UserSpy)) negotiate(
|
|
|
|
html = {
|
|
|
|
if (lila.common.HTTPRequest.isSynchronousHttp(ctx.req)) userShow(u, filterOption, page)
|
|
|
|
else userGames(u, filterOption, page) map {
|
|
|
|
case (filterName, pag) => html.user.games(u, pag, filterName)
|
|
|
|
}
|
|
|
|
} map { status(_) },
|
|
|
|
api = _ => userGames(u, filterOption, page) map {
|
2015-05-24 07:17:15 -06:00
|
|
|
case (filterName, pag) => Ok(Env.api.userGameApi.filter(filterName, pag))
|
2015-05-19 09:59:12 -06:00
|
|
|
})
|
|
|
|
else negotiate(
|
|
|
|
html = fuccess(NotFound(html.user.disabled(u))),
|
|
|
|
api = _ => fuccess(NotFound(Json.obj("error" -> "No such user, or account closed"))))
|
2013-10-20 02:03:23 -06:00
|
|
|
}
|
2013-05-04 17:12:53 -06:00
|
|
|
}
|
2013-03-20 07:24:47 -06:00
|
|
|
|
2015-09-16 09:32:02 -06:00
|
|
|
private def userShow(u: UserModel, filterOption: Option[String], page: Int)(implicit ctx: BodyContext[_]) = 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(
|
2015-09-03 03:37:25 -06:00
|
|
|
userGameSearch = userGameSearch,
|
2014-11-17 18:54:16 -07:00
|
|
|
user = u,
|
|
|
|
info = info.some,
|
|
|
|
filter = filters.current,
|
|
|
|
me = ctx.me,
|
2015-09-03 03:37:25 -06:00
|
|
|
page = page)(ctx.body)
|
2016-01-23 14:33:41 -07:00
|
|
|
relation <- ctx.userId ?? { relationApi.fetchRelation(_, u.id) }
|
2014-04-22 17:22:57 -06:00
|
|
|
notes <- ctx.me ?? { me =>
|
2016-01-23 14:33:41 -07:00
|
|
|
relationApi fetchFriends me.id flatMap { env.noteApi.get(u, me, _) }
|
2014-04-22 17:22:57 -06:00
|
|
|
}
|
2014-05-03 01:39:20 -06:00
|
|
|
followable <- ctx.isAuth ?? { Env.pref.api followable u.id }
|
2016-01-23 14:33:41 -07:00
|
|
|
blocked <- ctx.userId ?? { relationApi.fetchBlocks(u.id, _) }
|
2015-09-03 03:37:25 -06:00
|
|
|
searchForm = GameFilterMenu.searchForm(userGameSearch, filters.current)(ctx.body)
|
|
|
|
} yield html.user.show(u, info, pag, filters, searchForm, relation, notes, followable, blocked)
|
2013-05-08 09:41:12 -06:00
|
|
|
|
2015-09-16 09:32:02 -06:00
|
|
|
private def userGames(u: UserModel, filterOption: Option[String], page: Int)(implicit ctx: BodyContext[_]) = {
|
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({
|
2015-04-04 06:05:40 -06:00
|
|
|
Env.simul isHosting u.id map (_.fold(Playing, All).name)
|
2014-12-30 03:37:42 -07:00
|
|
|
})(fuccess) flatMap { filterName =>
|
|
|
|
GameFilterMenu.paginatorOf(
|
2015-09-03 03:37:25 -06:00
|
|
|
userGameSearch = userGameSearch,
|
2014-12-30 03:37:42 -07:00
|
|
|
user = u,
|
|
|
|
info = none,
|
|
|
|
filter = GameFilterMenu.currentOf(GameFilterMenu.all, filterName),
|
|
|
|
me = ctx.me,
|
|
|
|
page = page
|
2015-09-03 03:37:25 -06:00
|
|
|
)(ctx.body) map { 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 {
|
2016-01-19 10:01:57 -07:00
|
|
|
leaderboards <- env.cached.leaderboards
|
2016-01-10 19:07:45 -07:00
|
|
|
nbAllTime ← env.cached topNbGame nb
|
|
|
|
nbDay ← Env.game.cached activePlayerUidsDay nb map {
|
|
|
|
_ flatMap { pair =>
|
|
|
|
env lightUser pair.userId map { UserModel.LightCount(_, pair.nb) }
|
|
|
|
}
|
2014-08-02 11:33:46 -06:00
|
|
|
}
|
|
|
|
tourneyWinners ← Env.tournament.winners scheduled nb
|
2016-02-26 18:20:27 -07:00
|
|
|
online ← env.cached top50Online true
|
2015-05-29 03:51:12 -06:00
|
|
|
res <- negotiate(
|
|
|
|
html = fuccess(Ok(html.user.list(
|
|
|
|
tourneyWinners = tourneyWinners,
|
|
|
|
online = online,
|
2016-01-19 10:01:57 -07:00
|
|
|
leaderboards = leaderboards,
|
2015-10-07 01:59:40 -06:00
|
|
|
nbDay = nbDay,
|
2015-05-29 03:51:12 -06:00
|
|
|
nbAllTime = nbAllTime))),
|
|
|
|
api = _ => fuccess {
|
2016-02-22 21:50:36 -07:00
|
|
|
import lila.user.JsonView.lightPerfWrites
|
2015-05-29 03:51:12 -06:00
|
|
|
Ok(Json.obj(
|
2016-01-19 10:01:57 -07:00
|
|
|
"bullet" -> leaderboards.bullet,
|
|
|
|
"blitz" -> leaderboards.blitz,
|
|
|
|
"classical" -> leaderboards.classical,
|
|
|
|
"crazyhouse" -> leaderboards.crazyhouse,
|
|
|
|
"chess960" -> leaderboards.chess960,
|
|
|
|
"kingOfTheHill" -> leaderboards.kingOfTheHill,
|
|
|
|
"threeCheck" -> leaderboards.threeCheck,
|
|
|
|
"antichess" -> leaderboards.antichess,
|
|
|
|
"atomic" -> leaderboards.atomic,
|
|
|
|
"horde" -> leaderboards.horde,
|
|
|
|
"racingKings" -> leaderboards.racingKings))
|
2015-05-29 03:51:12 -06:00
|
|
|
})
|
|
|
|
} yield res
|
2013-05-08 09:41:12 -06:00
|
|
|
}
|
2013-03-20 08:23:41 -06:00
|
|
|
|
2016-01-10 18:12:37 -07:00
|
|
|
def top200(perfKey: String) = Open { implicit ctx =>
|
|
|
|
lila.rating.PerfType(perfKey).fold(notFound) { perfType =>
|
2016-02-23 19:45:38 -07:00
|
|
|
env.cached top200Perf perfType.id map { users =>
|
2016-01-10 18:12:37 -07:00
|
|
|
Ok(html.user.top200(perfType, users))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-22 21:50:36 -07:00
|
|
|
def topWeek = Open { implicit ctx =>
|
|
|
|
negotiate(
|
|
|
|
html = notFound,
|
2016-02-23 20:03:22 -07:00
|
|
|
api = _ => env.cached.topWeek(true).map { users =>
|
2016-02-23 20:27:29 -07:00
|
|
|
Ok(Json toJson users.map(env.jsonView.lightPerfIsOnline))
|
2016-02-22 21:50:36 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def mod(username: String) = Secure(_.UserSpy) { implicit ctx =>
|
|
|
|
me => OptionFuOk(UserRepo named username) { user =>
|
2015-08-12 16:50:05 -06:00
|
|
|
(!isGranted(_.SetEmail, user) ?? UserRepo.email(user.id)) zip
|
2015-08-12 05:17:16 -06:00
|
|
|
(Env.security userSpy user.id) zip
|
2015-12-09 02:31:39 -07:00
|
|
|
(Env.mod.assessApi.getPlayerAggregateAssessmentWithGames(user.id)) zip
|
2016-01-20 06:40:52 -07:00
|
|
|
Env.mod.logApi.userHistory(user.id) flatMap {
|
|
|
|
case ((((email, spy), playerAggregateAssessment), history)) =>
|
2015-08-11 17:13:44 -06:00
|
|
|
(Env.playban.api bans spy.usersSharingIp.map(_.id)) map { bans =>
|
2016-01-20 06:40:52 -07:00
|
|
|
html.user.mod(user, email, spy, playerAggregateAssessment, bans, history)
|
2015-04-25 23:44:35 -06:00
|
|
|
}
|
|
|
|
}
|
2014-01-13 14:11:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-01-23 14:33:41 -07:00
|
|
|
case ((u, nb), followable) => ctx.userId ?? {
|
|
|
|
relationApi.fetchRelation(_, u.id)
|
2014-05-21 12:56:50 -06:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 20:41:28 -07:00
|
|
|
def perfStat(username: String, perfKey: String) = Open { implicit ctx =>
|
2015-12-23 20:57:20 -07:00
|
|
|
OptionFuResult(UserRepo named username) { u =>
|
|
|
|
if ((u.disabled || (u.lame && !ctx.is(u))) && !isGranted(_.UserSpy)) notFound
|
|
|
|
else lila.rating.PerfType(perfKey).fold(notFound) { perfType =>
|
2015-12-25 23:52:06 -07:00
|
|
|
for {
|
|
|
|
perfStat <- Env.perfStat.get(u, perfType)
|
|
|
|
ranks <- Env.user.cached.ranking.getAll(u.id)
|
2015-12-26 02:41:38 -07:00
|
|
|
distribution <- u.perfs(perfType).established ?? {
|
2016-02-21 20:20:01 -07:00
|
|
|
Env.user.cached.ratingDistribution(perfType) map some
|
2015-12-26 02:41:38 -07:00
|
|
|
}
|
2015-12-25 23:52:06 -07:00
|
|
|
data = Env.perfStat.jsonView(u, perfStat, ranks get perfType.key, distribution)
|
2015-12-26 03:11:47 -07:00
|
|
|
response <- negotiate(
|
|
|
|
html = Ok(html.user.perfStat(u, ranks, perfType, data)).fuccess,
|
|
|
|
api = _ => Ok(data).fuccess)
|
|
|
|
} yield response
|
2015-12-23 20:41:28 -07: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
|
|
|
}
|