diff --git a/build.gradle.kts b/build.gradle.kts index b660bab..3372d47 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,10 +28,10 @@ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor") implementation("io.github.microutils:kotlin-logging-jvm:2.0.6") // https://mvnrepository.com/artifact/net.folivo/matrix-spring-boot-bot -// implementation ("net.folivo:matrix-spring-boot-bot:0.4.6") + implementation ("net.folivo:matrix-spring-boot-bot:0.4.6") developmentOnly("org.springframework.boot:spring-boot-devtools") runtimeOnly("com.h2database:h2") -// runtimeOnly("io.r2dbc:r2dbc-h2") + runtimeOnly("io.r2dbc:r2dbc-h2") providedRuntime("org.springframework.boot:spring-boot-starter-tomcat") testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("io.projectreactor:reactor-test") diff --git a/src/main/kotlin/eu/fosil/okupamicoche/entities/User.kt b/src/main/kotlin/eu/fosil/okupamicoche/entities/User.kt index f7552f2..f7e1801 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/entities/User.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/entities/User.kt @@ -11,8 +11,8 @@ class User( var matrixId: String, var name: String, var email: String? = null, - @OneToMany(mappedBy = "driver") + @OneToMany(mappedBy = "driver", fetch = FetchType.EAGER) // https://stackoverflow.com/questions/22821695/how-to-fix-hibernate-lazyinitializationexception-failed-to-lazily-initialize-a var travelsAsDriver: List = emptyList(), - @ManyToMany(mappedBy = "travelers") + @ManyToMany(mappedBy = "travelers", fetch = FetchType.EAGER) // https://stackoverflow.com/questions/22821695/how-to-fix-hibernate-lazyinitializationexception-failed-to-lazily-initialize-a var travelsAsTraveler: List = emptyList() ) \ No newline at end of file diff --git a/src/main/kotlin/eu/fosil/okupamicoche/spring/conf/WebSecurityConfig.kt b/src/main/kotlin/eu/fosil/okupamicoche/spring/conf/WebSecurityConfig.kt index 084e941..6023d8d 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/spring/conf/WebSecurityConfig.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/spring/conf/WebSecurityConfig.kt @@ -38,6 +38,5 @@ class CorsGlobalConfiguration : WebFluxConfigurer { corsRegistry.addMapping("/**") .allowedOrigins("http://localhost:4200") .allowedMethods("*") -// .maxAge(3600) } } diff --git a/src/main/kotlin/eu/fosil/okupamicoche/spring/controller/PrivateTravelRestController.kt b/src/main/kotlin/eu/fosil/okupamicoche/spring/controller/PrivateTravelRestController.kt index 07a62fa..8ec5245 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/spring/controller/PrivateTravelRestController.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/spring/controller/PrivateTravelRestController.kt @@ -65,7 +65,6 @@ class PrivateTravelRestController( @RequestMapping("/listusertravels") suspend fun listUserTravels(principal: Principal): ApiResponse> { - logger.debug { "principal=$principal" } return response { val userId = authService.currentUser(principal).id val useCase = ListUserTravels(travelRepository) diff --git a/src/main/kotlin/eu/fosil/okupamicoche/spring/services/AuthService.kt b/src/main/kotlin/eu/fosil/okupamicoche/spring/services/AuthService.kt index 906d91e..5676c63 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/spring/services/AuthService.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/spring/services/AuthService.kt @@ -5,6 +5,7 @@ import eu.fosil.okupamicoche.entities.UserId import eu.fosil.okupamicoche.entities.UserIdNotFoundException import eu.fosil.okupamicoche.entities.UserKeycloak import eu.fosil.okupamicoche.repositories.TravelRepository +import mu.KotlinLogging import org.springframework.data.repository.findByIdOrNull import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken import org.springframework.stereotype.Service @@ -14,11 +15,14 @@ import java.security.Principal class AuthService( private val travelRepository: TravelRepository ) { + private val logger = KotlinLogging.logger {} + /** * Devuelve el id del usuario actual. */ fun currentUser(principal: Principal): UserKeycloak { if (principal is JwtAuthenticationToken) { + logger.debug { "claims=${principal.token.claims}" } return UserKeycloak(principal.token.claims) } throw UserIdNotFoundException() diff --git a/src/main/kotlin/eu/fosil/okupamicoche/spring/services/MatrixService.kt b/src/main/kotlin/eu/fosil/okupamicoche/spring/services/MatrixService.kt index 51f94e0..b9775d6 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/spring/services/MatrixService.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/spring/services/MatrixService.kt @@ -3,33 +3,32 @@ package eu.fosil.okupamicoche.spring.services import eu.fosil.okupamicoche.entities.User import eu.fosil.okupamicoche.usecases.matrix.MatrixApi import mu.KotlinLogging +import net.folivo.matrix.core.model.MatrixId +import net.folivo.matrix.restclient.MatrixClient //import net.folivo.matrix.core.model.MatrixId //import net.folivo.matrix.restclient.MatrixClient import org.springframework.stereotype.Service import java.util.stream.Collectors @Service -class MatrixService( -// private val matrixClient: MatrixClient - ): MatrixApi { +class MatrixService(private val matrixClient: MatrixClient) : MatrixApi { private val logger = KotlinLogging.logger {} override suspend fun createRoom(name: String, alias: String, usersToInvite: Set, topic: String?): String { - logger.debug { "Creating room name=$name alias=$alias" } -// val usersToInviteId = usersToInvite.stream().map { user -> MatrixId.UserId(user.matrixId) } -// val roomId = matrixClient.roomsApi.createRoom( -// name = name, -// roomAliasId = MatrixId.RoomAliasId("#$alias:synapse"), -//// invite = usersToInviteId.collect(Collectors.toSet()), -// topic = topic -// ) -// return roomId.full - return "-1" + logger.debug { "Creating room name=$name alias=$alias full=${MatrixId.RoomAliasId("#$alias:synapse")}" } + val usersToInviteId = usersToInvite.stream().map { user -> MatrixId.UserId(user.matrixId) } + val roomId = matrixClient.roomsApi.createRoom( + name = name, + roomAliasId = MatrixId.RoomAliasId("#$alias:synapse"), + invite = usersToInviteId.collect(Collectors.toSet()), + topic = topic + ) + return roomId.full } override suspend fun inviteUser(roomId: String, user: User) { -// val matrixUserId = MatrixId.UserId(user.matrixId) -// logger.debug { "Invite user $matrixUserId to room $roomId" } -// matrixClient.roomsApi.inviteUser(MatrixId.RoomId(roomId), matrixUserId) + val matrixUserId = MatrixId.UserId(user.matrixId) + logger.debug { "Invite user $matrixUserId to room $roomId" } + matrixClient.roomsApi.inviteUser(MatrixId.RoomId(roomId), matrixUserId) } } \ No newline at end of file diff --git a/src/main/kotlin/eu/fosil/okupamicoche/usecases/matrix/CreateRoomForTravel.kt b/src/main/kotlin/eu/fosil/okupamicoche/usecases/matrix/CreateRoomForTravel.kt index 801116d..878024f 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/usecases/matrix/CreateRoomForTravel.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/usecases/matrix/CreateRoomForTravel.kt @@ -17,7 +17,7 @@ class CreateRoomForTravel(private val matrixApi: MatrixApi) { } private fun createRoomAlias(travel: Travel): String { - val alias = "$ROOM_ALIAS_PREFIX-${travel.origin}-${travel.destination}" + val alias = "${ROOM_ALIAS_PREFIX}_${travel.origin}-${travel.destination}" return alias.decapitalize().replace(' ', '_') } } \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f4d2d17..fa412f9 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -25,11 +25,11 @@ matrix: serverName: synapse # The localpart (username) of the user associated with the application service # or just the username of your bot. - username: test + username: okupamicoche # (optional) Display name for the bot user. displayname: Okupa mi coche # (optional) The mode you want to use to create a bot. Default is CLIENT. The other is APPSERVICE. - mode: CLIENT + mode: APPSERVICE # (optional) Configure how users managed by your bot do automatically join rooms. # ENABLED allows automatic joins to every invited room. # DISABLED disables this feature. @@ -57,7 +57,7 @@ matrix: # (optional) Use http or https. Default is true (so uses https). secure: false # The token to authenticate against the Homeserver. - token: "MDAxNWxvY2F0aW9uIHN5bmFwc2UKMDAxM2lkZW50aWZpZXIga2V5CjAwMTBjaWQgZ2VuID0gMQowMDIwY2lkIHVzZXJfaWQgPSBAdGVzdDpzeW5hcHNlCjAwMTZjaWQgdHlwZSA9IGFjY2VzcwowMDIxY2lkIG5vbmNlID0gYW5sX2M1fjU9eXlFflhyPQowMDJmc2lnbmF0dXJlILRwP6YGc6pNt9IB4Z3X3RCHLY2d5Qc8vCK8ubWqSfHBCg" + token: "30c05ae90a248a4188e620216fa72e349803310ec83e2a77b34fe90be6081f46" appservice: # A unique token for Homeservers to use to authenticate requests to application services. hsToken: "312df522183efd404ec1cd22d2ffa4bbc76a8c1ccf541dd692eef281356bb74e"