mirror of https://github.com/usbharu/Hideout.git
feat: ドメインイベントを受け取れるように
This commit is contained in:
parent
593029e2c3
commit
bf2d7986cc
|
@ -0,0 +1,10 @@
|
|||
package dev.usbharu.hideout.core.application.domainevent.subscribers
|
||||
|
||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||
|
||||
interface DomainEventSubscriber {
|
||||
fun <T : DomainEventBody> subscribe(eventName: String, domainEventConsumer: DomainEventConsumer<T>)
|
||||
}
|
||||
|
||||
typealias DomainEventConsumer<T> = (DomainEvent<T>) -> Unit
|
|
@ -0,0 +1,17 @@
|
|||
package dev.usbharu.hideout.core.application.domainevent.subscribers
|
||||
|
||||
import dev.usbharu.hideout.core.domain.event.post.PostEvent
|
||||
import dev.usbharu.hideout.core.domain.event.post.PostEventBody
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
class TimelinePostCreateSubscriber(domainEventSubscriber: DomainEventSubscriber) {
|
||||
init {
|
||||
domainEventSubscriber.subscribe<PostEventBody>(PostEvent.CREATE.eventName) {
|
||||
val post = it.body.getPost()
|
||||
val actor = it.body.getActor()
|
||||
|
||||
println(post.toString())
|
||||
}
|
||||
}
|
||||
}
|
|
@ -41,9 +41,9 @@ class RegisterLocalPostApplicationService(
|
|||
|
||||
override suspend fun internalExecute(command: RegisterLocalPost, executor: CommandExecutor): Long {
|
||||
val actorId = (
|
||||
userDetailRepository.findById(command.userDetailId)
|
||||
?: throw IllegalStateException("actor not found")
|
||||
).actorId
|
||||
userDetailRepository.findById(command.userDetailId)
|
||||
?: throw IllegalStateException("actor not found")
|
||||
).actorId
|
||||
|
||||
val actor = actorRepository.findById(actorId)!!
|
||||
|
||||
|
|
|
@ -48,14 +48,14 @@ class GetRelationshipApplicationService(
|
|||
val targetId = ActorId(command.targetActorId)
|
||||
val target = actorRepository.findById(targetId)!!
|
||||
val relationship = (
|
||||
relationshipRepository.findByActorIdAndTargetId(actor.id, targetId)
|
||||
?: dev.usbharu.hideout.core.domain.model.relationship.Relationship.default(actor.id, targetId)
|
||||
)
|
||||
relationshipRepository.findByActorIdAndTargetId(actor.id, targetId)
|
||||
?: dev.usbharu.hideout.core.domain.model.relationship.Relationship.default(actor.id, targetId)
|
||||
)
|
||||
|
||||
val relationship1 = (
|
||||
relationshipRepository.findByActorIdAndTargetId(targetId, actor.id)
|
||||
?: dev.usbharu.hideout.core.domain.model.relationship.Relationship.default(targetId, actor.id)
|
||||
)
|
||||
relationshipRepository.findByActorIdAndTargetId(targetId, actor.id)
|
||||
?: dev.usbharu.hideout.core.domain.model.relationship.Relationship.default(targetId, actor.id)
|
||||
)
|
||||
|
||||
val actorInstanceRelationship =
|
||||
actorInstanceRelationshipRepository.findByActorIdAndInstanceId(actor.id, target.instance)
|
||||
|
|
|
@ -21,7 +21,7 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
|||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||
|
||||
class ActorDomainEventFactory(private val actor: Actor) {
|
||||
fun createEvent(actorEvent: ActorEvent): DomainEvent {
|
||||
fun createEvent(actorEvent: ActorEvent): DomainEvent<ActorEventBody> {
|
||||
return DomainEvent.create(
|
||||
actorEvent.eventName,
|
||||
ActorEventBody(actor),
|
||||
|
|
|
@ -21,7 +21,7 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
|||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||
|
||||
class ActorInstanceRelationshipDomainEventFactory(private val actorInstanceRelationship: ActorInstanceRelationship) {
|
||||
fun createEvent(actorInstanceRelationshipEvent: ActorInstanceRelationshipEvent): DomainEvent {
|
||||
fun createEvent(actorInstanceRelationshipEvent: ActorInstanceRelationshipEvent): DomainEvent<ActorInstanceRelationshipEventBody> {
|
||||
return DomainEvent.create(
|
||||
actorInstanceRelationshipEvent.eventName,
|
||||
ActorInstanceRelationshipEventBody(actorInstanceRelationship)
|
||||
|
|
|
@ -21,7 +21,7 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
|||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||
|
||||
class InstanceEventFactory(private val instance: Instance) {
|
||||
fun createEvent(event: InstanceEvent): DomainEvent {
|
||||
fun createEvent(event: InstanceEvent): DomainEvent<InstanceEventBody> {
|
||||
return DomainEvent.create(
|
||||
event.eventName,
|
||||
InstanceEventBody(instance)
|
||||
|
|
|
@ -22,7 +22,7 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
|||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||
|
||||
class PostDomainEventFactory(private val post: Post, private val actor: Actor? = null) {
|
||||
fun createEvent(postEvent: PostEvent): DomainEvent {
|
||||
fun createEvent(postEvent: PostEvent): DomainEvent<PostEventBody> {
|
||||
return DomainEvent.create(
|
||||
postEvent.eventName,
|
||||
PostEventBody(post, actor)
|
||||
|
@ -30,7 +30,10 @@ class PostDomainEventFactory(private val post: Post, private val actor: Actor? =
|
|||
}
|
||||
}
|
||||
|
||||
class PostEventBody(post: Post, actor: Actor?) : DomainEventBody(mapOf("post" to post, "actor" to actor))
|
||||
class PostEventBody(post: Post, actor: Actor?) : DomainEventBody(mapOf("post" to post, "actor" to actor)) {
|
||||
fun getPost(): Post = toMap()["post"] as Post
|
||||
fun getActor(): Actor? = toMap()["actor"] as Actor?
|
||||
}
|
||||
|
||||
enum class PostEvent(val eventName: String) {
|
||||
DELETE("PostDelete"),
|
||||
|
|
|
@ -21,7 +21,7 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
|||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||
|
||||
class RelationshipEventFactory(private val relationship: Relationship) {
|
||||
fun createEvent(relationshipEvent: RelationshipEvent): DomainEvent =
|
||||
fun createEvent(relationshipEvent: RelationshipEvent): DomainEvent<RelationshipEventBody> =
|
||||
DomainEvent.create(relationshipEvent.eventName, RelationshipEventBody(relationship))
|
||||
}
|
||||
|
||||
|
|
|
@ -25,12 +25,12 @@ value class ActorPrivateKey(val privateKey: String) {
|
|||
fun create(privateKey: PrivateKey): ActorPrivateKey {
|
||||
return ActorPrivateKey(
|
||||
"-----BEGIN PRIVATE KEY-----\n" +
|
||||
Base64
|
||||
.getEncoder()
|
||||
.encodeToString(privateKey.encoded)
|
||||
.chunked(64)
|
||||
.joinToString("\n") +
|
||||
"\n-----END PRIVATE KEY-----"
|
||||
Base64
|
||||
.getEncoder()
|
||||
.encodeToString(privateKey.encoded)
|
||||
.chunked(64)
|
||||
.joinToString("\n") +
|
||||
"\n-----END PRIVATE KEY-----"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,12 +25,12 @@ value class ActorPublicKey(val publicKey: String) {
|
|||
fun create(publicKey: PublicKey): ActorPublicKey {
|
||||
return ActorPublicKey(
|
||||
"-----BEGIN PUBLIC KEY-----\n" +
|
||||
Base64
|
||||
.getEncoder()
|
||||
.encodeToString(publicKey.encoded)
|
||||
.chunked(64)
|
||||
.joinToString("\n") +
|
||||
"\n-----END PUBLIC KEY-----"
|
||||
Base64
|
||||
.getEncoder()
|
||||
.encodeToString(publicKey.encoded)
|
||||
.chunked(64)
|
||||
.joinToString("\n") +
|
||||
"\n-----END PUBLIC KEY-----"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,5 +20,5 @@ enum class Role {
|
|||
LOCAL,
|
||||
MODERATOR,
|
||||
ADMINISTRATOR,
|
||||
REMOTE;
|
||||
REMOTE
|
||||
}
|
||||
|
|
|
@ -89,12 +89,12 @@ class ActorInstanceRelationship(
|
|||
|
||||
override fun toString(): String {
|
||||
return "ActorInstanceRelationship(" +
|
||||
"actorId=$actorId, " +
|
||||
"instanceId=$instanceId, " +
|
||||
"blocking=$blocking, " +
|
||||
"muting=$muting, " +
|
||||
"doNotSendPrivate=$doNotSendPrivate" +
|
||||
")"
|
||||
"actorId=$actorId, " +
|
||||
"instanceId=$instanceId, " +
|
||||
"blocking=$blocking, " +
|
||||
"muting=$muting, " +
|
||||
"doNotSendPrivate=$doNotSendPrivate" +
|
||||
")"
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
|
|
@ -25,4 +25,4 @@ value class ApplicationName(val name: String) {
|
|||
companion object {
|
||||
const val LENGTH = 300
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package dev.usbharu.hideout.core.domain.model.filter
|
||||
|
||||
|
||||
class FilterName(name: String) {
|
||||
|
||||
|
||||
val name = name.take(LENGTH)
|
||||
|
||||
companion object {
|
||||
|
|
|
@ -37,15 +37,15 @@ class Media(
|
|||
}
|
||||
override fun toString(): String {
|
||||
return "Media(" +
|
||||
"id=$id, " +
|
||||
"name=$name, " +
|
||||
"url=$url, " +
|
||||
"remoteUrl=$remoteUrl, " +
|
||||
"thumbnailUrl=$thumbnailUrl, " +
|
||||
"type=$type, " +
|
||||
"mimeType=$mimeType, " +
|
||||
"blurHash=$blurHash, " +
|
||||
"description=$description" +
|
||||
")"
|
||||
"id=$id, " +
|
||||
"name=$name, " +
|
||||
"url=$url, " +
|
||||
"remoteUrl=$remoteUrl, " +
|
||||
"thumbnailUrl=$thumbnailUrl, " +
|
||||
"type=$type, " +
|
||||
"mimeType=$mimeType, " +
|
||||
"blurHash=$blurHash, " +
|
||||
"description=$description" +
|
||||
")"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,23 +28,23 @@ import java.util.*
|
|||
* @property body ドメインイベントのボディ
|
||||
* @property collectable trueで同じドメインイベント名でをまとめる
|
||||
*/
|
||||
data class DomainEvent(
|
||||
data class DomainEvent<out T : DomainEventBody>(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val occurredOn: Instant,
|
||||
val body: DomainEventBody,
|
||||
val body: T,
|
||||
val collectable: Boolean = false
|
||||
) {
|
||||
companion object {
|
||||
fun create(name: String, body: DomainEventBody, collectable: Boolean = false): DomainEvent =
|
||||
DomainEvent(UUID.randomUUID().toString(), name, Instant.now(), body, collectable)
|
||||
fun <T : DomainEventBody> create(name: String, body: T, collectable: Boolean = false): DomainEvent<T> =
|
||||
DomainEvent<T>(UUID.randomUUID().toString(), name, Instant.now(), body, collectable)
|
||||
|
||||
fun reconstruct(
|
||||
fun <T : DomainEventBody> reconstruct(
|
||||
id: String,
|
||||
name: String,
|
||||
occurredOn: Instant,
|
||||
body: DomainEventBody,
|
||||
body: T,
|
||||
collectable: Boolean
|
||||
): DomainEvent = DomainEvent(id, name, occurredOn, body, collectable)
|
||||
): DomainEvent<T> = DomainEvent(id, name, occurredOn, body, collectable)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,6 @@
|
|||
package dev.usbharu.hideout.core.domain.shared.domainevent
|
||||
|
||||
@Suppress("UnnecessaryAbstractClass")
|
||||
abstract class DomainEventBody(val map: Map<String, Any?>) {
|
||||
abstract class DomainEventBody(private val map: Map<String, Any?>) {
|
||||
fun toMap(): Map<String, Any?> = map
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package dev.usbharu.hideout.core.domain.shared.domainevent
|
||||
|
||||
interface DomainEventPublisher {
|
||||
suspend fun publishEvent(domainEvent: DomainEvent)
|
||||
suspend fun publishEvent(domainEvent: DomainEvent<*>)
|
||||
}
|
||||
|
|
|
@ -18,13 +18,13 @@ package dev.usbharu.hideout.core.domain.shared.domainevent
|
|||
|
||||
@Suppress("UnnecessaryAbstractClass")
|
||||
abstract class DomainEventStorable {
|
||||
private val domainEvents: MutableList<DomainEvent> = mutableListOf()
|
||||
private val domainEvents: MutableList<DomainEvent<*>> = mutableListOf()
|
||||
|
||||
protected fun addDomainEvent(domainEvent: DomainEvent) {
|
||||
protected fun addDomainEvent(domainEvent: DomainEvent<*>) {
|
||||
domainEvents.add(domainEvent)
|
||||
}
|
||||
|
||||
fun clearDomainEvents() = domainEvents.clear()
|
||||
|
||||
fun getDomainEvents(): List<DomainEvent> = domainEvents.toList()
|
||||
fun getDomainEvents(): List<DomainEvent<*>> = domainEvents.toList()
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package dev.usbharu.hideout.core.domain.shared.domainevent
|
||||
|
||||
interface DomainEventSubscriber {
|
||||
fun subscribe(eventName: String, domainEventConsumer: DomainEventConsumer)
|
||||
}
|
||||
|
||||
typealias DomainEventConsumer = (DomainEvent) -> Unit
|
|
@ -54,7 +54,7 @@ class ExposedActorInstanceRelationshipRepository(override val domainEventPublish
|
|||
query {
|
||||
ActorInstanceRelationships.deleteWhere {
|
||||
actorId eq actorInstanceRelationship.actorId.id and
|
||||
(instanceId eq actorInstanceRelationship.instanceId.instanceId)
|
||||
(instanceId eq actorInstanceRelationship.instanceId.instanceId)
|
||||
}
|
||||
}
|
||||
update(actorInstanceRelationship)
|
||||
|
@ -68,7 +68,7 @@ class ExposedActorInstanceRelationshipRepository(override val domainEventPublish
|
|||
.selectAll()
|
||||
.where {
|
||||
ActorInstanceRelationships.actorId eq actorId.id and
|
||||
(ActorInstanceRelationships.instanceId eq instanceId.instanceId)
|
||||
(ActorInstanceRelationships.instanceId eq instanceId.instanceId)
|
||||
}
|
||||
.singleOrNull()
|
||||
?.toActorInstanceRelationship()
|
||||
|
|
|
@ -24,7 +24,7 @@ import org.springframework.stereotype.Component
|
|||
@Component
|
||||
class SpringFrameworkDomainEventPublisher(private val applicationEventPublisher: ApplicationEventPublisher) :
|
||||
DomainEventPublisher {
|
||||
override suspend fun publishEvent(domainEvent: DomainEvent) {
|
||||
override suspend fun publishEvent(domainEvent: DomainEvent<*>) {
|
||||
applicationEventPublisher.publishEvent(domainEvent)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package dev.usbharu.hideout.core.infrastructure.springframework.domainevent
|
||||
|
||||
import dev.usbharu.hideout.core.application.domainevent.subscribers.DomainEventConsumer
|
||||
import dev.usbharu.hideout.core.application.domainevent.subscribers.DomainEventSubscriber
|
||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||
import org.springframework.context.event.EventListener
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
class SpringFrameworkDomainEventSubscriber : DomainEventSubscriber {
|
||||
|
||||
val map = mutableMapOf<String, MutableList<DomainEventConsumer<*>>>()
|
||||
|
||||
override fun <T : DomainEventBody> subscribe(eventName: String, domainEventConsumer: DomainEventConsumer<T>) {
|
||||
map.getOrPut(eventName) { mutableListOf() }.add(domainEventConsumer as DomainEventConsumer<*>)
|
||||
}
|
||||
|
||||
@EventListener
|
||||
fun onDomainEventPublished(domainEvent: DomainEvent<*>) {
|
||||
map[domainEvent.name]?.forEach {
|
||||
try {
|
||||
it.invoke(domainEvent)
|
||||
}
|
||||
catch (e: Exception) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -70,11 +70,11 @@ class HideoutUserDetails(
|
|||
|
||||
override fun toString(): String {
|
||||
return "HideoutUserDetails(" +
|
||||
"password='$password', " +
|
||||
"username='$username', " +
|
||||
"userDetailsId=$userDetailsId, " +
|
||||
"authorities=$authorities" +
|
||||
")"
|
||||
"password='$password', " +
|
||||
"username='$username', " +
|
||||
"userDetailsId=$userDetailsId, " +
|
||||
"authorities=$authorities" +
|
||||
")"
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
|
Loading…
Reference in New Issue