Skip to content

Commit

Permalink
Update Parent & Spotless apply
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuchss committed Sep 2, 2024
1 parent ea27be8 commit 6868628
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.fuchss</groupId>
<artifactId>maven-parent</artifactId>
<version>0.21.31</version>
<version>0.21.34</version>
</parent>

<groupId>org.fuchss.matrix</groupId>
Expand Down
28 changes: 15 additions & 13 deletions src/main/kotlin/org/fuchss/matrix/joinlink/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 11 additions & 4 deletions src/main/kotlin/org/fuchss/matrix/joinlink/handler/JoinRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -122,7 +124,12 @@ private suspend fun handleValidJoinEvent(
}

// Validate RoomsToJoin ..
val currentJoinLink = matrixBot.getStateEvent<JoinLinkEventContent>(roomToJoin).getOrNull()?.joinlinkRoom.decrypt(config)
val currentJoinLink =
matrixBot
.getStateEvent<JoinLinkEventContent>(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down Expand Up @@ -57,7 +59,12 @@ internal class LinkCommand(private val config: Config) : Command() {
return
}

val currentJoinLink = matrixBot.getStateEvent<JoinLinkEventContent>(targetRoom).getOrNull()?.joinlinkRoom.decrypt(config)
val currentJoinLink =
matrixBot
.getStateEvent<JoinLinkEventContent>(targetRoom)
.getOrNull()
?.joinlinkRoom
.decrypt(config)

if (currentJoinLink != null) {
matrixBot.room().sendMessage(roomId) { text("Link to share the Room (${targetRoom.matrixTo()}): ${currentJoinLink.matrixTo()}") }
Expand All @@ -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()
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -64,7 +72,12 @@ internal class UnlinkCommand(private val config: Config) : Command() {
}
}

val currentJoinLink = matrixBot.getStateEvent<JoinLinkEventContent>(targetRoom).getOrNull()?.joinlinkRoom.decrypt(config)
val currentJoinLink =
matrixBot
.getStateEvent<JoinLinkEventContent>(targetRoom)
.getOrNull()
?.joinlinkRoom
.decrypt(config)

if (currentJoinLink == null) {
matrixBot.room().sendMessage(roomId) { text("No Matrix Join Links available .. nothing to do ..") }
Expand Down

0 comments on commit 6868628

Please sign in to comment.