Deleted Spring Boot, migrated to Trixnity

This commit is contained in:
2022-02-08 19:33:01 +01:00
parent eb27af44e7
commit a9a854cba0
14 changed files with 280 additions and 360 deletions

View File

@@ -0,0 +1,112 @@
package eu.fosil
import io.ktor.http.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.folivo.trixnity.appservice.rest.DefaultAppserviceService
import net.folivo.trixnity.appservice.rest.MatrixAppserviceProperties
import net.folivo.trixnity.appservice.rest.event.AppserviceEventTnxService
import net.folivo.trixnity.appservice.rest.matrixAppserviceModule
import net.folivo.trixnity.appservice.rest.room.AppserviceRoomService
import net.folivo.trixnity.appservice.rest.room.CreateRoomParameter
import net.folivo.trixnity.appservice.rest.user.AppserviceUserService
import net.folivo.trixnity.appservice.rest.user.RegisterUserParameter
import net.folivo.trixnity.client.api.MatrixApiClient
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
val matrixClient = MatrixApiClient(
baseUrl = Url("http://okupamicoche-synapse:8008/"),
).apply { accessToken.value = "syt_dGVzdDM_OIheLExYmDBsfAeENWfF_3BwxFM" }
//).apply { accessToken.value = "30c05ae90a248a4188e620216fa72e349803310ec83e2a77b34fe90be6081f46" } // TODO Error 500
suspend fun main() {
coroutineScope {
matrixClient.sync.subscribe<RoomMessageEventContent.TextMessageEventContent> { println(it.content.body) }
matrixClient.sync.subscribeAllEvents { // this is a shortcut for .subscribe<EventContent> { }
// println(it)
}
launch {
matrixClient.sync.start(wait = true, scope = this) // you need to start the sync to receive messages
}
val appserviceService = DefaultAppserviceService(
EventTnxService(),
UserService(),
RoomService()
)
println(3)
launch {
embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
matrixAppserviceModule(
properties = MatrixAppserviceProperties("312df522183efd404ec1cd22d2ffa4bbc76a8c1ccf541dd692eef281356bb74e"),
appserviceService = appserviceService
)
}.start(wait = true)
}
println(4)
}
}
class EventTnxService : AppserviceEventTnxService {
override suspend fun eventTnxProcessingState(tnxId: String): AppserviceEventTnxService.EventTnxProcessingState {
println("eventTnxProcessingState")
return AppserviceEventTnxService.EventTnxProcessingState.NOT_PROCESSED
// return AppserviceEventTnxService.EventTnxProcessingState.PROCESSED
}
override suspend fun onEventTnxProcessed(tnxId: String) {
println("onEventTnxProcessed")
}
}
class UserService : AppserviceUserService {
override val matrixApiClient: MatrixApiClient
get() = matrixClient
override suspend fun getRegisterUserParameter(userId: UserId): RegisterUserParameter {
println("getRegisterUserParameter")
return RegisterUserParameter("user parameter")
}
override suspend fun onRegisteredUser(userId: UserId) {
println("onRegisteredUser")
}
override suspend fun userExistingState(userId: UserId): AppserviceUserService.UserExistingState {
println("userExistingState")
return AppserviceUserService.UserExistingState.CAN_BE_CREATED
}
}
class RoomService : AppserviceRoomService {
override val matrixApiClient: MatrixApiClient
get() = matrixClient
override suspend fun getCreateRoomParameter(roomAlias: RoomAliasId): CreateRoomParameter {
println("getCreateRoomParameter")
return CreateRoomParameter()
}
override suspend fun onCreatedRoom(roomAlias: RoomAliasId, roomId: RoomId) {
println("onCreatedRoom")
}
override suspend fun roomExistingState(roomAlias: RoomAliasId): AppserviceRoomService.RoomExistingState {
println("roomExistingState")
return AppserviceRoomService.RoomExistingState.DOES_NOT_EXISTS
}
}

View File

@@ -1,11 +0,0 @@
package eu.fosil
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class OkupaMiCocheAppServiceApplication
fun main(args: Array<String>) {
runApplication<OkupaMiCocheAppServiceApplication>(*args)
}

View File

@@ -1,79 +0,0 @@
//package eu.fosil
//
//import kotlinx.coroutines.flow.collect
//import kotlinx.coroutines.flow.filter
//import net.folivo.spring.matrix.bot.event.MatrixMessageHandler
//import net.folivo.spring.matrix.bot.event.MessageContext
//import net.folivo.spring.matrix.bot.user.MatrixUserService
//import net.folivo.spring.matrix.bot.util.BotServiceHelper
//import net.folivo.trixnity.client.rest.MatrixClient
//import net.folivo.trixnity.core.model.events.m.room.MessageEventContent
//import org.slf4j.LoggerFactory
//import org.springframework.stereotype.Component
//
//@Component
//class PingHandler(
// private val matrixClient: MatrixClient,
// private val helper: BotServiceHelper,
// private val userService: MatrixUserService
//) : MatrixMessageHandler {
// companion object {
// private val LOG = LoggerFactory.getLogger(this::class.java)
// }
//
// override suspend fun handleMessage(content: MessageEventContent, context: MessageContext) {
// println("EOEO")
// LOG.debug(content.body)
// LOG.debug(context.roomId.full)
// if (content is MessageEventContent.TextMessageEventContent) {
// if (content.body.contains("ping")) {
// userService.getUsersByRoom(context.roomId)
// .filter { it.isManaged }
// .collect { member ->
// val messageId = context.answer("pong", asUserId = member.id)
// LOG.info("pong (messageid: $messageId)")
// }
// }
// }
// }
//}
package eu.fosil
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import net.folivo.matrix.bot.event.MatrixMessageHandler
import net.folivo.matrix.bot.event.MessageContext
import net.folivo.matrix.bot.user.MatrixUserService
import net.folivo.matrix.bot.util.BotServiceHelper
import net.folivo.matrix.core.model.events.m.room.message.MessageEvent
import net.folivo.matrix.restclient.MatrixClient
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
@Component
class PingHandler(
private val matrixClient: MatrixClient,
private val helper: BotServiceHelper,
private val userService: MatrixUserService
) : MatrixMessageHandler {
companion object {
private val LOG = LoggerFactory.getLogger(this::class.java)
}
override suspend fun handleMessage(content: MessageEvent.MessageEventContent, context: MessageContext) {
println("handle message content=${content.body}")
userService.getUsersByRoom(context.roomId).collect { member ->
println(member.id)
println(member.isManaged)
}
if (content.body.contains("ping")) {
userService.getUsersByRoom(context.roomId).filter { it.isManaged }
.collect { member ->
val messageId = context.answer("pong", asUserId = member.id)
LOG.info("pong (messageid: $messageId)")
}
}
}
}

View File

@@ -1,70 +0,0 @@
#server:
# port: 8080
# port: 8081
#spring:
# datasource:
# driver-class-name: org.h2.Driver
# url: jdbc:h2:file:./okupamicoche;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
# jpa:
# database-platform: org.hibernate.dialect.H2Dialect
# hibernate:
# ddl-auto: update
# jackson:
# serialization:
# write-dates-as-timestamps: false
logging:
level:
org:
springframework: INFO
eu.fosil.okupamicoche: DEBUG
matrix:
bot:
# The domain-part of matrix-ids.E.g. example.org when your userIds look like @unicorn:example.org
serverName: okupamicoche-synapse
# The localpart (username) of the user associated with the application service
# or just the username of your bot.
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: 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.
# Default is RESTRICTED, which means, that only automatic joins to serverName are allowed.
autoJoin: ENABLED
# (optional) Configure if ALL membership changes should be tracked/saved with help of MatrixAppserviceRoomService
# or only membership changes of users, which are MANAGED by the bridge. Default is ALL (no tracking/saving).
trackMembership: ALL
# Connection setting to the database for migration purpose only (only jdbc drivers ar supported)
migration:
url: jdbc:h2:file:./matrix
username: sa
password:
# Connection settings to the database (only r2dbc drivers are supported)
database:
url: r2dbc:h2:file:///./matrix
username: sa
password:
client:
homeServer:
# The hostname of your Homeserver.
hostname: okupamicoche-synapse
# (optional) The port of your Homeserver. Default is 443.
port: 8008
# (optional) Use http or https. Default is true (so uses https).
secure: false
# The token to authenticate against the Homeserver.
token: "30c05ae90a248a4188e620216fa72e349803310ec83e2a77b34fe90be6081f46"
appservice:
# A unique token for Homeservers to use to authenticate requests to application services.
hsToken: "312df522183efd404ec1cd22d2ffa4bbc76a8c1ccf541dd692eef281356bb74e"
# A list of users, aliases and rooms namespaces that the application service controls.
namespaces:
users: [ ]
aliases:
- localpartRegex: "viaje_.*"
rooms: [ ]

View File

@@ -0,0 +1,12 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
<logger name="org.eclipse.jetty" level="INFO"/>
<logger name="io.netty" level="INFO"/>
</configuration>