Create travel
This commit is contained in:
@@ -62,7 +62,7 @@ object CommandParser {
|
||||
description = args[5]
|
||||
)
|
||||
|
||||
createTravel(userId, roomId, travelOptions)
|
||||
createTravel(travelOptions, userId, roomId)
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user