refactor: ActivityPubをAPに変更

This commit is contained in:
usbharu 2023-08-11 16:29:52 +09:00
parent f61c6c1713
commit 5f85a25daf
32 changed files with 137 additions and 134 deletions

View File

@ -15,8 +15,8 @@ import dev.usbharu.hideout.plugins.*
import dev.usbharu.hideout.query.FollowerQueryService
import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.routing.register
import dev.usbharu.hideout.service.activitypub.ActivityPubService
import dev.usbharu.hideout.service.activitypub.ActivityPubUserService
import dev.usbharu.hideout.service.ap.APService
import dev.usbharu.hideout.service.ap.APUserService
import dev.usbharu.hideout.service.api.IPostApiService
import dev.usbharu.hideout.service.api.IUserApiService
import dev.usbharu.hideout.service.api.UserAuthApiService
@ -109,9 +109,9 @@ fun Application.parent() {
)
configureRouting(
httpSignatureVerifyService = inject<HttpSignatureVerifyService>().value,
activityPubService = inject<ActivityPubService>().value,
apService = inject<APService>().value,
userService = inject<IUserService>().value,
activityPubUserService = inject<ActivityPubUserService>().value,
apUserService = inject<APUserService>().value,
postService = inject<IPostApiService>().value,
userApiService = inject<IUserApiService>().value,
userQueryService = inject<UserQueryService>().value,
@ -128,28 +128,28 @@ fun Application.worker() {
connectionDatabase = inject<Database>().value
}.start()
val activityPubService = inject<ActivityPubService>().value
val apService = inject<APService>().value
kJob.register(ReceiveFollowJob) {
execute {
activityPubService.processActivity(this, it)
apService.processActivity(this, it)
}
}
kJob.register(DeliverPostJob) {
execute {
activityPubService.processActivity(this, it)
apService.processActivity(this, it)
}
}
kJob.register(DeliverReactionJob) {
execute {
activityPubService.processActivity(this, it)
apService.processActivity(this, it)
}
}
kJob.register(DeliverRemoveReactionJob) {
execute {
activityPubService.processActivity(this, it)
apService.processActivity(this, it)
}
}
}

View File

@ -4,7 +4,7 @@ import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.JsonNode
import dev.usbharu.hideout.service.activitypub.ExtendedActivityVocabulary
import dev.usbharu.hideout.service.ap.ExtendedActivityVocabulary
class ObjectDeserializer : JsonDeserializer<Object>() {
@Suppress("LongMethod", "CyclomaticComplexMethod")

View File

@ -9,8 +9,8 @@ import dev.usbharu.hideout.routing.api.internal.v1.auth
import dev.usbharu.hideout.routing.api.internal.v1.posts
import dev.usbharu.hideout.routing.api.internal.v1.users
import dev.usbharu.hideout.routing.wellknown.webfinger
import dev.usbharu.hideout.service.activitypub.ActivityPubService
import dev.usbharu.hideout.service.activitypub.ActivityPubUserService
import dev.usbharu.hideout.service.ap.APService
import dev.usbharu.hideout.service.ap.APUserService
import dev.usbharu.hideout.service.api.IPostApiService
import dev.usbharu.hideout.service.api.IUserApiService
import dev.usbharu.hideout.service.api.UserAuthApiService
@ -25,9 +25,9 @@ import io.ktor.server.routing.*
@Suppress("LongParameterList")
fun Application.configureRouting(
httpSignatureVerifyService: HttpSignatureVerifyService,
activityPubService: ActivityPubService,
apService: APService,
userService: IUserService,
activityPubUserService: ActivityPubUserService,
apUserService: APUserService,
postService: IPostApiService,
userApiService: IUserApiService,
userQueryService: UserQueryService,
@ -38,9 +38,9 @@ fun Application.configureRouting(
) {
install(AutoHeadResponse)
routing {
inbox(httpSignatureVerifyService, activityPubService)
inbox(httpSignatureVerifyService, apService)
outbox()
usersAP(activityPubUserService, userQueryService, followerQueryService, transaction)
usersAP(apUserService, userQueryService, followerQueryService, transaction)
webfinger(webFingerApiService)
route("/api/internal/v1") {
posts(postService)

View File

@ -13,7 +13,7 @@ import io.ktor.server.routing.*
fun Routing.inbox(
httpSignatureVerifyService: HttpSignatureVerifyService,
activityPubService: dev.usbharu.hideout.service.activitypub.ActivityPubService
apService: dev.usbharu.hideout.service.ap.APService
) {
route("/inbox") {
get {
@ -25,9 +25,9 @@ fun Routing.inbox(
}
val json = call.receiveText()
call.application.log.trace("Received: $json")
val activityTypes = activityPubService.parseActivity(json)
val activityTypes = apService.parseActivity(json)
call.application.log.debug("ActivityTypes: ${activityTypes.name}")
val response = activityPubService.processActivity(json, activityTypes)
val response = apService.processActivity(json, activityTypes)
when (response) {
is ActivityPubObjectResponse -> call.respond(
response.httpStatusCode,
@ -54,9 +54,9 @@ fun Routing.inbox(
}
val json = call.receiveText()
call.application.log.trace("Received: $json")
val activityTypes = activityPubService.parseActivity(json)
val activityTypes = apService.parseActivity(json)
call.application.log.debug("ActivityTypes: ${activityTypes.name}")
val response = activityPubService.processActivity(json, activityTypes)
val response = apService.processActivity(json, activityTypes)
when (response) {
is ActivityPubObjectResponse -> call.respond(
response.httpStatusCode,

View File

@ -5,7 +5,7 @@ import dev.usbharu.hideout.exception.ParameterNotExistException
import dev.usbharu.hideout.plugins.respondAp
import dev.usbharu.hideout.query.FollowerQueryService
import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.service.activitypub.ActivityPubUserService
import dev.usbharu.hideout.service.ap.APUserService
import dev.usbharu.hideout.service.core.Transaction
import dev.usbharu.hideout.util.HttpUtil.Activity
import dev.usbharu.hideout.util.HttpUtil.JsonLd
@ -16,7 +16,7 @@ import io.ktor.server.response.*
import io.ktor.server.routing.*
fun Routing.usersAP(
activityPubUserService: ActivityPubUserService,
apUserService: APUserService,
userQueryService: UserQueryService,
followerQueryService: FollowerQueryService,
transaction: Transaction
@ -27,7 +27,7 @@ fun Routing.usersAP(
call.application.log.debug("Authorization: ${call.request.header("Authorization")}")
val name =
call.parameters["name"] ?: throw ParameterNotExistException("Parameter(name='name') does not exist.")
val person = activityPubUserService.getPersonByName(name)
val person = apUserService.getPersonByName(name)
return@handle call.respondAp(
person,
HttpStatusCode.OK

View File

@ -1,8 +1,8 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.ap.Accept
interface ActivityPubAcceptService {
interface APAcceptService {
suspend fun receiveAccept(accept: Accept): ActivityPubResponse
}

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.ActivityPubStringResponse
@ -11,10 +11,10 @@ import io.ktor.http.*
import org.koin.core.annotation.Single
@Single
class ActivityPubAcceptServiceImpl(
class APAcceptServiceImpl(
private val userService: IUserService,
private val userQueryService: UserQueryService
) : ActivityPubAcceptService {
) : APAcceptService {
override suspend fun receiveAccept(accept: Accept): ActivityPubResponse {
val value = accept.`object` ?: throw IllegalActivityPubObjectException("object is null")
if (value.type.contains("Follow").not()) {

View File

@ -1,8 +1,8 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.ap.Create
interface ActivityPubCreateService {
interface APCreateService {
suspend fun receiveCreate(create: Create): ActivityPubResponse
}

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.ActivityPubStringResponse
@ -10,10 +10,10 @@ import io.ktor.http.*
import org.koin.core.annotation.Single
@Single
class ActivityPubCreateServiceImpl(
private val activityPubNoteService: ActivityPubNoteService,
class APCreateServiceImpl(
private val apNoteService: APNoteService,
private val transaction: Transaction
) : ActivityPubCreateService {
) : APCreateService {
override suspend fun receiveCreate(create: Create): ActivityPubResponse {
val value = create.`object` ?: throw IllegalActivityPubObjectException("object is null")
if (value.type.contains("Note").not()) {
@ -22,7 +22,7 @@ class ActivityPubCreateServiceImpl(
return transaction.transaction {
val note = value as Note
activityPubNoteService.fetchNote(note)
apNoteService.fetchNote(note)
ActivityPubStringResponse(HttpStatusCode.OK, "Created")
}
}

View File

@ -1,8 +1,8 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.ap.Like
interface ActivityPubLikeService {
interface APLikeService {
suspend fun receiveLike(like: Like): ActivityPubResponse
}

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.ActivityPubStringResponse
@ -12,21 +12,21 @@ import io.ktor.http.*
import org.koin.core.annotation.Single
@Single
class ActivityPubLikeServiceImpl(
class APLikeServiceImpl(
private val reactionService: IReactionService,
private val activityPubUserService: ActivityPubUserService,
private val activityPubNoteService: ActivityPubNoteService,
private val apUserService: APUserService,
private val apNoteService: APNoteService,
private val userQueryService: UserQueryService,
private val postQueryService: PostQueryService,
private val transaction: Transaction
) : ActivityPubLikeService {
) : APLikeService {
override suspend fun receiveLike(like: Like): ActivityPubResponse {
val actor = like.actor ?: throw IllegalActivityPubObjectException("actor is null")
val content = like.content ?: throw IllegalActivityPubObjectException("content is null")
like.`object` ?: throw IllegalActivityPubObjectException("object is null")
transaction.transaction {
val person = activityPubUserService.fetchPerson(actor)
activityPubNoteService.fetchNote(like.`object`!!)
val person = apUserService.fetchPerson(actor)
apNoteService.fetchNote(like.`object`!!)
val user = userQueryService.findByUrl(
person.url

View File

@ -1,11 +1,11 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
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 kjob.core.job.JobProps
interface ActivityPubNoteService {
interface APNoteService {
suspend fun createNote(post: Post)
suspend fun createNoteJob(props: JobProps<DeliverPostJob>)

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.config.Config
@ -23,15 +23,15 @@ import org.slf4j.LoggerFactory
import java.time.Instant
@Single
class ActivityPubNoteServiceImpl(
class APNoteServiceImpl(
private val httpClient: HttpClient,
private val jobQueueParentService: JobQueueParentService,
private val postRepository: IPostRepository,
private val activityPubUserService: ActivityPubUserService,
private val apUserService: APUserService,
private val userQueryService: UserQueryService,
private val followerQueryService: FollowerQueryService,
private val postQueryService: PostQueryService
) : ActivityPubNoteService {
) : APNoteService {
private val logger = LoggerFactory.getLogger(this::class.java)
@ -75,9 +75,12 @@ class ActivityPubNoteServiceImpl(
override suspend fun fetchNote(url: String, targetActor: String?): Note {
val post = postQueryService.findByUrl(url)
if (post != null) {
try {
return postToNote(post)
} catch (_: NoSuchElementException) {
} catch (_: IllegalArgumentException) {
}
val response = httpClient.getAp(
url,
targetActor?.let { "$targetActor#pubkey" }
@ -118,7 +121,7 @@ class ActivityPubNoteServiceImpl(
}
private suspend fun internalNote(note: Note, targetActor: String?, url: String): Note {
val person = activityPubUserService.fetchPerson(
val person = apUserService.fetchPerson(
note.attributedTo ?: throw IllegalActivityPubObjectException("note.attributedTo is null"),
targetActor
)

View File

@ -1,11 +1,11 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
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
import kjob.core.job.JobProps
interface ActivityPubReactionService {
interface APReactionService {
suspend fun reaction(like: Reaction)
suspend fun removeReaction(like: Reaction)
suspend fun reactionJob(props: JobProps<DeliverReactionJob>)

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.config.Config
@ -19,14 +19,14 @@ import org.koin.core.annotation.Single
import java.time.Instant
@Single
class ActivityPubReactionServiceImpl(
class APReactionServiceImpl(
private val jobQueueParentService: JobQueueParentService,
private val iPostRepository: IPostRepository,
private val httpClient: HttpClient,
private val userQueryService: UserQueryService,
private val followerQueryService: FollowerQueryService,
private val postQueryService: PostQueryService
) : ActivityPubReactionService {
) : APReactionService {
override suspend fun reaction(like: Reaction) {
val followers = followerQueryService.findFollowersById(like.userId)
val user = userQueryService.findById(like.userId)

View File

@ -1,11 +1,11 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.ap.Follow
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
import kjob.core.job.JobProps
interface ActivityPubReceiveFollowService {
interface APReceiveFollowService {
suspend fun receiveFollow(follow: Follow): ActivityPubResponse
suspend fun receiveFollowJob(props: JobProps<ReceiveFollowJob>)
}

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.config.Config
@ -18,14 +18,14 @@ import kjob.core.job.JobProps
import org.koin.core.annotation.Single
@Single
class ActivityPubReceiveFollowServiceImpl(
class APReceiveFollowServiceImpl(
private val jobQueueParentService: JobQueueParentService,
private val activityPubUserService: ActivityPubUserService,
private val apUserService: APUserService,
private val userService: IUserService,
private val httpClient: HttpClient,
private val userQueryService: UserQueryService,
private val transaction: Transaction
) : ActivityPubReceiveFollowService {
) : APReceiveFollowService {
override suspend fun receiveFollow(follow: Follow): ActivityPubResponse {
// TODO: Verify HTTP Signature
jobQueueParentService.schedule(ReceiveFollowJob) {
@ -40,7 +40,7 @@ class ActivityPubReceiveFollowServiceImpl(
transaction.transaction {
val actor = props[ReceiveFollowJob.actor]
val targetActor = props[ReceiveFollowJob.targetActor]
val person = activityPubUserService.fetchPerson(actor, targetActor)
val person = apUserService.fetchPerson(actor, targetActor)
val follow = Config.configData.objectMapper.readValue<Follow>(props[ReceiveFollowJob.follow])
httpClient.postAp(
urlString = person.inbox ?: throw IllegalArgumentException("inbox is not found"),

View File

@ -1,7 +1,7 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.hideout.dto.SendFollowDto
interface ActivityPubSendFollowService {
interface APSendFollowService {
suspend fun sendFollow(sendFollowDto: SendFollowDto)
}

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ap.Follow
import dev.usbharu.hideout.domain.model.hideout.dto.SendFollowDto
@ -7,7 +7,7 @@ import io.ktor.client.*
import org.koin.core.annotation.Single
@Single
class ActivityPubSendFollowServiceImpl(private val httpClient: HttpClient) : ActivityPubSendFollowService {
class APSendFollowServiceImpl(private val httpClient: HttpClient) : APSendFollowService {
override suspend fun sendFollow(sendFollowDto: SendFollowDto) {
val follow = Follow(
name = "Follow",

View File

@ -1,10 +1,10 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.job.HideoutJob
import kjob.core.dsl.JobContextWithProps
interface ActivityPubService {
interface APService {
fun parseActivity(json: String): ActivityType
suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse?

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.module.kotlin.readValue
@ -14,15 +14,15 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
@Single
class ActivityPubServiceImpl(
private val activityPubReceiveFollowService: ActivityPubReceiveFollowService,
private val activityPubNoteService: ActivityPubNoteService,
private val activityPubUndoService: ActivityPubUndoService,
private val activityPubAcceptService: ActivityPubAcceptService,
private val activityPubCreateService: ActivityPubCreateService,
private val activityPubLikeService: ActivityPubLikeService,
private val activityPubReactionService: ActivityPubReactionService
) : ActivityPubService {
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
) : APService {
val logger: Logger = LoggerFactory.getLogger(this::class.java)
override fun parseActivity(json: String): ActivityType {
@ -44,17 +44,17 @@ class ActivityPubServiceImpl(
override suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse {
logger.debug("proccess activity: {}", type)
return when (type) {
ActivityType.Accept -> activityPubAcceptService.receiveAccept(configData.objectMapper.readValue(json))
ActivityType.Follow -> activityPubReceiveFollowService.receiveFollow(
ActivityType.Accept -> apAcceptService.receiveAccept(configData.objectMapper.readValue(json))
ActivityType.Follow -> apReceiveFollowService.receiveFollow(
configData.objectMapper.readValue(
json,
Follow::class.java
)
)
ActivityType.Create -> activityPubCreateService.receiveCreate(configData.objectMapper.readValue(json))
ActivityType.Like -> activityPubLikeService.receiveLike(configData.objectMapper.readValue(json))
ActivityType.Undo -> activityPubUndoService.receiveUndo(configData.objectMapper.readValue(json))
ActivityType.Create -> apCreateService.receiveCreate(configData.objectMapper.readValue(json))
ActivityType.Like -> apLikeService.receiveLike(configData.objectMapper.readValue(json))
ActivityType.Undo -> apUndoService.receiveUndo(configData.objectMapper.readValue(json))
else -> {
throw IllegalArgumentException("$type is not supported.")
@ -65,13 +65,13 @@ class ActivityPubServiceImpl(
override suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob) {
logger.debug("processActivity: ${hideoutJob.name}")
when (hideoutJob) {
ReceiveFollowJob -> activityPubReceiveFollowService.receiveFollowJob(
ReceiveFollowJob -> apReceiveFollowService.receiveFollowJob(
job.props as JobProps<ReceiveFollowJob>
)
DeliverPostJob -> activityPubNoteService.createNoteJob(job.props as JobProps<DeliverPostJob>)
DeliverReactionJob -> activityPubReactionService.reactionJob(job.props as JobProps<DeliverReactionJob>)
DeliverRemoveReactionJob -> activityPubReactionService.removeReactionJob(
DeliverPostJob -> apNoteService.createNoteJob(job.props as JobProps<DeliverPostJob>)
DeliverReactionJob -> apReactionService.reactionJob(job.props as JobProps<DeliverReactionJob>)
DeliverRemoveReactionJob -> apReactionService.removeReactionJob(
job.props as JobProps<DeliverRemoveReactionJob>
)
}

View File

@ -1,8 +1,8 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.ap.Undo
interface ActivityPubUndoService {
interface APUndoService {
suspend fun receiveUndo(undo: Undo): ActivityPubResponse
}

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.ActivityPubStringResponse
@ -12,12 +12,12 @@ import org.koin.core.annotation.Single
@Single
@Suppress("UnsafeCallOnNullableType")
class ActivityPubUndoServiceImpl(
class APUndoServiceImpl(
private val userService: IUserService,
private val activityPubUserService: ActivityPubUserService,
private val apUserService: APUserService,
private val userQueryService: UserQueryService,
private val transaction: Transaction
) : ActivityPubUndoService {
) : APUndoService {
override suspend fun receiveUndo(undo: Undo): ActivityPubResponse {
if (undo.actor == null) {
return ActivityPubStringResponse(HttpStatusCode.BadRequest, "actor is null")
@ -36,7 +36,7 @@ class ActivityPubUndoServiceImpl(
return ActivityPubStringResponse(HttpStatusCode.BadRequest, "object.object is null")
}
transaction.transaction {
activityPubUserService.fetchPerson(undo.actor!!, follow.`object`)
apUserService.fetchPerson(undo.actor!!, follow.`object`)
val follower = userQueryService.findByUrl(undo.actor!!)
val target = userQueryService.findByUrl(follow.`object`!!)
userService.unfollow(target.id, follower.id)

View File

@ -1,8 +1,8 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.domain.model.ap.Person
interface ActivityPubUserService {
interface APUserService {
suspend fun getPersonByName(name: String): Person
/**

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.config.Config
@ -19,13 +19,13 @@ import io.ktor.http.*
import org.koin.core.annotation.Single
@Single
class ActivityPubUserServiceImpl(
class APUserServiceImpl(
private val userService: IUserService,
private val httpClient: HttpClient,
private val userQueryService: UserQueryService,
private val transaction: Transaction
) :
ActivityPubUserService {
APUserService {
override suspend fun getPersonByName(name: String): Person {
val userEntity = transaction.transaction {

View File

@ -5,7 +5,7 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Post
import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.repository.IPostRepository
import dev.usbharu.hideout.repository.IUserRepository
import dev.usbharu.hideout.service.activitypub.ActivityPubNoteService
import dev.usbharu.hideout.service.ap.APNoteService
import org.koin.core.annotation.Single
import java.time.Instant
@ -13,7 +13,7 @@ import java.time.Instant
class PostServiceImpl(
private val postRepository: IPostRepository,
private val userRepository: IUserRepository,
private val activityPubNoteService: ActivityPubNoteService
private val apNoteService: APNoteService
) : IPostService {
override suspend fun createLocal(post: PostCreateDto): Post {
val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found")
@ -29,7 +29,7 @@ class PostServiceImpl(
repostId = null,
replyId = null
)
activityPubNoteService.createNote(createPost)
apNoteService.createNote(createPost)
return internalCreate(createPost)
}

View File

@ -3,13 +3,13 @@ package dev.usbharu.hideout.service.reaction
import dev.usbharu.hideout.domain.model.hideout.entity.Reaction
import dev.usbharu.hideout.query.ReactionQueryService
import dev.usbharu.hideout.repository.ReactionRepository
import dev.usbharu.hideout.service.activitypub.ActivityPubReactionService
import dev.usbharu.hideout.service.ap.APReactionService
import org.koin.core.annotation.Single
@Single
class ReactionServiceImpl(
private val reactionRepository: ReactionRepository,
private val activityPubReactionService: ActivityPubReactionService,
private val apReactionService: APReactionService,
private val reactionQueryService: ReactionQueryService
) : IReactionService {
override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) {
@ -27,7 +27,7 @@ class ReactionServiceImpl(
} else {
val reaction = Reaction(reactionRepository.generateId(), 0, postId, userId)
reactionRepository.save(reaction)
activityPubReactionService.reaction(reaction)
apReactionService.reaction(reaction)
}
}

View File

@ -9,7 +9,7 @@ import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.query.FollowerQueryService
import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.repository.IUserRepository
import dev.usbharu.hideout.service.activitypub.ActivityPubSendFollowService
import dev.usbharu.hideout.service.ap.APSendFollowService
import org.koin.core.annotation.Single
import java.time.Instant
@ -17,7 +17,7 @@ import java.time.Instant
class UserService(
private val userRepository: IUserRepository,
private val userAuthService: IUserAuthService,
private val activityPubSendFollowService: ActivityPubSendFollowService,
private val apSendFollowService: APSendFollowService,
private val userQueryService: UserQueryService,
private val followerQueryService: FollowerQueryService
) :
@ -77,7 +77,7 @@ class UserService(
if (userRepository.findFollowRequestsById(id, followerId)) {
// do-nothing
} else {
activityPubSendFollowService.sendFollow(SendFollowDto(follower, user))
apSendFollowService.sendFollow(SendFollowDto(follower, user))
}
false
}

View File

@ -3,8 +3,8 @@ package dev.usbharu.hideout.routing.activitypub
import dev.usbharu.hideout.exception.JsonParseException
import dev.usbharu.hideout.plugins.configureSerialization
import dev.usbharu.hideout.plugins.configureStatusPages
import dev.usbharu.hideout.service.activitypub.ActivityPubService
import dev.usbharu.hideout.service.activitypub.ActivityPubUserService
import dev.usbharu.hideout.service.ap.APService
import dev.usbharu.hideout.service.ap.APUserService
import dev.usbharu.hideout.service.auth.HttpSignatureVerifyService
import dev.usbharu.hideout.service.user.IUserService
import io.ktor.client.request.*
@ -44,16 +44,16 @@ class InboxRoutingKtTest {
val httpSignatureVerifyService = mock<HttpSignatureVerifyService> {
on { verify(any()) } doReturn true
}
val activityPubService = mock<ActivityPubService> {
val apService = mock<APService> {
on { parseActivity(any()) } doThrow JsonParseException()
}
mock<IUserService>()
mock<ActivityPubUserService>()
mock<APUserService>()
application {
configureStatusPages()
configureSerialization()
routing {
inbox(httpSignatureVerifyService, activityPubService)
inbox(httpSignatureVerifyService, apService)
}
}
client.post("/inbox").let {
@ -85,16 +85,16 @@ class InboxRoutingKtTest {
val httpSignatureVerifyService = mock<HttpSignatureVerifyService> {
on { verify(any()) } doReturn true
}
val activityPubService = mock<ActivityPubService> {
val apService = mock<APService> {
on { parseActivity(any()) } doThrow JsonParseException()
}
mock<IUserService>()
mock<ActivityPubUserService>()
mock<APUserService>()
application {
configureStatusPages()
configureSerialization()
routing {
inbox(httpSignatureVerifyService, activityPubService)
inbox(httpSignatureVerifyService, apService)
}
}
client.post("/users/test/inbox").let {

View File

@ -12,7 +12,7 @@ import dev.usbharu.hideout.domain.model.ap.Person
import dev.usbharu.hideout.domain.model.hideout.entity.User
import dev.usbharu.hideout.plugins.configureSerialization
import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.service.activitypub.ActivityPubUserService
import dev.usbharu.hideout.service.ap.APUserService
import dev.usbharu.hideout.util.HttpUtil.Activity
import dev.usbharu.hideout.util.HttpUtil.JsonLd
import io.ktor.client.request.*
@ -63,14 +63,14 @@ class UsersAPTest {
)
person.context = listOf("https://www.w3.org/ns/activitystreams")
val activityPubUserService = mock<ActivityPubUserService> {
val apUserService = mock<APUserService> {
onBlocking { getPersonByName(anyString()) } doReturn person
}
application {
configureSerialization()
routing {
usersAP(activityPubUserService, mock(), mock(), TestTransaction)
usersAP(apUserService, mock(), mock(), TestTransaction)
}
}
client.get("/users/test") {
@ -121,14 +121,14 @@ class UsersAPTest {
)
person.context = listOf("https://www.w3.org/ns/activitystreams")
val activityPubUserService = mock<ActivityPubUserService> {
val apUserService = mock<APUserService> {
onBlocking { getPersonByName(anyString()) } doReturn person
}
application {
configureSerialization()
routing {
usersAP(activityPubUserService, mock(), mock(), TestTransaction)
usersAP(apUserService, mock(), mock(), TestTransaction)
}
}
client.get("/users/test") {

View File

@ -1,7 +1,7 @@
@file:OptIn(ExperimentalCoroutinesApi::class)
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.config.ConfigData
@ -25,7 +25,7 @@ import utils.JsonObjectMapper
import java.time.Instant
import kotlin.test.assertEquals
class ActivityPubNoteServiceImplTest {
class APNoteServiceImplTest {
@Test
fun `createPost 新しい投稿`() = runTest {
val followers = listOf<User>(
@ -76,7 +76,7 @@ class ActivityPubNoteServiceImplTest {
}
val jobQueueParentService = mock<JobQueueParentService>()
val activityPubNoteService =
ActivityPubNoteServiceImpl(
APNoteServiceImpl(
mock(),
jobQueueParentService,
mock(),
@ -107,7 +107,7 @@ class ActivityPubNoteServiceImplTest {
respondOk()
}
)
val activityPubNoteService = ActivityPubNoteServiceImpl(
val activityPubNoteService = APNoteServiceImpl(
httpClient,
mock(),
mock(),

View File

@ -1,7 +1,7 @@
@file:OptIn(ExperimentalCoroutinesApi::class)
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
package dev.usbharu.hideout.service.activitypub
package dev.usbharu.hideout.service.ap
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.config.Config
@ -27,14 +27,14 @@ import utils.JsonObjectMapper
import utils.TestTransaction
import java.time.Instant
class ActivityPubReceiveFollowServiceImplTest {
class APReceiveFollowServiceImplTest {
@Test
fun `receiveFollow フォロー受付処理`() = runTest {
val jobQueueParentService = mock<JobQueueParentService> {
onBlocking { schedule(eq(ReceiveFollowJob), any()) } doReturn Unit
}
val activityPubFollowService =
ActivityPubReceiveFollowServiceImpl(jobQueueParentService, mock(), mock(), mock(), mock(), TestTransaction)
APReceiveFollowServiceImpl(jobQueueParentService, mock(), mock(), mock(), mock(), TestTransaction)
activityPubFollowService.receiveFollow(
Follow(
emptyList(),
@ -96,7 +96,7 @@ class ActivityPubReceiveFollowServiceImplTest {
)
)
val activityPubUserService = mock<ActivityPubUserService> {
val apUserService = mock<APUserService> {
onBlocking { fetchPerson(anyString(), any()) } doReturn person
}
val userQueryService = mock<UserQueryService> {
@ -132,9 +132,9 @@ class ActivityPubReceiveFollowServiceImplTest {
onBlocking { followRequest(any(), any()) } doReturn false
}
val activityPubFollowService =
ActivityPubReceiveFollowServiceImpl(
APReceiveFollowServiceImpl(
mock(),
activityPubUserService,
apUserService,
userService,
HttpClient(
MockEngine { httpRequestData ->