<commit_before>package lila.msg

import play.api.libs.json._

import lila.common.Json.jodaWrites
import lila.common.LightUser
import lila.common.config._
import lila.common.paginator._
import lila.user.{ LightUserApi, User }

final class MsgCompat(
    api: MsgApi,
    isOnline: lila.socket.IsOnline,
    lightUserApi: LightUserApi
)(implicit ec: scala.concurrent.ExecutionContext) {

  private val maxPerPage = MaxPerPage(25)

  def inbox(me: User, pageOpt: Option[Int]): Fu[JsObject] = {
    val page = pageOpt.fold(1)(_ atLeast 1 atMost 2)
    api.threadsOf(me) flatMap { allThreads =>
      val threads = allThreads.drop((page - 1) * maxPerPage.value).take(maxPerPage.value)
      lightUserApi.preloadMany(threads.map(_ other me)) inject
        PaginatorJson {
          Paginator
            .fromResults(
              currentPageResults = threads,
              nbResults = allThreads.size,
              currentPage = page,
              maxPerPage = maxPerPage
            )
            .mapResults { t =>
              val user = lightUserApi.sync(t other me) | LightUser.fallback(t other me)
              Json.obj(
                "id"        -> user.id,
                "author"    -> user.titleName,
                "name"      -> t.lastMsg.text,
                "updatedAt" -> t.lastMsg.date,
                "isUnread"  -> t.lastMsg.unreadBy(me.id)
              )
            }
        }
    }
  }

  def thread(me: User, c: MsgConvo): JsObject =
    Json.obj(
      "id"   -> c.contact.id,
      "name" -> c.contact.name,
      "posts" -> c.msgs.map { msg =>
        Json.obj(
          "sender"    -> renderUser(if (msg.user == c.contact.id) c.contact else me.light),
          "receiver"  -> renderUser(if (msg.user != c.contact.id) c.contact else me.light),
          "text"      -> msg.text,
          "createdAt" -> msg.date
        )
      }
    )

  private def renderUser(user: LightUser) =
    LightUser.lightUserWrites.writes(user) ++ Json.obj(
      "online"   -> isOnline(user.id),
      "username" -> user.name // for mobile app BC
    )
}<commit_msg>msg compat msgs in reverse order<commit_after>package lila.msg

import play.api.libs.json._

import lila.common.Json.jodaWrites
import lila.common.LightUser
import lila.common.config._
import lila.common.paginator._
import lila.user.{ LightUserApi, User }

final class MsgCompat(
    api: MsgApi,
    isOnline: lila.socket.IsOnline,
    lightUserApi: LightUserApi
)(implicit ec: scala.concurrent.ExecutionContext) {

  private val maxPerPage = MaxPerPage(25)

  def inbox(me: User, pageOpt: Option[Int]): Fu[JsObject] = {
    val page = pageOpt.fold(1)(_ atLeast 1 atMost 2)
    api.threadsOf(me) flatMap { allThreads =>
      val threads = allThreads.drop((page - 1) * maxPerPage.value).take(maxPerPage.value)
      lightUserApi.preloadMany(threads.map(_ other me)) inject
        PaginatorJson {
          Paginator
            .fromResults(
              currentPageResults = threads,
              nbResults = allThreads.size,
              currentPage = page,
              maxPerPage = maxPerPage
            )
            .mapResults { t =>
              val user = lightUserApi.sync(t other me) | LightUser.fallback(t other me)
              Json.obj(
                "id"        -> user.id,
                "author"    -> user.titleName,
                "name"      -> t.lastMsg.text,
                "updatedAt" -> t.lastMsg.date,
                "isUnread"  -> t.lastMsg.unreadBy(me.id)
              )
            }
        }
    }
  }

  def thread(me: User, c: MsgConvo): JsObject =
    Json.obj(
      "id"   -> c.contact.id,
      "name" -> c.contact.name,
      "posts" -> c.msgs.reverse.map { msg =>
        Json.obj(
          "sender"    -> renderUser(if (msg.user == c.contact.id) c.contact else me.light),
          "receiver"  -> renderUser(if (msg.user != c.contact.id) c.contact else me.light),
          "text"      -> msg.text,
          "createdAt" -> msg.date
        )
      }
    )

  private def renderUser(user: LightUser) =
    LightUser.lightUserWrites.writes(user) ++ Json.obj(
      "online"   -> isOnline(user.id),
      "username" -> user.name // for mobile app BC
    )
}
{'fraction_non_alphanumeric': 0.12419547079856973, 'fraction_numerical': 0.0028605482717520858, 'mean_word_length': 1.9118667591950034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, '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': '7642480', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1343, 'n_words': 277}