Skip to content

Commit

Permalink
let players erase their own forum posts
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar authored and TheYoBots committed Aug 29, 2022
1 parent 8ba4785 commit d793d87
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 28 deletions.
11 changes: 9 additions & 2 deletions app/controllers/ForumPost.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,15 @@ final class ForumPost(env: Env) extends LilaController(env) with ForumController

def delete(categSlug: String, id: String) =
Auth { implicit ctx => me =>
CategGrantMod(categSlug) {
postApi.delete(categSlug, id, me) map { Ok(_) }
postApi getPost id flatMap {
_ ?? { post =>
if (me.id == ~post.userId)
postApi.erasePost(id) inject Redirect(routes.ForumPost.redirect(id))
else
isGrantedMod(categSlug) flatMap { granted =>
(granted | isGranted(_.ModerateForum)) ?? postApi.delete(categSlug, id, me) map { Ok(_) }
}
}
}
}

Expand Down
5 changes: 1 addition & 4 deletions app/templating/ForumHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,5 @@ trait ForumHelper { self: UserHelper with StringHelper with HasEnv =>
modIcon: Boolean = false
)(implicit lang: Lang): Frag =
if (post.erased) span(cls := "author")("<erased>")
else
post.userId.fold(frag(lila.user.User.anonymous)) { userId =>
userIdLink(userId.some, cssClass = cssClass, withOnline = withOnline, modIcon = modIcon)
}
else userIdLink(post.userId, cssClass = cssClass, withOnline = withOnline, modIcon = modIcon)
}
2 changes: 1 addition & 1 deletion app/templating/UserHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ trait UserHelper { self: I18nHelper with StringHelper with NumberHelper =>
def isStreaming(userId: String) = env.streamer.liveStreamApi isStreaming userId

def userIdLink(
userIdOption: Option[String],
userIdOption: Option[User.ID],
cssClass: Option[String] = None,
withOnline: Boolean = true,
withTitle: Boolean = true,
Expand Down
32 changes: 21 additions & 11 deletions app/views/forum/post.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object post {
)(implicit ctx: Context) = {
st.article(cls := List("forum-post" -> true, "erased" -> post.erased), id := post.number)(
div(cls := "forum-post__metas")(
div(
!post.erased option div(
authorLink(
post = post,
cssClass = s"author${(topic.userId == post.userId) ?? " author--op"}".some
Expand All @@ -59,23 +59,33 @@ object post {
}
),
isGranted(_.IpBan) option span(cls := "mod postip")(post.ip),
ctx.userId.fold(false)(post.shouldShowEditForm(_)) option
ctx.userId.exists(post.shouldShowEditForm) option
a(cls := "mod edit button button-empty text", dataIcon := "m")("Edit"),
canModCateg option a(
cls := "mod delete button button-empty button-red",
href := routes.ForumPost.delete(categ.slug, post.id),
dataIcon := "q",
title := "Delete"
)
if (ctx.userId.has(~post.userId))
postForm(action := routes.ForumPost.delete(categ.slug, post.id))(
submitButton(
cls := "mod delete button button-empty confirm",
dataIcon := "q",
title := "Delete"
)
)
else if (canModCateg)
a(
cls := "mod delete button button-empty",
href := routes.ForumPost.delete(categ.slug, post.id),
dataIcon := "q",
title := "Delete"
)
else emptyFrag
),
a(cls := "anchor", href := url)(s"#${post.number}")
),
p(cls := "forum-post__message")(
if (post.erased) "<erased>"
if (post.erased) "<Comment deleted by user>"
else richText(post.text)
),
reactions(post, canReact),
ctx.userId.exists(post.shouldShowEditForm(_)) option
!post.erased option reactions(post, canReact),
ctx.userId.exists(post.shouldShowEditForm) option
postForm(cls := "edit-post-form", action := routes.ForumPost.edit(post.id))(
textarea(
bits.dataTopic := topic.id,
Expand Down
2 changes: 1 addition & 1 deletion modules/forum/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ final class Env(

lila.common.Bus.subscribeFun("team", "gdprErase") {
case CreateTeam(id, name, _) => categApi.makeTeam(id, name).unit
case lila.user.User.GDPRErase(user) => postApi.erase(user).unit
case lila.user.User.GDPRErase(user) => postApi.eraseAllOf(user).unit
}
}
10 changes: 8 additions & 2 deletions modules/forum/src/main/PostApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,15 @@ final class PostApi(
}

def get(postId: String): Fu[Option[(Topic, Post)]] =
env.postRepo.coll.byId[Post](postId) flatMap {
getPost(postId) flatMap {
_ ?? { post =>
env.topicRepo.coll.byId[Topic](post.topicId) dmap2 { _ -> post }
}
}

def getPost(postId: String): Fu[Option[Post]] =
env.postRepo.coll.byId[Post](postId)

def react(postId: String, me: User, reaction: String, v: Boolean): Fu[Option[Post]] =
Post.reactions(reaction) ?? {
if (v) lila.mon.forum.reaction(reaction).increment()
Expand Down Expand Up @@ -238,7 +241,10 @@ final class PostApi(
ReadPreference.secondaryPreferred
)

def erase(user: User): Funit =
def erasePost(id: Post.ID) =
env.postRepo.coll.updateField($id(id), "erasedAt", DateTime.now).void

def eraseAllOf(user: User): Funit =
env.postRepo.coll.update
.one(
$doc("userId" -> user.id),
Expand Down
25 changes: 18 additions & 7 deletions ui/site/css/forum/_post.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
color: $c-link-dim;
font-weight: bold;
}

.delete {
color: $c-font;
margin-left: 1em;
&:hover {
color: $c-bad;
}
}
}
.mod {
@media (hover: hover) {
Expand All @@ -52,13 +60,16 @@
margin-bottom: 3rem;
}
&.erased {
background: $c-shade;
opacity: 0.6;
}
&.erased .forum-post__message {
font-style: italic;
text-align: center;
margin: 0;
color: $c-font-dimmer;

.forum-post__message {
font-style: italic;
text-align: center;
margin-bottom: 2em;
}
.forum-post__metas {
justify-content: flex-end;
}
}

&__message {
Expand Down

0 comments on commit d793d87

Please sign in to comment.