From 53dba8599128d77b2c2fb14515e4d44f036d1c89 Mon Sep 17 00:00:00 2001 From: violine1101 Date: Mon, 7 Oct 2024 20:10:51 +0200 Subject: [PATCH] Make comment authors optional --- .../kotlin/io/github/mojira/arisa/Executor.kt | 18 ++++++++++++------ .../io/github/mojira/arisa/domain/Comment.kt | 2 +- .../mojira/arisa/modules/CommandModule.kt | 4 ++-- .../arisa/modules/HideImpostorsModule.kt | 2 +- .../arisa/modules/RemoveBotCommentModule.kt | 2 +- .../arisa/modules/ReopenAwaitingModule.kt | 6 +++--- .../mojira/arisa/modules/ShadowbanModule.kt | 4 ++-- .../modules/commands/DeleteCommentsCommand.kt | 2 +- 8 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/io/github/mojira/arisa/Executor.kt b/src/main/kotlin/io/github/mojira/arisa/Executor.kt index a54306879..bcfd4bdb4 100644 --- a/src/main/kotlin/io/github/mojira/arisa/Executor.kt +++ b/src/main/kotlin/io/github/mojira/arisa/Executor.kt @@ -130,11 +130,17 @@ private fun getSearchResultsFromJira( return searchResult .issues - .map { - it.toDomain( - jiraClient, - ProjectCache.getProjectFromTicketId(it.key), - config - ) + .mapNotNull { + @Suppress("TooGenericExceptionCaught") + try { + it.toDomain( + jiraClient, + ProjectCache.getProjectFromTicketId(it.key), + config + ) + } catch (exception: Exception) { + log.error("Error mapping bug report ${it.key}:" + exception.message + "\n" + exception.stackTrace) + null + } } } 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..fd4b3a62f 100644 --- a/src/main/kotlin/io/github/mojira/arisa/domain/Comment.kt +++ b/src/main/kotlin/io/github/mojira/arisa/domain/Comment.kt @@ -5,7 +5,7 @@ import java.time.Instant data class Comment( val id: String, val body: String?, - val author: User, + val author: User?, val getAuthorGroups: () -> List?, val created: Instant, val updated: Instant, diff --git a/src/main/kotlin/io/github/mojira/arisa/modules/CommandModule.kt b/src/main/kotlin/io/github/mojira/arisa/modules/CommandModule.kt index ef7716f5a..a2d74dc03 100644 --- a/src/main/kotlin/io/github/mojira/arisa/modules/CommandModule.kt +++ b/src/main/kotlin/io/github/mojira/arisa/modules/CommandModule.kt @@ -98,7 +98,7 @@ class CommandModule( ) { val issueKey = issue.key val commentId = comment.id - val user = comment.author.name + val user = comment.author?.name val commandStr = command.command commandResult.fold( { exception -> @@ -162,7 +162,7 @@ class CommandModule( private fun userIsVolunteer(comment: Comment): Boolean { // Ignore comments from the bot itself to prevent accidental infinite recursion and command // injection by malicious user - if (ignoreOwnCommands && comment.author.name == botUserName) { + if (ignoreOwnCommands && comment.author?.name == botUserName) { return false } 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..73baf8b5d 100644 --- a/src/main/kotlin/io/github/mojira/arisa/modules/HideImpostorsModule.kt +++ b/src/main/kotlin/io/github/mojira/arisa/modules/HideImpostorsModule.kt @@ -30,7 +30,7 @@ class HideImpostorsModule : Module { .plus(1, ChronoUnit.DAYS) .isAfter(Instant.now()) - private fun userContainsBrackets(comment: Comment) = with(comment.author.displayName) { + private fun userContainsBrackets(comment: Comment) = with(comment.author?.displayName) { this != null && matches("""\[(?:\p{L}|\p{N}|\s)+]\s.+""".toRegex()) } diff --git a/src/main/kotlin/io/github/mojira/arisa/modules/RemoveBotCommentModule.kt b/src/main/kotlin/io/github/mojira/arisa/modules/RemoveBotCommentModule.kt index f906ebf0d..32eb0a1b4 100644 --- a/src/main/kotlin/io/github/mojira/arisa/modules/RemoveBotCommentModule.kt +++ b/src/main/kotlin/io/github/mojira/arisa/modules/RemoveBotCommentModule.kt @@ -38,7 +38,7 @@ class RemoveBotCommentModule( listOf("staff", "global-moderators").contains(comment.visibilityValue) private fun shouldBeRemoved(comment: Comment) = - comment.author.name == botUserName && + comment.author?.name == botUserName && isVolunteerRestricted(comment) && comment.body.equals(removalTag) } diff --git a/src/main/kotlin/io/github/mojira/arisa/modules/ReopenAwaitingModule.kt b/src/main/kotlin/io/github/mojira/arisa/modules/ReopenAwaitingModule.kt index d6f109fe3..33bcee9fc 100644 --- a/src/main/kotlin/io/github/mojira/arisa/modules/ReopenAwaitingModule.kt +++ b/src/main/kotlin/io/github/mojira/arisa/modules/ReopenAwaitingModule.kt @@ -69,7 +69,7 @@ class ReopenAwaitingModule( // regular users can reopen and have commented OR (!onlyOp && isSoftAR) || // reporter has commented - validComments.any { it.author.name == reporter?.name } + validComments.any { it.author?.name == reporter?.name } } private fun isOPTag(comment: Comment) = comment.visibilityType == "group" && @@ -80,7 +80,7 @@ class ReopenAwaitingModule( comment.visibilityValue == "staff" && (comment.body?.contains(keepARTag) ?: false) - private fun isKeepARMessage(comment: Comment, project: Project) = comment.author.name == "arisabot" && + private fun isKeepARMessage(comment: Comment, project: Project) = comment.author?.name == "arisabot" && ( comment.body?.contains( HelperMessageService.getMessageWithBotSignature( @@ -99,7 +99,7 @@ class ReopenAwaitingModule( lastRun: Instant ): List = comments .filter { it.created.isAfter(resolveTime) && it.created.isAfter(lastRun) } - .filter { !it.author.isNewUser() || it.author.name == reporter?.name } + .filter { it.author != null && (!it.author.isNewUser() || it.author.name == reporter?.name) } .filter { val roles = it.getAuthorGroups() roles == null || roles.intersect(blacklistedRoles).isEmpty() diff --git a/src/main/kotlin/io/github/mojira/arisa/modules/ShadowbanModule.kt b/src/main/kotlin/io/github/mojira/arisa/modules/ShadowbanModule.kt index 20258d11a..9c7a300fd 100644 --- a/src/main/kotlin/io/github/mojira/arisa/modules/ShadowbanModule.kt +++ b/src/main/kotlin/io/github/mojira/arisa/modules/ShadowbanModule.kt @@ -59,9 +59,9 @@ class ShadowbanModule : Module { private fun Issue.checkCommentsForShadowban(timeframe: ExecutionTimeframe): Int = comments .filter { it.isNotStaffRestricted() } - .filter { it.author.isNotVolunteer() } + .filter { it.author != null && it.author.isNotVolunteer() } .filter { - timeframe.shadowbans[it.author.name]?.banTimeContains(it.created) ?: false + timeframe.shadowbans[it.author?.name]?.banTimeContains(it.created) ?: false } .map { it.restrict("${it.body}\n\n_Removed by Arisa -- User is shadowbanned_") } .size diff --git a/src/main/kotlin/io/github/mojira/arisa/modules/commands/DeleteCommentsCommand.kt b/src/main/kotlin/io/github/mojira/arisa/modules/commands/DeleteCommentsCommand.kt index 342fbd928..51c38df71 100644 --- a/src/main/kotlin/io/github/mojira/arisa/modules/commands/DeleteCommentsCommand.kt +++ b/src/main/kotlin/io/github/mojira/arisa/modules/commands/DeleteCommentsCommand.kt @@ -14,7 +14,7 @@ class DeleteCommentsCommand { operator fun invoke(issue: Issue, userName: String): Int { val comments = issue.comments .filter { it.visibilityValue != "staff" } - .filter { it.author.name == userName } + .filter { it.author?.name == userName } Thread { comments