commit
02a6683534
|
@ -5,6 +5,7 @@ import play.api.data.Form
|
|||
import play.api.mvc._
|
||||
import play.api.mvc.Results._
|
||||
import play.twirl.api.Html
|
||||
import play.api.libs.json._
|
||||
|
||||
import lila.api.Context
|
||||
import lila.app._
|
||||
|
@ -22,32 +23,47 @@ object Message extends LilaController {
|
|||
def inbox(page: Int) = Auth { implicit ctx =>
|
||||
me =>
|
||||
NotForKids {
|
||||
api.inbox(me, page) map { html.message.inbox(me, _) }
|
||||
negotiate (
|
||||
html = api.inbox(me, page) map { html.message.inbox(me, _) },
|
||||
api = _ => api.inbox(me, page) map { Env.message.jsonView.inbox(me, _) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
def thread(id: String) = Auth { implicit ctx =>
|
||||
implicit me =>
|
||||
NotForKids {
|
||||
OptionFuOk(api.thread(id, me)) { thread =>
|
||||
relationApi.fetchBlocks(thread otherUserId me, me.id) map { blocked =>
|
||||
html.message.thread(thread, forms.post, blocked)
|
||||
}
|
||||
} map NoCache
|
||||
negotiate (
|
||||
html = OptionFuOk(api.thread(id, me)) { thread =>
|
||||
relationApi.fetchBlocks(thread otherUserId me, me.id) map { blocked =>
|
||||
html.message.thread(thread, forms.post, blocked)
|
||||
}
|
||||
} map NoCache,
|
||||
api = _ => JsonOptionFuOk(api.thread(id, me)) { thread => Env.message.jsonView.thread(thread) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
def answer(id: String) = AuthBody { implicit ctx =>
|
||||
implicit me =>
|
||||
OptionFuResult(api.thread(id, me)) { thread =>
|
||||
implicit val req = ctx.body
|
||||
forms.post.bindFromRequest.fold(
|
||||
err => relationApi.fetchBlocks(thread otherUserId me, me.id) map { blocked =>
|
||||
BadRequest(html.message.thread(thread, err, blocked))
|
||||
},
|
||||
text => api.makePost(thread, text, me) inject Redirect(routes.Message.thread(thread.id) + "#bottom")
|
||||
)
|
||||
}
|
||||
negotiate (
|
||||
html = OptionFuResult(api.thread(id, me)) { thread =>
|
||||
implicit val req = ctx.body
|
||||
forms.post.bindFromRequest.fold(
|
||||
err => relationApi.fetchBlocks(thread otherUserId me, me.id) map { blocked =>
|
||||
BadRequest(html.message.thread(thread, err, blocked))
|
||||
},
|
||||
text => api.makePost(thread, text, me) inject Redirect(routes.Message.thread(thread.id) + "#bottom")
|
||||
)
|
||||
},
|
||||
api = _ => OptionFuResult(api.thread(id, me)) { thread =>
|
||||
implicit val req = ctx.body
|
||||
forms.post.bindFromRequest.fold(
|
||||
err => fuccess(BadRequest(Json.obj("err" -> "Malformed request"))),
|
||||
text => api.makePost(thread, text, me) inject Ok(Json.obj("ok" -> true, "id" -> thread.id))
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
def form = Auth { implicit ctx =>
|
||||
|
@ -61,11 +77,18 @@ object Message extends LilaController {
|
|||
implicit me =>
|
||||
NotForKids {
|
||||
implicit val req = ctx.body
|
||||
forms.thread(me).bindFromRequest.fold(
|
||||
err => renderForm(me, none, _ => err) map { BadRequest(_) },
|
||||
data => api.makeThread(data, me) map { thread =>
|
||||
Redirect(routes.Message.thread(thread.id))
|
||||
})
|
||||
negotiate (
|
||||
html = forms.thread(me).bindFromRequest.fold(
|
||||
err => renderForm(me, none, _ => err) map { BadRequest(_) },
|
||||
data => api.makeThread(data, me) map { thread =>
|
||||
Redirect(routes.Message.thread(thread.id))
|
||||
}),
|
||||
api = _ => forms.thread(me).bindFromRequest.fold(
|
||||
err => fuccess(BadRequest(Json.obj("err" -> "Malformed request"))),
|
||||
data => api.makeThread(data, me) map { thread =>
|
||||
Ok(Json.obj("ok" -> true, "id" -> thread.id))
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,6 +108,9 @@ object Message extends LilaController {
|
|||
|
||||
def delete(id: String) = AuthBody { implicit ctx =>
|
||||
implicit me =>
|
||||
api.deleteThread(id, me) inject Redirect(routes.Message.inbox(1))
|
||||
negotiate (
|
||||
html = api.deleteThread(id, me) inject Redirect(routes.Message.inbox(1)),
|
||||
api = _ => api.deleteThread(id, me) inject Ok(Json.obj("ok" -> true))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@ final class Env(
|
|||
|
||||
lazy val forms = new DataForm(security = security)
|
||||
|
||||
lazy val jsonView = new JsonView()
|
||||
|
||||
lazy val batch = new MessageBatch(
|
||||
coll = threadColl,
|
||||
notifyApi = notifyApi)
|
||||
|
|
41
modules/message/src/main/JsonView.scala
Normal file
41
modules/message/src/main/JsonView.scala
Normal file
|
@ -0,0 +1,41 @@
|
|||
package lila.message
|
||||
|
||||
import play.api.libs.json._
|
||||
import play.api.mvc.Result
|
||||
import play.api.mvc.Results._
|
||||
|
||||
import lila.common.PimpedJson._
|
||||
import lila.user.User
|
||||
import lila.common.paginator._
|
||||
|
||||
final class JsonView() {
|
||||
|
||||
def inbox(me: User, threads: Paginator[Thread]): Result =
|
||||
Ok(PaginatorJson(threads.mapResults { t =>
|
||||
Json.obj(
|
||||
"id" -> t.id,
|
||||
"author" -> t.otherUserId(me),
|
||||
"name" -> t.name,
|
||||
"updatedAt" -> t.updatedAt,
|
||||
"isUnread" -> t.isUnReadBy(me)
|
||||
)
|
||||
})
|
||||
)
|
||||
|
||||
def thread(thread: Thread): Fu[JsValue] =
|
||||
fuccess (
|
||||
Json.obj(
|
||||
"id" -> thread.id,
|
||||
"name" -> thread.name,
|
||||
"posts" -> thread.posts.map { post => threadPost(thread, post)}
|
||||
)
|
||||
)
|
||||
|
||||
def threadPost(thread: Thread, post: Post): JsValue =
|
||||
Json.obj(
|
||||
"sender" -> thread.senderOf(post),
|
||||
"receiver" -> thread.receiverOf(post),
|
||||
"text" -> post.text,
|
||||
"createdAt" -> post.createdAt
|
||||
)
|
||||
}
|
Loading…
Reference in a new issue