lila/app/controllers/Report.scala

161 lines
5.6 KiB
Scala
Raw Normal View History

package controllers
2017-09-12 17:26:36 -06:00
import play.api.mvc.AnyContentAsFormUrlEncoded
import views._
2017-09-12 17:26:36 -06:00
import lila.api.{ Context, BodyContext }
import lila.app._
2018-03-16 17:18:09 -06:00
import lila.common.HTTPRequest
import lila.report.{ Room, Report => ReportModel, Mod => AsMod, Reporter, Suspect }
2019-12-04 16:39:16 -07:00
import lila.user.{ User => UserModel }
2019-12-04 18:47:46 -07:00
final class Report(
env: Env,
2019-12-05 14:51:18 -07:00
userC: => User,
modC: => Mod
2019-12-04 18:47:46 -07:00
) extends LilaController(env) {
2019-12-04 16:39:16 -07:00
private def api = env.report.api
def list = Secure(_.SeeReport) { implicit ctx => me =>
2019-12-04 16:39:16 -07:00
if (env.streamer.liveStreamApi.isStreaming(me.id) && !getBool("force")) fuccess(Forbidden(html.site.message.streamingMod))
2019-12-04 21:32:03 -07:00
else renderList(env.report.modFilters.get(me).fold("all")(_.key))
}
2017-05-10 05:42:10 -06:00
def listWithFilter(room: String) = Secure(_.SeeReport) { implicit ctx => me =>
2019-12-04 21:32:03 -07:00
env.report.modFilters.set(me, Room(room))
2017-05-10 05:42:10 -06:00
renderList(room)
}
2013-07-27 14:55:29 -06:00
2017-05-10 05:42:10 -06:00
private def renderList(room: String)(implicit ctx: Context) =
2018-11-05 07:36:22 -07:00
api.openAndRecentWithFilter(12, Room(room)) zip
2017-12-31 08:05:15 -07:00
api.countOpenByRooms zip
2019-12-04 16:39:16 -07:00
env.streamer.api.approval.countRequests flatMap {
2017-12-31 08:05:15 -07:00
case reports ~ counts ~ streamers =>
2019-12-04 16:39:16 -07:00
(env.user.lightUserApi preloadMany reports.flatMap(_.report.userIds)) inject
2017-12-31 08:05:15 -07:00
Ok(html.report.list(reports, room, counts, streamers))
2017-03-30 09:17:02 -06:00
}
2017-05-09 17:23:10 -06:00
def inquiry(id: String) = Secure(_.SeeReport) { implicit ctx => me =>
2017-09-11 15:59:53 -06:00
for {
2019-12-04 21:32:03 -07:00
current <- api.inquiries ofModId me.id
2017-09-11 15:59:53 -06:00
newInquiry <- api.inquiries.toggle(AsMod(me), id)
} yield newInquiry.fold(Redirect(routes.Report.list))(onInquiryStart)
2017-05-09 17:23:10 -06:00
}
private def onInquiryStart(inquiry: ReportModel) =
inquiry.room match {
case Room.Comm => Redirect(routes.Mod.communicationPrivate(inquiry.user))
2019-12-04 18:47:46 -07:00
case _ => modC.redirect(inquiry.user)
}
protected[controllers] def onInquiryClose(
inquiry: Option[ReportModel],
me: UserModel,
goTo: Option[Suspect],
force: Boolean = false
)(implicit ctx: BodyContext[_]) = {
2018-03-16 17:18:09 -06:00
goTo.ifTrue(HTTPRequest isXhr ctx.req) match {
2019-12-04 18:47:46 -07:00
case Some(suspect) => userC.renderModZoneActions(suspect.user.username)
case None =>
2019-08-02 03:33:37 -06:00
val dataOpt = ctx.body.body match {
case AnyContentAsFormUrlEncoded(data) => data.some
case _ => none
}
2018-03-16 17:18:09 -06:00
inquiry match {
case None =>
goTo.fold(Redirect(routes.Report.list).fuccess) { s =>
2019-12-04 18:47:46 -07:00
userC.modZoneOrRedirect(s.user.username, me)
2018-03-16 17:18:09 -06:00
}
case Some(prev) =>
2019-08-02 03:33:37 -06:00
def thenGoTo = dataOpt.flatMap(_ get "then").flatMap(_.headOption) flatMap {
case "back" => HTTPRequest referer ctx.req
2019-12-04 18:47:46 -07:00
case "profile" => modC.userUrl(prev.user, true).some
2019-08-02 03:33:37 -06:00
case url => url.some
}
thenGoTo match {
case Some(url) => api.inquiries.toggle(AsMod(me), prev.id) inject Redirect(url)
case _ =>
def redirectToList = Redirect(routes.Report.listWithFilter(prev.room.key))
if (dataOpt.flatMap(_ get "next").exists(_.headOption contains "1"))
api.next(prev.room) flatMap {
_.fold(redirectToList.fuccess) { report =>
api.inquiries.toggle(AsMod(me), report.id) map {
_.fold(redirectToList)(onInquiryStart)
}
}
}
2019-12-04 18:47:46 -07:00
else if (force) userC.modZoneOrRedirect(prev.user, me)
2019-08-02 03:33:37 -06:00
else api.inquiries.toggle(AsMod(me), prev.id) map {
2018-03-16 17:18:09 -06:00
_.fold(redirectToList)(onInquiryStart)
}
2017-09-12 17:26:36 -06:00
}
}
2017-09-12 17:26:36 -06:00
}
}
2017-09-12 17:26:36 -06:00
def process(id: String) = SecureBody(_.SeeReport) { implicit ctx => me =>
2019-12-04 21:32:03 -07:00
api.inquiries ofModId me.id flatMap { inquiry =>
api.process(AsMod(me), id) >> onInquiryClose(inquiry, me, none, force = true)
2017-09-11 15:59:53 -06:00
}
2013-07-27 14:55:29 -06:00
}
def xfiles(id: String) = Secure(_.SeeReport) { implicit ctx => me =>
api.moveToXfiles(id) inject Redirect(routes.Report.list)
}
2018-03-15 20:27:09 -06:00
def currentCheatInquiry(username: String) = Secure(_.Hunter) { implicit ctx => me =>
2019-12-04 18:47:46 -07:00
OptionFuResult(env.user.repo named username) { user =>
2019-12-04 21:32:03 -07:00
api.currentCheatReport(lila.report.Suspect(user)) flatMap {
2018-03-15 20:27:09 -06:00
_ ?? { report =>
2019-12-04 21:32:03 -07:00
api.inquiries.toggle(lila.report.Mod(me), report.id)
} inject modC.redirect(username, true)
2018-03-15 20:27:09 -06:00
}
}
}
2016-10-19 08:55:32 -06:00
def form = Auth { implicit ctx => implicit me =>
2019-12-04 18:47:46 -07:00
get("username") ?? env.user.repo.named flatMap { user =>
2019-12-04 21:32:03 -07:00
env.report.forms.createWithCaptcha map {
2017-11-30 09:04:26 -07:00
case (form, captcha) => Ok(html.report.form(form, user, captcha))
}
2016-10-19 08:55:32 -06:00
}
}
2016-10-19 08:55:32 -06:00
def create = AuthBody { implicit ctx => implicit me =>
implicit val req = ctx.body
2019-12-04 21:32:03 -07:00
env.report.forms.create.bindFromRequest.fold(
2019-12-04 18:47:46 -07:00
err => get("username") ?? env.user.repo.named flatMap { user =>
2019-12-04 21:32:03 -07:00
env.report.forms.anyCaptcha map { captcha =>
2016-10-19 08:55:32 -06:00
BadRequest(html.report.form(err, user, captcha))
}
},
2017-10-23 06:36:54 -06:00
data =>
if (data.user == me) notFound
2017-11-30 22:22:12 -07:00
else api.create(data candidate lila.report.Reporter(me)) map { report =>
2017-10-23 06:36:54 -06:00
Redirect(routes.Report.thanks(data.user.username))
}
)
}
def flag = AuthBody { implicit ctx => implicit me =>
implicit val req = ctx.body
2019-12-04 21:32:03 -07:00
env.report.forms.flag.bindFromRequest.fold(
err => BadRequest.fuccess,
2019-12-04 18:47:46 -07:00
data => env.user.repo named data.username flatMap {
_ ?? { user =>
if (user == me) BadRequest.fuccess
else api.commFlag(Reporter(me), Suspect(user), data.resource, data.text) inject Ok
}
}
)
}
2016-10-19 08:55:32 -06:00
def thanks(reported: String) = Auth { implicit ctx => me =>
2019-12-04 16:39:16 -07:00
env.relation.api.fetchBlocks(me.id, reported) map { blocked =>
2016-10-19 08:55:32 -06:00
html.report.thanks(reported, blocked)
}
}
}