Skip to content

Commit

Permalink
Fix IDs in edited messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuchss committed Aug 2, 2024
1 parent 9cf9422 commit 26bdb26
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
26 changes: 15 additions & 11 deletions src/main/kotlin/org/fuchss/matrix/yarb/TimerManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import net.folivo.trixnity.client.room.getTimelineEventReactionAggregation
import net.folivo.trixnity.client.room.message.mentions
import net.folivo.trixnity.client.room.message.reply
import net.folivo.trixnity.client.room.message.text
import net.folivo.trixnity.client.store.eventId
import net.folivo.trixnity.core.model.EventId
import net.folivo.trixnity.core.model.RoomId
import net.folivo.trixnity.core.model.UserId
Expand All @@ -26,7 +25,7 @@ import java.nio.file.StandardCopyOption
import java.time.LocalTime
import java.util.Timer
import java.util.TimerTask
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

class TimerManager(private val matrixBot: MatrixBot, javaTimer: Timer, config: Config) {
companion object {
Expand Down Expand Up @@ -66,7 +65,7 @@ class TimerManager(private val matrixBot: MatrixBot, javaTimer: Timer, config: C
}
},
millisecondsToNextMinute,
1.minutes.inWholeMilliseconds
30.seconds.inWholeMilliseconds
)
}

Expand All @@ -75,8 +74,8 @@ class TimerManager(private val matrixBot: MatrixBot, javaTimer: Timer, config: C
saveTimers()
}

fun removeByRequestMessage(eventId: EventId): TimerData? {
val timer = timers.find { it.requestMessage() == eventId } ?: return null
fun removeByOriginalRequestMessage(eventId: EventId): TimerData? {
val timer = timers.find { it.originalRequestMessage() == eventId } ?: return null
timers.remove(timer)
saveTimers()
return timer
Expand Down Expand Up @@ -126,22 +125,25 @@ class TimerManager(private val matrixBot: MatrixBot, javaTimer: Timer, config: C

data class TimerData(
@JsonProperty val roomId: String,
@JsonProperty val requestMessage: String,
@JsonProperty val originalRequestMessage: String,
@JsonProperty val currentRequestMessage: String,
@JsonProperty val timeToRemind: LocalTime,
@JsonProperty val content: String,
@JsonProperty val botMessageId: String,
@JsonProperty val botReactionMessageId: String
) {
constructor(
roomId: RoomId,
requestMessage: EventId,
originalRequestMessage: EventId,
currentRequestMessage: EventId,
timeToRemind: LocalTime,
content: String,
botMessageId: EventId,
botReactionMessageId: EventId
) : this(
roomId.full,
requestMessage.full,
originalRequestMessage.full,
currentRequestMessage.full,
timeToRemind,
content,
botMessageId.full,
Expand All @@ -150,19 +152,21 @@ class TimerManager(private val matrixBot: MatrixBot, javaTimer: Timer, config: C

fun roomId() = RoomId(roomId)

fun requestMessage() = EventId(requestMessage)
fun originalRequestMessage() = EventId(originalRequestMessage)

fun currentRequestMessage() = EventId(currentRequestMessage)

fun botMessageId() = EventId(botMessageId)

fun botReactionMessageId() = EventId(botReactionMessageId)

suspend fun redactAll(matrixBot: MatrixBot) {
redactBotReaction(matrixBot)
matrixBot.roomApi().redactEvent(roomId(), botMessageId()).getOrThrow()
matrixBot.roomApi().redactEvent(roomId(), botMessageId())
}

suspend fun redactBotReaction(matrixBot: MatrixBot) {
matrixBot.roomApi().redactEvent(roomId(), botReactionMessageId()).getOrThrow()
matrixBot.roomApi().redactEvent(roomId(), botReactionMessageId())
}
}
}
31 changes: 24 additions & 7 deletions src/main/kotlin/org/fuchss/matrix/yarb/commands/ReminderCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ class ReminderCommand(private val config: Config, private val timerManager: Time
parameters: String,
textEventId: EventId,
textEvent: RoomMessageEventContent.TextBased.Text
) {
// Handle New Messages
execute(matrixBot, roomId, parameters, textEventId, textEventId)
}

private suspend fun execute(
matrixBot: MatrixBot,
roomId: RoomId,
parameters: String,
currentMessageEventId: EventId,
initialMessageEventId: EventId
) {
val timeXmessage = parameters.split(" ", limit = 2)
if (timeXmessage.size != 2) {
Expand All @@ -61,28 +72,34 @@ class ReminderCommand(private val config: Config, private val timerManager: Time
return
}

logger.debug("Reminder for {} with '{}'", time, timeXmessage[1])

val botMessageTransactionId =
matrixBot.room().sendMessage(roomId) {
reply(textEventId, null)
reply(initialMessageEventId, null)
text("I'll remind all people at $time with '${timeXmessage[1]}'. If you want to receive a message please click on $EMOJI")
}

logger.debug("Bot Message TransactionId: {}", botMessageTransactionId)
val botMessageId = matrixBot.room().getMessageId(botMessageTransactionId)
if (botMessageId == null) {
logger.error("Could not send bot message :( -- TransactionId: {}", botMessageTransactionId)
return
}
logger.debug("Bot Message Id: {}", botMessageId)

val botReactionMessageTransactionId =
matrixBot.room().sendMessage(roomId) {
react(botMessageId, EMOJI)
}
logger.debug("Bot Reaction Message TransactionId: {}", botReactionMessageTransactionId)
val botReactionMessageId = matrixBot.room().getMessageId(botReactionMessageTransactionId)
if (botReactionMessageId == null) {
logger.error("Could not send bot reaction message :( -- TransactionId: {}", botReactionMessageTransactionId)
return
}
val timer = TimerManager.TimerData(roomId, textEventId, time, timeXmessage[1], botMessageId, botReactionMessageId)
logger.debug("Bot Reaction Message Id: {}", botReactionMessageId)

val timer = TimerManager.TimerData(roomId, initialMessageEventId, currentMessageEventId, time, timeXmessage[1], botMessageId, botReactionMessageId)
timerManager.addTimer(timer)
}

Expand All @@ -93,8 +110,7 @@ class ReminderCommand(private val config: Config, private val timerManager: Time
if (event.senderOrNull == matrixBot.self()) {
return
}

val timer = timerManager.removeByRequestMessage(event.content.redacts) ?: return
val timer = timerManager.removeByOriginalRequestMessage(event.content.redacts) ?: return
timer.redactAll(matrixBot)
}

Expand All @@ -105,13 +121,14 @@ class ReminderCommand(private val config: Config, private val timerManager: Time
roomId: RoomId,
textEvent: RoomMessageEventContent.TextBased.Text
) {
logger.debug("Edit Message: {}", textEvent)
val relatesTo = textEvent.relatesTo ?: return

if (relatesTo.relationType != RelationType.Replace) {
return
}

val timer = this.timerManager.removeByRequestMessage(relatesTo.eventId) ?: return
val timer = this.timerManager.removeByOriginalRequestMessage(relatesTo.eventId) ?: return
timer.redactAll(matrixBot)

val replace = (textEvent.relatesTo as? RelatesTo.Replace) ?: return
Expand All @@ -121,6 +138,6 @@ class ReminderCommand(private val config: Config, private val timerManager: Time
if (parameters.startsWith(COMMAND_NAME)) {
parameters = parameters.substring(COMMAND_NAME.length).trim()
}
execute(matrixBot, senderId, roomId, parameters, replace.eventId, textEvent)
execute(matrixBot, roomId, parameters, eventId, relatesTo.eventId)
}
}

0 comments on commit 26bdb26

Please sign in to comment.