App service in Matrix

This commit is contained in:
2021-03-30 01:24:08 +02:00
parent 6742ffb959
commit e7546c41e6
8 changed files with 27 additions and 26 deletions

View File

@@ -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<Travel> = 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<Travel> = emptyList()
)

View File

@@ -38,6 +38,5 @@ class CorsGlobalConfiguration : WebFluxConfigurer {
corsRegistry.addMapping("/**")
.allowedOrigins("http://localhost:4200")
.allowedMethods("*")
// .maxAge(3600)
}
}

View File

@@ -65,7 +65,6 @@ class PrivateTravelRestController(
@RequestMapping("/listusertravels")
suspend fun listUserTravels(principal: Principal): ApiResponse<ListDto<TravelDto>> {
logger.debug { "principal=$principal" }
return response {
val userId = authService.currentUser(principal).id
val useCase = ListUserTravels(travelRepository)

View File

@@ -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()

View File

@@ -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<User>, 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)
}
}

View File

@@ -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(' ', '_')
}
}

View File

@@ -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"