fix: フォローリクエストAPIを修正

This commit is contained in:
usbharu 2023-12-12 13:54:35 +09:00
parent ddaf630ed3
commit ef6c97b08c
7 changed files with 85 additions and 14 deletions

View File

@ -5,6 +5,7 @@ import dev.usbharu.hideout.core.domain.model.relationship.Relationships
import dev.usbharu.hideout.core.domain.model.relationship.toRelationships import dev.usbharu.hideout.core.domain.model.relationship.toRelationships
import dev.usbharu.hideout.core.query.RelationshipQueryService import dev.usbharu.hideout.core.query.RelationshipQueryService
import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.andWhere
import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.select
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@ -15,16 +16,28 @@ class RelationshipQueryServiceImpl : RelationshipQueryService {
.map { it.toRelationships() } .map { it.toRelationships() }
override suspend fun findByTargetIdAndFollowRequestAndIgnoreFollowRequest( override suspend fun findByTargetIdAndFollowRequestAndIgnoreFollowRequest(
maxId: Long?,
sinceId: Long?,
limit: Int,
targetId: Long, targetId: Long,
followRequest: Boolean, followRequest: Boolean,
ignoreFollowRequest: Boolean ignoreFollowRequest: Boolean
): List<Relationship> { ): List<Relationship> {
return Relationships val query = Relationships
.select { .select {
Relationships.targetActorId.eq(targetId) Relationships.targetActorId.eq(targetId)
.and(Relationships.followRequest.eq(followRequest)) .and(Relationships.followRequest.eq(followRequest))
.and(Relationships.ignoreFollowRequestFromTarget.eq(ignoreFollowRequest)) .and(Relationships.ignoreFollowRequestFromTarget.eq(ignoreFollowRequest))
} }.limit(limit)
.map { it.toRelationships() }
if (maxId != null) {
query.andWhere { Relationships.id greater maxId }
}
if (sinceId != null) {
query.andWhere { Relationships.id less sinceId }
}
return query.map { it.toRelationships() }
} }
} }

View File

@ -6,6 +6,9 @@ interface RelationshipQueryService {
suspend fun findByTargetIdAndFollowing(targetId: Long, following: Boolean): List<Relationship> suspend fun findByTargetIdAndFollowing(targetId: Long, following: Boolean): List<Relationship>
suspend fun findByTargetIdAndFollowRequestAndIgnoreFollowRequest( suspend fun findByTargetIdAndFollowRequestAndIgnoreFollowRequest(
maxId: Long?,
sinceId: Long?,
limit: Int,
targetId: Long, targetId: Long,
followRequest: Boolean, followRequest: Boolean,
ignoreFollowRequest: Boolean ignoreFollowRequest: Boolean

View File

@ -73,14 +73,17 @@ class RelationshipServiceImpl(
relationshipRepository.save(relationship) relationshipRepository.save(relationship)
val remoteUser = isRemoteUser(targetId) val remoteUser = isRemoteUser(targetId)
if (remoteUser != null) { if (remoteUser != null) {
val user = actorQueryService.findById(actorId) val user = actorQueryService.findById(actorId)
apSendFollowService.sendFollow(SendFollowDto(user, remoteUser)) apSendFollowService.sendFollow(SendFollowDto(user, remoteUser))
} else { } else {
// TODO: フォロー許可制ユーザーを実装したら消す val target = actorQueryService.findById(targetId)
acceptFollowRequest(targetId, actorId) if (target.locked.not()) {
acceptFollowRequest(targetId, actorId)
}
} }
logger.info("SUCCESS Follow Request userId: {} targetId: {}", actorId, targetId) logger.info("SUCCESS Follow Request userId: {} targetId: {}", actorId, targetId)

View File

@ -153,4 +153,36 @@ class MastodonAccountApiController(
return ResponseEntity.ok(removeFromFollowers) return ResponseEntity.ok(removeFromFollowers)
} }
override suspend fun apiV1FollowRequestsAccountIdAuthorizePost(accountId: String): ResponseEntity<Relationship> {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
val userid = principal.getClaim<String>("uid").toLong()
val acceptFollowRequest = accountApiService.acceptFollowRequest(userid, accountId.toLong())
return ResponseEntity.ok(acceptFollowRequest)
}
override suspend fun apiV1FollowRequestsAccountIdRejectPost(accountId: String): ResponseEntity<Relationship> {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
val userid = principal.getClaim<String>("uid").toLong()
val rejectFollowRequest = accountApiService.rejectFollowRequest(userid, accountId.toLong())
return ResponseEntity.ok(rejectFollowRequest)
}
override fun apiV1FollowRequestsGet(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 accountFlow =
accountApiService.followRequests(userid, maxId?.toLong(), sinceId?.toLong(), limit ?: 20, false)
.asFlow()
ResponseEntity.ok(accountFlow)
}
} }

View File

@ -50,7 +50,14 @@ interface AccountApiService {
suspend fun unfollow(userid: Long, target: Long): Relationship suspend fun unfollow(userid: Long, target: Long): Relationship
suspend fun removeFromFollowers(userid: Long, target: Long): Relationship suspend fun removeFromFollowers(userid: Long, target: Long): Relationship
suspend fun updateProfile(userid: Long, updateCredentials: UpdateCredentials?): Account suspend fun updateProfile(userid: Long, updateCredentials: UpdateCredentials?): Account
suspend fun followRequests(loginUser: Long): List<Account> suspend fun followRequests(
loginUser: Long,
maxId: Long?,
sinceId: Long?,
limit: Int = 20,
withIgnore: Boolean
): List<Account>
suspend fun acceptFollowRequest(loginUser: Long, target: Long): Relationship suspend fun acceptFollowRequest(loginUser: Long, target: Long): Relationship
suspend fun rejectFollowRequest(loginUser: Long, target: Long): Relationship suspend fun rejectFollowRequest(loginUser: Long, target: Long): Relationship
} }
@ -208,9 +215,22 @@ class AccountApiServiceImpl(
accountService.findById(userid) accountService.findById(userid)
} }
override suspend fun followRequests(loginUser: Long): List<Account> = transaction.transaction { override suspend fun followRequests(
loginUser: Long,
maxId: Long?,
sinceId: Long?,
limit: Int,
withIgnore: Boolean
): List<Account> = transaction.transaction {
val actorIdList = relationshipQueryService val actorIdList = relationshipQueryService
.findByTargetIdAndFollowRequestAndIgnoreFollowRequest(loginUser, true, true) .findByTargetIdAndFollowRequestAndIgnoreFollowRequest(
maxId = maxId,
sinceId = sinceId,
limit = limit,
targetId = loginUser,
followRequest = true,
ignoreFollowRequest = withIgnore
)
.map { it.actorId } .map { it.actorId }
return@transaction accountService.findByIds(actorIdList) return@transaction accountService.findByIds(actorIdList)

View File

@ -36,7 +36,7 @@ class AccountServiceImpl(
avatarStatic = "$userUrl/icon.jpg", avatarStatic = "$userUrl/icon.jpg",
header = "$userUrl/header.jpg", header = "$userUrl/header.jpg",
headerStatic = "$userUrl/header.jpg", headerStatic = "$userUrl/header.jpg",
locked = false, locked = findById.locked,
fields = emptyList(), fields = emptyList(),
emojis = emptyList(), emojis = emptyList(),
bot = false, bot = false,

View File

@ -579,7 +579,7 @@ paths:
/api/v1/follow_requests: /api/v1/follow_requests:
get: get:
tags: tags:
- accounts - account
security: security:
- OAuth2: - OAuth2:
- "read:follows" - "read:follows"
@ -587,12 +587,12 @@ paths:
- in: query - in: query
name: max_id name: max_id
schema: schema:
type: integer type: string
required: false required: false
- in: query - in: query
name: since_id name: since_id
schema: schema:
type: integer type: string
required: false required: false
- in: query - in: query
name: limit name: limit
@ -617,7 +617,7 @@ paths:
/api/v1/follow_requests/{account_id}/authorize: /api/v1/follow_requests/{account_id}/authorize:
post: post:
tags: tags:
- accounts - account
security: security:
- OAuth2: - OAuth2:
- "write:follows" - "write:follows"
@ -638,7 +638,7 @@ paths:
/api/v1/follow_requests/{account_id}/reject: /api/v1/follow_requests/{account_id}/reject:
post: post:
tags: tags:
- accounts - account
security: security:
- OAuth2: - OAuth2:
- "write:follows" - "write:follows"