From 84b038f4724e5465de614bb81e08cb567461b6d3 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 17 May 2024 21:53:30 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=87=8D=E7=B5=90=E3=81=95=E3=82=8C?= =?UTF-8?q?=E3=81=9F=E3=83=A6=E3=83=BC=E3=82=B6=E3=83=BC=E3=81=AE=E5=BE=A9?= =?UTF-8?q?=E6=97=A7=E3=81=AB=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/objects/user/APUserService.kt | 29 +++++++++----- .../hideout/core/service/user/UserService.kt | 2 +- .../core/service/user/UserServiceImpl.kt | 4 +- .../hideout/worker/UpdateActorWorker.kt | 40 +++++++++++++++++++ 4 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 hideout-worker/src/main/kotlin/dev/usbharu/hideout/worker/UpdateActorWorker.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/user/APUserService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/user/APUserService.kt index 8f1aa04f..336a4de5 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/user/APUserService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/user/APUserService.kt @@ -40,9 +40,13 @@ interface APUserService { * @param targetActor 署名するユーザー * @return */ - suspend fun fetchPerson(url: String, targetActor: String? = null): Person + suspend fun fetchPerson(url: String, targetActor: String? = null, idOverride: Long? = null): Person - suspend fun fetchPersonWithEntity(url: String, targetActor: String? = null): Pair + suspend fun fetchPersonWithEntity( + url: String, + targetActor: String? = null, + idOverride: Long? = null, + ): Pair } @Service @@ -51,7 +55,7 @@ class APUserServiceImpl( private val transaction: Transaction, private val applicationConfig: ApplicationConfig, private val apResourceResolveService: APResourceResolveService, - private val actorRepository: ActorRepository + private val actorRepository: ActorRepository, ) : APUserService { @@ -88,13 +92,17 @@ class APUserServiceImpl( ) } - override suspend fun fetchPerson(url: String, targetActor: String?): Person = - fetchPersonWithEntity(url, targetActor).first + override suspend fun fetchPerson(url: String, targetActor: String?, idOverride: Long?): Person = + fetchPersonWithEntity(url, targetActor, idOverride).first - override suspend fun fetchPersonWithEntity(url: String, targetActor: String?): Pair { + override suspend fun fetchPersonWithEntity( + url: String, + targetActor: String?, + idOverride: Long?, + ): Pair { val userEntity = actorRepository.findByUrl(url) - if (userEntity != null) { + if (userEntity != null && idOverride == null) { return entityToPerson(userEntity, userEntity.url) to userEntity } @@ -104,7 +112,7 @@ class APUserServiceImpl( val actor = actorRepository.findByUrlWithLock(id) - if (actor != null) { + if (actor != null && idOverride == null) { return person to actor } @@ -123,13 +131,14 @@ class APUserServiceImpl( followers = person.followers, sharedInbox = person.endpoints["sharedInbox"], locked = person.manuallyApprovesFollowers - ) + ), + idOverride ) } private fun entityToPerson( actorEntity: Actor, - id: String + id: String, ) = Person( type = emptyList(), name = actorEntity.name, diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt index 3569d4f6..f1d42b42 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserService.kt @@ -26,7 +26,7 @@ interface UserService { suspend fun createLocalUser(user: UserCreateDto): Actor - suspend fun createRemoteUser(user: RemoteUserCreateDto): Actor + suspend fun createRemoteUser(user: RemoteUserCreateDto, idOverride: Long? = null): Actor suspend fun updateUser(userId: Long, updateUserDto: UpdateUserDto) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt index d1e09216..f69e9510 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt @@ -93,7 +93,7 @@ class UserServiceImpl( return save } - override suspend fun createRemoteUser(user: RemoteUserCreateDto): Actor { + override suspend fun createRemoteUser(user: RemoteUserCreateDto, idOverride: Long?): Actor { logger.info("START Create New remote user. name: {} url: {}", user.name, user.url) val deletedActor = deletedActorRepository.findByNameAndDomain(user.name, user.domain) @@ -107,7 +107,7 @@ class UserServiceImpl( val nextId = actorRepository.nextId() val userEntity = actorBuilder.of( - id = nextId, + id = idOverride ?: nextId, name = user.name, domain = user.domain, screenName = user.screenName, diff --git a/hideout-worker/src/main/kotlin/dev/usbharu/hideout/worker/UpdateActorWorker.kt b/hideout-worker/src/main/kotlin/dev/usbharu/hideout/worker/UpdateActorWorker.kt new file mode 100644 index 00000000..6021db6f --- /dev/null +++ b/hideout-worker/src/main/kotlin/dev/usbharu/hideout/worker/UpdateActorWorker.kt @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2024 usbharu + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.usbharu.hideout.worker + +import dev.usbharu.hideout.activitypub.service.objects.user.APUserService +import dev.usbharu.hideout.application.external.Transaction +import dev.usbharu.hideout.core.external.job.UpdateActorTask +import dev.usbharu.hideout.core.external.job.UpdateActorTaskDef +import dev.usbharu.owl.consumer.AbstractTaskRunner +import dev.usbharu.owl.consumer.TaskRequest +import dev.usbharu.owl.consumer.TaskResult +import org.springframework.stereotype.Component + +@Component +class UpdateActorWorker( + private val transaction: Transaction, + private val apUserService: APUserService, +) : AbstractTaskRunner(UpdateActorTaskDef) { + override suspend fun typedRun(typedParam: UpdateActorTask, taskRequest: TaskRequest): TaskResult { + transaction.transaction { + apUserService.fetchPerson(typedParam.apId, idOverride = typedParam.id) + } + + return TaskResult.ok() + } +} \ No newline at end of file