Skip to content

Commit

Permalink
Start work on outbox
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuchss committed Aug 2, 2024
1 parent d63e21b commit 2779a9b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 36 deletions.
10 changes: 0 additions & 10 deletions src/main/kotlin/org/fuchss/matrix/yarb/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<RoomMessageEventContent.TextBased.Text>(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<RoomMessageEventContent.TextBased.Text> { eventId, sender, roomId, content ->
reminderCommand.handleUserEditMessage(matrixBot, eventId, sender, roomId, content)
Expand Down
37 changes: 11 additions & 26 deletions src/main/kotlin/org/fuchss/matrix/yarb/commands/ReminderCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 2779a9b

Please sign in to comment.