Files
dc-project/src/main/kotlin/fr/dcproject/event/EventNotification.kt
Fabrice Lecomte 1418dd95bc Implement Websocket for push Notification
create auth with jwt in query string
2020-02-27 01:38:34 +01:00

57 lines
1.7 KiB
Kotlin

package fr.dcproject.event
import fr.dcproject.entity.Article
import fr.postgresjson.entity.Serializable
import fr.postgresjson.entity.immutable.UuidEntity
import io.ktor.application.*
import io.ktor.util.AttributeKey
import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.DisposableHandle
import org.joda.time.DateTime
open class Notification(
val type: String,
val createdAt: DateTime = DateTime.now()
) : Serializable
open class EntityEvent(
val target: UuidEntity,
type: String,
val action: String
) : Notification(type) {
enum class Type(val event: EventDefinition<ArticleUpdate>) {
UPDATE_ARTICLE(EventDefinition<ArticleUpdate>())
}
}
class ArticleUpdate(
target: Article
) : EntityEvent(target, "article", "update")
/**
* Installation Class
*/
class EventNotification {
class Configuration(private val monitor: ApplicationEvents) {
private val subscribers = mutableListOf<DisposableHandle>()
fun <T: Notification> subscribe(definition: EventDefinition<T>, handler: EventHandler<T>): DisposableHandle {
return monitor.subscribe(definition, handler).also {
subscribers.add(it)
}
}
}
companion object Feature : ApplicationFeature<Application, Configuration, EventNotification> {
override val key = AttributeKey<EventNotification>("EventNotification")
@KtorExperimentalAPI
override fun install(
pipeline: Application,
configure: Configuration.() -> Unit
): EventNotification {
Configuration(pipeline.environment.monitor).apply(configure)
return EventNotification()
}
}
}