Config file reworked

This commit is contained in:
2022-03-11 00:08:50 +01:00
parent aa16be7722
commit 950b9cc542
4 changed files with 44 additions and 15 deletions

View File

@@ -27,19 +27,23 @@ suspend fun main() {
}
val matrixApiClient = MatrixApiClient(
baseUrl = Url(config.homeServerUrl),
).apply { accessToken.value = config.asToken }
baseUrl = Url(
(if (config.homeserver.secure) "https" else "http") +
"://${config.homeserver.host}:${config.homeserver.port}"
),
).apply { accessToken.value = config.tokens.appService }
coroutineScope {
launch {
val roomMainAlias = "#${config.mainRoom}:${config.homeserver.host}"
val roomAliasRes =
matrixApiClient.rooms.getRoomAlias(RoomAliasId(config.mainRoomId)).getOrNull()
matrixApiClient.rooms.getRoomAlias(RoomAliasId(roomMainAlias)).getOrNull()
if (roomAliasRes == null) {
logger.info("Creating ${config.mainRoomId} public room")
logger.info("Creating $roomMainAlias public room")
matrixApiClient.rooms.createRoom(
visibility = Visibility.PUBLIC,
roomAliasId = RoomAliasId(config.mainRoomId)
roomAliasId = RoomAliasId(roomMainAlias)
)
} else {
val mainRoomId = roomAliasRes.roomId
@@ -56,7 +60,7 @@ suspend fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
matrixAppserviceModule(
properties = MatrixAppserviceProperties(config.hsToken),
properties = MatrixAppserviceProperties(config.tokens.homeserver),
appserviceService = createAppService(matrixApiClient)
)
}.start(wait = true)

View File

@@ -1,10 +1,13 @@
package eu.fosil.okupamicoche.cli
import eu.fosil.okupamicoche.config.ConfigReader.config
import eu.fosil.okupamicoche.db
import eu.fosil.okupamicoche.model.Travel
import io.ktor.http.*
import mu.KotlinLogging
import net.folivo.trixnity.client.api.MatrixApiClient
import net.folivo.trixnity.client.api.model.rooms.Visibility
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
@@ -64,9 +67,15 @@ object CommandParser {
val places = args[4].toIntOrNull() ?: 0
val description = args[5]
val newRoomAliasId = RoomAliasId("#viaje_$origin-${destination}_${date}_$time:${config?.homeserver}")
val newRoomId = matrixApiClient.rooms.createRoom(
visibility = Visibility.PUBLIC,
roomAliasId = newRoomAliasId
).getOrThrow()
transaction(db) {
Travel.new {
this.roomId = roomId.full
this.roomId = newRoomId.full
this.driver = userId.full
this.origin = origin
this.destination = destination
@@ -79,12 +88,14 @@ object CommandParser {
}
}
// Send text message
val displayName = matrixApiClient.users.getDisplayName(userId).getOrNull() ?: userId.full
matrixApiClient.rooms.sendMessageEvent(
roomId,
RoomMessageEventContent.TextMessageEventContent("$displayName created a new travel! $origin-$destination on $date $time with $places free places.")
)
// Send new travel event
}
}

View File

@@ -1,8 +1,18 @@
package eu.fosil.okupamicoche.config
data class Config(
val mainRoomId: String,
val homeServerUrl: String,
val asToken: String,
val hsToken: String
val homeserver: HomeserverConfig,
val tokens: TokensConfig,
val mainRoom: String
)
data class HomeserverConfig(
val host: String,
val secure: Boolean,
val port: Number
)
data class TokensConfig(
val appService: String,
val homeserver: String
)

View File

@@ -1,4 +1,8 @@
mainRoomId: "#viajes:okupamicoche-synapse"
homeServerUrl: "http://okupamicoche-synapse:8008/"
asToken: "30c05ae90a248a4188e620216fa72e349803310ec83e2a77b34fe90be6081f46"
hsToken: "312df522183efd404ec1cd22d2ffa4bbc76a8c1ccf541dd692eef281356bb74e"
homeserver:
host: okupamicoche-synapse
secure: false
port: 8008
tokens:
appService: "30c05ae90a248a4188e620216fa72e349803310ec83e2a77b34fe90be6081f46"
homeserver: "312df522183efd404ec1cd22d2ffa4bbc76a8c1ccf541dd692eef281356bb74e"
mainRoom: "viajes"