Implement forum moderation

pull/1/merge
Thibault Duplessis 2012-05-27 13:20:32 +02:00
parent d073b601eb
commit 91279f59b4
8 changed files with 48 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package controllers
import lila._
import views._
import security.Permission
object ForumPost extends LilaController with Forum {
@ -9,7 +10,7 @@ object ForumPost extends LilaController with Forum {
def postApi = env.forum.postApi
def forms = env.forum.forms
val recent = Open { implicit ctx =>
val recent = Open { implicit ctx
Ok(html.forum.post.recent(env.forum.recent(ctx.me)))
}
@ -31,5 +32,8 @@ object ForumPost extends LilaController with Forum {
}
}
def delete(id: String) = TODO
def delete(id: String) = Secure(Permission.ModerateForum) { implicit ctx
_
IOk(postApi delete id)
}
}

View File

@ -67,4 +67,22 @@ final class PostApi(env: ForumEnv, maxPerPage: Int) {
currentPage = page,
maxPerPage = maxPerPage
) | paginator(topic, 1)
def delete(postId: String): 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 ()
),
io()
)
} yield ()
}

View File

@ -42,6 +42,10 @@ final class PostRepo(
upsert = true)
}
def removeIO(post: Post): IO[Unit] = io {
remove(DBObject("_id" -> post.id))
}
def byTopicQuery(topic: Topic) = DBObject("topicId" -> topic.id)
private def byTopicsQuery(topics: List[Topic]) =

View File

@ -59,6 +59,12 @@ final class TopicApi(env: ForumEnv, maxPerPage: Int) {
maxPerPage = maxPerPage
) | paginator(categ, 1)
def delete(categ: Categ, topic: Topic): IO[Unit] = for {
_ env.topicRepo removeIO topic
_ env.categApi denormalize categ
_ env.recent.invalidate
} yield ()
def denormalize(topic: Topic): IO[Unit] = for {
nbPosts env.postRepo countByTopics List(topic)
lastPost env.postRepo lastByTopics List(topic)

View File

@ -58,6 +58,10 @@ final class TopicRepo(
upsert = true)
}
def removeIO(topic: Topic): IO[Unit] = io {
remove(DBObject("_id" -> topic.id))
}
val sortQuery = DBObject("updatedAt" -> -1)
def byCategQuery(categ: Categ) = DBObject("categId" -> categ.slug)

View File

@ -36,9 +36,6 @@ title = categ.name) {
<a href="@routes.ForumTopic.show(categ.slug, topic.slug, topic pageOf post)#@post.number">@showDate(post.createdAt)</a><br />by @authorName(post)
}
</td>
@if(isGranted(Permission.ModerateForum)) {
<td><a class="delete" href="@routes.ForumTopic.delete(topic.id)">Delete</a></td>
}
</tr>
}
</tbody>

View File

@ -96,8 +96,7 @@ GET /forum/:categSlug/form controllers.ForumTopic.form(categSlug: St
POST /forum/:categSlug/new controllers.ForumTopic.create(categSlug: String)
GET /forum/:categSlug/:slug controllers.ForumTopic.show(categSlug: String, slug: String, page: Int ?= 1)
POST /forum/:categSlug/:slug/new controllers.ForumPost.create(categSlug: String, slug: String, page: Int ?= 1)
GET /forum/delete/topic/:id controllers.ForumTopic.delete(id: String)
GET /forum/delete/post/:id controllers.ForumPost.delete(id: String)
POST /forum/delete/post/:id controllers.ForumPost.delete(id: String)
# Monitor
GET /monitor controllers.Monitor.index

View File

@ -10,5 +10,13 @@ $(function() {
$input.val(i2);
});
});
$("#lichess_forum a.delete").unbind("click").click(function() {
if (confirm("Delete?")) {
var $this = $(this)
$.post($this.attr("href"), function(d) {
$this.closest(".post").slideUp(500);
});
}
return false;
});
});