mirror of https://github.com/usbharu/Hideout.git
Merge pull request #98 from usbharu/feature/job-queue
Feature/job queue
This commit is contained in:
commit
6dd9400113
|
@ -1,7 +1,7 @@
|
|||
package dev.usbharu.hideout
|
||||
|
||||
import dev.usbharu.hideout.domain.model.job.HideoutJob
|
||||
import dev.usbharu.hideout.service.ap.APService
|
||||
import dev.usbharu.hideout.service.ap.job.ApJobService
|
||||
import dev.usbharu.hideout.service.job.JobQueueParentService
|
||||
import dev.usbharu.hideout.service.job.JobQueueWorkerService
|
||||
import org.slf4j.Logger
|
||||
|
@ -27,7 +27,7 @@ class JobQueueRunner(private val jobQueueParentService: JobQueueParentService, p
|
|||
class JobQueueWorkerRunner(
|
||||
private val jobQueueWorkerService: JobQueueWorkerService,
|
||||
private val jobs: List<HideoutJob>,
|
||||
private val apService: APService
|
||||
private val apJobService: ApJobService
|
||||
) : ApplicationRunner {
|
||||
override fun run(args: ApplicationArguments?) {
|
||||
LOGGER.info("Init job queue worker.")
|
||||
|
@ -36,7 +36,7 @@ class JobQueueWorkerRunner(
|
|||
it to {
|
||||
execute {
|
||||
LOGGER.debug("excute job ${it.name}")
|
||||
apService.processActivity(
|
||||
apJobService.processActivity(
|
||||
job = this,
|
||||
hideoutJob = it
|
||||
)
|
||||
|
|
|
@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RestController
|
|||
|
||||
@RestController
|
||||
class InboxControllerImpl(private val apService: APService) : InboxController {
|
||||
@Suppress("TooGenericExceptionCaught")
|
||||
override suspend fun inbox(@RequestBody string: String): ResponseEntity<Unit> {
|
||||
val parseActivity = try {
|
||||
apService.parseActivity(string)
|
||||
|
|
|
@ -6,7 +6,5 @@ import org.springframework.stereotype.Component
|
|||
|
||||
@Component
|
||||
class UserQueryMapper(private val userResultRowMapper: ResultRowMapper<User>) : QueryMapper<User> {
|
||||
override fun map(query: Query): List<User> {
|
||||
return query.map(userResultRowMapper::map)
|
||||
}
|
||||
override fun map(query: Query): List<User> = query.map(userResultRowMapper::map)
|
||||
}
|
||||
|
|
|
@ -55,9 +55,8 @@ class UserRepositoryImpl(
|
|||
return user
|
||||
}
|
||||
|
||||
override suspend fun findById(id: Long): User? {
|
||||
return Users.select { Users.id eq id }.singleOrNull()?.let(userResultRowMapper::map)
|
||||
}
|
||||
override suspend fun findById(id: Long): User? =
|
||||
Users.select { Users.id eq id }.singleOrNull()?.let(userResultRowMapper::map)
|
||||
|
||||
override suspend fun deleteFollowRequest(id: Long, follower: Long) {
|
||||
FollowRequests.deleteWhere { userId.eq(id) and followerId.eq(follower) }
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
package dev.usbharu.hideout.service.ap
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import dev.usbharu.hideout.config.ApplicationConfig
|
||||
import dev.usbharu.hideout.domain.model.ap.Create
|
||||
import dev.usbharu.hideout.domain.model.ap.Document
|
||||
import dev.usbharu.hideout.domain.model.ap.Note
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.Post
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.Visibility
|
||||
|
@ -19,12 +15,10 @@ import dev.usbharu.hideout.query.UserQueryService
|
|||
import dev.usbharu.hideout.repository.PostRepository
|
||||
import dev.usbharu.hideout.service.ap.resource.APResourceResolveService
|
||||
import dev.usbharu.hideout.service.ap.resource.resolve
|
||||
import dev.usbharu.hideout.service.core.Transaction
|
||||
import dev.usbharu.hideout.service.job.JobQueueParentService
|
||||
import dev.usbharu.hideout.service.post.PostCreateInterceptor
|
||||
import dev.usbharu.hideout.service.post.PostService
|
||||
import io.ktor.client.plugins.*
|
||||
import kjob.core.job.JobProps
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
@ -40,7 +34,6 @@ import java.time.Instant
|
|||
interface APNoteService {
|
||||
|
||||
suspend fun createNote(post: Post)
|
||||
suspend fun createNoteJob(props: JobProps<DeliverPostJob>)
|
||||
|
||||
@Cacheable("fetchNote")
|
||||
fun fetchNoteAsync(url: String, targetActor: String? = null): Deferred<Note> {
|
||||
|
@ -66,12 +59,9 @@ class APNoteServiceImpl(
|
|||
private val postQueryService: PostQueryService,
|
||||
private val mediaQueryService: MediaQueryService,
|
||||
@Qualifier("activitypub") private val objectMapper: ObjectMapper,
|
||||
private val applicationConfig: ApplicationConfig,
|
||||
private val postService: PostService,
|
||||
private val apResourceResolveService: APResourceResolveService,
|
||||
private val apRequestService: APRequestService,
|
||||
private val postBuilder: Post.PostBuilder,
|
||||
private val transaction: Transaction
|
||||
private val postBuilder: Post.PostBuilder
|
||||
|
||||
) : APNoteService, PostCreateInterceptor {
|
||||
|
||||
|
@ -104,41 +94,6 @@ class APNoteServiceImpl(
|
|||
logger.debug("SUCCESS Create Local Note ${post.url}")
|
||||
}
|
||||
|
||||
override suspend fun createNoteJob(props: JobProps<DeliverPostJob>) {
|
||||
val actor = props[DeliverPostJob.actor]
|
||||
val postEntity = objectMapper.readValue<Post>(props[DeliverPostJob.post])
|
||||
val mediaList =
|
||||
objectMapper.readValue<List<dev.usbharu.hideout.domain.model.hideout.entity.Media>>(
|
||||
props[DeliverPostJob.media]
|
||||
)
|
||||
val note = Note(
|
||||
name = "Note",
|
||||
id = postEntity.url,
|
||||
attributedTo = actor,
|
||||
content = postEntity.text,
|
||||
published = Instant.ofEpochMilli(postEntity.createdAt).toString(),
|
||||
to = listOf(public, "$actor/follower"),
|
||||
attachment = mediaList.map { Document(mediaType = "image/jpeg", url = it.url) }
|
||||
|
||||
)
|
||||
val inbox = props[DeliverPostJob.inbox]
|
||||
logger.debug("createNoteJob: actor={}, note={}, inbox={}", actor, postEntity, inbox)
|
||||
|
||||
transaction.transaction {
|
||||
val signer = userQueryService.findByUrl(actor)
|
||||
apRequestService.apPost(
|
||||
inbox,
|
||||
Create(
|
||||
name = "Create Note",
|
||||
`object` = note,
|
||||
actor = note.attributedTo,
|
||||
id = "${applicationConfig.url}/create/note/${postEntity.id}"
|
||||
),
|
||||
signer
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun fetchNote(url: String, targetActor: String?): Note {
|
||||
logger.debug("START Fetch Note url: {}", url)
|
||||
try {
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
package dev.usbharu.hideout.service.ap
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import dev.usbharu.hideout.config.ApplicationConfig
|
||||
import dev.usbharu.hideout.domain.model.ap.Like
|
||||
import dev.usbharu.hideout.domain.model.ap.Undo
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.Reaction
|
||||
import dev.usbharu.hideout.domain.model.job.DeliverReactionJob
|
||||
import dev.usbharu.hideout.domain.model.job.DeliverRemoveReactionJob
|
||||
|
@ -12,16 +8,12 @@ import dev.usbharu.hideout.query.FollowerQueryService
|
|||
import dev.usbharu.hideout.query.PostQueryService
|
||||
import dev.usbharu.hideout.query.UserQueryService
|
||||
import dev.usbharu.hideout.service.job.JobQueueParentService
|
||||
import kjob.core.job.JobProps
|
||||
import org.springframework.beans.factory.annotation.Qualifier
|
||||
import org.springframework.stereotype.Service
|
||||
import java.time.Instant
|
||||
|
||||
interface APReactionService {
|
||||
suspend fun reaction(like: Reaction)
|
||||
suspend fun removeReaction(like: Reaction)
|
||||
suspend fun reactionJob(props: JobProps<DeliverReactionJob>)
|
||||
suspend fun removeReactionJob(props: JobProps<DeliverRemoveReactionJob>)
|
||||
}
|
||||
|
||||
@Service
|
||||
|
@ -30,9 +22,7 @@ class APReactionServiceImpl(
|
|||
private val userQueryService: UserQueryService,
|
||||
private val followerQueryService: FollowerQueryService,
|
||||
private val postQueryService: PostQueryService,
|
||||
@Qualifier("activitypub") private val objectMapper: ObjectMapper,
|
||||
private val applicationConfig: ApplicationConfig,
|
||||
private val apRequestService: APRequestService
|
||||
@Qualifier("activitypub") private val objectMapper: ObjectMapper
|
||||
) : APReactionService {
|
||||
override suspend fun reaction(like: Reaction) {
|
||||
val followers = followerQueryService.findFollowersById(like.userId)
|
||||
|
@ -64,46 +54,4 @@ class APReactionServiceImpl(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
|
||||
val signer = userQueryService.findByUrl(actor)
|
||||
|
||||
apRequestService.apPost(
|
||||
inbox,
|
||||
Like(
|
||||
name = "Like",
|
||||
actor = actor,
|
||||
`object` = postUrl,
|
||||
id = "${applicationConfig.url}/like/note/$id",
|
||||
content = content
|
||||
),
|
||||
signer
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun removeReactionJob(props: JobProps<DeliverRemoveReactionJob>) {
|
||||
val inbox = props[DeliverRemoveReactionJob.inbox]
|
||||
val actor = props[DeliverRemoveReactionJob.actor]
|
||||
val like = objectMapper.readValue<Like>(props[DeliverRemoveReactionJob.like])
|
||||
|
||||
val signer = userQueryService.findByUrl(actor)
|
||||
|
||||
apRequestService.apPost(
|
||||
inbox,
|
||||
Undo(
|
||||
name = "Undo Reaction",
|
||||
actor = actor,
|
||||
`object` = like,
|
||||
id = "${applicationConfig.url}/undo/note/${like.id}",
|
||||
published = Instant.now()
|
||||
),
|
||||
signer
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,36 +1,24 @@
|
|||
package dev.usbharu.hideout.service.ap
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import dev.usbharu.hideout.domain.model.ActivityPubResponse
|
||||
import dev.usbharu.hideout.domain.model.ActivityPubStringResponse
|
||||
import dev.usbharu.hideout.domain.model.ap.Accept
|
||||
import dev.usbharu.hideout.domain.model.ap.Follow
|
||||
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
|
||||
import dev.usbharu.hideout.query.UserQueryService
|
||||
import dev.usbharu.hideout.service.core.Transaction
|
||||
import dev.usbharu.hideout.service.job.JobQueueParentService
|
||||
import dev.usbharu.hideout.service.user.UserService
|
||||
import io.ktor.http.*
|
||||
import kjob.core.job.JobProps
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Qualifier
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
interface APReceiveFollowService {
|
||||
suspend fun receiveFollow(follow: Follow): ActivityPubResponse
|
||||
suspend fun receiveFollowJob(props: JobProps<ReceiveFollowJob>)
|
||||
}
|
||||
|
||||
@Service
|
||||
class APReceiveFollowServiceImpl(
|
||||
private val jobQueueParentService: JobQueueParentService,
|
||||
private val apUserService: APUserService,
|
||||
private val userService: UserService,
|
||||
private val userQueryService: UserQueryService,
|
||||
private val transaction: Transaction,
|
||||
@Qualifier("activitypub") private val objectMapper: ObjectMapper,
|
||||
private val apRequestService: APRequestService
|
||||
@Qualifier("activitypub") private val objectMapper: ObjectMapper
|
||||
) : APReceiveFollowService {
|
||||
override suspend fun receiveFollow(follow: Follow): ActivityPubResponse {
|
||||
logger.info("FOLLOW from: {} to: {}", follow.actor, follow.`object`)
|
||||
|
@ -42,37 +30,6 @@ class APReceiveFollowServiceImpl(
|
|||
return ActivityPubStringResponse(HttpStatusCode.OK, "{}", ContentType.Application.Json)
|
||||
}
|
||||
|
||||
override suspend fun receiveFollowJob(props: JobProps<ReceiveFollowJob>) {
|
||||
transaction.transaction {
|
||||
val actor = props[ReceiveFollowJob.actor]
|
||||
val targetActor = props[ReceiveFollowJob.targetActor]
|
||||
val person = apUserService.fetchPerson(actor, targetActor)
|
||||
val follow = objectMapper.readValue<Follow>(props[ReceiveFollowJob.follow])
|
||||
logger.info("START Follow from: {} to: {}", targetActor, actor)
|
||||
|
||||
val signer = userQueryService.findByUrl(targetActor)
|
||||
|
||||
val urlString = person.inbox ?: throw IllegalArgumentException("inbox is not found")
|
||||
|
||||
apRequestService.apPost(
|
||||
url = urlString,
|
||||
body = Accept(
|
||||
name = "Follow",
|
||||
`object` = follow,
|
||||
actor = targetActor
|
||||
),
|
||||
signer = signer
|
||||
)
|
||||
|
||||
val targetEntity = userQueryService.findByUrl(targetActor)
|
||||
val followActorEntity =
|
||||
userQueryService.findByUrl(follow.actor ?: throw java.lang.IllegalArgumentException("Actor is null"))
|
||||
|
||||
userService.followRequest(targetEntity.id, followActorEntity.id)
|
||||
logger.info("SUCCESS Follow from: {} to: {}", targetActor, actor)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(APReceiveFollowServiceImpl::class.java)
|
||||
}
|
||||
|
|
|
@ -5,10 +5,10 @@ import com.fasterxml.jackson.databind.ObjectMapper
|
|||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import dev.usbharu.hideout.domain.model.ActivityPubResponse
|
||||
import dev.usbharu.hideout.domain.model.ap.Follow
|
||||
import dev.usbharu.hideout.domain.model.job.*
|
||||
import dev.usbharu.hideout.exception.JsonParseException
|
||||
import kjob.core.dsl.JobContextWithProps
|
||||
import kjob.core.job.JobProps
|
||||
import dev.usbharu.hideout.service.ap.job.APReceiveFollowJobService
|
||||
import dev.usbharu.hideout.service.ap.job.ApNoteJobService
|
||||
import dev.usbharu.hideout.service.ap.job.ApReactionJobService
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Qualifier
|
||||
|
@ -18,8 +18,6 @@ interface APService {
|
|||
fun parseActivity(json: String): ActivityType
|
||||
|
||||
suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse?
|
||||
|
||||
suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob)
|
||||
}
|
||||
|
||||
enum class ActivityType {
|
||||
|
@ -176,13 +174,14 @@ enum class ExtendedVocabulary {
|
|||
@Service
|
||||
class APServiceImpl(
|
||||
private val apReceiveFollowService: APReceiveFollowService,
|
||||
private val apNoteService: APNoteService,
|
||||
private val apUndoService: APUndoService,
|
||||
private val apAcceptService: APAcceptService,
|
||||
private val apCreateService: APCreateService,
|
||||
private val apLikeService: APLikeService,
|
||||
private val apReactionService: APReactionService,
|
||||
@Qualifier("activitypub") private val objectMapper: ObjectMapper
|
||||
@Qualifier("activitypub") private val objectMapper: ObjectMapper,
|
||||
private val apReceiveFollowJobService: APReceiveFollowJobService,
|
||||
private val apNoteJobService: ApNoteJobService,
|
||||
private val apReactionJobService: ApReactionJobService
|
||||
) : APService {
|
||||
|
||||
val logger: Logger = LoggerFactory.getLogger(APServiceImpl::class.java)
|
||||
|
@ -197,7 +196,8 @@ class APServiceImpl(
|
|||
|
|
||||
|***** Trace End Activity *****
|
||||
|
|
||||
""".trimMargin(), readTree.toPrettyString()
|
||||
""".trimMargin(),
|
||||
readTree.toPrettyString()
|
||||
)
|
||||
if (readTree.isObject.not()) {
|
||||
throw JsonParseException("Json is not object.")
|
||||
|
@ -228,29 +228,4 @@ class APServiceImpl(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("REDUNDANT_ELSE_IN_WHEN")
|
||||
override suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob) {
|
||||
logger.debug("processActivity: ${hideoutJob.name}")
|
||||
|
||||
@Suppress("ElseCaseInsteadOfExhaustiveWhen")
|
||||
// Springで作成されるプロキシの都合上パターンマッチングが壊れるので必須
|
||||
when (hideoutJob) {
|
||||
is ReceiveFollowJob -> {
|
||||
apReceiveFollowService.receiveFollowJob(
|
||||
job.props as JobProps<ReceiveFollowJob>
|
||||
)
|
||||
}
|
||||
|
||||
is DeliverPostJob -> apNoteService.createNoteJob(job.props as JobProps<DeliverPostJob>)
|
||||
is DeliverReactionJob -> apReactionService.reactionJob(job.props as JobProps<DeliverReactionJob>)
|
||||
is DeliverRemoveReactionJob -> apReactionService.removeReactionJob(
|
||||
job.props as JobProps<DeliverRemoveReactionJob>
|
||||
)
|
||||
|
||||
else -> {
|
||||
throw IllegalStateException("WTF")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package dev.usbharu.hideout.service.ap.job
|
||||
|
||||
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
|
||||
import kjob.core.job.JobProps
|
||||
|
||||
interface APReceiveFollowJobService {
|
||||
suspend fun receiveFollowJob(props: JobProps<ReceiveFollowJob>)
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package dev.usbharu.hideout.service.ap.job
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import dev.usbharu.hideout.domain.model.ap.Accept
|
||||
import dev.usbharu.hideout.domain.model.ap.Follow
|
||||
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
|
||||
import dev.usbharu.hideout.query.UserQueryService
|
||||
import dev.usbharu.hideout.service.ap.APRequestService
|
||||
import dev.usbharu.hideout.service.ap.APUserService
|
||||
import dev.usbharu.hideout.service.core.Transaction
|
||||
import dev.usbharu.hideout.service.user.UserService
|
||||
import kjob.core.job.JobProps
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Qualifier
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
class APReceiveFollowJobServiceImpl(
|
||||
private val apUserService: APUserService,
|
||||
private val userQueryService: UserQueryService,
|
||||
private val apRequestService: APRequestService,
|
||||
private val userService: UserService,
|
||||
@Qualifier("activitypub") private val objectMapper: ObjectMapper,
|
||||
private val transaction: Transaction
|
||||
) : APReceiveFollowJobService {
|
||||
override suspend fun receiveFollowJob(props: JobProps<ReceiveFollowJob>) {
|
||||
transaction.transaction {
|
||||
val actor = props[ReceiveFollowJob.actor]
|
||||
val targetActor = props[ReceiveFollowJob.targetActor]
|
||||
val person = apUserService.fetchPerson(actor, targetActor)
|
||||
val follow = objectMapper.readValue<Follow>(props[ReceiveFollowJob.follow])
|
||||
logger.info("START Follow from: {} to: {}", targetActor, actor)
|
||||
|
||||
val signer = userQueryService.findByUrl(targetActor)
|
||||
|
||||
val urlString = person.inbox ?: throw IllegalArgumentException("inbox is not found")
|
||||
|
||||
apRequestService.apPost(
|
||||
url = urlString,
|
||||
body = Accept(
|
||||
name = "Follow",
|
||||
`object` = follow,
|
||||
actor = targetActor
|
||||
),
|
||||
signer = signer
|
||||
)
|
||||
|
||||
val targetEntity = userQueryService.findByUrl(targetActor)
|
||||
val followActorEntity =
|
||||
userQueryService.findByUrl(follow.actor ?: throw java.lang.IllegalArgumentException("Actor is null"))
|
||||
|
||||
userService.followRequest(targetEntity.id, followActorEntity.id)
|
||||
logger.info("SUCCESS Follow from: {} to: {}", targetActor, actor)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(APReceiveFollowJobServiceImpl::class.java)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package dev.usbharu.hideout.service.ap.job
|
||||
|
||||
import dev.usbharu.hideout.domain.model.job.HideoutJob
|
||||
import kjob.core.dsl.JobContextWithProps
|
||||
|
||||
interface ApJobService {
|
||||
suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob)
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package dev.usbharu.hideout.service.ap.job
|
||||
|
||||
import dev.usbharu.hideout.domain.model.job.*
|
||||
import kjob.core.dsl.JobContextWithProps
|
||||
import kjob.core.job.JobProps
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class ApJobServiceImpl(
|
||||
private val apReceiveFollowJobService: APReceiveFollowJobService,
|
||||
private val apNoteJobService: ApNoteJobService,
|
||||
private val apReactionJobService: ApReactionJobService
|
||||
) : ApJobService {
|
||||
@Suppress("REDUNDANT_ELSE_IN_WHEN")
|
||||
override suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob) {
|
||||
logger.debug("processActivity: ${hideoutJob.name}")
|
||||
|
||||
@Suppress("ElseCaseInsteadOfExhaustiveWhen")
|
||||
// Springで作成されるプロキシの都合上パターンマッチングが壊れるので必須
|
||||
when (hideoutJob) {
|
||||
is ReceiveFollowJob -> {
|
||||
apReceiveFollowJobService.receiveFollowJob(
|
||||
job.props as JobProps<ReceiveFollowJob>
|
||||
)
|
||||
}
|
||||
|
||||
is DeliverPostJob -> apNoteJobService.createNoteJob(job.props as JobProps<DeliverPostJob>)
|
||||
is DeliverReactionJob -> apReactionJobService.reactionJob(job.props as JobProps<DeliverReactionJob>)
|
||||
is DeliverRemoveReactionJob -> apReactionJobService.removeReactionJob(
|
||||
job.props as JobProps<DeliverRemoveReactionJob>
|
||||
)
|
||||
|
||||
else -> {
|
||||
throw IllegalStateException("WTF")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(ApJobServiceImpl::class.java)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package dev.usbharu.hideout.service.ap.job
|
||||
|
||||
import dev.usbharu.hideout.domain.model.job.DeliverPostJob
|
||||
import kjob.core.job.JobProps
|
||||
|
||||
interface ApNoteJobService {
|
||||
suspend fun createNoteJob(props: JobProps<DeliverPostJob>)
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package dev.usbharu.hideout.service.ap.job
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import dev.usbharu.hideout.config.ApplicationConfig
|
||||
import dev.usbharu.hideout.domain.model.ap.Create
|
||||
import dev.usbharu.hideout.domain.model.ap.Document
|
||||
import dev.usbharu.hideout.domain.model.ap.Note
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.Post
|
||||
import dev.usbharu.hideout.domain.model.job.DeliverPostJob
|
||||
import dev.usbharu.hideout.query.UserQueryService
|
||||
import dev.usbharu.hideout.service.ap.APNoteServiceImpl
|
||||
import dev.usbharu.hideout.service.ap.APRequestService
|
||||
import dev.usbharu.hideout.service.core.Transaction
|
||||
import kjob.core.job.JobProps
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Qualifier
|
||||
import org.springframework.stereotype.Component
|
||||
import java.time.Instant
|
||||
|
||||
@Component
|
||||
class ApNoteJobServiceImpl(
|
||||
private val userQueryService: UserQueryService,
|
||||
private val apRequestService: APRequestService,
|
||||
@Qualifier("activitypub") private val objectMapper: ObjectMapper,
|
||||
private val transaction: Transaction,
|
||||
private val applicationConfig: ApplicationConfig
|
||||
) : ApNoteJobService {
|
||||
override suspend fun createNoteJob(props: JobProps<DeliverPostJob>) {
|
||||
val actor = props[DeliverPostJob.actor]
|
||||
val postEntity = objectMapper.readValue<Post>(props[DeliverPostJob.post])
|
||||
val mediaList =
|
||||
objectMapper.readValue<List<dev.usbharu.hideout.domain.model.hideout.entity.Media>>(
|
||||
props[DeliverPostJob.media]
|
||||
)
|
||||
val note = Note(
|
||||
name = "Note",
|
||||
id = postEntity.url,
|
||||
attributedTo = actor,
|
||||
content = postEntity.text,
|
||||
published = Instant.ofEpochMilli(postEntity.createdAt).toString(),
|
||||
to = listOf(APNoteServiceImpl.public, "$actor/follower"),
|
||||
attachment = mediaList.map { Document(mediaType = "image/jpeg", url = it.url) }
|
||||
|
||||
)
|
||||
val inbox = props[DeliverPostJob.inbox]
|
||||
logger.debug("createNoteJob: actor={}, note={}, inbox={}", actor, postEntity, inbox)
|
||||
|
||||
transaction.transaction {
|
||||
val signer = userQueryService.findByUrl(actor)
|
||||
apRequestService.apPost(
|
||||
inbox,
|
||||
Create(
|
||||
name = "Create Note",
|
||||
`object` = note,
|
||||
actor = note.attributedTo,
|
||||
id = "${applicationConfig.url}/create/note/${postEntity.id}"
|
||||
),
|
||||
signer
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(ApNoteJobServiceImpl::class.java)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package dev.usbharu.hideout.service.ap.job
|
||||
|
||||
import dev.usbharu.hideout.domain.model.job.DeliverReactionJob
|
||||
import dev.usbharu.hideout.domain.model.job.DeliverRemoveReactionJob
|
||||
import kjob.core.job.JobProps
|
||||
|
||||
interface ApReactionJobService {
|
||||
suspend fun reactionJob(props: JobProps<DeliverReactionJob>)
|
||||
suspend fun removeReactionJob(props: JobProps<DeliverRemoveReactionJob>)
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package dev.usbharu.hideout.service.ap.job
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import dev.usbharu.hideout.config.ApplicationConfig
|
||||
import dev.usbharu.hideout.domain.model.ap.Like
|
||||
import dev.usbharu.hideout.domain.model.ap.Undo
|
||||
import dev.usbharu.hideout.domain.model.job.DeliverReactionJob
|
||||
import dev.usbharu.hideout.domain.model.job.DeliverRemoveReactionJob
|
||||
import dev.usbharu.hideout.query.UserQueryService
|
||||
import dev.usbharu.hideout.service.ap.APRequestService
|
||||
import kjob.core.job.JobProps
|
||||
import org.springframework.beans.factory.annotation.Qualifier
|
||||
import org.springframework.stereotype.Service
|
||||
import java.time.Instant
|
||||
|
||||
@Service
|
||||
class ApReactionJobServiceImpl(
|
||||
private val userQueryService: UserQueryService,
|
||||
private val apRequestService: APRequestService,
|
||||
private val applicationConfig: ApplicationConfig,
|
||||
@Qualifier("activitypub") private val objectMapper: ObjectMapper
|
||||
) : ApReactionJobService {
|
||||
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]
|
||||
|
||||
val signer = userQueryService.findByUrl(actor)
|
||||
|
||||
apRequestService.apPost(
|
||||
inbox,
|
||||
Like(
|
||||
name = "Like",
|
||||
actor = actor,
|
||||
`object` = postUrl,
|
||||
id = "${applicationConfig.url}/like/note/$id",
|
||||
content = content
|
||||
),
|
||||
signer
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun removeReactionJob(props: JobProps<DeliverRemoveReactionJob>) {
|
||||
val inbox = props[DeliverRemoveReactionJob.inbox]
|
||||
val actor = props[DeliverRemoveReactionJob.actor]
|
||||
val like = objectMapper.readValue<Like>(props[DeliverRemoveReactionJob.like])
|
||||
|
||||
val signer = userQueryService.findByUrl(actor)
|
||||
|
||||
apRequestService.apPost(
|
||||
inbox,
|
||||
Undo(
|
||||
name = "Undo Reaction",
|
||||
actor = actor,
|
||||
`object` = like,
|
||||
id = "${applicationConfig.url}/undo/note/${like.id}",
|
||||
published = Instant.now()
|
||||
),
|
||||
signer
|
||||
)
|
||||
}
|
||||
}
|
|
@ -40,6 +40,7 @@ class HttpSignatureUserDetailsService(
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("TooGenericExceptionCaught")
|
||||
val verify = try {
|
||||
httpSignatureVerifier.verify(
|
||||
token.credentials as HttpRequest,
|
||||
|
|
|
@ -12,6 +12,7 @@ import dev.usbharu.hideout.domain.model.job.DeliverPostJob
|
|||
import dev.usbharu.hideout.query.FollowerQueryService
|
||||
import dev.usbharu.hideout.query.MediaQueryService
|
||||
import dev.usbharu.hideout.query.UserQueryService
|
||||
import dev.usbharu.hideout.service.ap.job.ApNoteJobServiceImpl
|
||||
import dev.usbharu.hideout.service.job.JobQueueParentService
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.engine.mock.*
|
||||
|
@ -24,7 +25,7 @@ import org.mockito.Mockito.anyLong
|
|||
import org.mockito.Mockito.eq
|
||||
import org.mockito.kotlin.*
|
||||
import utils.JsonObjectMapper.objectMapper
|
||||
import utils.TestApplicationConfig.testApplicationConfig
|
||||
import utils.TestTransaction
|
||||
import java.net.URL
|
||||
import java.time.Instant
|
||||
import kotlin.test.assertEquals
|
||||
|
@ -104,11 +105,8 @@ class APNoteServiceImplTest {
|
|||
postQueryService = mock(),
|
||||
mediaQueryService = mediaQueryService,
|
||||
objectMapper = objectMapper,
|
||||
applicationConfig = testApplicationConfig,
|
||||
postService = mock(),
|
||||
apResourceResolveService = mock(),
|
||||
apRequestService = mock(),
|
||||
transaction = mock(),
|
||||
postBuilder = postBuilder
|
||||
)
|
||||
val postEntity = postBuilder.of(
|
||||
|
@ -138,21 +136,13 @@ class APNoteServiceImplTest {
|
|||
respondOk()
|
||||
}
|
||||
)
|
||||
val activityPubNoteService = APNoteServiceImpl(
|
||||
jobQueueParentService = mock(),
|
||||
postRepository = mock(),
|
||||
apUserService = mock(),
|
||||
val activityPubNoteService = ApNoteJobServiceImpl(
|
||||
|
||||
userQueryService = mock(),
|
||||
followerQueryService = mock(),
|
||||
postQueryService = mock(),
|
||||
mediaQueryService = mediaQueryService,
|
||||
objectMapper = objectMapper,
|
||||
applicationConfig = testApplicationConfig,
|
||||
postService = mock(),
|
||||
apResourceResolveService = mock(),
|
||||
apRequestService = mock(),
|
||||
transaction = mock(),
|
||||
postBuilder = postBuilder
|
||||
transaction = TestTransaction,
|
||||
applicationConfig = ApplicationConfig(URL("https://example.com"))
|
||||
)
|
||||
activityPubNoteService.createNoteJob(
|
||||
JobProps(
|
||||
|
|
|
@ -13,6 +13,7 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Post
|
|||
import dev.usbharu.hideout.domain.model.hideout.entity.User
|
||||
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
|
||||
import dev.usbharu.hideout.query.UserQueryService
|
||||
import dev.usbharu.hideout.service.ap.job.APReceiveFollowJobServiceImpl
|
||||
import dev.usbharu.hideout.service.job.JobQueueParentService
|
||||
import dev.usbharu.hideout.service.user.UserService
|
||||
import kjob.core.dsl.ScheduleContext
|
||||
|
@ -42,12 +43,7 @@ class APReceiveFollowServiceImplTest {
|
|||
val activityPubFollowService =
|
||||
APReceiveFollowServiceImpl(
|
||||
jobQueueParentService,
|
||||
mock(),
|
||||
mock(),
|
||||
mock(),
|
||||
TestTransaction,
|
||||
objectMapper,
|
||||
mock()
|
||||
objectMapper
|
||||
)
|
||||
activityPubFollowService.receiveFollow(
|
||||
Follow(
|
||||
|
@ -151,14 +147,13 @@ class APReceiveFollowServiceImplTest {
|
|||
onBlocking { followRequest(any(), any()) } doReturn false
|
||||
}
|
||||
val activityPubFollowService =
|
||||
APReceiveFollowServiceImpl(
|
||||
mock(),
|
||||
APReceiveFollowJobServiceImpl(
|
||||
apUserService,
|
||||
userService,
|
||||
userQueryService,
|
||||
TestTransaction,
|
||||
mock(),
|
||||
userService,
|
||||
objectMapper,
|
||||
mock()
|
||||
TestTransaction
|
||||
)
|
||||
activityPubFollowService.receiveFollowJob(
|
||||
JobProps(
|
||||
|
|
Loading…
Reference in New Issue