From ea2349759c0f697c9e30a3aaa2429b4596885a25 Mon Sep 17 00:00:00 2001 From: Eneko Nieto Date: Fri, 18 Mar 2022 00:02:32 +0100 Subject: [PATCH] Create travel --- .../fosil/okupamicoche/cli/CommandParser.kt | 2 +- .../usecase/travel/createTravel.kt | 82 ++++++++++++++----- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/eu/fosil/okupamicoche/cli/CommandParser.kt b/src/main/kotlin/eu/fosil/okupamicoche/cli/CommandParser.kt index 27c1031..af6880d 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/cli/CommandParser.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/cli/CommandParser.kt @@ -62,7 +62,7 @@ object CommandParser { description = args[5] ) - createTravel(userId, roomId, travelOptions) + createTravel(travelOptions, userId, roomId) } diff --git a/src/main/kotlin/eu/fosil/okupamicoche/usecase/travel/createTravel.kt b/src/main/kotlin/eu/fosil/okupamicoche/usecase/travel/createTravel.kt index 5f6bfd8..967ed75 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/usecase/travel/createTravel.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/usecase/travel/createTravel.kt @@ -19,23 +19,16 @@ import java.time.LocalDateTime import java.time.ZoneId import java.time.format.DateTimeFormatter - private val logger = KotlinLogging.logger {} suspend fun createTravel( + travelOptions: TravelOptions, driver: UserId, - roomId: RoomId, - travelOptions: TravelOptions + roomId: RoomId ) { requireNotNull(matrixApiClient) - val newRoomAliasId = getRoomAlias(travelOptions) - val newRoomId = matrixApiClient.rooms.createRoom( - visibility = Visibility.PUBLIC, - roomAliasId = newRoomAliasId, -// name = - ).getOrThrow() - + val newRoomId = createRoom(travelOptions, driver) val travel = Travel( newRoomId, driver, @@ -46,24 +39,72 @@ suspend fun createTravel( transaction(db) { TravelEntity.new(travel) - TravelEntity.all().forEachIndexed { index, travel -> - logger.info("#$index: travel id=${travel.id} origin=${travel.from} dest=${travel.to} travel=$travel") - } } } -private fun getRoomAlias(travelOptions: TravelOptions): RoomAliasId { +private suspend fun getRoomAlias(travelOptions: TravelOptions): RoomAliasId { + val roomAliasPrefix = getRoomAliasPrefix(travelOptions) + logger.info { "roomAliasPrefix=$roomAliasPrefix" } + var i = 0 + // Look for available room alias + while (!isRoomAliasAvailable(getRoomAliasAttempt(roomAliasPrefix, i))) + i++ - val timezone = ZoneId.of("Europe/Paris") + logger.info { "getRoomAlias=${getRoomAliasAttempt(roomAliasPrefix, i)}" } + return getRoomAliasAttempt(roomAliasPrefix, i) +} + +private fun getRoomAliasPrefix(travelOptions: TravelOptions): String { + val timezone = ZoneId.of("UTC") +// val timezone = ZoneId.of("Europe/Paris") val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(travelOptions.time), timezone) val date = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(localDateTime) val time = DateTimeFormatter.ofPattern("H:mm").format(localDateTime).replace(':', 'H') - val newRoomAliasId = travelOptions.run { - RoomAliasId("#viaje_${from}-${to}_${date}_${time}:${ConfigReader.config?.homeserver}") + return travelOptions.run { + "#viaje_${from}-${to}_${date}_${time}" } +} - return newRoomAliasId +private fun getRoomAliasAttempt(roomAliasPrefix: String, attempt: Int): RoomAliasId { + val config = requireNotNull(ConfigReader.config) + val attemptPart = if (attempt <= 0) "" else "_$attempt" + + return RoomAliasId("${roomAliasPrefix}$attemptPart:${config.homeserver.host}") +} + +private suspend fun isRoomAliasAvailable(roomAliasId: RoomAliasId): Boolean { + requireNotNull(matrixApiClient) + + val roomId = matrixApiClient.rooms.getRoomAlias(roomAliasId).getOrNull() + logger.info { "$roomAliasId roomId=$roomId" } + + return roomId == null +} + +private fun getRoomName(travelOptions: TravelOptions): String { + val timezone = ZoneId.of("UTC") +// val timezone = ZoneId.of("Europe/Paris") + val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(travelOptions.time), timezone) + val date = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(localDateTime) + val time = DateTimeFormatter.ofPattern("H:mm").format(localDateTime) + + return travelOptions.run { + "Viaje ${from}-${to} $date $time | $places places available" + } +} + +private suspend fun createRoom(travelOptions: TravelOptions, driver: UserId): RoomId { + requireNotNull(matrixApiClient) + + val newRoomAliasId = getRoomAlias(travelOptions) + return matrixApiClient.rooms.createRoom( + visibility = Visibility.PUBLIC, + roomAliasId = newRoomAliasId, + name = getRoomName(travelOptions), + topic = travelOptions.description, + invite = setOf(driver) + ).getOrThrow() } private suspend fun sendCreateEvents( @@ -72,7 +113,8 @@ private suspend fun sendCreateEvents( ) { requireNotNull(matrixApiClient) - val timezone = ZoneId.of("Europe/Paris") + val timezone = ZoneId.of("UTC") +// val timezone = ZoneId.of("Europe/Paris") val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(travel.options.time), timezone) val date = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(localDateTime) val time = DateTimeFormatter.ofPattern("H:mm").format(localDateTime) @@ -92,4 +134,4 @@ private suspend fun sendCreateEvents( TravelCreatedEventContent(travel) ) -} \ No newline at end of file +}