feat: 投稿をinboxに送りつける処理を追加

This commit is contained in:
usbharu 2023-04-21 00:10:17 +09:00
parent 6234315086
commit d91d810e4d
10 changed files with 144 additions and 9 deletions

View File

@ -28,6 +28,7 @@ data class Post(
data class PostEntity( data class PostEntity(
val id: Long, val id: Long,
val userId:Long,
val overview: String? = null, val overview: String? = null,
val text: String, val text: String,
val createdAt: Long, val createdAt: Long,
@ -40,6 +41,7 @@ data class PostEntity(
fun ResultRow.toPost():PostEntity{ fun ResultRow.toPost():PostEntity{
return PostEntity( return PostEntity(
id = this[Posts.id], id = this[Posts.id],
userId = this[Posts.userId],
overview = this[Posts.overview], overview = this[Posts.overview],
text = this[Posts.text], text = this[Posts.text],
createdAt = this[Posts.createdAt], createdAt = this[Posts.createdAt],

View File

@ -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<String> = emptyList()
protected constructor() : super()
constructor(
type: List<String> = emptyList(),
name: String,
id: String?,
attributedTo: String?,
content: String?,
published: String?,
to: List<String> = 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()}"
}
}

View File

@ -9,3 +9,9 @@ object ReceiveFollowJob : HideoutJob("ReceiveFollowJob"){
val follow = string("follow") val follow = string("follow")
val targetActor = string("targetActor") val targetActor = string("targetActor")
} }
object DeliverPostJob : HideoutJob("DeliverPostJob"){
val post = string("post")
val actor = string("actor")
val inbox = string("inbox")
}

View File

@ -1,3 +0,0 @@
package dev.usbharu.hideout.repository
interface IUserKeyRepository

View File

@ -1,4 +0,0 @@
package dev.usbharu.hideout.repository
class UserKeyRepository {
}

View File

@ -0,0 +1,7 @@
package dev.usbharu.hideout.service
import dev.usbharu.hideout.domain.model.Post
interface IPostService {
suspend fun create(post:Post)
}

View File

@ -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<DeliverPostJob>)
}

View File

@ -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<DeliverPostJob>) {
val actor = props[DeliverPostJob.actor]
val note = Config.configData.objectMapper.readValue<Note>(props[DeliverPostJob.post])
val inbox = props[DeliverPostJob.inbox]
httpClient.postAp(
urlString = inbox,
username = "$actor#pubkey",
jsonLd = note
)
}
}

View File

@ -1,9 +1,10 @@
package dev.usbharu.hideout.service.activitypub package dev.usbharu.hideout.service.activitypub
import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.JsonNode
import dev.usbharu.hideout.domain.model.ap.Follow
import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.domain.model.ActivityPubResponse 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.HideoutJob
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
import dev.usbharu.hideout.exception.JsonParseException import dev.usbharu.hideout.exception.JsonParseException
@ -11,7 +12,10 @@ import kjob.core.dsl.JobContextWithProps
import kjob.core.job.JobProps import kjob.core.job.JobProps
import org.slf4j.LoggerFactory 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) val logger = LoggerFactory.getLogger(this::class.java)
override fun parseActivity(json: String): ActivityType { override fun parseActivity(json: String): ActivityType {
@ -72,6 +76,7 @@ class ActivityPubServiceImpl(private val activityPubFollowService: ActivityPubFo
override suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob) { override suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob) {
when (hideoutJob) { when (hideoutJob) {
ReceiveFollowJob -> activityPubFollowService.receiveFollowJob(job.props as JobProps<ReceiveFollowJob>) ReceiveFollowJob -> activityPubFollowService.receiveFollowJob(job.props as JobProps<ReceiveFollowJob>)
DeliverPostJob -> activityPubNoteService.createNoteJob(job.props as JobProps<DeliverPostJob>)
} }
} }

View File

@ -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)
}
}