feat: リアクション情報の配送の実装を作成

This commit is contained in:
usbharu 2023-07-31 01:07:14 +09:00
parent 88f8b405e3
commit e340d68dc0
8 changed files with 82 additions and 14 deletions

View File

@ -8,6 +8,7 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.config.ConfigData
import dev.usbharu.hideout.domain.model.job.DeliverPostJob
import dev.usbharu.hideout.domain.model.job.DeliverReactionJob
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
import dev.usbharu.hideout.plugins.*
import dev.usbharu.hideout.repository.IUserRepository
@ -135,4 +136,10 @@ fun Application.worker() {
activityPubService.processActivity(this, it)
}
}
kJob.register(DeliverReactionJob) {
execute {
activityPubService.processActivity(this, it)
}
}
}

View File

@ -11,13 +11,13 @@ open class Like : Object {
protected constructor() : super()
constructor(
type: List<String>,
type: List<String> = emptyList(),
name: String?,
actor: String?,
id: String?,
`object`: String?,
content: String?,
tag: List<Object>
tag: List<Object> = emptyList()
) : super(
type = add(type, "Like"),
name = name,

View File

@ -21,4 +21,5 @@ object DeliverReactionJob : HideoutJob("DeliverReactionJob") {
val postUrl = string("postUrl")
val actor = string("actor")
val inbox = string("inbox")
val id = string("id")
}

View File

@ -0,0 +1,57 @@
package dev.usbharu.hideout.service.activitypub
import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.domain.model.ap.Like
import dev.usbharu.hideout.domain.model.hideout.entity.Reaction
import dev.usbharu.hideout.domain.model.job.DeliverReactionJob
import dev.usbharu.hideout.exception.PostNotFoundException
import dev.usbharu.hideout.plugins.postAp
import dev.usbharu.hideout.repository.IPostRepository
import dev.usbharu.hideout.service.job.JobQueueParentService
import dev.usbharu.hideout.service.user.IUserService
import io.ktor.client.*
import kjob.core.job.JobProps
import org.koin.core.annotation.Single
@Single
class ActivityPubReactionServiceImpl(
private val userService: IUserService,
private val jobQueueParentService: JobQueueParentService,
private val iPostRepository: IPostRepository,
private val httpClient: HttpClient
) : ActivityPubReactionService {
override suspend fun reaction(like: Reaction) {
val followers = userService.findFollowersById(like.userId)
val user = userService.findById(like.userId)
val post =
iPostRepository.findOneById(like.postId) ?: throw PostNotFoundException("${like.postId} was not found.")
followers.forEach { follower ->
jobQueueParentService.schedule(DeliverReactionJob) {
props[it.actor] = user.url
props[it.reaction] = ""
props[it.inbox] = follower.inbox
props[it.postUrl] = post.url
props[it.id] = post.id.toString()
}
}
}
override suspend fun reactionJob(props: JobProps<DeliverReactionJob>) {
val inbox = props[DeliverReactionJob.inbox]
val actor = props[DeliverReactionJob.actor]
val postUrl = props[DeliverReactionJob.postUrl]
val id = props[DeliverReactionJob.id]
val content = props[DeliverReactionJob.reaction]
httpClient.postAp(
urlString = inbox,
username = "$actor#pubkey",
jsonLd = Like(
name = "Like",
actor = actor,
`object` = postUrl,
id = "${Config.configData.url}/like/note/$id",
content = content
)
)
}
}

View File

@ -6,6 +6,7 @@ import dev.usbharu.hideout.config.Config.configData
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.DeliverReactionJob
import dev.usbharu.hideout.domain.model.job.HideoutJob
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
import dev.usbharu.hideout.exception.JsonParseException
@ -22,7 +23,8 @@ class ActivityPubServiceImpl(
private val activityPubUndoService: ActivityPubUndoService,
private val activityPubAcceptService: ActivityPubAcceptService,
private val activityPubCreateService: ActivityPubCreateService,
private val activityPubLikeService: ActivityPubLikeService
private val activityPubLikeService: ActivityPubLikeService,
private val activityPubReactionService: ActivityPubReactionService
) : ActivityPubService {
val logger: Logger = LoggerFactory.getLogger(this::class.java)
@ -71,6 +73,7 @@ class ActivityPubServiceImpl(
)
DeliverPostJob -> activityPubNoteService.createNoteJob(job.props as JobProps<DeliverPostJob>)
DeliverReactionJob -> activityPubReactionService.reactionJob(job.props as JobProps<DeliverReactionJob>)
}
}
}

View File

@ -20,7 +20,7 @@ class ReactionServiceImpl(
override suspend fun sendReaction(name: String, userId: Long, postId: Long) {
if (reactionRepository.reactionAlreadyExist(postId, userId, 0)) {
//delete
// delete
} else {
val reaction = Reaction(reactionRepository.generateId(), 0, postId, userId)
reactionRepository.save(reaction)