lila/app/controllers/Report.scala

216 lines
7.4 KiB
Scala
Raw Normal View History

package controllers
import play.api.mvc.{ AnyContentAsFormUrlEncoded, Result }
import views._
2019-12-13 07:30:20 -07:00
import lila.api.{ BodyContext, Context }
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 }
import lila.user.{ User => UserModel, Holder }
import play.api.data._
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
implicit private def asMod(holder: Holder) = AsMod(holder.user)
2020-05-05 22:11:15 -06:00
def list =
Secure(_.SeeReport) { implicit ctx => me =>
if (env.streamer.liveStreamApi.isStreaming(me.user.id) && !getBool("force"))
2020-05-05 22:11:15 -06:00
fuccess(Forbidden(html.site.message.streamingMod))
else renderList(me, env.report.modFilters.get(me).fold("all")(_.key))
2020-05-05 22:11:15 -06:00
}
2020-05-05 22:11:15 -06:00
def listWithFilter(room: String) =
Secure(_.SeeReport) { implicit ctx => me =>
env.report.modFilters.set(me, Room(room))
if (Room(room).fold(true)(Room.isGrantedFor(me))) renderList(me, room)
else notFound
2020-05-05 22:11:15 -06:00
}
2013-07-27 14:55:29 -06:00
protected[controllers] def getScores =
api.maxScores zip env.streamer.api.approval.countRequests zip env.appeal.api.countUnread
2020-07-30 03:37:46 -06:00
private def renderList(me: Holder, room: String)(implicit ctx: Context) =
api.openAndRecentWithFilter(asMod(me), 12, Room(room)) zip getScores flatMap {
case (reports, ((scores, streamers), appeals)) =>
2019-12-13 07:30:20 -07:00
(env.user.lightUserApi preloadMany reports.flatMap(_.report.userIds)) inject
Ok(
html.report
.list(
reports.filter(r => lila.report.Reason.isGrantedFor(me)(r.report.reason)),
room,
scores,
streamers,
appeals
)
)
}
2020-05-05 22:11:15 -06:00
def inquiry(id: String) =
Secure(_.SeeReport) { _ => me =>
api.inquiries.toggle(me, id) flatMap { case (prev, next) =>
2021-03-21 03:46:25 -06:00
prev.filter(_.isAppeal).map(_.user).??(env.appeal.api.setUnreadById) inject
next.fold(
Redirect {
if (prev.exists(_.isAppeal)) routes.Appeal.queue
else routes.Report.list
}
)(onInquiryStart)
2020-05-05 22:11:15 -06:00
}
2019-12-08 11:12:00 -07:00
}
2017-05-09 17:23:10 -06:00
private def onInquiryStart(inquiry: ReportModel): Result =
if (inquiry.isRecentComm) Redirect(routes.Mod.communicationPrivate(inquiry.user))
else if (inquiry.isComm) Redirect(routes.Mod.communicationPublic(inquiry.user))
else modC.redirect(inquiry.user)
protected[controllers] def onInquiryClose(
2019-12-13 07:30:20 -07:00
inquiry: Option[ReportModel],
me: Holder,
2019-12-13 07:30:20 -07:00
goTo: Option[Suspect],
force: Boolean = false
)(implicit ctx: BodyContext[_]): Fu[Result] =
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 =>
2018-03-16 17:18:09 -06:00
inquiry match {
case None =>
goTo.fold(Redirect(routes.Report.list).fuccess) { s =>
userC.modZoneOrRedirect(me, s.user.username)
2018-03-16 17:18:09 -06:00
}
case Some(prev) if prev.isSpontaneous => Redirect(modC.userUrl(prev.user, mod = true)).fuccess
2018-03-16 17:18:09 -06:00
case Some(prev) =>
val dataOpt = ctx.body.body match {
case AnyContentAsFormUrlEncoded(data) => data.some
case _ => none
}
2020-05-05 22:11:15 -06:00
def thenGoTo =
dataOpt.flatMap(_ get "then").flatMap(_.headOption) flatMap {
2020-08-16 06:42:29 -06:00
case "profile" => modC.userUrl(prev.user, mod = true).some
2020-05-05 22:11:15 -06:00
case url => url.some
}
2019-08-02 03:33:37 -06:00
thenGoTo match {
case Some(url) => Redirect(url).fuccess
2019-08-02 03:33:37 -06:00
case _ =>
def redirectToList = Redirect(routes.Report.listWithFilter(prev.room.key))
if (prev.isAppeal) Redirect(routes.Appeal.queue).fuccess
else if (dataOpt.flatMap(_ get "next").exists(_.headOption contains "1"))
api.inquiries.toggleNext(me, prev.room) map {
_.fold(redirectToList)(onInquiryStart)
2020-05-05 22:11:15 -06:00
}
else if (force) userC.modZoneOrRedirect(me, prev.user)
2019-12-13 07:30:20 -07:00
else
api.inquiries.toggle(me, prev.id) map { case (prev, next) =>
next
.fold(
if (prev.exists(_.isAppeal)) Redirect(routes.Appeal.queue)
else redirectToList
)(onInquiryStart)
2019-08-02 03:33:37 -06:00
}
2017-09-12 17:26:36 -06:00
}
}
2017-09-12 17:26:36 -06:00
}
2020-05-05 22:11:15 -06:00
def process(id: String) =
SecureBody(_.SeeReport) { implicit ctx => me =>
2020-06-06 07:51:03 -06:00
api byId id flatMap { inquiry =>
2021-03-21 03:46:25 -06:00
inquiry.filter(_.isAppeal).map(_.user).??(env.appeal.api.setReadById) >>
api.process(me, id) >>
onInquiryClose(inquiry, me, none, force = true)
2020-05-05 22:11:15 -06:00
}
2017-09-11 15:59:53 -06:00
}
2013-07-27 14:55:29 -06:00
2020-05-05 22:11:15 -06:00
def xfiles(id: String) =
Secure(_.SeeReport) { _ => _ =>
api.moveToXfiles(id) inject Redirect(routes.Report.list)
2020-05-05 22:11:15 -06:00
}
2021-03-21 03:46:25 -06:00
def snooze(id: String, dur: String) =
SecureBody(_.SeeReport) { implicit ctx => me =>
api.snooze(me, id, dur) map {
_.fold(Redirect(routes.Report.list))(onInquiryStart)
}
}
2020-05-05 22:11:15 -06:00
def currentCheatInquiry(username: String) =
Secure(_.Hunter) { implicit ctx => me =>
OptionFuResult(env.user.repo named username) { user =>
api.currentCheatReport(lila.report.Suspect(user)) flatMap {
_ ?? { report =>
api.inquiries.toggle(me, report.id).void
2020-08-16 06:42:29 -06:00
} inject modC.redirect(username, mod = true)
2020-05-05 22:11:15 -06:00
}
2018-03-15 20:27:09 -06:00
}
}
2020-05-05 22:11:15 -06:00
def form =
Auth { implicit ctx => _ =>
get("username") ?? env.user.repo.named flatMap { user =>
2021-03-24 04:49:56 -06:00
if (user.map(_.id) has UserModel.lichessId) Redirect(routes.Main.contact).fuccess
else
env.report.forms.createWithCaptcha map { case (form, captcha) =>
val filledForm: Form[lila.report.ReportSetup] = (user, get("postUrl")) match {
2021-04-11 04:10:31 -06:00
case (Some(u), Some(pid)) =>
2021-09-01 11:23:48 -06:00
form.fill(
lila.report.ReportSetup(user = u.light, reason = ~get("reason"), text = s"$pid\n\n", "", "")
)
2021-04-11 04:10:31 -06:00
case _ => form
}
Ok(html.report.form(filledForm, user, captcha))
2021-03-24 04:49:56 -06:00
}
}
2016-10-19 08:55:32 -06:00
}
2020-05-05 22:11:15 -06:00
def create =
AuthBody { implicit ctx => implicit me =>
implicit val req = ctx.body
2020-07-22 04:52:52 -06:00
env.report.forms.create
.bindFromRequest()
.fold(
err =>
get("username") ?? env.user.repo.named flatMap { user =>
env.report.forms.anyCaptcha map { captcha =>
BadRequest(html.report.form(err, user, captcha))
}
},
data =>
if (data.user.id == me.id) notFound
else
api.create(data, Reporter(me)) inject
Redirect(routes.Report.thanks(data.user.name))
)
2020-05-05 22:11:15 -06:00
}
2020-05-05 22:11:15 -06:00
def flag =
AuthBody { implicit ctx => implicit me =>
implicit val req = ctx.body
2020-07-22 04:52:52 -06:00
env.report.forms.flag
.bindFromRequest()
.fold(
_ => BadRequest.fuccess,
data =>
env.user.repo named data.username flatMap {
_ ?? { user =>
if (user == me) BadRequest.fuccess
2021-09-03 08:45:42 -06:00
else api.commFlag(Reporter(me), Suspect(user), data.resource, data.text) inject jsonOkResult
2020-07-22 04:52:52 -06:00
}
2020-05-05 22:11:15 -06:00
}
2020-07-22 04:52:52 -06:00
)
2020-05-05 22:11:15 -06:00
}
2020-05-05 22:11:15 -06:00
def thanks(reported: String) =
Auth { implicit ctx => me =>
env.relation.api.fetchBlocks(me.id, reported) map { blocked =>
html.report.thanks(reported, blocked)
}
2016-10-19 08:55:32 -06:00
}
}