diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/Posts.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/Posts.kt index 69d202a9..4dfe0e59 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/Posts.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/Posts.kt @@ -28,6 +28,7 @@ data class Post( data class PostEntity( val id: Long, + val userId:Long, val overview: String? = null, val text: String, val createdAt: Long, @@ -40,6 +41,7 @@ data class PostEntity( fun ResultRow.toPost():PostEntity{ return PostEntity( id = this[Posts.id], + userId = this[Posts.userId], overview = this[Posts.overview], text = this[Posts.text], createdAt = this[Posts.createdAt], diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/ap/Note.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/ap/Note.kt new file mode 100644 index 00000000..caced1da --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/ap/Note.kt @@ -0,0 +1,53 @@ +package dev.usbharu.hideout.domain.model.ap + +open class Note : Object { + var id:String? = null + var attributedTo:String? = null + var content:String? = null + var published:String? = null + var to:List = emptyList() + protected constructor() : super() + constructor( + type: List = emptyList(), + name: String, + id: String?, + attributedTo: String?, + content: String?, + published: String?, + to: List = emptyList() + ) : super(add(type,"Note"), name) { + this.id = id + this.attributedTo = attributedTo + this.content = content + this.published = published + this.to = to + } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is Note) return false + if (!super.equals(other)) return false + + if (id != other.id) return false + if (attributedTo != other.attributedTo) return false + if (content != other.content) return false + if (published != other.published) return false + return to == other.to + } + + override fun hashCode(): Int { + var result = super.hashCode() + result = 31 * result + (id?.hashCode() ?: 0) + result = 31 * result + (attributedTo?.hashCode() ?: 0) + result = 31 * result + (content?.hashCode() ?: 0) + result = 31 * result + (published?.hashCode() ?: 0) + result = 31 * result + to.hashCode() + return result + } + + override fun toString(): String { + return "Note(id=$id, attributedTo=$attributedTo, content=$content, published=$published, to=$to) ${super.toString()}" + } + + +} diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/job/HideoutJob.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/job/HideoutJob.kt index c499807c..6bce8a95 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/job/HideoutJob.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/job/HideoutJob.kt @@ -9,3 +9,9 @@ object ReceiveFollowJob : HideoutJob("ReceiveFollowJob"){ val follow = string("follow") val targetActor = string("targetActor") } + +object DeliverPostJob : HideoutJob("DeliverPostJob"){ + val post = string("post") + val actor = string("actor") + val inbox = string("inbox") +} diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/IUserKeyRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/IUserKeyRepository.kt deleted file mode 100644 index d72bac45..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/repository/IUserKeyRepository.kt +++ /dev/null @@ -1,3 +0,0 @@ -package dev.usbharu.hideout.repository - -interface IUserKeyRepository diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/UserKeyRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/UserKeyRepository.kt deleted file mode 100644 index b8a8de36..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/repository/UserKeyRepository.kt +++ /dev/null @@ -1,4 +0,0 @@ -package dev.usbharu.hideout.repository - -class UserKeyRepository { -} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/IPostService.kt b/src/main/kotlin/dev/usbharu/hideout/service/IPostService.kt new file mode 100644 index 00000000..52e3ba05 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/IPostService.kt @@ -0,0 +1,7 @@ +package dev.usbharu.hideout.service + +import dev.usbharu.hideout.domain.model.Post + +interface IPostService { + suspend fun create(post:Post) +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteService.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteService.kt new file mode 100644 index 00000000..29efd890 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteService.kt @@ -0,0 +1,12 @@ +package dev.usbharu.hideout.service.activitypub + +import dev.usbharu.hideout.domain.model.PostEntity +import dev.usbharu.hideout.domain.model.ap.Note +import dev.usbharu.hideout.domain.model.job.DeliverPostJob +import kjob.core.job.JobProps + +interface ActivityPubNoteService { + + suspend fun createNote(post:PostEntity) + suspend fun createNoteJob(props:JobProps) +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt new file mode 100644 index 00000000..e9a9f1d9 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt @@ -0,0 +1,44 @@ +package dev.usbharu.hideout.service.activitypub + +import com.fasterxml.jackson.module.kotlin.readValue +import dev.usbharu.hideout.config.Config +import dev.usbharu.hideout.domain.model.PostEntity +import dev.usbharu.hideout.domain.model.ap.Note +import dev.usbharu.hideout.domain.model.job.DeliverPostJob +import dev.usbharu.hideout.plugins.postAp +import dev.usbharu.hideout.service.impl.UserService +import dev.usbharu.hideout.service.job.JobQueueParentService +import io.ktor.client.* +import kjob.core.job.JobProps + +class ActivityPubNoteServiceImpl( + private val httpClient: HttpClient, + private val jobQueueParentService: JobQueueParentService, + private val userService: UserService +) : ActivityPubNoteService { + + override suspend fun createNote(post: PostEntity) { + val followers = userService.findFollowersById(post.userId) + val userEntity = userService.findById(post.userId) + val note = Config.configData.objectMapper.writeValueAsString(post) + followers.forEach { followerEntity -> + jobQueueParentService.schedule(DeliverPostJob) { + props[it.actor] = userEntity.url + props[it.post] = note + props[it.inbox] = followerEntity.inbox + } + } + } + + + override suspend fun createNoteJob(props: JobProps) { + val actor = props[DeliverPostJob.actor] + val note = Config.configData.objectMapper.readValue(props[DeliverPostJob.post]) + val inbox = props[DeliverPostJob.inbox] + httpClient.postAp( + urlString = inbox, + username = "$actor#pubkey", + jsonLd = note + ) + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt index c6424167..7fffacb8 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt @@ -1,9 +1,10 @@ package dev.usbharu.hideout.service.activitypub import com.fasterxml.jackson.databind.JsonNode -import dev.usbharu.hideout.domain.model.ap.Follow import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.domain.model.ActivityPubResponse +import dev.usbharu.hideout.domain.model.ap.Follow +import dev.usbharu.hideout.domain.model.job.DeliverPostJob import dev.usbharu.hideout.domain.model.job.HideoutJob import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob import dev.usbharu.hideout.exception.JsonParseException @@ -11,7 +12,10 @@ import kjob.core.dsl.JobContextWithProps import kjob.core.job.JobProps import org.slf4j.LoggerFactory -class ActivityPubServiceImpl(private val activityPubFollowService: ActivityPubFollowService) : ActivityPubService { +class ActivityPubServiceImpl( + private val activityPubFollowService: ActivityPubFollowService, + private val activityPubNoteService: ActivityPubNoteService +) : ActivityPubService { val logger = LoggerFactory.getLogger(this::class.java) override fun parseActivity(json: String): ActivityType { @@ -72,6 +76,7 @@ class ActivityPubServiceImpl(private val activityPubFollowService: ActivityPubFo override suspend fun processActivity(job: JobContextWithProps, hideoutJob: HideoutJob) { when (hideoutJob) { ReceiveFollowJob -> activityPubFollowService.receiveFollowJob(job.props as JobProps) + DeliverPostJob -> activityPubNoteService.createNoteJob(job.props as JobProps) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt new file mode 100644 index 00000000..f5bd2fa7 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt @@ -0,0 +1,13 @@ +package dev.usbharu.hideout.service.impl + +import dev.usbharu.hideout.domain.model.Post +import dev.usbharu.hideout.repository.IPostRepository +import dev.usbharu.hideout.service.IPostService +import dev.usbharu.hideout.service.job.JobQueueParentService + +class PostService(private val postRepository:IPostRepository,private val jobQueueParentService: JobQueueParentService) : IPostService { + override suspend fun create(post: Post) { + postRepository.insert(post) + + } +}