lila/app/controllers/Message.scala

78 lines
2.2 KiB
Scala
Raw Normal View History

2013-05-06 15:52:48 -06:00
package controllers
import play.api._
import play.api.mvc._
import play.api.mvc.Results._
import lila.app._
2014-02-17 02:12:19 -07:00
import lila.user.{ User => UserModel, UserRepo }
import views._
2013-05-06 15:52:48 -06:00
object Message extends LilaController {
private def api = Env.message.api
private def forms = Env.message.forms
2014-04-17 03:34:55 -06:00
private def relationApi = Env.relation.api
2013-05-06 15:52:48 -06:00
2014-02-17 02:12:19 -07:00
def inbox(page: Int) = Auth { implicit ctx =>
me =>
api updateUser me.id
api.inbox(me, page) map { html.message.inbox(me, _) }
2013-05-28 15:15:13 -06:00
}
2014-02-17 02:12:19 -07:00
def preview = Auth { implicit ctx =>
me => api.preview(me.id) map { html.message.preview(me, _) }
2013-05-06 15:52:48 -06:00
}
2014-02-17 02:12:19 -07:00
def thread(id: String) = Auth { implicit ctx =>
implicit me =>
2014-04-17 03:34:55 -06:00
OptionFuOk(api.thread(id, me)) { thread =>
relationApi.blocks(thread otherUserId me, me.id) map { blocked =>
html.message.thread(thread, forms.post, blocked)
}
}
2013-05-06 15:52:48 -06:00
}
2014-02-17 02:12:19 -07:00
def answer(id: String) = AuthBody { implicit ctx =>
implicit me =>
OptionFuResult(api.thread(id, me)) { thread =>
implicit val req = ctx.body
forms.post.bindFromRequest.fold(
2014-04-17 03:34:55 -06:00
err => relationApi.blocks(thread otherUserId me, me.id) map { blocked =>
BadRequest(html.message.thread(thread, err, blocked))
},
2014-02-17 02:12:19 -07:00
text => api.makePost(thread, text, me) inject Redirect(routes.Message.thread(thread.id) + "#bottom")
)
2013-05-06 15:52:48 -06:00
}
}
2014-02-17 02:12:19 -07:00
def form = Auth { implicit ctx =>
implicit me =>
2014-05-27 01:03:48 -06:00
get("user") ?? UserRepo.named map { user =>
Ok(html.message.form(forms.thread(me), user, get("title")))
2013-05-24 03:33:43 -06:00
}
2013-05-06 15:52:48 -06:00
}
2014-02-17 02:12:19 -07:00
def create = AuthBody { implicit ctx =>
implicit me =>
2013-05-16 11:20:42 -06:00
implicit val req = ctx.body
forms.thread(me).bindFromRequest.fold(
2014-02-17 02:12:19 -07:00
err => get("username") ?? UserRepo.named map { user =>
BadRequest(html.message.form(err, user))
},
2014-02-17 02:12:19 -07:00
data => api.makeThread(data, me) map { thread =>
2013-05-16 11:20:42 -06:00
Redirect(routes.Message.thread(thread.id))
})
2013-05-06 15:52:48 -06:00
}
2014-02-17 02:12:19 -07:00
def delete(id: String) = AuthBody { implicit ctx =>
implicit me =>
2013-05-06 15:52:48 -06:00
api.deleteThread(id, me) inject Redirect(routes.Message.inbox(1))
}
def markAsRead(id: String) = AuthBody { implicit ctx =>
implicit me =>
api.markThreadAsRead(id, me)
}
2013-05-06 15:52:48 -06:00
}