From 0310f3db5102e381f3c1e851c7d7df8bc1987f29 Mon Sep 17 00:00:00 2001 From: violine1101 Date: Fri, 19 Apr 2024 14:47:11 +0200 Subject: [PATCH] Allow staff to unrestrict comments hidden by HideImpostors module --- .../io/github/mojira/arisa/domain/Comment.kt | 2 ++ .../arisa/infrastructure/jira/Mappers.kt | 2 ++ .../arisa/modules/HideImpostorsModule.kt | 12 ++++++--- .../arisa/modules/HideImpostorsModuleTest.kt | 25 +++++++++++++++++++ .../github/mojira/arisa/utils/MockDomain.kt | 4 +++ 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/io/github/mojira/arisa/domain/Comment.kt b/src/main/kotlin/io/github/mojira/arisa/domain/Comment.kt index f12048d58..163a0887a 100644 --- a/src/main/kotlin/io/github/mojira/arisa/domain/Comment.kt +++ b/src/main/kotlin/io/github/mojira/arisa/domain/Comment.kt @@ -6,7 +6,9 @@ data class Comment( val id: String, val body: String?, val author: User, + val updateAuthor: User?, val getAuthorGroups: () -> List?, + val getUpdateAuthorGroups: () -> List?, val created: Instant, val updated: Instant, val visibilityType: String?, diff --git a/src/main/kotlin/io/github/mojira/arisa/infrastructure/jira/Mappers.kt b/src/main/kotlin/io/github/mojira/arisa/infrastructure/jira/Mappers.kt index 22221bbe8..b97fea76c 100644 --- a/src/main/kotlin/io/github/mojira/arisa/infrastructure/jira/Mappers.kt +++ b/src/main/kotlin/io/github/mojira/arisa/infrastructure/jira/Mappers.kt @@ -221,7 +221,9 @@ fun JiraComment.toDomain( id, body, author.toDomain(jiraClient, config), + updateAuthor?.toDomain(jiraClient, config), { getGroups(jiraClient, author.name).fold({ null }, { it }) }, + { if (updateAuthor == null) emptyList() else getGroups(jiraClient, updateAuthor.name).fold({ null }, { it }) }, createdDate.toInstant(), updatedDate.toInstant(), visibility?.type, diff --git a/src/main/kotlin/io/github/mojira/arisa/modules/HideImpostorsModule.kt b/src/main/kotlin/io/github/mojira/arisa/modules/HideImpostorsModule.kt index b858b45a9..007b5dbf1 100644 --- a/src/main/kotlin/io/github/mojira/arisa/modules/HideImpostorsModule.kt +++ b/src/main/kotlin/io/github/mojira/arisa/modules/HideImpostorsModule.kt @@ -16,7 +16,8 @@ class HideImpostorsModule : Module { .filter(::commentIsRecent) .filter(::userContainsBrackets) .filter(::isNotStaffRestricted) - .filter(::userIsNotVolunteer) + .filter(::authorIsNotVolunteer) + .filter(::updateAuthorIsNotVolunteer) .map { it.restrict.partially1(it.body ?: "") } .toList() @@ -34,8 +35,13 @@ class HideImpostorsModule : Module { this != null && matches("""\[(?:\p{L}|\p{N}|\s)+]\s.+""".toRegex()) } - private fun userIsNotVolunteer(comment: Comment) = - !(comment.getAuthorGroups()?.any { it == "helper" || it == "global-moderators" || it == "staff" } ?: false) + private val staffGroups = setOf("helper", "global-moderators", "staff") + + private fun authorIsNotVolunteer(comment: Comment) = + comment.getAuthorGroups()?.none { staffGroups.contains(it) } ?: true + + private fun updateAuthorIsNotVolunteer(comment: Comment) = + comment.getUpdateAuthorGroups()?.none { staffGroups.contains(it) } ?: true private fun isNotStaffRestricted(comment: Comment) = comment.visibilityType != "group" || comment.visibilityValue != "staff" diff --git a/src/test/kotlin/io/github/mojira/arisa/modules/HideImpostorsModuleTest.kt b/src/test/kotlin/io/github/mojira/arisa/modules/HideImpostorsModuleTest.kt index d15d2ebe4..1220194e0 100644 --- a/src/test/kotlin/io/github/mojira/arisa/modules/HideImpostorsModuleTest.kt +++ b/src/test/kotlin/io/github/mojira/arisa/modules/HideImpostorsModuleTest.kt @@ -7,6 +7,7 @@ import io.github.mojira.arisa.utils.mockUser import io.kotest.assertions.arrow.either.shouldBeLeft import io.kotest.assertions.arrow.either.shouldBeRight import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.booleans.shouldBeFalse import io.kotest.matchers.booleans.shouldBeTrue import java.time.Instant import java.time.temporal.ChronoUnit @@ -168,6 +169,26 @@ class HideImpostorsModuleTest : StringSpec({ result.shouldBeLeft(OperationNotNeededModuleResponse) } + "should return OperationNotNeededModuleResponse when comment was edited by a staff+ user" { + var isRestricted = false + val module = HideImpostorsModule() + val comment = getComment( + author = "[test] test", + updateAuthor = "[Mod] Moderator", + getAuthorGroups = { listOf("user") }, + getUpdateAuthorGroups = { listOf("staff") }, + restrict = { isRestricted = true } + ) + val issue = mockIssue( + comments = listOf(comment) + ) + + val result = module(issue, RIGHT_NOW) + + result.shouldBeLeft(OperationNotNeededModuleResponse) + isRestricted.shouldBeFalse() + } + "should hide comment when user starts with a valid tag but is not of a permission group" { var isRestricted = false val module = HideImpostorsModule() @@ -279,14 +300,18 @@ private fun getUser(displayName: String) = mockUser(name = "", displayName = dis private fun getComment( author: String = "User", + updateAuthor: String? = null, getAuthorGroups: () -> List = { emptyList() }, + getUpdateAuthorGroups: () -> List = { emptyList() }, created: Instant = RIGHT_NOW, visibilityType: String? = null, visibilityValue: String? = null, restrict: (String) -> Unit = { } ) = mockComment( author = getUser(displayName = author), + updateAuthor = if (updateAuthor == null) null else getUser(displayName = updateAuthor), getAuthorGroups = getAuthorGroups, + getUpdateAuthorGroups = getUpdateAuthorGroups, created = created, updated = created, visibilityType = visibilityType, diff --git a/src/test/kotlin/io/github/mojira/arisa/utils/MockDomain.kt b/src/test/kotlin/io/github/mojira/arisa/utils/MockDomain.kt index 311a9cfa8..e368d95c6 100644 --- a/src/test/kotlin/io/github/mojira/arisa/utils/MockDomain.kt +++ b/src/test/kotlin/io/github/mojira/arisa/utils/MockDomain.kt @@ -67,7 +67,9 @@ fun mockComment( id: String = "0", body: String = "", author: User = mockUser(), + updateAuthor: User? = null, getAuthorGroups: () -> List = { emptyList() }, + getUpdateAuthorGroups: () -> List = { emptyList() }, created: Instant = RIGHT_NOW, updated: Instant = created, visibilityType: String? = null, @@ -79,7 +81,9 @@ fun mockComment( id, body, author, + updateAuthor, getAuthorGroups, + getUpdateAuthorGroups, created, updated, visibilityType,