feat: フォローリクエストAPIを追加

This commit is contained in:
usbharu 2023-12-12 13:02:19 +09:00
parent 4e8cffdbe2
commit ddaf630ed3
11 changed files with 196 additions and 59 deletions

View File

@ -45,6 +45,6 @@ Ja15+ZWbOA4vJA9pOh3x4XM=
'http://localhost/users/test-user#pubkey', 'http://localhost/users/test-user/following',
'http://localhost/users/test-users/followers', null, false);
insert into user_details (actor_id, password, auto_accept_follow_request, auto_accept_followee_follow_request)
insert into user_details (actor_id, password, auto_accept_followee_follow_request)
values ( 1730415786666758144
, '$2a$10$/mWC/n7nC7X3l9qCEOKnredxne2zewoqEsJWTOdlKfg2zXKJ0F9Em', true, true)
, '$2a$10$/mWC/n7nC7X3l9qCEOKnredxne2zewoqEsJWTOdlKfg2zXKJ0F9Em', true)

View File

@ -9,7 +9,7 @@ package dev.usbharu.hideout.core.domain.model.relationship
* @property blocking ブロックしているか
* @property muting ミュートしているか
* @property followRequest フォローリクエストを送っているか
* @property ignoreFollowRequestFromTarget フォローリクエストを無視しているか
* @property ignoreFollowRequestToTarget フォローリクエストを無視しているか
*/
data class Relationship(
val actorId: Long,
@ -18,5 +18,5 @@ data class Relationship(
val blocking: Boolean,
val muting: Boolean,
val followRequest: Boolean,
val ignoreFollowRequestFromTarget: Boolean
val ignoreFollowRequestToTarget: Boolean
)

View File

@ -25,7 +25,7 @@ class RelationshipRepositoryImpl : RelationshipRepository {
it[blocking] = relationship.blocking
it[muting] = relationship.muting
it[followRequest] = relationship.followRequest
it[ignoreFollowRequestFromTarget] = relationship.ignoreFollowRequestFromTarget
it[ignoreFollowRequestFromTarget] = relationship.ignoreFollowRequestToTarget
}
} else {
Relationships
@ -37,7 +37,7 @@ class RelationshipRepositoryImpl : RelationshipRepository {
it[blocking] = relationship.blocking
it[muting] = relationship.muting
it[followRequest] = relationship.followRequest
it[ignoreFollowRequestFromTarget] = relationship.ignoreFollowRequestFromTarget
it[ignoreFollowRequestFromTarget] = relationship.ignoreFollowRequestToTarget
}
}
return relationship
@ -66,7 +66,7 @@ fun ResultRow.toRelationships(): Relationship = Relationship(
blocking = this[Relationships.blocking],
muting = this[Relationships.muting],
followRequest = this[Relationships.followRequest],
ignoreFollowRequestFromTarget = this[Relationships.ignoreFollowRequestFromTarget]
ignoreFollowRequestToTarget = this[Relationships.ignoreFollowRequestFromTarget]
)
object Relationships : LongIdTable("relationships") {

View File

@ -13,4 +13,18 @@ class RelationshipQueryServiceImpl : RelationshipQueryService {
override suspend fun findByTargetIdAndFollowing(targetId: Long, following: Boolean): List<Relationship> =
Relationships.select { Relationships.targetActorId eq targetId and (Relationships.following eq following) }
.map { it.toRelationships() }
override suspend fun findByTargetIdAndFollowRequestAndIgnoreFollowRequest(
targetId: Long,
followRequest: Boolean,
ignoreFollowRequest: Boolean
): List<Relationship> {
return Relationships
.select {
Relationships.targetActorId.eq(targetId)
.and(Relationships.followRequest.eq(followRequest))
.and(Relationships.ignoreFollowRequestFromTarget.eq(ignoreFollowRequest))
}
.map { it.toRelationships() }
}
}

View File

@ -5,4 +5,9 @@ import dev.usbharu.hideout.core.domain.model.relationship.Relationship
interface RelationshipQueryService {
suspend fun findByTargetIdAndFollowing(targetId: Long, following: Boolean): List<Relationship>
suspend fun findByTargetIdAndFollowRequestAndIgnoreFollowRequest(
targetId: Long,
followRequest: Boolean,
ignoreFollowRequest: Boolean
): List<Relationship>
}

View File

@ -38,7 +38,7 @@ class RelationshipServiceImpl(
blocking = false,
muting = false,
followRequest = true,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
val inverseRelationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, actorId) ?: Relationship(
@ -48,7 +48,7 @@ class RelationshipServiceImpl(
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
if (inverseRelationship.blocking) {
@ -60,7 +60,7 @@ class RelationshipServiceImpl(
logger.debug("FAILED Blocking user. userId: {} targetId: {}", actorId, targetId)
return
}
if (inverseRelationship.ignoreFollowRequestFromTarget) {
if (relationship.ignoreFollowRequestToTarget) {
logger.debug("SUCCESS Ignore Follow Request. userId: {} targetId: {}", actorId, targetId)
return
}
@ -95,7 +95,7 @@ class RelationshipServiceImpl(
blocking = true,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
val inverseRelationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, actorId)
@ -126,7 +126,7 @@ class RelationshipServiceImpl(
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
if (relationship == null) {
@ -191,16 +191,16 @@ class RelationshipServiceImpl(
}
override suspend fun ignoreFollowRequest(actorId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(actorId, targetId)
?.copy(ignoreFollowRequestFromTarget = true)
val relationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, actorId)
?.copy(ignoreFollowRequestToTarget = true)
?: Relationship(
actorId = actorId,
targetActorId = targetId,
actorId = targetId,
targetActorId = actorId,
following = false,
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = true
ignoreFollowRequestToTarget = true
)
relationshipRepository.save(relationship)
@ -263,7 +263,7 @@ class RelationshipServiceImpl(
blocking = false,
muting = true,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
relationshipRepository.save(relationship)

View File

@ -2,6 +2,7 @@ package dev.usbharu.hideout.mastodon.service.account
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.domain.model.relationship.RelationshipRepository
import dev.usbharu.hideout.core.query.RelationshipQueryService
import dev.usbharu.hideout.core.service.media.MediaService
import dev.usbharu.hideout.core.service.relationship.RelationshipService
import dev.usbharu.hideout.core.service.user.UpdateUserDto
@ -49,6 +50,9 @@ 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 acceptFollowRequest(loginUser: Long, target: Long): Relationship
suspend fun rejectFollowRequest(loginUser: Long, target: Long): Relationship
}
@Service
@ -59,7 +63,8 @@ class AccountApiServiceImpl(
private val statusQueryService: StatusQueryService,
private val relationshipService: RelationshipService,
private val relationshipRepository: RelationshipRepository,
private val mediaService: MediaService
private val mediaService: MediaService,
private val relationshipQueryService: RelationshipQueryService
) :
AccountApiService {
override suspend fun accountsStatuses(
@ -203,6 +208,26 @@ class AccountApiServiceImpl(
accountService.findById(userid)
}
override suspend fun followRequests(loginUser: Long): List<Account> = transaction.transaction {
val actorIdList = relationshipQueryService
.findByTargetIdAndFollowRequestAndIgnoreFollowRequest(loginUser, true, true)
.map { it.actorId }
return@transaction accountService.findByIds(actorIdList)
}
override suspend fun acceptFollowRequest(loginUser: Long, target: Long): Relationship = transaction.transaction {
relationshipService.acceptFollowRequest(loginUser, target)
return@transaction fetchRelationship(loginUser, target)
}
override suspend fun rejectFollowRequest(loginUser: Long, target: Long): Relationship = transaction.transaction {
relationshipService.rejectFollowRequest(loginUser, target)
return@transaction fetchRelationship(loginUser, target)
}
private fun from(account: Account): CredentialAccount {
return CredentialAccount(
id = account.id,
@ -250,7 +275,7 @@ class AccountApiServiceImpl(
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
val inverseRelationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, userid)
@ -261,7 +286,7 @@ class AccountApiServiceImpl(
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
return Relationship(

View File

@ -1,6 +1,7 @@
package dev.usbharu.hideout.mastodon.service.account
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.domain.mastodon.model.generated.Account
import org.springframework.stereotype.Service
@ -8,6 +9,7 @@ import org.springframework.stereotype.Service
@Service
interface AccountService {
suspend fun findById(id: Long): Account
suspend fun findByIds(ids: List<Long>): List<Account>
}
@Service
@ -17,6 +19,10 @@ class AccountServiceImpl(
) : AccountService {
override suspend fun findById(id: Long): Account {
val findById = actorQueryService.findById(id)
return toAccount(findById)
}
private fun toAccount(findById: Actor): Account {
val userUrl = applicationConfig.url.toString() + "/users/" + findById.id.toString()
return Account(
@ -42,4 +48,7 @@ class AccountServiceImpl(
followersCount = 0,
)
}
override suspend fun findByIds(ids: List<Long>): List<Account> =
actorQueryService.findByIds(ids).map { toAccount(it) }
}

View File

@ -576,6 +576,86 @@ paths:
schema:
$ref: "#/components/schemas/MediaAttachment"
/api/v1/follow_requests:
get:
tags:
- accounts
security:
- OAuth2:
- "read:follows"
parameters:
- in: query
name: max_id
schema:
type: integer
required: false
- in: query
name: since_id
schema:
type: integer
required: false
- in: query
name: limit
schema:
type: integer
required: false
responses:
200:
description: 成功
headers:
Link:
schema:
type: string
description: ページネーション
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Account"
/api/v1/follow_requests/{account_id}/authorize:
post:
tags:
- accounts
security:
- OAuth2:
- "write:follows"
parameters:
- in: path
name: account_id
schema:
type: string
required: true
responses:
200:
description: 成功
content:
application/json:
schema:
$ref: "#/components/schemas/Relationship"
/api/v1/follow_requests/{account_id}/reject:
post:
tags:
- accounts
security:
- OAuth2:
- "write:follows"
parameters:
- in: path
name: account_id
schema:
type: string
required: true
responses:
200:
description: 成功
content:
application/json:
schema:
$ref: "#/components/schemas/Relationship"
components:
schemas:
V1MediaRequest:

View File

@ -67,7 +67,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = true,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -91,7 +91,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = true,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -110,7 +110,7 @@ class RelationshipServiceImplTest {
blocking = true,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -129,7 +129,7 @@ class RelationshipServiceImplTest {
blocking = true,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -152,7 +152,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -167,7 +167,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -186,7 +186,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = true
)
)
@ -198,7 +198,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = true
ignoreFollowRequestToTarget = false
)
)
@ -222,7 +222,7 @@ class RelationshipServiceImplTest {
blocking = true,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -246,7 +246,7 @@ class RelationshipServiceImplTest {
blocking = true,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -266,7 +266,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = true,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -280,7 +280,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -302,7 +302,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = true,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -317,7 +317,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -366,7 +366,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -420,7 +420,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = true,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -435,7 +435,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -459,7 +459,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = true,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -474,7 +474,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -500,7 +500,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -516,13 +516,13 @@ class RelationshipServiceImplTest {
verify(relationshipRepository, times(1)).save(
eq(
Relationship(
actorId = 1234,
targetActorId = 5678,
actorId = 5678,
targetActorId = 1234,
following = false,
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = true
ignoreFollowRequestToTarget = true
)
)
)
@ -539,7 +539,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -554,7 +554,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -578,7 +578,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -593,7 +593,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -618,7 +618,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -638,7 +638,7 @@ class RelationshipServiceImplTest {
blocking = true,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -653,7 +653,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -677,7 +677,7 @@ class RelationshipServiceImplTest {
blocking = true,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -717,7 +717,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -739,7 +739,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = true,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)
@ -756,7 +756,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = true,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -771,7 +771,7 @@ class RelationshipServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
)

View File

@ -4,6 +4,7 @@ import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.domain.model.relationship.RelationshipRepository
import dev.usbharu.hideout.core.query.FollowerQueryService
import dev.usbharu.hideout.core.query.RelationshipQueryService
import dev.usbharu.hideout.core.service.media.MediaService
import dev.usbharu.hideout.core.service.relationship.RelationshipService
import dev.usbharu.hideout.core.service.user.UserService
@ -49,6 +50,9 @@ class AccountApiServiceImplTest {
@Mock
private lateinit var relationshipRepository: RelationshipRepository
@Mock
private lateinit var relationshipQueryService: RelationshipQueryService
@Mock
private lateinit var mediaService: MediaService
@ -214,7 +218,7 @@ class AccountApiServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
@ -249,7 +253,7 @@ class AccountApiServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)
whenever(relationshipRepository.findByUserIdAndTargetUserId(eq(userId), eq(followeeId))).doReturn(
@ -260,7 +264,7 @@ class AccountApiServiceImplTest {
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
)