lila/app/controllers/ForumPost.scala

75 lines
2.6 KiB
Scala
Raw Normal View History

package controllers
import lila.app._
2017-02-15 17:53:15 -07:00
import lila.common.{ HTTPRequest, IpAddress }
2017-01-24 16:11:42 -07:00
import scala.concurrent.duration._
import views._
2019-12-04 16:39:16 -07:00
final class ForumPost(env: Env) extends LilaController(env) with ForumController {
2019-12-13 07:30:20 -07:00
private val CreateRateLimit =
new lila.memo.RateLimit[IpAddress](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 {
if (text.trim.isEmpty) Redirect(routes.ForumCateg.index).fuccess
2019-12-04 16:39:16 -07:00
else env.forumSearch(text, page, ctx.troll) map { html.forum.search(text, _) }
2015-04-10 02:47:00 -06:00
}
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 =>
2018-11-28 05:32:17 -07:00
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 if (topic.isOld) fuccess(BadRequest("This topic is archived"))
2019-12-13 07:30:20 -07:00
else
forms.post.bindFromRequest.fold(
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 =>
CreateRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
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
2017-01-24 16:11:42 -07:00
forms.postEdit.bindFromRequest.fold(
2019-12-08 10:35:26 -07:00
_ => Redirect(routes.ForumPost.redirect(postId)).fuccess,
2019-12-13 07:30:20 -07:00
data =>
CreateRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
postApi.editPost(postId, data.changes, me).map { post =>
Redirect(routes.ForumPost.redirect(post.id))
}
2017-01-24 16:11:42 -07:00
}
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)
}
}
}