Create public room
This commit is contained in:
@@ -2,4 +2,5 @@
|
||||
|
||||
Matrix Application Service for Okupa mi coche
|
||||
|
||||
http://172.17.0.1:8080/_matrix/app/v1/users/@test34:okupamicoche-synapse?access_token=312df522183efd404ec1cd22d2ffa4bbc76a8c1ccf541dd692eef281356bb74e
|
||||
## Launch Element with dev profile
|
||||
`element-desktop --profile omc-dev`
|
||||
|
||||
52
src/main/kotlin/eu/fosil/okupamicoche/AppServiceFactory.kt
Normal file
52
src/main/kotlin/eu/fosil/okupamicoche/AppServiceFactory.kt
Normal file
@@ -0,0 +1,52 @@
|
||||
package eu.fosil.okupamicoche
|
||||
|
||||
import eu.fosil.okupamicoche.services.EventTnxService
|
||||
import eu.fosil.okupamicoche.services.RoomService
|
||||
import eu.fosil.okupamicoche.services.UserService
|
||||
import io.ktor.http.*
|
||||
import mu.KotlinLogging
|
||||
import net.folivo.trixnity.appservice.rest.AppserviceService
|
||||
import net.folivo.trixnity.appservice.rest.DefaultAppserviceService
|
||||
import net.folivo.trixnity.client.api.MatrixApiClient
|
||||
import net.folivo.trixnity.core.model.events.Event
|
||||
import net.folivo.trixnity.core.model.events.m.room.CreateEventContent
|
||||
import net.folivo.trixnity.core.model.events.m.room.RoomMessageEventContent
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
||||
val matrixClient = MatrixApiClient(
|
||||
baseUrl = Url("http://okupamicoche-synapse:8008/"),
|
||||
).apply { accessToken.value = "30c05ae90a248a4188e620216fa72e349803310ec83e2a77b34fe90be6081f46" }
|
||||
|
||||
fun createAppService(): AppserviceService {
|
||||
val appserviceService = DefaultAppserviceService(
|
||||
EventTnxService(), UserService(), RoomService()
|
||||
)
|
||||
|
||||
appserviceService.subscribeAllEvents {
|
||||
logger.debug("All events: $it")
|
||||
}
|
||||
appserviceService.subscribe<CreateEventContent> {
|
||||
if (it is Event.RoomEvent) {
|
||||
logger.info("${it.content.creator} created room ${it.roomId}")
|
||||
}
|
||||
}
|
||||
appserviceService.subscribe<RoomMessageEventContent.TextMessageEventContent> {
|
||||
if (it is Event.MessageEvent) {
|
||||
val roomId = it.roomId
|
||||
logger.info("${it.sender} sent \"${it.content.body}\" on $roomId")
|
||||
logger.info("$it")
|
||||
|
||||
if (it.content.body.startsWith("!travel")) {
|
||||
logger.info("send!")
|
||||
matrixClient.rooms.joinRoom(roomId)
|
||||
matrixClient.rooms.sendMessageEvent(
|
||||
roomId,
|
||||
RoomMessageEventContent.TextMessageEventContent("Te creo un viaje?")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return appserviceService
|
||||
}
|
||||
@@ -1,70 +1,36 @@
|
||||
package eu.fosil.okupamicoche
|
||||
|
||||
import eu.fosil.okupamicoche.services.EventTnxService
|
||||
import eu.fosil.okupamicoche.services.RoomService
|
||||
import eu.fosil.okupamicoche.services.UserService
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.netty.*
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import mu.KotlinLogging
|
||||
import net.folivo.trixnity.appservice.rest.DefaultAppserviceService
|
||||
import net.folivo.trixnity.appservice.rest.MatrixAppserviceProperties
|
||||
import net.folivo.trixnity.appservice.rest.matrixAppserviceModule
|
||||
import net.folivo.trixnity.client.api.MatrixApiClient
|
||||
import net.folivo.trixnity.core.model.events.Event
|
||||
import net.folivo.trixnity.core.model.events.MessageEventContent
|
||||
import net.folivo.trixnity.core.model.events.m.room.CreateEventContent
|
||||
import net.folivo.trixnity.core.model.events.m.room.RoomMessageEventContent
|
||||
|
||||
val matrixClient = MatrixApiClient(
|
||||
baseUrl = Url("http://okupamicoche-synapse:8008/"),
|
||||
).apply { accessToken.value = "30c05ae90a248a4188e620216fa72e349803310ec83e2a77b34fe90be6081f46" }
|
||||
import net.folivo.trixnity.client.api.model.rooms.Visibility
|
||||
import net.folivo.trixnity.core.model.RoomAliasId
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
||||
suspend fun main() {
|
||||
coroutineScope {
|
||||
val appserviceService = DefaultAppserviceService(
|
||||
EventTnxService(), UserService(), RoomService()
|
||||
)
|
||||
|
||||
appserviceService.subscribeAllEvents {
|
||||
logger.debug("All events: $it")
|
||||
}
|
||||
|
||||
appserviceService.subscribe<CreateEventContent> {
|
||||
if (it is Event.RoomEvent) {
|
||||
logger.info("${it.content.creator} created room ${it.roomId}")
|
||||
}
|
||||
}
|
||||
|
||||
appserviceService.subscribe<RoomMessageEventContent.TextMessageEventContent> {
|
||||
if (it is Event.MessageEvent) {
|
||||
val roomId = it.roomId
|
||||
logger.info("${it.sender} sent \"${it.content.body}\" on $roomId")
|
||||
|
||||
if (it.content.body.startsWith("!travel")) {
|
||||
logger.info("send!")
|
||||
matrixClient.rooms.joinRoom(roomId)
|
||||
matrixClient.rooms.sendMessageEvent(
|
||||
roomId,
|
||||
RoomMessageEventContent.TextMessageEventContent("Te creo un viaje?")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
launch {
|
||||
|
||||
val roomAliasRes = matrixClient.rooms.getRoomAlias(RoomAliasId("#viajes:okupamicoche-synapse")).getOrNull()
|
||||
if (roomAliasRes == null) {
|
||||
logger.info("Creating #viajes:okupamicoche-synapse public room")
|
||||
matrixClient.rooms.createRoom(
|
||||
visibility = Visibility.PUBLIC,
|
||||
roomAliasId = RoomAliasId("#viajes:okupamicoche-synapse")
|
||||
)
|
||||
}
|
||||
|
||||
embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
|
||||
matrixAppserviceModule(
|
||||
properties = MatrixAppserviceProperties("312df522183efd404ec1cd22d2ffa4bbc76a8c1ccf541dd692eef281356bb74e"),
|
||||
appserviceService = appserviceService
|
||||
appserviceService = createAppService()
|
||||
)
|
||||
}.start(wait = true)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user