make controllers more async and readable
This commit is contained in:
parent
5eedd1d3dc
commit
880cf89f0c
|
@ -1,8 +1,8 @@
|
|||
package controllers
|
||||
|
||||
import play.api.data.Form
|
||||
import play.twirl.api.Html
|
||||
import play.api.libs.json._
|
||||
import play.twirl.api.Html
|
||||
|
||||
import lila.api.Context
|
||||
import lila.app._
|
||||
|
@ -17,20 +17,21 @@ object Message extends LilaController {
|
|||
private def forms = Env.message.forms
|
||||
private def relationApi = Env.relation.api
|
||||
|
||||
def inbox(page: Int) = Auth { implicit ctx =>
|
||||
me =>
|
||||
def inbox(page: Int) = Auth { implicit ctx => me =>
|
||||
NotForKids {
|
||||
negotiate (
|
||||
html = api.inbox(me, page) map { html.message.inbox(me, _) },
|
||||
api = _ => api.inbox(me, page) map { Env.message.jsonView.inbox(me, _) }
|
||||
)
|
||||
for {
|
||||
pag <- api.inbox(me, page)
|
||||
_ <- Env.user.lightUserApi preloadMany pag.currentPageResults.flatMap(_.userIds)
|
||||
res <- negotiate(
|
||||
html = fuccess(html.message.inbox(me, pag)),
|
||||
api = _ => fuccess(Env.message.jsonView.inbox(me, pag)))
|
||||
} yield res
|
||||
}
|
||||
}
|
||||
|
||||
def thread(id: String) = Auth { implicit ctx =>
|
||||
implicit me =>
|
||||
def thread(id: String) = Auth { implicit ctx => implicit me =>
|
||||
NotForKids {
|
||||
negotiate (
|
||||
negotiate(
|
||||
html = OptionFuOk(api.thread(id, me)) { thread =>
|
||||
relationApi.fetchBlocks(thread otherUserId me, me.id) map { blocked =>
|
||||
html.message.thread(thread, forms.post, blocked)
|
||||
|
@ -41,9 +42,8 @@ object Message extends LilaController {
|
|||
}
|
||||
}
|
||||
|
||||
def answer(id: String) = AuthBody { implicit ctx =>
|
||||
implicit me =>
|
||||
negotiate (
|
||||
def answer(id: String) = AuthBody { implicit ctx => implicit me =>
|
||||
negotiate(
|
||||
html = OptionFuResult(api.thread(id, me)) { thread =>
|
||||
implicit val req = ctx.body
|
||||
forms.post.bindFromRequest.fold(
|
||||
|
@ -63,20 +63,18 @@ object Message extends LilaController {
|
|||
)
|
||||
}
|
||||
|
||||
def form = Auth { implicit ctx =>
|
||||
implicit me =>
|
||||
def form = Auth { implicit ctx => implicit me =>
|
||||
NotForKids {
|
||||
renderForm(me, get("title"), identity) map { Ok(_) }
|
||||
}
|
||||
}
|
||||
|
||||
def create = AuthBody { implicit ctx =>
|
||||
implicit me =>
|
||||
def create = AuthBody { implicit ctx => implicit me =>
|
||||
NotForKids {
|
||||
import play.api.Play.current
|
||||
import play.api.i18n.Messages.Implicits._
|
||||
implicit val req = ctx.body
|
||||
negotiate (
|
||||
negotiate(
|
||||
html = forms.thread(me).bindFromRequest.fold(
|
||||
err => renderForm(me, none, _ => err) map { BadRequest(_) },
|
||||
data => api.makeThread(data, me) map { thread =>
|
||||
|
@ -99,15 +97,13 @@ object Message extends LilaController {
|
|||
}
|
||||
}
|
||||
|
||||
def batch = AuthBody { implicit ctx =>
|
||||
implicit me =>
|
||||
def batch = AuthBody { implicit ctx => implicit me =>
|
||||
val ids = get("ids").??(_.split(",").toList).distinct take 200
|
||||
Env.message.batch(me, ~get("action"), ids) inject Redirect(routes.Message.inbox(1))
|
||||
}
|
||||
|
||||
def delete(id: String) = AuthBody { implicit ctx =>
|
||||
implicit me =>
|
||||
negotiate (
|
||||
def delete(id: String) = AuthBody { implicit ctx => implicit me =>
|
||||
negotiate(
|
||||
html = api.deleteThread(id, me) inject Redirect(routes.Message.inbox(1)),
|
||||
api = _ => api.deleteThread(id, me) inject Ok(Json.obj("ok" -> true))
|
||||
)
|
||||
|
|
|
@ -85,33 +85,32 @@ object Study extends LilaController {
|
|||
|
||||
private def showQuery(query: Fu[Option[WithChapter]])(implicit ctx: Context) =
|
||||
OptionFuResult(query) {
|
||||
case WithChapter(study, chapter) => CanViewResult(study) {
|
||||
env.chapterRepo.orderedMetadataByStudy(study.id) flatMap { chapters =>
|
||||
env.api.resetIfOld(study, chapters) flatMap { study =>
|
||||
if (HTTPRequest isSynchronousHttp ctx.req) env.studyRepo.incViews(study)
|
||||
val pov = UserAnalysis.makePov(chapter.root.fen.value.some, chapter.setup.variant)
|
||||
Env.round.jsonView.userAnalysisJson(pov, ctx.pref, chapter.setup.orientation, owner = false) zip
|
||||
chatOf(study) zip
|
||||
env.jsonView(study, chapters, chapter, ctx.me) zip
|
||||
env.version(study.id) flatMap {
|
||||
case (((baseData, chat), studyJson), sVersion) =>
|
||||
case WithChapter(s, chapter) => CanViewResult(s) {
|
||||
import lila.tree.Node.partitionTreeJsonWriter
|
||||
val analysis = baseData ++ Json.obj(
|
||||
for {
|
||||
chapters <- env.chapterRepo.orderedMetadataByStudy(s.id)
|
||||
study <- env.api.resetIfOld(s, chapters)
|
||||
_ <- Env.user.lightUserApi preloadMany study.members.ids.toList
|
||||
_ = if (HTTPRequest isSynchronousHttp ctx.req) env.studyRepo.incViews(study)
|
||||
pov = UserAnalysis.makePov(chapter.root.fen.value.some, chapter.setup.variant)
|
||||
baseData <- Env.round.jsonView.userAnalysisJson(pov, ctx.pref, chapter.setup.orientation, owner = false)
|
||||
studyJson <- env.jsonView(study, chapters, chapter, ctx.me)
|
||||
data = lila.study.JsonView.JsData(
|
||||
study = studyJson,
|
||||
analysis = baseData ++ Json.obj(
|
||||
"treeParts" -> partitionTreeJsonWriter.writes {
|
||||
lila.study.TreeBuilder(chapter.root, chapter.setup.variant)
|
||||
})
|
||||
val data = lila.study.JsonView.JsData(
|
||||
study = studyJson,
|
||||
analysis = analysis)
|
||||
negotiate(
|
||||
html = Ok(html.study.show(study, data, chat, sVersion)).fuccess,
|
||||
}))
|
||||
res <- negotiate(
|
||||
html = for {
|
||||
chat <- chatOf(study)
|
||||
sVersion <- env.version(study.id)
|
||||
} yield Ok(html.study.show(study, data, chat, sVersion)),
|
||||
api = _ => Ok(Json.obj(
|
||||
"study" -> data.study,
|
||||
"analysis" -> data.analysis)).fuccess
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
} yield res
|
||||
}
|
||||
} map NoCache
|
||||
|
||||
|
|
|
@ -37,14 +37,13 @@ object User extends LilaController {
|
|||
|
||||
def showMini(username: String) = Open { implicit ctx =>
|
||||
OptionFuResult(UserRepo named username) { user =>
|
||||
if (user.enabled) GameRepo lastPlayedPlaying user zip
|
||||
(ctx.userId ?? { relationApi.fetchBlocks(user.id, _) }) zip
|
||||
(ctx.userId ?? { Env.game.crosstableApi(user.id, _) }) zip
|
||||
(ctx.isAuth ?? { Env.pref.api.followable(user.id) }) zip
|
||||
(ctx.userId ?? { relationApi.fetchRelation(_, user.id) }) flatMap {
|
||||
case ((((pov, blocked), crosstable), followable), relation) =>
|
||||
negotiate(
|
||||
html = fuccess {
|
||||
if (user.enabled) for {
|
||||
blocked <- ctx.userId ?? { relationApi.fetchBlocks(user.id, _) }
|
||||
crosstable <- ctx.userId ?? { Env.game.crosstableApi(user.id, _) }
|
||||
followable <- ctx.isAuth ?? { Env.pref.api.followable(user.id) }
|
||||
relation <- ctx.userId ?? { relationApi.fetchRelation(_, user.id) }
|
||||
res <- negotiate(
|
||||
html = GameRepo lastPlayedPlaying user map { pov =>
|
||||
Ok(html.user.mini(user, pov, blocked, followable, relation, crosstable))
|
||||
.withHeaders(CACHE_CONTROL -> "max-age=5")
|
||||
},
|
||||
|
@ -55,7 +54,7 @@ object User extends LilaController {
|
|||
"perfs" -> lila.user.JsonView.perfs(user, user.best8Perfs)
|
||||
)))
|
||||
})
|
||||
}
|
||||
} yield res
|
||||
else fuccess(Ok(html.user.miniClosed(user)))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue