lila/app/controllers/ForumPost.scala

81 lines
2.5 KiB
Scala
Raw Normal View History

package controllers
import scala.concurrent.duration._
2016-09-03 01:39:17 -06:00
import lila.common.HTTPRequest
import lila.app._
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 {
Env.forum.recent(ctx.me, teamCache.teamIds) map { posts =>
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(
err => forms.anyCaptcha flatMap { captcha =>
ctx.userId ?? Env.timeline.status(s"forum:${topic.id}") map { unsub =>
BadRequest(html.forum.topic.show(categ, topic, posts, Some(err -> captcha), unsub))
}
},
data => postApi.makePost(categ, topic, data) map { post =>
Redirect(routes.ForumPost.redirect(post.id))
}
)
}
2013-05-06 11:46:12 -06:00
}
}
}
def edit(postId: String) = AuthBody { implicit ctx =>
2016-09-16 17:31:51 -06:00
me =>
implicit val req = ctx.body
2016-09-15 16:36:36 -06:00
2016-09-16 17:31:51 -06: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-15 16:36:36 -06:00
}
2014-02-17 02:12:19 -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)
}
}
}