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

View File

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

View File

@ -153,4 +153,36 @@ class MastodonAccountApiController(
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 removeFromFollowers(userid: Long, target: Long): Relationship
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 rejectFollowRequest(loginUser: Long, target: Long): Relationship
}
@ -208,9 +215,22 @@ class AccountApiServiceImpl(
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
.findByTargetIdAndFollowRequestAndIgnoreFollowRequest(loginUser, true, true)
.findByTargetIdAndFollowRequestAndIgnoreFollowRequest(
maxId = maxId,
sinceId = sinceId,
limit = limit,
targetId = loginUser,
followRequest = true,
ignoreFollowRequest = withIgnore
)
.map { it.actorId }
return@transaction accountService.findByIds(actorIdList)

View File

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

View File

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