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._
|
2013-06-28 05:52:52 -06:00
|
|
|
import lila.common.LilaCookie
|
|
|
|
import lila.db.api.$find
|
2013-12-24 06:58:54 -07:00
|
|
|
import lila.game.GameRepo
|
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
|
2013-05-08 09:41:12 -06:00
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def show(username: String) = Open { implicit ctx =>
|
2013-07-26 16:29:07 -06:00
|
|
|
filter(username, "all", 1)
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def showMini(username: String) = Open { implicit ctx =>
|
|
|
|
OptionFuResult(UserRepo named username) { user =>
|
|
|
|
GameRepo nowPlaying user.id map { game =>
|
2014-02-01 05:00:59 -07:00
|
|
|
Ok(html.user.mini(user, game)).withHeaders(CACHE_CONTROL -> "max-age=60")
|
|
|
|
}
|
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 =>
|
2013-05-20 22:12:10 -06:00
|
|
|
filter(username, filterName, page)
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def online = Open { implicit req =>
|
|
|
|
UserRepo.byIdsSortRating(env.onlineUserIdMemo.keys, 1000) map { users =>
|
2013-07-26 04:50:29 -06:00
|
|
|
html.user.online(users)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-20 22:12:10 -06:00
|
|
|
private def filter(username: String, filterName: String, page: Int)(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({
|
|
|
|
userShow(u, filterName, page) map { Ok(_) }
|
2014-02-17 02:12:19 -07:00
|
|
|
}, UserRepo isArtificial u.id map { artificial =>
|
2013-12-22 06:15:02 -07:00
|
|
|
NotFound(html.user.disabled(u, artificial))
|
|
|
|
})
|
2013-10-20 02:03:23 -06:00
|
|
|
}
|
2013-05-04 17:12:53 -06:00
|
|
|
}
|
2013-03-20 07:24:47 -06:00
|
|
|
|
2013-10-20 02:03:23 -06:00
|
|
|
private def userShow(u: UserModel, filterName: String, page: Int)(implicit ctx: Context) = for {
|
|
|
|
info ← Env.current.userInfo(u, ctx)
|
|
|
|
filters = mashup.GameFilterMenu(info, ctx.me, filterName)
|
2014-02-17 02:12:19 -07:00
|
|
|
pag ← (filters.query.fold(Env.bookmark.api.gamePaginatorByUser(u, page)) { query =>
|
2013-10-20 02:03:23 -06:00
|
|
|
gamePaginator.recentlyCreated(query, filters.cachedNb)(page)
|
|
|
|
})
|
|
|
|
} yield html.user.show(u, info, pag, filters)
|
2013-05-08 09:41:12 -06:00
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def list(page: Int) = Open { implicit ctx =>
|
2013-05-08 09:41:12 -06:00
|
|
|
Reasonable(page) {
|
2013-12-21 08:10:14 -07:00
|
|
|
val nb = 10
|
2013-12-21 12:17:52 -07:00
|
|
|
for {
|
|
|
|
progressDay ← env.cached.topProgressDay(nb)
|
|
|
|
progressWeek ← env.cached.topProgressWeek(nb)
|
|
|
|
progressMonth ← env.cached.topProgressMonth(nb)
|
|
|
|
rating ← env.cached.topRating(nb)
|
|
|
|
ratingDay ← env.cached.topRatingDay(nb)
|
|
|
|
ratingWeek ← env.cached.topRatingWeek(nb)
|
|
|
|
online ← env.cached.topOnline(30)
|
|
|
|
bullet ← env.cached.topBullet(nb)
|
|
|
|
blitz ← env.cached.topBlitz(nb)
|
|
|
|
slow ← env.cached.topSlow(nb)
|
2014-02-17 02:12:19 -07:00
|
|
|
active ← env.cached.topNbGame(nb) map2 { (user: UserModel) =>
|
2013-12-21 12:17:52 -07:00
|
|
|
user -> user.count.game
|
2013-07-24 16:59:45 -06:00
|
|
|
}
|
2014-02-17 02:12:19 -07:00
|
|
|
activeDay ← Env.game.cached.activePlayerUidsDay(nb) flatMap { pairs =>
|
2013-12-21 12:17:52 -07:00
|
|
|
UserRepo.byOrderedIds(pairs.map(_._1)) map (_ zip pairs.map(_._2))
|
|
|
|
}
|
2014-02-17 02:12:19 -07:00
|
|
|
activeWeek ← Env.game.cached.activePlayerUidsWeek(nb) flatMap { pairs =>
|
2013-12-21 12:17:52 -07:00
|
|
|
UserRepo.byOrderedIds(pairs.map(_._1)) map (_ zip pairs.map(_._2))
|
|
|
|
}
|
|
|
|
} yield html.user.list(
|
|
|
|
progressDay = progressDay,
|
|
|
|
progressWeek = progressWeek,
|
|
|
|
progressMonth = progressMonth,
|
|
|
|
rating = rating,
|
|
|
|
ratingDay = ratingDay,
|
|
|
|
ratingWeek = ratingWeek,
|
|
|
|
online = online,
|
|
|
|
bullet = bullet,
|
|
|
|
blitz = blitz,
|
|
|
|
slow = slow,
|
|
|
|
nb = active,
|
|
|
|
nbDay = activeDay,
|
|
|
|
nbWeek = activeWeek)
|
2013-05-08 09:41:12 -06:00
|
|
|
}
|
|
|
|
}
|
2013-03-20 08:23:41 -06:00
|
|
|
|
2014-02-01 13:40:05 -07:00
|
|
|
def leaderboard = Open { implicit ctx =>
|
|
|
|
UserRepo topRating 500 map { users =>
|
|
|
|
html.user.leaderboard(users)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 =>
|
|
|
|
Env.evaluation.evaluator.generate(user, true) map { _ =>
|
2014-01-13 14:11:17 -07:00
|
|
|
Redirect(routes.User.show(username).url + "?mod")
|
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 =>
|
|
|
|
lila.game.BestOpponents(user.id, 50) map { ops =>
|
2013-05-19 18:01:20 -06:00
|
|
|
html.user.opponents(user, ops)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def autocomplete = Open { implicit ctx =>
|
|
|
|
get("term", ctx.req).filter(""!=).fold(BadRequest("No search term provided").fuccess: Fu[SimpleResult]) { term =>
|
2013-05-06 15:52:48 -06:00
|
|
|
JsonOk(UserRepo usernamesLike term)
|
|
|
|
}
|
|
|
|
}
|
2013-03-20 08:23:41 -06:00
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def export(username: String) = Open { implicit ctx =>
|
|
|
|
OptionFuResult(UserRepo named username) { u =>
|
|
|
|
Env.game export u map { url =>
|
2013-06-28 05:52:52 -06:00
|
|
|
Redirect(Env.api.Net.Protocol + Env.api.Net.AssetDomain + url)
|
|
|
|
}
|
2013-05-10 03:57:18 -06:00
|
|
|
}
|
|
|
|
}
|
2013-03-20 08:23:41 -06:00
|
|
|
}
|