Skip to content

Commit

Permalink
Allow staff to unrestrict comments hidden by HideImpostors module
Browse files Browse the repository at this point in the history
  • Loading branch information
violine1101 committed Apr 19, 2024
1 parent aa3ea16 commit 0310f3d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/io/github/mojira/arisa/domain/Comment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ data class Comment(
val id: String,
val body: String?,
val author: User,
val updateAuthor: User?,
val getAuthorGroups: () -> List<String>?,
val getUpdateAuthorGroups: () -> List<String>?,
val created: Instant,
val updated: Instant,
val visibilityType: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -279,14 +300,18 @@ private fun getUser(displayName: String) = mockUser(name = "", displayName = dis

private fun getComment(
author: String = "User",
updateAuthor: String? = null,
getAuthorGroups: () -> List<String> = { emptyList() },
getUpdateAuthorGroups: () -> List<String> = { 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,
Expand Down
4 changes: 4 additions & 0 deletions src/test/kotlin/io/github/mojira/arisa/utils/MockDomain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ fun mockComment(
id: String = "0",
body: String = "",
author: User = mockUser(),
updateAuthor: User? = null,
getAuthorGroups: () -> List<String> = { emptyList() },
getUpdateAuthorGroups: () -> List<String> = { emptyList() },
created: Instant = RIGHT_NOW,
updated: Instant = created,
visibilityType: String? = null,
Expand All @@ -79,7 +81,9 @@ fun mockComment(
id,
body,
author,
updateAuthor,
getAuthorGroups,
getUpdateAuthorGroups,
created,
updated,
visibilityType,
Expand Down

0 comments on commit 0310f3d

Please sign in to comment.