<commit_before>package controllers

import play.api.libs.json._

import lila.app._
import lila.common.LightUser.lightUserWrites

final class Msg(
    env: Env,
    apiC: => Api
) extends LilaController(env) {

  def home =
    Auth { implicit ctx => me =>
      ctx.hasInbox ?? negotiate(
        html =
          inboxJson(me) map { json =>
            Ok(views.html.msg.home(json))
          },
        api = v =>
          {
            if (v >= 5) inboxJson(me)
            else env.msg.compat.inbox(me, getInt("page"))
          } map { Ok(_) }
      )
    }

  def convo(username: String, before: Option[Long] = None) =
    Auth { implicit ctx => me =>
      if (username == "new") Redirect(get("user").fold(routes.Msg.home())(routes.Msg.convo(_))).fuccess
      else
        ctx.hasInbox ?? env.msg.api.convoWith(me, username, before).flatMap {
          case None =>
            negotiate(
              html = Redirect(routes.Msg.home()).fuccess,
              api = _ => notFoundJson()
            )
          case Some(c) =>
            def newJson = inboxJson(me).map { _ + ("convo" -> env.msg.json.convo(c)) }
            negotiate(
              html =
                newJson map { json =>
                  Ok(views.html.msg.home(json))
                },
              api = v =>
                {
                  if (v >= 5) newJson
                  else fuccess(env.msg.compat.thread(me, c))
                } map { Ok(_) }
            )
        }
    }

  def search(q: String) =
    Auth { ctx => me =>
      ctx.hasInbox ?? {
        q.trim.some.filter(_.length > 1).filter(lila.user.User.couldBeUsername) match {
          case None    => env.msg.json.searchResult(me)(env.msg.search.empty) map { Ok(_) }
          case Some(q) => env.msg.search(me, q) flatMap env.msg.json.searchResult(me) map { Ok(_) }
        }
      }
    }

  def unreadCount =
    Auth { ctx => me =>
      JsonOk {
        ctx.hasInbox ?? {
          env.msg.api unreadCount me
        }
      }
    }

  def convoDelete(username: String) =
    Auth { _ => me =>
      env.msg.api.delete(me, username) >>
        inboxJson(me) map { Ok(_) }
    }

  def compatCreate =
    AuthBody { implicit ctx => me =>
      ctx.hasInbox ?? {
        env.msg.compat
          .create(me)(ctx.body)
          .fold(
            jsonFormError,
            _ map { id =>
              Ok(Json.obj("ok" -> true, "id" -> id))
            }<commit_msg>code tweak<commit_after>package controllers

import play.api.libs.json._

import lila.app._
import lila.common.LightUser.lightUserWrites

final class Msg(
    env: Env,
    apiC: => Api
) extends LilaController(env) {

  def home =
    Auth { implicit ctx => me =>
      ctx.hasInbox ?? negotiate(
        html =
          inboxJson(me) map { json =>
            Ok(views.html.msg.home(json))
          },
        api = v =>
          {
            if (v >= 5) inboxJson(me)
            else env.msg.compat.inbox(me, getInt("page"))
          } map { Ok(_) }
      )
    }

  def convo(username: String, before: Option[Long] = None) =
    Auth { implicit ctx => me =>
      if (username == "new") Redirect(get("user").fold(routes.Msg.home())(routes.Msg.convo(_))).fuccess
      else
        ctx.hasInbox ?? env.msg.api.convoWith(me, username, before).flatMap {
          case None =>
            negotiate(
              html = Redirect(routes.Msg.home()).fuccess,
              api = _ => notFoundJson()
            )
          case Some(c) =>
            def newJson = inboxJson(me).map { _ + ("convo" -> env.msg.json.convo(c)) }
            negotiate(
              html =
                newJson map { json =>
                  Ok(views.html.msg.home(json))
                },
              api = v =>
                {
                  if (v >= 5) newJson
                  else fuccess(env.msg.compat.thread(me, c))
                } map { Ok(_) }
            )
        }
    }

  def search(q: String) =
    Auth { ctx => me =>
      ctx.hasInbox ?? {
        q.trim.some.filter(lila.user.User.couldBeUsername) match {
          case None    => env.msg.json.searchResult(me)(env.msg.search.empty) map { Ok(_) }
          case Some(q) => env.msg.search(me, q) flatMap env.msg.json.searchResult(me) map { Ok(_) }
        }
      }
    }

  def unreadCount =
    Auth { ctx => me =>
      JsonOk {
        ctx.hasInbox ?? {
          env.msg.api unreadCount me
        }
      }
    }

  def convoDelete(username: String) =
    Auth { _ => me =>
      env.msg.api.delete(me, username) >>
        inboxJson(me) map { Ok(_) }
    }

  def compatCreate =
    AuthBody { implicit ctx => me =>
      ctx.hasInbox ?? {
        env.msg.compat
          .create(me)(ctx.body)
          .fold(
            jsonFormError,
            _ map { id =>
              Ok(Json.obj("ok" -> true, "id" -> id))
            }
{'fraction_non_alphanumeric': 0.1422965716646014, 'fraction_numerical': 0.0010326311441553077, 'mean_word_length': 1.4835897435897436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 64, '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': '4978364', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1533, 'n_words': 302}