cancelTravel: WIP

This commit is contained in:
2022-04-23 23:41:09 +02:00
parent eb45ea10d6
commit cc1de1d2d2
4 changed files with 55 additions and 10 deletions

View File

@@ -69,6 +69,14 @@ object MatrixApiClient {
}
}
suspend fun removeRoomAliases(
roomId: RoomId
) {
MatrixApiClient.client.rooms.getRoomAliases(roomId).getOrNull()?.forEach { alias ->
MatrixApiClient.client.rooms.deleteRoomAlias(alias)
}
}
private suspend fun createMainRoomIfNeeded(config: Config): RoomId? {
val mainRoomId: RoomId?

View File

@@ -5,6 +5,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.folivo.trixnity.core.model.events.MessageEventContent
import net.folivo.trixnity.core.model.events.RelatesTo
import net.folivo.trixnity.core.model.events.StateEventContent
const val TRAVEL_CREATED_MESSAGE_EVENT_TYPE = "eu.fosil.travel.created"
@@ -24,16 +25,19 @@ data class TravelCreatedMessageEventContent(
val places: Int,
@SerialName("description")
val description: String,
@SerialName("duplicateNum")
val duplicateNum: Int?,
@SerialName("m.relates_to")
override val relatesTo: RelatesTo.Reference? = null
) : MessageEventContent {
constructor(travel: Travel) : this(
) : MessageEventContent, StateEventContent {
constructor(travel: Travel, duplicateNum: Int? = null) : this(
travel.room.full,
travel.driver.full,
travel.options.from,
travel.options.to,
travel.options.time,
travel.options.places,
travel.options.description
travel.options.description,
duplicateNum
)
}

View File

@@ -6,10 +6,14 @@ import eu.fosil.okupamicoche.usecase.Usecase
import net.folivo.trixnity.clientserverapi.model.rooms.DirectoryVisibility
import net.folivo.trixnity.core.model.RoomId
import net.folivo.trixnity.core.model.UserId
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
suspend fun Usecase.cancelTravel(
roomId: RoomId,
driverId: UserId
driverId: UserId,
eventSentToRoomId: RoomId
) {
val isTravel = TravelRepository.isTravel(roomId)
val isDriver = TravelRepository.isDriver(roomId, driverId)
@@ -38,9 +42,38 @@ suspend fun Usecase.cancelTravel(
MatrixApiClient.setRoomName(roomId, roomName)
// Remove alias
// TODO
MatrixApiClient.removeRoomAliases(roomId)
// Leave the room
MatrixApiClient.client.rooms.leaveRoom(roomId, "Travel canceled")
// Send messages to main room
sendCancelMessageEvents(roomId, eventSentToRoomId)
}
}
private suspend fun sendCancelMessageEvents(
roomId: RoomId,
eventSentToRoomId: RoomId
) {
val mainRoomId = requireNotNull(MatrixApiClient.mainRoomId)
val travel = requireNotNull(TravelRepository.getTravel(roomId))
val instant = Instant.ofEpochMilli(travel.options.time)
val date = DateTimeFormatter.ofPattern("yyyy/MM/dd").withZone(ZoneId.systemDefault()).format(instant)
val time = DateTimeFormatter.ofPattern("H:mm").withZone(ZoneId.systemDefault()).format(instant)
// Send text message to main room
val body = "TRAVEL CANCELED: ${travel.options.from}-${travel.options.to} on $date $time."
MatrixApiClient.sendErrorTextMessage(mainRoomId, body)
// Send text message to room where create command was sent (if it is not main room)
if (eventSentToRoomId != mainRoomId)
MatrixApiClient.sendErrorTextMessage(roomId, body)
// TODO send state event to room and custom event to main room
// Send travel canceled message event to main room
// MatrixApiClient.client.rooms.sendMessageEvent(
// mainRoomId,
// TravelCreatedMessageEventContent(travel)
// )
}

View File

@@ -24,7 +24,7 @@ import java.time.format.DateTimeFormatter
suspend fun Usecase.createTravel(
travelOptions: TravelOptions,
driver: UserId,
createdFromRoomId: RoomId
eventSentToRoomId: RoomId
) {
val newRoomId = createRoom(travelOptions, driver)
val travel = Travel(
@@ -33,7 +33,7 @@ suspend fun Usecase.createTravel(
travelOptions
)
sendCreateMessageEvents(travel, createdFromRoomId)
sendCreateMessageEvents(travel, eventSentToRoomId)
transaction(db) {
TravelEntity.new(travel)
@@ -83,7 +83,7 @@ private suspend fun createRoom(travelOptions: TravelOptions, driver: UserId): Ro
private suspend fun sendCreateMessageEvents(
travel: Travel,
createdFromRoomId: RoomId
eventSentToRoomId: RoomId
) {
val mainRoomId = requireNotNull(MatrixApiClient.mainRoomId)
@@ -114,9 +114,9 @@ private suspend fun sendCreateMessageEvents(
)
// Send text message to room where create command was sent (if it is not main room)
if (createdFromRoomId != mainRoomId) {
if (eventSentToRoomId != mainRoomId) {
MatrixApiClient.client.rooms.sendMessageEvent(
createdFromRoomId,
eventSentToRoomId,
RoomMessageEventContent.TextMessageEventContent(
body = "Travel created!",
format = "org.matrix.custom.html",