From 2779a9b0fef86bbf7b566b42166f9a24782c9f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Fuch=C3=9F?= Date: Fri, 2 Aug 2024 22:03:07 +0200 Subject: [PATCH] Start work on outbox --- .../kotlin/org/fuchss/matrix/yarb/Main.kt | 10 ----- .../matrix/yarb/commands/ReminderCommand.kt | 37 ++++++------------- 2 files changed, 11 insertions(+), 36 deletions(-) diff --git a/src/main/kotlin/org/fuchss/matrix/yarb/Main.kt b/src/main/kotlin/org/fuchss/matrix/yarb/Main.kt index fe46afd..7a77a19 100644 --- a/src/main/kotlin/org/fuchss/matrix/yarb/Main.kt +++ b/src/main/kotlin/org/fuchss/matrix/yarb/Main.kt @@ -51,16 +51,6 @@ fun main() { matrixBot.subscribeContent { event -> handleCommand(commands, event, matrixBot, config, ReminderCommand.COMMAND_NAME) } matrixBot.subscribeContent { encEvent -> handleEncryptedCommand(commands, encEvent, matrixBot, config, ReminderCommand.COMMAND_NAME) } - // Listen to own messages (i.e., reminder messages) - matrixBot.subscribeContent(listenNonUsers = true, listenBotEvents = true) { eventId, sender, roomId, content -> - reminderCommand.handleBotMessageForReminder(matrixBot, eventId, sender, roomId, content) - } - matrixBot.subscribeContent(listenNonUsers = true, listenBotEvents = true) { encryptedEvent -> - decryptMessage(encryptedEvent, matrixBot) { eventId, userId, roomId, text -> - reminderCommand.handleBotMessageForReminder(matrixBot, eventId, userId, roomId, text) - } - } - // Listen for edits of user messages matrixBot.subscribeContent { eventId, sender, roomId, content -> reminderCommand.handleUserEditMessage(matrixBot, eventId, sender, roomId, content) 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 e229bf4..11921ae 100644 --- a/src/main/kotlin/org/fuchss/matrix/yarb/commands/ReminderCommand.kt +++ b/src/main/kotlin/org/fuchss/matrix/yarb/commands/ReminderCommand.kt @@ -15,6 +15,7 @@ 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.bots.firstWithTimeout import org.fuchss.matrix.yarb.Config import org.fuchss.matrix.yarb.TimerManager import java.time.LocalTime @@ -64,36 +65,20 @@ class ReminderCommand(private val config: Config, private val timerManager: Time val timelineEvent = matrixBot.getTimelineEvent(roomId, textEventId) ?: return timerManager.addTimer(roomId, textEventId, time, timeXmessage[1]) - matrixBot.room().sendMessage(roomId) { - reply(timelineEvent) - text("I'll remind all people at $time with '${timeXmessage[1]}'. If you want to receive a message please click on $EMOJI") - } - } - - suspend fun handleBotMessageForReminder( - matrixBot: MatrixBot, - eventId: EventId, - sender: UserId, - roomId: RoomId, - textEvent: RoomMessageEventContent.TextBased.Text - ) { - if (sender != matrixBot.self()) { - return - } - - val relatesTo = textEvent.relatesTo ?: return + val transactionId = + matrixBot.room().sendMessage(roomId) { + reply(timelineEvent) + text("I'll remind all people at $time with '${timeXmessage[1]}'. If you want to receive a message please click on $EMOJI") + } - if (relatesTo.relationType != RelationType.Reply) { + val outboxWithTransaction = matrixBot.room().getOutbox().firstWithTimeout { it[transactionId] != null } + if (outboxWithTransaction == null) { + logger.error("Cannot find outbox") return } - - val isRelated = timerManager.addBotMessageToTimer(relatesTo.eventId, eventId) - if (!isRelated) { - return - } - + val botMessageId = outboxWithTransaction[transactionId]?.firstWithTimeout { it?.eventId != null }?.eventId ?: return matrixBot.room().sendMessage(roomId) { - react(eventId, EMOJI) + react(botMessageId, EMOJI) } }