From be55e85c58e7925fa5b9c4d404f2f29e89329a52 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Wed, 1 Aug 2012 15:46:56 +0200 Subject: [PATCH] log forum post deletions --- app/controllers/ForumPost.scala | 4 ++-- app/core/CoreEnv.scala | 3 ++- app/forum/ForumEnv.scala | 6 ++++-- app/forum/PostApi.scala | 34 ++++++++++++++++++++------------- app/mod/ModlogApi.scala | 6 ++++++ 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/app/controllers/ForumPost.scala b/app/controllers/ForumPost.scala index 4de4c3e786..61f4fa70ca 100644 --- a/app/controllers/ForumPost.scala +++ b/app/controllers/ForumPost.scala @@ -38,7 +38,7 @@ object ForumPost extends LilaController with forum.Controller { } def delete(id: String) = Secure(Permission.ModerateForum) { implicit ctx ⇒ - _ ⇒ - IOk(postApi delete id) + me ⇒ + IOk(postApi.delete(id, me)) } } diff --git a/app/core/CoreEnv.scala b/app/core/CoreEnv.scala index 91c5dcbff1..4a947bdd76 100644 --- a/app/core/CoreEnv.scala +++ b/app/core/CoreEnv.scala @@ -35,7 +35,8 @@ final class CoreEnv private (application: Application, val settings: Settings) { settings = settings, captcha = site.captcha, mongodb = mongodb.apply _, - userRepo = user.userRepo) + userRepo = user.userRepo, + modLog = mod.logApi) lazy val message = new lila.message.MessageEnv( settings = settings, diff --git a/app/forum/ForumEnv.scala b/app/forum/ForumEnv.scala index fd98f62076..fc5ce10d92 100644 --- a/app/forum/ForumEnv.scala +++ b/app/forum/ForumEnv.scala @@ -4,6 +4,7 @@ package forum import user.{ User, UserRepo } import core.Settings import site.Captcha +import mod.ModlogApi import com.mongodb.casbah.MongoCollection @@ -11,7 +12,8 @@ final class ForumEnv( settings: Settings, captcha: Captcha, mongodb: String ⇒ MongoCollection, - userRepo: UserRepo) { + userRepo: UserRepo, + modLog: ModlogApi) { import settings._ @@ -25,7 +27,7 @@ final class ForumEnv( lazy val topicApi = new TopicApi(this, ForumTopicMaxPerPage) - lazy val postApi = new PostApi(this, ForumPostMaxPerPage) + lazy val postApi = new PostApi(this, modLog, ForumPostMaxPerPage) lazy val recent = new Recent( env = this, diff --git a/app/forum/PostApi.scala b/app/forum/PostApi.scala index a0f9d2becf..7c5129867e 100644 --- a/app/forum/PostApi.scala +++ b/app/forum/PostApi.scala @@ -3,12 +3,16 @@ package forum import user.User import http.Context +import mod.ModlogApi import scalaz.effects._ import com.github.ornicar.paginator._ import scala.math.ceil -final class PostApi(env: ForumEnv, maxPerPage: Int) { +final class PostApi( + env: ForumEnv, + modLog: ModlogApi, + maxPerPage: Int) { def create(categSlug: String, slug: String, page: Int): IO[Option[(Categ, Topic, Paginator[Post])]] = for { @@ -64,7 +68,7 @@ final class PostApi(env: ForumEnv, maxPerPage: Int) { def lastNumberOf(topic: Topic): IO[Int] = env.postRepo lastByTopics List(topic) map (_.number) - def lastPageOf(topic: Topic) = + def lastPageOf(topic: Topic) = ceil(topic.nbPosts / maxPerPage.toFloat).toInt def paginator(topic: Topic, page: Int): Paginator[Post] = @@ -77,21 +81,25 @@ final class PostApi(env: ForumEnv, maxPerPage: Int) { maxPerPage = maxPerPage ) | paginator(topic, 1) - def delete(postId: String): IO[Unit] = for { + def delete(postId: String, mod: User): IO[Unit] = for { postOption ← env.postRepo byId postId viewOption ← postOption.fold(view, io(none)) _ ← viewOption.fold( - view ⇒ (view.topic.nbPosts == 1).fold( - env.topicApi.delete(view.categ, view.topic), - for { - _ ← env.postRepo removeIO view.post - _ ← env.topicApi denormalize view.topic - _ ← env.categApi denormalize view.categ - _ ← env.recent.invalidate - } yield () - ), + view ⇒ for { + _ ← (view.topic.nbPosts == 1).fold( + env.topicApi.delete(view.categ, view.topic), + for { + _ ← env.postRepo removeIO view.post + _ ← env.topicApi denormalize view.topic + _ ← env.categApi denormalize view.categ + _ ← env.recent.invalidate + } yield () + ) + post = view.post + _ ← modLog.deletePost(mod, post.userId, post.author, post.ip, + text = "%s / %s / %s".format(view.categ.name, view.topic.name, post.text)) + } yield (), io() ) } yield () - } diff --git a/app/mod/ModlogApi.scala b/app/mod/ModlogApi.scala index a6918f569c..718f01aea0 100644 --- a/app/mod/ModlogApi.scala +++ b/app/mod/ModlogApi.scala @@ -23,6 +23,12 @@ final class ModlogApi(repo: ModlogRepo) { Modlog(mod.id, none, Modlog.ipban, ip.some) } + def deletePost(mod: User, userId: Option[String], author: Option[String], ip: Option[String], text: String) = add { + Modlog(mod.id, userId, Modlog.deletePost, details = Some( + author.fold(_ + " ", "") + ip.fold(_ + " ", "") + text.take(140) + )) + } + def recent = repo recent 200 private def add(modLog: Modlog): IO[Unit] = repo saveIO modLog