diff --git a/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt b/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt index eb19802f..e42ad9b9 100644 --- a/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt @@ -197,6 +197,7 @@ class SecurityConfig { authorize(GET, "/api/v1/accounts/*", permitAll) authorize(GET, "/api/v1/accounts/*/statuses", permitAll) authorize(POST, "/api/v1/accounts/*/follow", hasAnyScope("write", "write:follows")) + authorize(POST, "/api/v1/accounts/*/block", hasAnyScope("write", "write:blocks")) authorize(POST, "/api/v1/media", hasAnyScope("write", "write:media")) authorize(POST, "/api/v1/statuses", hasAnyScope("write", "write:statuses")) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/block/BlockServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/block/BlockServiceImpl.kt index bc0eef96..77209855 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/block/BlockServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/block/BlockServiceImpl.kt @@ -28,7 +28,7 @@ class BlockServiceImpl( private val applicationConfig: ApplicationConfig ) : BlockService { - override suspend fun block(userId: Long, target: Long): Unit = transaction.transaction { + override suspend fun block(userId: Long, target: Long) { logger.debug("Block userId: {} → target: {}", userId, target) blockRepository.save(Block(userId, target)) if (followerQueryService.alreadyFollow(userId, target)) { @@ -39,13 +39,13 @@ class BlockServiceImpl( val user = userRepository.findById(userId) ?: throw IllegalStateException("Block user was not found.") if (user.domain == applicationConfig.url.host) { - return@transaction + return } val target = userRepository.findById(target) ?: throw IllegalStateException("Block use was not found.") if (target.domain == applicationConfig.url.host) { - return@transaction + return } val blockJobParam = DeliverBlockJobParam( diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt index a7f3741c..0ebed08e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt @@ -103,4 +103,14 @@ class MastodonAccountApiController( .asFlow() ) } + + override suspend fun apiV1AccountsIdBlockPost(id: String): ResponseEntity { + val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt + + val userid = principal.getClaim("uid").toLong() + + val block = accountApiService.block(userid, id.toLong()) + + return ResponseEntity.ok(block) + } } diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/service/account/AccountApiService.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/service/account/AccountApiService.kt index a3315107..2e9f76f4 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/service/account/AccountApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/service/account/AccountApiService.kt @@ -1,8 +1,11 @@ package dev.usbharu.hideout.mastodon.service.account import dev.usbharu.hideout.application.external.Transaction +import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException +import dev.usbharu.hideout.core.domain.model.block.BlockRepository import dev.usbharu.hideout.core.domain.model.user.UserRepository import dev.usbharu.hideout.core.query.FollowerQueryService +import dev.usbharu.hideout.core.service.block.BlockService import dev.usbharu.hideout.core.service.user.UserCreateDto import dev.usbharu.hideout.core.service.user.UserService import dev.usbharu.hideout.domain.mastodon.model.generated.* @@ -32,6 +35,7 @@ interface AccountApiService { suspend fun follow(userid: Long, followeeId: Long): Relationship suspend fun account(id: Long): Account suspend fun relationships(userid: Long, id: List, withSuspended: Boolean): List + suspend fun block(userid: Long, target: Long): Relationship } @Service @@ -41,7 +45,9 @@ class AccountApiServiceImpl( private val userService: UserService, private val followerQueryService: FollowerQueryService, private val userRepository: UserRepository, - private val statusQueryService: StatusQueryService + private val statusQueryService: StatusQueryService, + private val blockService: BlockService, + private val blockRepository: BlockRepository ) : AccountApiService { override suspend fun accountsStatuses( @@ -160,6 +166,35 @@ class AccountApiServiceImpl( } } + override suspend fun block(userid: Long, target: Long): Relationship = transaction.transaction { + blockService.block(userid, target) + + val blocked = try { + blockRepository.findByUserIdAndTarget(target, userid) + true + } catch (e: FailedToGetResourcesException) { + false + } + + val requested = userRepository.findFollowRequestsById(target, userid) + + Relationship( + target.toString(), + false, + true, + false, + false, + true, + blocked, + false, + false, + requested, + false, + false, + "" + ) + } + private fun from(account: Account): CredentialAccount { return CredentialAccount( id = account.id, diff --git a/src/main/resources/openapi/mastodon.yaml b/src/main/resources/openapi/mastodon.yaml index 3049c1d9..592726ae 100644 --- a/src/main/resources/openapi/mastodon.yaml +++ b/src/main/resources/openapi/mastodon.yaml @@ -287,6 +287,27 @@ paths: schema: $ref: "#/components/schemas/Relationship" + /api/v1/accounts/{id}/block: + post: + tags: + - account + security: + - OAuth2: + - "write:blocks" + parameters: + - in: path + name: id + required: true + schema: + type: string + responses: + 200: + description: 成功 + content: + application/json: + schema: + $ref: "#/components/schemas/Relationship" + /api/v1/accounts/{id}/statuses: get: tags: