feat: ユーザーのミュートのMastodon互換APIを実装

This commit is contained in:
usbharu 2024-01-25 22:53:43 +09:00
parent 128e536330
commit d204cfc37f
5 changed files with 158 additions and 0 deletions

View File

@ -42,4 +42,12 @@ interface RelationshipRepository {
followRequest: Boolean,
ignoreFollowRequest: Boolean
): List<Relationship>
suspend fun findByActorIdAntMutingAndMaxIdAndSinceId(
actorId: Long,
muting: Boolean,
maxId: Long?,
sinceId: Long?,
limit: Int
): List<Relationship>
}

View File

@ -97,6 +97,28 @@ class RelationshipRepositoryImpl : RelationshipRepository, AbstractRepository()
return@query query.map { it.toRelationships() }
}
override suspend fun findByActorIdAntMutingAndMaxIdAndSinceId(
actorId: Long,
muting: Boolean,
maxId: Long?,
sinceId: Long?,
limit: Int
): List<Relationship> = query {
val query = Relationships.select {
Relationships.actorId.eq(actorId).and(Relationships.muting.eq(muting))
}.limit(limit)
if (maxId != null) {
query.andWhere { Relationships.id lessEq maxId }
}
if (sinceId != null) {
query.andWhere { Relationships.id greaterEq sinceId }
}
return@query query.map { it.toRelationships() }
}
companion object {
private val logger = LoggerFactory.getLogger(RelationshipRepositoryImpl::class.java)
}

View File

@ -186,4 +186,36 @@ class MastodonAccountApiController(
.asFlow()
ResponseEntity.ok(accountFlow)
}
override suspend fun apiV1AccountsIdMutePost(id: String): ResponseEntity<Relationship> {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
val userid = principal.getClaim<String>("uid").toLong()
val mute = accountApiService.mute(userid, id.toLong())
return ResponseEntity.ok(mute)
}
override suspend fun apiV1AccountsIdUnmutePost(id: String): ResponseEntity<Relationship> {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
val userid = principal.getClaim<String>("uid").toLong()
val unmute = accountApiService.unmute(userid, id.toLong())
return ResponseEntity.ok(unmute)
}
override fun apiV1MutesGet(maxId: String?, sinceId: String?, limit: Int?): ResponseEntity<Flow<Account>> =
runBlocking {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
val userid = principal.getClaim<String>("uid").toLong()
val unmute =
accountApiService.mutesAccount(userid, maxId?.toLong(), sinceId?.toLong(), limit ?: 20).asFlow()
return@runBlocking ResponseEntity.ok(unmute)
}
}

View File

@ -60,6 +60,9 @@ interface AccountApiService {
suspend fun acceptFollowRequest(loginUser: Long, target: Long): Relationship
suspend fun rejectFollowRequest(loginUser: Long, target: Long): Relationship
suspend fun mute(userid: Long, target: Long): Relationship
suspend fun unmute(userid: Long, target: Long): Relationship
suspend fun mutesAccount(userid: Long, maxId: Long?, sinceId: Long?, limit: Int): List<Account>
}
@Service
@ -245,6 +248,25 @@ class AccountApiServiceImpl(
return@transaction fetchRelationship(loginUser, target)
}
override suspend fun mute(userid: Long, target: Long): Relationship = transaction.transaction {
relationshipService.mute(userid, target)
return@transaction fetchRelationship(userid, target)
}
override suspend fun unmute(userid: Long, target: Long): Relationship = transaction.transaction {
relationshipService.mute(userid, target)
return@transaction fetchRelationship(userid, target)
}
override suspend fun mutesAccount(userid: Long, maxId: Long?, sinceId: Long?, limit: Int): List<Account> {
val mutedAccounts =
relationshipRepository.findByActorIdAntMutingAndMaxIdAndSinceId(userid, true, maxId, sinceId, limit)
return accountService.findByIds(mutedAccounts.map { it.targetActorId })
}
private fun from(account: Account): CredentialAccount {
return CredentialAccount(
id = account.id,

View File

@ -464,6 +464,80 @@ paths:
schema:
$ref: "#/components/schemas/Relationship"
/api/v1/accounts/{id}/mute:
post:
tags:
- account
security:
- OAuth2:
- "write:mutes"
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}/unmute:
post:
tags:
- account
security:
- OAuth2:
- "write:mutes"
parameters:
- in: path
name: id
required: true
schema:
type: string
responses:
200:
description: 成功
content:
application/json:
schema:
$ref: "#/components/schemas/Relationship"
/api/v1/mutes:
get:
tags:
- account
security:
- OAuth2:
- "read:mutes"
parameters:
- in: query
name: max_id
required: false
schema:
type: string
- in: query
name: since_id
required: false
schema:
type: string
- in: query
name: limit
schema:
type: integer
required: false
responses:
200:
description: 成功
content:
application/json:
schema:
items:
$ref: "#/components/schemas/Account"
/api/v1/accounts/{id}/statuses:
get:
tags: