lila/app/controllers/ForumPost.scala

80 lines
2.5 KiB
Scala
Raw Normal View History

package controllers
import lila.app._
2017-01-24 16:11:42 -07:00
import lila.common.HTTPRequest
import scala.concurrent.duration._
import views._
object ForumPost extends LilaController with ForumController {
2016-09-01 15:54:43 -06:00
private val CreateRateLimit = new lila.memo.RateLimit(4, 5 minutes,
name = "forum create post",
key = "forum.post")
2014-02-17 02:12:19 -07:00
def search(text: String, page: Int) = OpenBody { implicit ctx =>
2015-04-10 02:47:00 -06:00
NotForKids {
text.trim.isEmpty.fold(
Redirect(routes.ForumCateg.index).fuccess,
Env.forumSearch(text, page, isGranted(_.StaffForum), ctx.troll) map { paginator =>
html.forum.search(text, paginator)
}
)
}
2013-05-06 11:46:12 -06:00
}
2014-02-17 02:12:19 -07:00
def recent = Open { implicit ctx =>
2015-04-10 02:47:00 -06:00
NotForKids {
2017-02-05 04:19:53 -07:00
Env.forum.recent(ctx.me, teamCache.teamIdsList) map { posts =>
2015-04-10 02:47:00 -06:00
html.forum.post.recent(posts)
}
2013-05-06 11:46:12 -06:00
}
}
2014-02-17 02:12:19 -07:00
def create(categSlug: String, slug: String, page: Int) = OpenBody { implicit ctx =>
CreateRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
CategGrantWrite(categSlug) {
implicit val req = ctx.body
OptionFuResult(topicApi.show(categSlug, slug, page, ctx.troll)) {
case (categ, topic, posts) =>
if (topic.closed) fuccess(BadRequest("This topic is closed"))
else forms.post.bindFromRequest.fold(
2017-01-24 16:11:42 -07:00
err => for {
captcha <- forms.anyCaptcha
unsub <- ctx.userId ?? Env.timeline.status(s"forum:${topic.id}")
canModCateg <- isGrantedMod(categ.slug)
} yield BadRequest(html.forum.topic.show(categ, topic, posts, Some(err -> captcha), unsub, canModCateg = canModCateg)),
data => postApi.makePost(categ, topic, data) map { post =>
Redirect(routes.ForumPost.redirect(post.id))
}
)
}
2013-05-06 11:46:12 -06:00
}
}
}
2017-01-24 16:11:42 -07:00
def edit(postId: String) = AuthBody { implicit ctx => me =>
2016-09-16 17:31:51 -06:00
implicit val req = ctx.body
2016-09-15 16:36:36 -06:00
2017-01-24 16:11:42 -07:00
forms.postEdit.bindFromRequest.fold(
err => Redirect(routes.ForumPost.redirect(postId)).fuccess,
data =>
postApi.editPost(postId, data.changes, me).map { post =>
Redirect(routes.ForumPost.redirect(post.id))
}
2016-09-16 17:31:51 -06:00
)
2016-09-15 16:36:36 -06:00
}
2017-01-24 16:11:42 -07:00
def delete(categSlug: String, id: String) = Auth { implicit ctx => me =>
CategGrantMod(categSlug) {
postApi.delete(categSlug, id, me) map { Ok(_) }
}
2013-05-06 11:46:12 -06:00
}
2013-05-27 09:01:19 -06:00
2014-02-17 02:12:19 -07:00
def redirect(id: String) = Open { implicit ctx =>
2013-05-27 09:01:19 -06:00
OptionResult(postApi.urlData(id, ctx.troll)) {
2014-02-17 02:12:19 -07:00
case lila.forum.PostUrlData(categ, topic, page, number) =>
2013-05-27 09:01:19 -06:00
Redirect(routes.ForumTopic.show(categ, topic, page).url + "#" + number)
}
}
}