getSeatsLeft() re-implemented
This commit is contained in:
@@ -19,7 +19,7 @@ dependencies {
|
||||
implementation("io.ktor:ktor-client-cio:$ktorVersion")
|
||||
|
||||
// Trixnity
|
||||
implementation("net.folivo:trixnity-applicationservice:2.0.0-RC2")
|
||||
implementation("net.folivo:trixnity-applicationservice:2.0.0-RC5")
|
||||
|
||||
// Exposed
|
||||
val exposedVersion: String by project
|
||||
@@ -30,7 +30,7 @@ dependencies {
|
||||
}
|
||||
|
||||
// H2 database
|
||||
implementation("com.h2database:h2:2.1.210")
|
||||
implementation("com.h2database:h2:2.1.212")
|
||||
|
||||
// Jackson (YAML parser)
|
||||
val jacksonVersion: String by project
|
||||
|
||||
@@ -44,6 +44,7 @@ object CommandParser {
|
||||
"join" -> Usecase.joinTravel(room, user, args)
|
||||
"leave" -> Usecase.leaveTravel(room, user)
|
||||
"cancel" -> Usecase.cancelTravel(room, user)
|
||||
// TODO help
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logger.error { "Exception captured: $e" }
|
||||
@@ -61,6 +62,8 @@ object CommandParser {
|
||||
roomId: RoomId,
|
||||
args: List<String>
|
||||
) {
|
||||
// TODO check arguments and show help on error
|
||||
|
||||
val date = args[2]
|
||||
val time = args[3]
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:mm").withZone(ZoneId.systemDefault())
|
||||
@@ -70,7 +73,7 @@ object CommandParser {
|
||||
from = args[0],
|
||||
to = args[1],
|
||||
time = unixTime,
|
||||
seats = args[4].toIntOrNull() ?: 0,
|
||||
seats = args[4].toInt(),
|
||||
description = args.subList(5, args.size).joinToString(" ")
|
||||
)
|
||||
|
||||
|
||||
@@ -54,17 +54,21 @@ object TravelRepository {
|
||||
(content is TravelMembershipStateEventContent) && (content.membership == Membership.JOIN.value)
|
||||
}?.map { UserId("@${it.stateKey}") }
|
||||
|
||||
private suspend fun getUsedSeats(roomId: RoomId) = MatrixApiClient.client.rooms.getState(roomId).getOrNull()?.map {
|
||||
it.content
|
||||
}?.filterIsInstance(TravelMembershipStateEventContent::class.java)?.filter {
|
||||
it.membership == Membership.JOIN.value
|
||||
}?.sumOf { it.seats } ?: 0
|
||||
|
||||
suspend fun getSeatsLeft(roomId: RoomId): Int? {
|
||||
// Get travel total seats (not counting driver)
|
||||
val roomState = MatrixApiClient.client.rooms.getStateEvent<TravelStateEventContent>(roomId).getOrNull()
|
||||
?: return null
|
||||
|
||||
// TODO sum seats per traveler
|
||||
// Get used seats
|
||||
val usedSeats = getUsedSeats(roomId)
|
||||
|
||||
// Get travelers count
|
||||
val travelersCount = getTravelers(roomId)?.size ?: 0
|
||||
|
||||
return roomState.seats - travelersCount
|
||||
return roomState.seats - usedSeats
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user