Skip to content

Commit

Permalink
Use offset correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuchss committed Jul 27, 2024
1 parent 7d725fc commit 166161c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/main/kotlin/org/fuchss/matrix/yarb/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data class Config(
@JsonProperty override val dataDirectory: String,
@JsonProperty override val admins: List<String>,
@JsonProperty override val users: List<String> = listOf(),
@JsonProperty("offset_in_minutes") val offsetInMinutes: Int = 0
@JsonProperty("offset_in_minutes") val offsetInMinutes: Long = 0
) : IConfig {
companion object {
private val log: Logger = LoggerFactory.getLogger(Config::class.java)
Expand All @@ -49,4 +49,11 @@ data class Config(
return config
}
}

override fun validate() {
super.validate()
if (offsetInMinutes < 0) {
error("Offset must be greater or equal to 0.")
}
}
}
14 changes: 10 additions & 4 deletions src/main/kotlin/org/fuchss/matrix/yarb/commands/ReminderCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,21 @@ class ReminderCommand(private val config: Config, private val timer: Timer) : Co
return
}

val time = LocalTime.parse(timeXmessage[0])
val time = LocalTime.parse(timeXmessage[0]).withSecond(0).minusMinutes(config.offsetInMinutes)
val now = LocalTime.now()
if (now.isAfter(time)) {
matrixBot.room().sendMessage(roomId) { text("Time ($time) is in the past. I can only remind you at the same day :)") }
matrixBot.room()
.sendMessage(roomId) {
text(
"Time $time is in the past. I can only remind you at the same day :) Also remember that I'll inform you ${config.offsetInMinutes} min before :)"
)
}
return
}

val timelineEvent = matrixBot.getTimelineEvent(roomId, textEventId) ?: return

val timerData = TimerData(matrixBot, roomId, textEventId, time, null)
val timerData = TimerData(matrixBot, roomId, textEventId, time, timeXmessage[1], null)
timers.add(timerData)

matrixBot.room().sendMessage(roomId) {
Expand Down Expand Up @@ -165,7 +170,7 @@ class ReminderCommand(private val config: Config, private val timer: Timer) : Co
val timelineEvent = matrixBot.getTimelineEvent(roomId, messageId) ?: return
matrixBot.room().sendMessage(roomId) {
reply(timelineEvent)
text("Reminder for ${peopleToRemind.joinToString(", ")}")
text("'${timer.content}' ${peopleToRemind.joinToString(", ")}")
}
} catch (e: Exception) {
logger.error("Error during remind: ${e.message}", e)
Expand All @@ -177,6 +182,7 @@ class ReminderCommand(private val config: Config, private val timer: Timer) : Co
val roomId: RoomId,
val requestMessage: EventId,
val timeToRemind: LocalTime,
val content: String,
var botMessageId: EventId?
)
}

0 comments on commit 166161c

Please sign in to comment.