lila/app/controllers/Msg.scala

74 lines
2.1 KiB
Scala
Raw Normal View History

2020-01-24 15:48:23 -07:00
package controllers
import play.api.libs.json._
2020-01-26 13:49:25 -07:00
import lila.api.Context
2020-01-24 15:48:23 -07:00
import lila.app._
import lila.common.LightUser.lightUserWrites
final class Msg(
env: Env
) extends LilaController(env) {
def home = Auth { implicit ctx => me =>
2020-01-26 22:56:58 -07:00
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(_) }
)
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-26 12:37:15 -07:00
if (username == "new") Redirect(get("user").fold(routes.Msg.home()) { routes.Msg.threadWith(_) }).fuccess
2020-01-26 13:49:25 -07:00
else renderConvo(me, username)
2020-01-24 18:36:02 -07:00
}
2020-01-26 13:49:25 -07:00
private def renderConvo(me: lila.user.User, username: String)(implicit ctx: Context) =
2020-01-26 22:56:58 -07:00
env.msg.api.convoWith(me, username) flatMap { convo =>
def newJson = inboxJson(me).map { _ + ("convo" -> env.msg.json.convo(convo)) }
negotiate(
2020-01-26 23:45:11 -07:00
html =
if (convo.contact.id == me.id) Redirect(routes.Msg.home).fuccess
else
newJson map { json =>
Ok(views.html.msg.home(json))
},
api = v =>
if (convo.contact.id == me.id) notFoundJson()
else {
if (v >= 5) newJson
else fuccess(env.msg.compat.thread(me, convo))
} map { Ok(_) }
2020-01-26 13:49:25 -07:00
)
2020-01-26 22:56:58 -07:00
}
2020-01-26 13:49:25 -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 {
2020-01-27 00:02:41 -07:00
case None => env.msg.json.searchResult(me)(env.msg.search.empty) map { Ok(_) }
2020-01-25 09:31:11 -07:00
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-26 13:49:25 -07:00
def threadDelete(username: String) = Auth { _ => me =>
env.msg.api.delete(me, username) >>
2020-01-26 22:56:58 -07:00
inboxJson(me) map { Ok(_) }
2020-01-26 13:49:25 -07:00
}
2020-01-26 22:56:58 -07:00
private def inboxJson(me: lila.user.User) =
env.msg.api.threadsOf(me) flatMap env.msg.json.threads(me) map { threads =>
Json.obj(
"me" -> me.light,
"contacts" -> threads
)
}
2020-01-24 15:48:23 -07:00
}