4 Commits

9 changed files with 166 additions and 132 deletions
@@ -26,10 +26,8 @@ class GetActorDetailApplicationService(
actorRepository.findById(ActorId(command.id))
?: throw IllegalArgumentException("Actor ${command.id} not found.")
} else if (command.actorName != null) {
val host = if (command.actorName.host.isEmpty()) {
val host = command.actorName.host.ifEmpty {
applicationConfig.url.host
} else {
command.actorName.host
}
actorRepository.findByNameAndDomain(command.actorName.userpart, host)
?: throw IllegalArgumentException("Actor ${command.actorName} not found.")
@@ -1,8 +0,0 @@
package dev.usbharu.hideout.core.application.actor
import org.springframework.stereotype.Service
@Service
interface SetAlsoKnownAsLocalActorApplicationService {
suspend fun setAlsoKnownAs(actorId: Long, alsoKnownAs: List<Long>)
}
@@ -1,3 +0,0 @@
package dev.usbharu.hideout.core.application.actor
data class SuspendLocalActor(val actorId: Long)
@@ -1,44 +0,0 @@
/*
* 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.core.application.actor
import dev.usbharu.hideout.core.application.shared.LocalUserAbstractApplicationService
import dev.usbharu.hideout.core.application.shared.Transaction
import dev.usbharu.hideout.core.domain.model.actor.ActorId
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.domain.model.support.principal.LocalUser
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class SuspendLocalActorApplicationService(
transaction: Transaction,
private val actorRepository: ActorRepository,
) : LocalUserAbstractApplicationService<SuspendLocalActor, Unit>(transaction, logger) {
override suspend fun internalExecute(command: SuspendLocalActor, principal: LocalUser) {
val id = ActorId(command.actorId)
val actor =
actorRepository.findById(id) ?: throw IllegalArgumentException("Actor ${command.actorId} not found.")
actor.suspend = true
actorRepository.save(actor)
}
companion object {
private val logger = LoggerFactory.getLogger(SuspendLocalActorApplicationService::class.java)
}
}
@@ -1,3 +0,0 @@
package dev.usbharu.hideout.core.application.actor
data class UnsuspendLocalActor(val actorId: Long)
@@ -1,44 +0,0 @@
/*
* 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.core.application.actor
import dev.usbharu.hideout.core.application.shared.LocalUserAbstractApplicationService
import dev.usbharu.hideout.core.application.shared.Transaction
import dev.usbharu.hideout.core.domain.model.actor.ActorId
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.domain.model.support.principal.LocalUser
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class UnsuspendLocalActorApplicationService(
transaction: Transaction,
private val actorRepository: ActorRepository,
) : LocalUserAbstractApplicationService<UnsuspendLocalActor, Unit>(transaction, logger) {
override suspend fun internalExecute(command: UnsuspendLocalActor, principal: LocalUser) {
val findById = actorRepository.findById(ActorId(command.actorId))
?: throw IllegalArgumentException("Actor ${command.actorId} not found.")
findById.suspend = false
actorRepository.save(findById)
}
companion object {
private val logger = LoggerFactory.getLogger(UnsuspendLocalActorApplicationService::class.java)
}
}
@@ -1,24 +0,0 @@
/*
* 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.core.domain.model.actor
enum class Role {
LOCAL,
MODERATOR,
ADMINISTRATOR,
REMOTE
}
@@ -0,0 +1,160 @@
package dev.usbharu.hideout.core.application.actor
import dev.usbharu.hideout.core.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.actor.ActorId
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.domain.model.actor.TestActorFactory
import dev.usbharu.hideout.core.domain.model.media.*
import dev.usbharu.hideout.core.domain.model.support.acct.Acct
import dev.usbharu.hideout.core.domain.model.support.principal.Anonymous
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Spy
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.*
import utils.TestTransaction
import java.net.URI
@ExtendWith(MockitoExtension::class)
class GetActorDetailApplicationServiceTest {
@InjectMocks
lateinit var service: GetActorDetailApplicationService
@Mock
lateinit var actorRepository: ActorRepository
@Mock
lateinit var mediaRepository: MediaRepository
@Spy
val applicationConfig = ApplicationConfig(URI.create("http://localhost:8081").toURL())
@Spy
val transaction = TestTransaction
@Test
fun idとname両方nullならエラー() = runTest {
assertThrows<IllegalArgumentException> {
service.execute(GetActorDetail(null, null), Anonymous)
}
}
@Test
fun idがnullじゃない場合idから取得() = runTest {
val actor = TestActorFactory.create(id = 1)
whenever(actorRepository.findById(ActorId(1))).doReturn(actor)
val actual = service.execute(GetActorDetail(null, 1), Anonymous)
assertEquals(actual, ActorDetail.of(actor, null, null))
}
@Test
fun idが存在しないとエラー() = runTest {
assertThrows<IllegalArgumentException> {
service.execute(GetActorDetail(null, 2), Anonymous)
}
}
@Test
fun idがnullでacctがnullじゃない場合acctから取得() = runTest {
val actor = TestActorFactory.create(actorName = "test", domain = "example.com")
whenever(actorRepository.findByNameAndDomain("test", "example.com")).thenReturn(actor)
val actual = service.execute(GetActorDetail(Acct("test", "example.com"), null), Anonymous)
assertEquals(actual, ActorDetail.of(actor, null, null))
}
@Test
fun acctのhostが空ならローカルのhostが使われる() = runTest {
val actor = TestActorFactory.create(actorName = "test", domain = "localhost")
whenever(actorRepository.findByNameAndDomain("test", "localhost")).thenReturn(actor)
val actual = service.execute(GetActorDetail(Acct("test", ""), null), Anonymous)
assertEquals(actual, ActorDetail.of(actor, null, null))
}
@Test
fun acctが存在しないとエラー() = runTest {
assertThrows<IllegalArgumentException> {
service.execute(GetActorDetail(Acct("test", "example.com"), null), Anonymous)
}
}
@Test
fun idとacctがnullじゃない場合idが優先される() = runTest {
val actor = TestActorFactory.create(id = 1)
whenever(actorRepository.findById(ActorId(1))).doReturn(actor)
val actual = service.execute(GetActorDetail(null, 1), Anonymous)
assertEquals(actual, ActorDetail.of(actor, null, null))
verify(actorRepository, never()).findByNameAndDomain(any(), any())
}
@Test
fun iconがnullじゃない時取得する() = runTest {
val actor = TestActorFactory.create(id = 1, icon = 1)
whenever(actorRepository.findById(ActorId(1))).doReturn(actor)
whenever(mediaRepository.findById(MediaId(1))).doReturn(
Media(
id = MediaId(1),
name = MediaName(""),
url = URI.create("http://example.com"),
remoteUrl = null,
thumbnailUrl = null,
type = FileType.Image,
mimeType = MimeType("image", "jpeg", FileType.Image),
blurHash = null,
description = null,
actorId = ActorId(1)
)
)
val actual = service.execute(GetActorDetail(null, 1), Anonymous)
assertEquals(actual, ActorDetail.of(actor, URI.create("http://example.com"), null))
}
@Test
fun bannerがnullじゃない時取得する() = runTest {
val actor = TestActorFactory.create(id = 1, banner = 1)
whenever(actorRepository.findById(ActorId(1))).doReturn(actor)
whenever(mediaRepository.findById(MediaId(1))).doReturn(
Media(
id = MediaId(1),
name = MediaName(""),
url = URI.create("http://example.com"),
remoteUrl = null,
thumbnailUrl = null,
type = FileType.Image,
mimeType = MimeType("image", "jpeg", FileType.Image),
blurHash = null,
description = null,
actorId = ActorId(1)
)
)
val actual = service.execute(GetActorDetail(null, 1), Anonymous)
assertEquals(actual, ActorDetail.of(actor, null, URI.create("http://example.com")))
}
@Test
fun iconとbannerが見つからなかった場合null() = runTest {
val actor = TestActorFactory.create(id = 1, icon = 1, banner = 1)
whenever(actorRepository.findById(ActorId(1))).doReturn(actor)
val actual = service.execute(GetActorDetail(null, 1), Anonymous)
assertEquals(actual, ActorDetail.of(actor, null, null))
}
}
@@ -2,6 +2,7 @@ package dev.usbharu.hideout.core.domain.model.actor
import dev.usbharu.hideout.core.domain.model.emoji.EmojiId
import dev.usbharu.hideout.core.domain.model.instance.InstanceId
import dev.usbharu.hideout.core.domain.model.media.MediaId
import dev.usbharu.hideout.core.domain.model.support.domain.Domain
import dev.usbharu.hideout.core.infrastructure.other.TwitterSnowflakeIdGenerateService
import kotlinx.coroutines.runBlocking
@@ -37,7 +38,8 @@ object TestActorFactory {
moveTo: Long? = null,
emojiIds: Set<EmojiId> = emptySet(),
deleted: Boolean = false,
roles: Set<Role> = emptySet(),
icon: Long? = null,
banner: Long? = null
): Actor {
return runBlocking {
Actor(
@@ -66,8 +68,8 @@ object TestActorFactory {
moveTo = moveTo?.let { ActorId(it) },
emojiIds = emojiIds,
deleted = deleted,
icon = null,
banner = null,
icon = icon?.let { MediaId(it) },
banner = banner?.let { MediaId(it) },
)
}