MatrixApiClient.sendErrorTextMessage()
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package eu.fosil.okupamicoche.cli
|
||||
|
||||
import eu.fosil.okupamicoche.matrix.MatrixApiClient
|
||||
import eu.fosil.okupamicoche.matrix.matrixApiClient
|
||||
import eu.fosil.okupamicoche.model.TravelOptions
|
||||
import eu.fosil.okupamicoche.usecase.Usecase
|
||||
@@ -9,7 +10,6 @@ import eu.fosil.okupamicoche.usecase.travel.leaveTravel
|
||||
import mu.KotlinLogging
|
||||
import net.folivo.trixnity.core.model.RoomId
|
||||
import net.folivo.trixnity.core.model.UserId
|
||||
import net.folivo.trixnity.core.model.events.m.room.RoomMessageEventContent
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
import java.time.format.DateTimeFormatter
|
||||
@@ -49,9 +49,10 @@ object CommandParser {
|
||||
} catch (e: Exception) {
|
||||
logger.error { "Exception captured: $e" }
|
||||
e.printStackTrace()
|
||||
matrixApiClient?.rooms?.sendMessageEvent(
|
||||
MatrixApiClient.sendErrorTextMessage(
|
||||
room,
|
||||
RoomMessageEventContent.TextMessageEventContent("Error executing command: $e")
|
||||
"Error executing command: $e",
|
||||
"Error executing command: <code>$e<code>"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import net.folivo.trixnity.clientserverapi.model.rooms.DirectoryVisibility
|
||||
import net.folivo.trixnity.core.model.RoomAliasId
|
||||
import net.folivo.trixnity.core.model.RoomId
|
||||
import net.folivo.trixnity.core.model.UserId
|
||||
import net.folivo.trixnity.core.model.events.m.room.RoomMessageEventContent
|
||||
import net.folivo.trixnity.core.serialization.createEventContentSerializerMappings
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
@@ -55,6 +56,24 @@ object MatrixApiClient {
|
||||
userId.full.substring(1) // Synapse throws 403 if stateKey starts with '@'
|
||||
)?.getOrNull()?.membership
|
||||
|
||||
suspend fun sendErrorTextMessage(
|
||||
roomId: RoomId,
|
||||
body: String,
|
||||
formattedBody: String? = null
|
||||
) {
|
||||
requireNotNull(matrixApiClient)
|
||||
|
||||
val formattedBodyWithWarningIcon = "⚠️" + (formattedBody ?: body)
|
||||
matrixApiClient.rooms.sendMessageEvent(
|
||||
roomId,
|
||||
RoomMessageEventContent.TextMessageEventContent(
|
||||
body = body,
|
||||
format = "org.matrix.custom.html",
|
||||
formattedBody = formattedBodyWithWarningIcon
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun createMainRoomIfNeeded(): RoomId? {
|
||||
requireNotNull(config)
|
||||
requireNotNull(matrixApiClient)
|
||||
|
||||
@@ -19,25 +19,16 @@ suspend fun Usecase.joinTravel(
|
||||
val membershipState = MatrixApiClient.getTravelMembership(roomId, userId)
|
||||
|
||||
if (!isTravel) {
|
||||
sendErrorTextMessage(roomId, "There is no travel.")
|
||||
MatrixApiClient.sendErrorTextMessage(roomId, "There is no travel.")
|
||||
} else if (isDriver) {
|
||||
sendErrorTextMessage(roomId, "You are the driver, you don't need to join.")
|
||||
MatrixApiClient.sendErrorTextMessage(roomId, "You are the driver, you don't need to join.")
|
||||
} else if (membershipState == Membership.JOIN.value) {
|
||||
sendErrorTextMessage(roomId, "You were already joined.")
|
||||
MatrixApiClient.sendErrorTextMessage(roomId, "You were already joined.")
|
||||
} else {
|
||||
sendJoinedMessageEvents(roomId, userId)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun sendErrorTextMessage(roomId: RoomId, messageBody: String) {
|
||||
requireNotNull(matrixApiClient)
|
||||
|
||||
matrixApiClient.rooms.sendMessageEvent(
|
||||
roomId,
|
||||
RoomMessageEventContent.TextMessageEventContent(messageBody)
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun sendJoinedMessageEvents(
|
||||
roomId: RoomId,
|
||||
userId: UserId
|
||||
|
||||
@@ -19,27 +19,22 @@ suspend fun Usecase.leaveTravel(
|
||||
val membershipState = MatrixApiClient.getTravelMembership(roomId, userId)
|
||||
|
||||
if (!isTravel) {
|
||||
sendErrorTextMessage(roomId, "There is no travel.")
|
||||
MatrixApiClient.sendErrorTextMessage(roomId, "There is no travel.")
|
||||
} else if (isDriver) {
|
||||
sendErrorTextMessage(roomId, "You are the driver, you cannot leave. You can cancel the travel if you want.")
|
||||
MatrixApiClient.sendErrorTextMessage(
|
||||
roomId,
|
||||
"You are the driver, you cannot leave. You can cancel the travel sending '!cancel' if you want.",
|
||||
"You are the driver, you cannot leave. You can cancel the travel sending <code>!cancel</code> if you want."
|
||||
)
|
||||
} else if (membershipState != Membership.JOIN.value) {
|
||||
sendErrorTextMessage(roomId, "You are not joined. state=$membershipState")
|
||||
MatrixApiClient.sendErrorTextMessage(roomId, "You are not joined.")
|
||||
} else if (membershipState == Membership.LEAVE.value) {
|
||||
sendErrorTextMessage(roomId, "You already left.")
|
||||
MatrixApiClient.sendErrorTextMessage(roomId, "You already left.")
|
||||
} else {
|
||||
sendLeftMessageEvents(roomId, userId)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun sendErrorTextMessage(roomId: RoomId, messageBody: String) {
|
||||
requireNotNull(matrixApiClient)
|
||||
|
||||
matrixApiClient.rooms.sendMessageEvent(
|
||||
roomId,
|
||||
RoomMessageEventContent.TextMessageEventContent(messageBody)
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun sendLeftMessageEvents(
|
||||
roomId: RoomId,
|
||||
userId: UserId
|
||||
|
||||
Reference in New Issue
Block a user