lila/app/controllers/Mod.scala

198 lines
6.5 KiB
Scala
Raw Normal View History

package controllers
import lila.app._
import lila.security.Permission
2013-05-16 16:00:28 -06:00
import lila.user.UserRepo
2014-02-26 17:18:09 -07:00
import views._
2015-01-23 01:13:56 -07:00
import org.joda.time.DateTime
import play.api.mvc._
import play.api.mvc.Results._
import lila.evaluation.{ PlayerAssessment }
2015-01-15 04:15:28 -07:00
2015-01-19 08:07:02 -07:00
import chess.Color
object Mod extends LilaController {
private def modApi = Env.mod.api
private def modLogApi = Env.mod.logApi
2015-01-15 04:15:28 -07:00
private def assessApi = Env.mod.assessApi
2014-02-17 02:12:19 -07:00
def engine(username: String) = Secure(_.MarkEngine) { _ =>
me => modApi.toggleEngine(me.id, username) inject redirect(username)
2013-05-10 03:56:34 -06:00
}
2015-02-13 07:14:39 -07:00
def booster(username: String) = Secure(_.MarkBooster) { _ =>
me => modApi.toggleBooster(me.id, username) inject redirect(username)
2015-02-13 07:14:39 -07:00
}
2015-07-01 03:15:54 -06:00
def troll(username: String) = Secure(_.MarkTroll) { implicit ctx =>
2014-02-17 02:12:19 -07:00
me =>
modApi.troll(me.id, username, getBool("set")) inject {
2015-07-01 03:15:54 -06:00
get("then") match {
case Some("reports") => Redirect(routes.Report.list)
case _ => redirect(username)
}
}
2013-05-10 03:56:34 -06:00
}
2014-02-17 02:12:19 -07:00
def ban(username: String) = Secure(_.IpBan) { implicit ctx =>
me => modApi.ban(me.id, username) inject redirect(username)
2013-05-10 03:56:34 -06:00
}
2014-02-17 02:12:19 -07:00
def ipban(ip: String) = Secure(_.IpBan) { implicit ctx =>
me => modApi.ipban(me.id, ip)
}
2014-02-17 02:12:19 -07:00
def closeAccount(username: String) = Secure(_.CloseAccount) { implicit ctx =>
2016-03-01 18:27:36 -07:00
me => modApi.closeAccount(me.id, username) flatMap {
_ ?? Account.doClose
} inject redirect(username)
2014-02-01 06:13:22 -07:00
}
2014-02-17 02:12:19 -07:00
def reopenAccount(username: String) = Secure(_.ReopenAccount) { implicit ctx =>
me => modApi.reopenAccount(me.id, username) inject redirect(username)
2013-09-11 04:38:16 -06:00
}
2016-06-20 10:44:30 -06:00
def setTitle(username: String) = SecureBody(_.SetTitle) { implicit ctx =>
2014-02-26 17:18:09 -07:00
me =>
implicit def req = ctx.body
2016-06-20 10:44:30 -06:00
lila.user.DataForm.title.bindFromRequest.fold(
err => fuccess(redirect(username, mod = true)),
title => modApi.setTitle(me.id, username, title) inject redirect(username, mod = false)
)
2014-02-26 17:18:09 -07:00
}
2016-06-20 10:44:30 -06:00
def setEmail(username: String) = SecureBody(_.SetEmail) { implicit ctx =>
2015-08-12 05:17:16 -06:00
me =>
implicit def req = ctx.body
2015-08-12 16:50:05 -06:00
OptionFuResult(UserRepo named username) { user =>
2016-06-20 10:44:30 -06:00
Env.security.forms.modEmail(user).bindFromRequest.fold(
err => BadRequest(err.toString).fuccess,
email => modApi.setEmail(me.id, user.id, email) inject redirect(user.username, mod = true)
)
2015-08-12 16:50:05 -06:00
}
2015-08-12 05:17:16 -06:00
}
2016-01-04 21:18:39 -07:00
def notifySlack(username: String) = Auth { implicit ctx =>
me =>
OptionFuResult(UserRepo named username) { user =>
Env.slack.api.userMod(user = user, mod = me) inject redirect(user.username)
}
}
2015-09-12 16:28:27 -06:00
def log = Secure(_.SeeReport) { implicit ctx =>
2014-02-17 02:12:19 -07:00
me => modLogApi.recent map { html.mod.log(_) }
2013-05-10 03:56:34 -06:00
}
2014-01-16 01:46:01 -07:00
def communication(username: String) = Secure(_.MarkTroll) { implicit ctx =>
me =>
OptionFuOk(UserRepo named username) { user =>
for {
povs <- lila.game.GameRepo.recentPovsByUser(user, 100)
chats <- povs.map(p => Env.chat.api.playerChat findNonEmpty p.gameId).sequence
povWithChats = (povs zip chats) collect {
case (p, Some(c)) => p -> c
} take 9
2015-05-29 05:09:30 -06:00
threads <- {
lila.message.ThreadRepo.visibleByUser(user.id, 50) map {
_ filter (_ hasPostsWrittenBy user.id) take 9
}
}
publicLines <- Env.shutup.api getPublicLines user.id
spy <- Env.security userSpy user.id
} yield html.mod.communication(user, povWithChats, threads, publicLines, spy)
}
}
2015-08-20 16:45:48 -06:00
private val ipIntelCache =
2016-03-11 21:29:21 -07:00
lila.memo.AsyncCache[String, Int](ip => {
import play.api.libs.ws.WS
import play.api.Play.current
val email = "lichess.contact@gmail.com"
val url = s"http://check.getipintel.net/check.php?ip=$ip&contact=$email"
2016-03-11 21:29:21 -07:00
WS.url(url).get().map(_.body).mon(_.security.proxy.request.time).flatMap { str =>
2016-06-20 03:58:59 -06:00
parseFloatOption(str).fold[Fu[Int]](fufail(s"Invalid ratio ${str.take(140)}")) { ratio =>
2016-03-11 21:29:21 -07:00
fuccess((ratio * 100).toInt)
}
}.addEffects(
fail = _ => lila.mon.security.proxy.request.failure(),
succ = percent => {
lila.mon.security.proxy.percent(percent max 0)
2016-03-11 21:29:21 -07:00
lila.mon.security.proxy.request.success()
})
}, maxCapacity = 1024)
2015-08-20 16:45:48 -06:00
def ipIntel(ip: String) = Secure(_.IpBan) { ctx =>
me =>
2016-07-10 03:52:09 -06:00
ipIntelCache(ip).map { Ok(_) }.recover {
case e: Exception => InternalServerError(e.getMessage)
}
}
def redirect(username: String, mod: Boolean = true) = Redirect(routes.User.show(username).url + mod.??("?mod"))
def refreshUserAssess(username: String) = Secure(_.MarkEngine) { implicit ctx =>
me => assessApi.refreshAssessByUsername(username) inject redirect(username)
}
2016-01-10 21:09:37 -07:00
def gamify = Secure(_.SeeReport) { implicit ctx =>
me =>
2016-01-11 02:37:09 -07:00
Env.mod.gamify.leaderboards zip
Env.mod.gamify.history(orCompute = true) map {
case (leaderboards, history) => Ok(html.mod.gamify.index(leaderboards, history))
}
2016-01-10 22:18:26 -07:00
}
def gamifyPeriod(periodStr: String) = Secure(_.SeeReport) { implicit ctx =>
me =>
lila.mod.Gamify.Period(periodStr).fold(notFound) { period =>
Env.mod.gamify.leaderboards map { leaderboards =>
Ok(html.mod.gamify.period(leaderboards, period))
}
2016-01-10 21:09:37 -07:00
}
}
def search = Secure(_.UserSearch) { implicit ctx =>
me =>
val query = (~get("q")).trim
Env.mod.search(query) map { users =>
html.mod.search(query, users)
}
}
2016-06-10 18:13:57 -06:00
2016-06-19 08:03:04 -06:00
def chatUser(username: String) = Secure(_.ChatTimeout) { implicit ctx =>
2016-06-10 18:13:57 -06:00
me =>
implicit val lightUser = Env.user.lightUser _
JsonOptionOk {
Env.chat.api.userChat userModInfo username map2 lila.chat.JsonView.userModInfo
}
}
2016-06-20 10:44:30 -06:00
def powaaa(username: String) = Secure(_.ChangePermission) { implicit ctx =>
me =>
OptionOk(UserRepo named username) { user =>
html.mod.powaaa(user)
}
}
def savePowaaa(username: String) = SecureBody(_.ChangePermission) { implicit ctx =>
me =>
implicit def req = ctx.body
OptionFuResult(UserRepo named username) { user =>
import play.api.data._
import play.api.data.Forms._
Form(single(
"permissions" -> list(nonEmptyText.verifying { str =>
lila.security.Permission.allButSuperAdmin.exists(_.name == str)
})
)).bindFromRequest.fold(
err => BadRequest(html.mod.powaaa(user)).fuccess,
permissions =>
UserRepo.setRoles(user.id, permissions map (_.toUpperCase)) inject
Redirect(routes.User.show(user.username) + "?mod")
)
}
}
}