From b8899f0de2ca1878a51a7e2e2d679e3ebc18288c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Fuch=C3=9F?= Date: Sun, 28 Jul 2024 00:24:04 +0200 Subject: [PATCH] Handle deletion of events --- src/main/kotlin/org/fuchss/matrix/yarb/Main.kt | 1 + .../org/fuchss/matrix/yarb/TimerManager.kt | 3 ++- .../matrix/yarb/commands/ReminderCommand.kt | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/org/fuchss/matrix/yarb/Main.kt b/src/main/kotlin/org/fuchss/matrix/yarb/Main.kt index 54bb60f..fe46afd 100644 --- a/src/main/kotlin/org/fuchss/matrix/yarb/Main.kt +++ b/src/main/kotlin/org/fuchss/matrix/yarb/Main.kt @@ -70,6 +70,7 @@ fun main() { reminderCommand.handleUserEditMessage(matrixBot, eventId, userId, roomId, text) } } + matrixBot.subscribeContent { event -> reminderCommand.handleUserDeleteMessage(matrixBot, event) } val loggedOut = matrixBot.startBlocking() timer.cancel() diff --git a/src/main/kotlin/org/fuchss/matrix/yarb/TimerManager.kt b/src/main/kotlin/org/fuchss/matrix/yarb/TimerManager.kt index 83e9a8a..2d85e2b 100644 --- a/src/main/kotlin/org/fuchss/matrix/yarb/TimerManager.kt +++ b/src/main/kotlin/org/fuchss/matrix/yarb/TimerManager.kt @@ -2,6 +2,7 @@ package org.fuchss.matrix.yarb import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.registerKotlinModule @@ -30,7 +31,7 @@ class TimerManager(private val matrixBot: MatrixBot, javaTimer: Timer, config: C private val logger = LoggerFactory.getLogger(TimerManager::class.java) } - private val objectMapper = ObjectMapper().registerKotlinModule().registerModule(JavaTimeModule()) + private val objectMapper = ObjectMapper().registerKotlinModule().registerModule(JavaTimeModule()).enable(SerializationFeature.INDENT_OUTPUT) private val timerFileLocation = config.dataDirectory + "/timers.json" private val timers = mutableListOf() diff --git a/src/main/kotlin/org/fuchss/matrix/yarb/commands/ReminderCommand.kt b/src/main/kotlin/org/fuchss/matrix/yarb/commands/ReminderCommand.kt index 431a45e..825c161 100644 --- a/src/main/kotlin/org/fuchss/matrix/yarb/commands/ReminderCommand.kt +++ b/src/main/kotlin/org/fuchss/matrix/yarb/commands/ReminderCommand.kt @@ -6,9 +6,13 @@ import net.folivo.trixnity.client.room.message.text import net.folivo.trixnity.core.model.EventId import net.folivo.trixnity.core.model.RoomId import net.folivo.trixnity.core.model.UserId +import net.folivo.trixnity.core.model.events.ClientEvent import net.folivo.trixnity.core.model.events.m.RelatesTo import net.folivo.trixnity.core.model.events.m.RelationType +import net.folivo.trixnity.core.model.events.m.room.RedactionEventContent import net.folivo.trixnity.core.model.events.m.room.RoomMessageEventContent +import net.folivo.trixnity.core.model.events.roomIdOrNull +import net.folivo.trixnity.core.model.events.senderOrNull import org.fuchss.matrix.bots.MatrixBot import org.fuchss.matrix.bots.command.Command import org.fuchss.matrix.yarb.Config @@ -92,6 +96,19 @@ class ReminderCommand(private val config: Config, private val timerManager: Time } } + suspend fun handleUserDeleteMessage( + matrixBot: MatrixBot, + event: ClientEvent + ) { + if (event.senderOrNull == matrixBot.self()) { + return + } + + val botMessage = timerManager.removeByRequestMessage(event.content.redacts) ?: return + val roomId = event.roomIdOrNull ?: return + matrixBot.roomApi().redactEvent(roomId, botMessage).getOrThrow() + } + suspend fun handleUserEditMessage( matrixBot: MatrixBot, eventId: EventId,