lila/app/controllers/Msg.scala

60 lines
1.6 KiB
Scala
Raw Normal View History

2020-01-24 15:48:23 -07:00
package controllers
import play.api.libs.json._
import lila.app._
import lila.common.LightUser.lightUserWrites
final class Msg(
env: Env
) extends LilaController(env) {
def home = Auth { implicit ctx => me =>
2020-01-24 18:36:02 -07:00
jsonThreads(me) flatMap { threads =>
val json = Json.obj(
"me" -> me.light,
"threads" -> threads
)
negotiate(
html = Ok(views.html.msg.home(json)).fuccess,
api = _ => Ok(json).fuccess
2020-01-24 15:48:23 -07:00
)
}
}
2020-01-24 18:36:02 -07:00
def threadWith(username: String) = Auth { implicit ctx => me =>
2020-01-25 17:18:21 -07:00
for {
2020-01-25 19:50:56 -07:00
convo <- env.msg.api.convoWith(me, username)
2020-01-25 17:18:21 -07:00
threads <- jsonThreads(me, convo.thread.some.filter(_.lastMsg.isEmpty))
json = Json.obj(
"me" -> me.light,
"threads" -> threads,
2020-01-25 19:50:56 -07:00
"convo" -> env.msg.json.convo(convo)
2020-01-25 17:18:21 -07:00
)
res <- negotiate(
html = Ok(views.html.msg.home(json)).fuccess,
api = _ => Ok(json).fuccess
)
} yield res
2020-01-24 18:36:02 -07:00
}
2020-01-25 09:31:11 -07:00
def search(q: String) = Auth { _ => me =>
q.trim.some.filter(_.size > 1).filter(lila.user.User.couldBeUsername) match {
case None => BadRequest(jsonError("Invalid search query")).fuccess
case Some(q) => env.msg.search(me, q) flatMap env.msg.json.searchResult(me) map { Ok(_) }
}
}
2020-01-25 10:40:38 -07:00
def unreadCount = Auth { _ => me =>
JsonOk(env.msg.api unreadCount me)
}
2020-01-25 09:57:44 -07:00
private def jsonThreads(me: lila.user.User, also: Option[lila.msg.MsgThread] = none) =
2020-01-25 19:50:56 -07:00
env.msg.api.threadsOf(me) flatMap { threads =>
2020-01-25 09:57:44 -07:00
val all = also.fold(threads) { thread =>
if (threads.exists(_.id == thread.id)) threads else thread :: threads
}
env.msg.json.threads(me)(all)
}
2020-01-24 15:48:23 -07:00
}