<commit_before>package controllers

import play.api.data.Form
import play.api.mvc.{ Result, Results, Call, RequestHeader, Accepting }

import lila.api.{ Context, BodyContext }
import lila.app._
import lila.common.{ HTTPRequest, LilaCookie }
import lila.game.{ GameRepo, Pov, AnonCookie }
import lila.user.UserRepo
import views._

object Setup extends LilaController with TheftPrevention with play.api.http.ContentTypes {

  private def env = Env.setup

  def aiForm = Open { implicit ctx =>
    if (HTTPRequest isXhr ctx.req)
      env.forms aiFilled get("fen") map { form =>
        html.setup.ai(form, Env.ai.aiPerfApi.intRatings)
      }
    else fuccess {
      Redirect(routes.Lobby.home + "#ai")
    }
  }

  def ai = process(env.forms.ai) { config =>
    implicit ctx =>
      env.processor ai config map { pov =>
        pov -> routes.Round.player(pov.fullId)
      }
  }

  def friendForm(userId: Option[String]) = Open { implicit ctx =>
    if (HTTPRequest isXhr ctx.req) userId ?? UserRepo.named flatMap {
      case None => env.forms friendFilled get("fen") map {
        html.setup.friend(_, none, none)
      }
      case Some(user) => challenge(user) flatMap { error =>
        env.forms friendFilled get("fen") map {
          html.setup.friend(_, user.some, error)
        }
      }
    }
    else fuccess {
      Redirect(routes.Lobby.home + "#friend")
    }
  }

  private def challenge(user: lila.user.User)(implicit ctx: Context): Fu[Option[String]] = ctx.me match {
    case None => fuccess("Only registered players can send challenges.".some)
    case Some(me) => Env.relation.api.blocks(user.id, me.id) flatMap {
      case true => fuccess(s"{{user}} doesn't accept challenges from you.".some)
      case false => Env.pref.api getPref user zip Env.relation.api.follows(user.id, me.id) map {
        case (pref, follow) => lila.pref.Pref.Challenge.block(me, user, pref.challenge, follow)
      }
    }
  }

  def friend(userId: Option[String]) = process(env.forms.friend) { config =>
    implicit ctx =>
      env.processor friend config map { pov =>
        pov -> routes.Setup.await(pov.fullId, userId)
      }
  }

  def decline(gameId: String) = Auth { implicit ctx =>
    me =>
      OptionResult(GameRepo game gameId) { game =>
        if (game.started) BadRequest("Cannot decline started challenge")
        else {
          Env.game.maintenance remove game.id
          Env.hub.actor.challenger ! lila.hub.actorApi.setup.DeclineChallenge(gameId)
          Ok("ok")
        }
      }
  }

  def hookForm = Open { implicit ctx =>
    if (HTTPRequest isXhr ctx.req)
      env.forms.hookFilled map { html.setup.hook(_) }
    else fuccess {
      Redirect(routes.Lobby.home + "#hook")
    }
  }

  def hook(uid: String) = OpenBody { implicit ctx =>
    implicit val req = ctx.body
    env.forms.hook(ctx).bindFromRequest.fold(
      err => negotiate(
        html = BadRequest("Invalid form data").fuccess,
        api = _ => BadRequest(err.errorsAsJson).fuccess),
      config => (ctx.userId ?? Env.relation.api.blocking) flatMap {
        env.processor.hook(config, uid, lila.common.HTTPRequest sid req, _)
      }
    )
  }<commit_msg>make hook POST request return JSON<commit_after>package controllers

import play.api.data.Form
import play.api.mvc.{ Result, Results, Call, RequestHeader, Accepting }
import play.api.libs.json.Json

import lila.api.{ Context, BodyContext }
import lila.app._
import lila.common.{ HTTPRequest, LilaCookie }
import lila.game.{ GameRepo, Pov, AnonCookie }
import lila.user.UserRepo
import views._

object Setup extends LilaController with TheftPrevention with play.api.http.ContentTypes {

  private def env = Env.setup

  def aiForm = Open { implicit ctx =>
    if (HTTPRequest isXhr ctx.req)
      env.forms aiFilled get("fen") map { form =>
        html.setup.ai(form, Env.ai.aiPerfApi.intRatings)
      }
    else fuccess {
      Redirect(routes.Lobby.home + "#ai")
    }
  }

  def ai = process(env.forms.ai) { config =>
    implicit ctx =>
      env.processor ai config map { pov =>
        pov -> routes.Round.player(pov.fullId)
      }
  }

  def friendForm(userId: Option[String]) = Open { implicit ctx =>
    if (HTTPRequest isXhr ctx.req) userId ?? UserRepo.named flatMap {
      case None => env.forms friendFilled get("fen") map {
        html.setup.friend(_, none, none)
      }
      case Some(user) => challenge(user) flatMap { error =>
        env.forms friendFilled get("fen") map {
          html.setup.friend(_, user.some, error)
        }
      }
    }
    else fuccess {
      Redirect(routes.Lobby.home + "#friend")
    }
  }

  private def challenge(user: lila.user.User)(implicit ctx: Context): Fu[Option[String]] = ctx.me match {
    case None => fuccess("Only registered players can send challenges.".some)
    case Some(me) => Env.relation.api.blocks(user.id, me.id) flatMap {
      case true => fuccess(s"{{user}} doesn't accept challenges from you.".some)
      case false => Env.pref.api getPref user zip Env.relation.api.follows(user.id, me.id) map {
        case (pref, follow) => lila.pref.Pref.Challenge.block(me, user, pref.challenge, follow)
      }
    }
  }

  def friend(userId: Option[String]) = process(env.forms.friend) { config =>
    implicit ctx =>
      env.processor friend config map { pov =>
        pov -> routes.Setup.await(pov.fullId, userId)
      }
  }

  def decline(gameId: String) = Auth { implicit ctx =>
    me =>
      OptionResult(GameRepo game gameId) { game =>
        if (game.started) BadRequest("Cannot decline started challenge")
        else {
          Env.game.maintenance remove game.id
          Env.hub.actor.challenger ! lila.hub.actorApi.setup.DeclineChallenge(gameId)
          Ok("ok")
        }
      }
  }

  def hookForm = Open { implicit ctx =>
    if (HTTPRequest isXhr ctx.req)
      env.forms.hookFilled map { html.setup.hook(_) }
    else fuccess {
      Redirect(routes.Lobby.home + "#hook")
    }
  }

  def hook(uid: String) = OpenBody { implicit ctx =>
    implicit val req = ctx.body
    env.forms.hook(ctx).bindFromRequest.fold(
      err => negotiate(
        html = BadRequest("Invalid form data").fuccess,
        api = _ => BadRequest(err.errorsAsJson).fuccess),
      config => (ctx.userId ?? Env.relation.api.blocking) flatMap { blocking =>
        JsonOk {
          env.processor.hook(config, uid, lila.common.HTTPRequest sid req, blocking) inject Json.obj("ok" -> true)
        }
      }
    )
  }
{'fraction_non_alphanumeric': 0.12712914214927223, 'fraction_numerical': 0.0, 'mean_word_length': 2.6971951917572983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5870840', 'n_tokens_mistral': 2063, 'n_tokens_neox': 2057, 'n_words': 497}