From 6868628a7ad56e0368d7a93d5225715858b2da63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Fuch=C3=9F?= Date: Mon, 2 Sep 2024 21:32:01 +0200 Subject: [PATCH] Update Parent & Spotless apply --- pom.xml | 2 +- .../kotlin/org/fuchss/matrix/joinlink/Main.kt | 28 +++++----- .../matrix/joinlink/handler/JoinRoom.kt | 15 +++-- .../joinlink/handler/command/LinkCommand.kt | 55 ++++++++++++------- .../joinlink/handler/command/UnlinkCommand.kt | 19 ++++++- 5 files changed, 78 insertions(+), 41 deletions(-) diff --git a/pom.xml b/pom.xml index d31ce82..d9f3dad 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.fuchss maven-parent - 0.21.31 + 0.21.34 org.fuchss.matrix diff --git a/src/main/kotlin/org/fuchss/matrix/joinlink/Main.kt b/src/main/kotlin/org/fuchss/matrix/joinlink/Main.kt index 5c90e48..cc655b4 100644 --- a/src/main/kotlin/org/fuchss/matrix/joinlink/Main.kt +++ b/src/main/kotlin/org/fuchss/matrix/joinlink/Main.kt @@ -63,24 +63,26 @@ fun main() { private suspend fun getMatrixClient(config: Config): MatrixClient { val existingMatrixClient = - MatrixClient.fromStore(createRepositoriesModule(config), createMediaStore(config)) { - modules = createDefaultTrixnityModules() + joinLinkModule - }.getOrThrow() + MatrixClient + .fromStore(createRepositoriesModule(config), createMediaStore(config)) { + modules = createDefaultTrixnityModules() + joinLinkModule + }.getOrThrow() if (existingMatrixClient != null) { return existingMatrixClient } val matrixClient = - MatrixClient.login( - baseUrl = Url(config.baseUrl), - identifier = IdentifierType.User(config.username), - password = config.password, - repositoriesModule = createRepositoriesModule(config), - mediaStore = createMediaStore(config), - initialDeviceDisplayName = "${MatrixBot::class.java.`package`.name}-${Random.Default.nextInt()}" - ) { - modules = createDefaultTrixnityModules() + joinLinkModule - }.getOrThrow() + MatrixClient + .login( + baseUrl = Url(config.baseUrl), + identifier = IdentifierType.User(config.username), + password = config.password, + repositoriesModule = createRepositoriesModule(config), + mediaStore = createMediaStore(config), + initialDeviceDisplayName = "${MatrixBot::class.java.`package`.name}-${Random.Default.nextInt()}" + ) { + modules = createDefaultTrixnityModules() + joinLinkModule + }.getOrThrow() return matrixClient } diff --git a/src/main/kotlin/org/fuchss/matrix/joinlink/handler/JoinRoom.kt b/src/main/kotlin/org/fuchss/matrix/joinlink/handler/JoinRoom.kt index 6cedf26..d20dd70 100644 --- a/src/main/kotlin/org/fuchss/matrix/joinlink/handler/JoinRoom.kt +++ b/src/main/kotlin/org/fuchss/matrix/joinlink/handler/JoinRoom.kt @@ -64,9 +64,11 @@ internal suspend fun handleJoinsToMatrixJoinLinkRooms( // Cleanup recent list val now = Clock.System.now() - recentHandledJoinEventIdToTimestamp.toMap().filter { - now - Instant.fromEpochMilliseconds(it.value) > 20.seconds - }.forEach { recentHandledJoinEventIdToTimestamp.remove(it.key) } + recentHandledJoinEventIdToTimestamp + .toMap() + .filter { + now - Instant.fromEpochMilliseconds(it.value) > 20.seconds + }.forEach { recentHandledJoinEventIdToTimestamp.remove(it.key) } val eventId = event.idOrNull?.full ?: "$roomId-$userId" val originTimestamp = event.originTimestampOrNull ?: now.toEpochMilliseconds() @@ -122,7 +124,12 @@ private suspend fun handleValidJoinEvent( } // Validate RoomsToJoin .. - val currentJoinLink = matrixBot.getStateEvent(roomToJoin).getOrNull()?.joinlinkRoom.decrypt(config) + val currentJoinLink = + matrixBot + .getStateEvent(roomToJoin) + .getOrNull() + ?.joinlinkRoom + .decrypt(config) if (currentJoinLink != roomId) { logger.error("I will not invite $userId to $roomToJoin because the link to $roomId is broken ($currentJoinLink <-> $roomId). Skipping ..") return diff --git a/src/main/kotlin/org/fuchss/matrix/joinlink/handler/command/LinkCommand.kt b/src/main/kotlin/org/fuchss/matrix/joinlink/handler/command/LinkCommand.kt index 28df824..30ff82b 100644 --- a/src/main/kotlin/org/fuchss/matrix/joinlink/handler/command/LinkCommand.kt +++ b/src/main/kotlin/org/fuchss/matrix/joinlink/handler/command/LinkCommand.kt @@ -23,7 +23,9 @@ import org.fuchss.matrix.joinlink.events.RoomToJoinEventContent import org.fuchss.matrix.joinlink.helper.decrypt import org.fuchss.matrix.joinlink.helper.encrypt -internal class LinkCommand(private val config: Config) : Command() { +internal class LinkCommand( + private val config: Config +) : Command() { override val name: String = "link" override val params: String = "[Link/ID to TargetRoom] {Readable Name of Link}" override val help: String = "create a join link for the room (if none provided, the room the message was sent in is used)" @@ -57,7 +59,12 @@ internal class LinkCommand(private val config: Config) : Command() { return } - val currentJoinLink = matrixBot.getStateEvent(targetRoom).getOrNull()?.joinlinkRoom.decrypt(config) + val currentJoinLink = + matrixBot + .getStateEvent(targetRoom) + .getOrNull() + ?.joinlinkRoom + .decrypt(config) if (currentJoinLink != null) { matrixBot.room().sendMessage(roomId) { text("Link to share the Room (${targetRoom.matrixTo()}): ${currentJoinLink.matrixTo()}") } @@ -73,23 +80,25 @@ internal class LinkCommand(private val config: Config) : Command() { // Create Join Room val joinLink = - matrixBot.roomApi().createRoom( - visibility = DirectoryVisibility.PRIVATE, - name = "Matrix Join Link '$nameOfLink'", - topic = "This is the Matrix Join Link Room called '$nameOfLink'. You can leave the room :)", - preset = CreateRoom.Request.Preset.PUBLIC, - powerLevelContentOverride = - PowerLevelsEventContent( - users = mapOf(matrixBot.self() to ADMIN_POWER_LEVEL), - events = - mapOf( - RoomToJoinEventContent.ID to ADMIN_POWER_LEVEL, - JoinLinkEventContent.ID to ADMIN_POWER_LEVEL - ), - eventsDefault = ADMIN_POWER_LEVEL, - stateDefault = ADMIN_POWER_LEVEL - ) - ).getOrThrow() + matrixBot + .roomApi() + .createRoom( + visibility = DirectoryVisibility.PRIVATE, + name = "Matrix Join Link '$nameOfLink'", + topic = "This is the Matrix Join Link Room called '$nameOfLink'. You can leave the room :)", + preset = CreateRoom.Request.Preset.PUBLIC, + powerLevelContentOverride = + PowerLevelsEventContent( + users = mapOf(matrixBot.self() to ADMIN_POWER_LEVEL), + events = + mapOf( + RoomToJoinEventContent.ID to ADMIN_POWER_LEVEL, + JoinLinkEventContent.ID to ADMIN_POWER_LEVEL + ), + eventsDefault = ADMIN_POWER_LEVEL, + stateDefault = ADMIN_POWER_LEVEL + ) + ).getOrThrow() // Set History Visibility to Joined to prevent others from seeing too much history matrixBot.roomApi().sendStateEvent(joinLink, HistoryVisibilityEventContent(HistoryVisibilityEventContent.HistoryVisibility.JOINED)).getOrThrow() @@ -111,7 +120,13 @@ internal class LinkCommand(private val config: Config) : Command() { targetRoom: RoomId ): Boolean { // Check that the user is in the room - if (!matrixBot.roomApi().getJoinedMembers(targetRoom).getOrThrow().joined.containsKey(sender)) { + if (!matrixBot + .roomApi() + .getJoinedMembers(targetRoom) + .getOrThrow() + .joined + .containsKey(sender) + ) { logger.info("User ${sender.full} is not in the room (${targetRoom.matrixTo()})") matrixBot.room().sendMessage(roomId) { text("You are not in the room (${targetRoom.matrixTo()})") } return false diff --git a/src/main/kotlin/org/fuchss/matrix/joinlink/handler/command/UnlinkCommand.kt b/src/main/kotlin/org/fuchss/matrix/joinlink/handler/command/UnlinkCommand.kt index 018c739..575893c 100644 --- a/src/main/kotlin/org/fuchss/matrix/joinlink/handler/command/UnlinkCommand.kt +++ b/src/main/kotlin/org/fuchss/matrix/joinlink/handler/command/UnlinkCommand.kt @@ -17,7 +17,9 @@ import org.fuchss.matrix.joinlink.events.JoinLinkEventContent import org.fuchss.matrix.joinlink.events.RoomToJoinEventContent import org.fuchss.matrix.joinlink.helper.decrypt -internal class UnlinkCommand(private val config: Config) : Command() { +internal class UnlinkCommand( + private val config: Config +) : Command() { override val name: String = "unlink" override val params: String = "[Link/ID to TargetRoom]" override val help: String = "remove all join links for the room" @@ -48,7 +50,13 @@ internal class UnlinkCommand(private val config: Config) : Command() { // Bot Admins are allowed to unlink rooms. Check permissions for non-bot-admins. if (!config.isBotAdmin(sender)) { // Check that the user is in the room - if (!matrixBot.roomApi().getJoinedMembers(targetRoom).getOrThrow().joined.containsKey(sender)) { + if (!matrixBot + .roomApi() + .getJoinedMembers(targetRoom) + .getOrThrow() + .joined + .containsKey(sender) + ) { logger.info("User ${sender.full} is not in the room (${targetRoom.matrixTo()})") matrixBot.room().sendMessage(roomId) { text("You are not in the room (${targetRoom.matrixTo()})") } return @@ -64,7 +72,12 @@ internal class UnlinkCommand(private val config: Config) : Command() { } } - val currentJoinLink = matrixBot.getStateEvent(targetRoom).getOrNull()?.joinlinkRoom.decrypt(config) + val currentJoinLink = + matrixBot + .getStateEvent(targetRoom) + .getOrNull() + ?.joinlinkRoom + .decrypt(config) if (currentJoinLink == null) { matrixBot.room().sendMessage(roomId) { text("No Matrix Join Links available .. nothing to do ..") }