lila/app/controllers/Msg.scala

128 lines
3.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,
apiC: => Api
2020-01-24 15:48:23 -07:00
) extends LilaController(env) {
2020-05-05 22:11:15 -06:00
def home =
Auth { implicit ctx => me =>
2020-09-05 11:02:39 -06:00
negotiate(
2020-09-21 01:28:28 -06:00
html = inboxJson(me) map { json =>
Ok(views.html.msg.home(json))
},
2020-05-05 22:11:15 -06:00
api = v =>
{
if (v >= 5) inboxJson(me)
else env.msg.compat.inbox(me, getInt("page"))
} map { Ok(_) }
)
}
2020-01-24 18:36:02 -07:00
2020-05-05 22:11:15 -06:00
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
2020-05-05 22:11:15 -06:00
else
2020-09-05 11:02:39 -06:00
env.msg.api.convoWith(me, username, before).flatMap {
2020-05-05 22:11:15 -06:00
case None =>
negotiate(
html = Redirect(routes.Msg.home).fuccess,
2020-05-05 22:11:15 -06:00
api = _ => notFoundJson()
)
case Some(c) =>
def newJson = inboxJson(me).map { _ + ("convo" -> env.msg.json.convo(c)) }
negotiate(
2020-09-21 01:28:28 -06:00
html = newJson map { json =>
Ok(views.html.msg.home(json))
},
2020-05-05 22:11:15 -06:00
api = v =>
{
if (v >= 5) newJson
else fuccess(env.msg.compat.thread(me, c))
} map { Ok(_) }
)
}
}
2020-01-24 18:36:02 -07:00
2020-05-05 22:11:15 -06:00
def search(q: String) =
Auth { ctx => me =>
2020-09-05 11:02:39 -06:00
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(_) }
2020-01-27 15:03:57 -07:00
}
2020-01-25 09:31:11 -07:00
}
2020-05-05 22:11:15 -06:00
def unreadCount =
Auth { ctx => me =>
JsonOk {
env.msg.compat unreadCount me
2020-01-27 15:03:57 -07:00
}
}
2020-01-25 10:40:38 -07:00
2020-05-05 22:11:15 -06:00
def convoDelete(username: String) =
Auth { _ => me =>
env.msg.api.delete(me, username) >>
inboxJson(me) map { Ok(_) }
}
2020-01-26 13:49:25 -07:00
2020-05-05 22:11:15 -06:00
def compatCreate =
AuthBody { implicit ctx => me =>
2020-09-05 11:02:39 -06:00
ctx.noKid ?? {
2020-05-05 22:11:15 -06:00
env.msg.compat
.create(me)(ctx.body, formBinding)
2020-05-05 22:11:15 -06:00
.fold(
jsonFormError,
_ map { id =>
Ok(Json.obj("ok" -> true, "id" -> id))
}
)
}
2020-01-27 15:03:57 -07:00
}
2020-01-27 08:38:50 -07:00
2020-01-27 09:21:37 -07:00
def apiPost(username: String) = {
val userId = lila.user.User normalize username
2020-01-27 09:21:37 -07:00
AuthOrScopedBody(_.Msg.Write)(
// compat: reply
auth = implicit ctx =>
me =>
2020-09-05 11:02:39 -06:00
env.msg.compat
.reply(me, userId)(ctx.body, formBinding)
2020-09-05 11:02:39 -06:00
.fold(
jsonFormError,
_ inject Ok(Json.obj("ok" -> true, "id" -> userId))
),
2020-01-27 09:21:37 -07:00
// new API: create/reply
scoped = implicit req =>
2020-01-27 15:03:57 -07:00
me =>
2020-01-28 09:33:51 -07:00
(!me.kid && userId != me.id) ?? {
2020-01-27 15:03:57 -07:00
import play.api.data._
import play.api.data.Forms._
2020-07-22 04:52:52 -06:00
Form(single("text" -> nonEmptyText))
.bindFromRequest()
2020-01-27 15:03:57 -07:00
.fold(
err => jsonFormErrorFor(err, req, me.some),
text =>
env.msg.api.post(me.id, userId, text) map {
case lila.msg.MsgApi.PostResult.Success => jsonOkResult
case lila.msg.MsgApi.PostResult.Limited => rateLimitedJson
case _ => BadRequest(jsonError("The message was rejected"))
}
2020-01-27 15:03:57 -07:00
)
}
2020-01-27 09:21:37 -07:00
)
2020-01-27 08:38:50 -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(
2020-01-27 15:03:57 -07:00
"me" -> lightUserWrites.writes(me.light).add("kid" -> me.kid),
2020-01-26 22:56:58 -07:00
"contacts" -> threads
)
}
2020-01-24 15:48:23 -07:00
}