feat: ブロックのMastodon APIを追加

This commit is contained in:
usbharu 2023-12-08 12:46:52 +09:00
parent 7e93db6c7e
commit 2c1da54a77
5 changed files with 71 additions and 4 deletions

View File

@ -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"))

View File

@ -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(

View File

@ -103,4 +103,14 @@ class MastodonAccountApiController(
.asFlow()
)
}
override suspend fun apiV1AccountsIdBlockPost(id: String): ResponseEntity<Relationship> {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
val userid = principal.getClaim<String>("uid").toLong()
val block = accountApiService.block(userid, id.toLong())
return ResponseEntity.ok(block)
}
}

View File

@ -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<Long>, withSuspended: Boolean): List<Relationship>
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,

View File

@ -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: