From 539c65ada26e56aaa7b2a88d7f7de9651029e6c6 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 11 Dec 2023 11:28:43 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Mastodon=20API=E3=81=8B=E3=82=89?= =?UTF-8?q?=E3=83=95=E3=82=A9=E3=83=AD=E3=83=BC=E8=A7=A3=E9=99=A4=E3=83=BB?= =?UTF-8?q?=E3=83=96=E3=83=AD=E3=83=83=E3=82=AF=E8=A7=A3=E9=99=A4=E3=81=8C?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/config/SecurityConfig.kt | 2 + .../relationship/RelationshipServiceImpl.kt | 2 +- .../account/MastodonAccountApiController.kt | 20 +++++++++ .../service/account/AccountApiService.kt | 18 +++++++- src/main/resources/openapi/mastodon.yaml | 43 +++++++++++++++++++ 5 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt b/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt index e42ad9b9..dadee6f4 100644 --- a/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt @@ -197,7 +197,9 @@ 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/*/unfollow", hasAnyScope("write", "write:follows")) authorize(POST, "/api/v1/accounts/*/block", hasAnyScope("write", "write:blocks")) + authorize(POST, "/api/v1/accounts/*/unblock", hasAnyScope("write", "write:blocks")) authorize(POST, "/api/v1/media", hasAnyScope("write", "write:media")) authorize(POST, "/api/v1/statuses", hasAnyScope("write", "write:statuses")) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/relationship/RelationshipServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/relationship/RelationshipServiceImpl.kt index bea66075..08e0ac9f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/relationship/RelationshipServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/relationship/RelationshipServiceImpl.kt @@ -167,7 +167,7 @@ class RelationshipServiceImpl( return } - if (relationship.followRequest.not()) { + if (relationship.followRequest.not() && relationship.following.not()) { logger.warn("FAILED Follow Request Not Found. (Follow Request) userId: {} targetId: {}", userId, targetId) return } diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt index e032ac01..8311596f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt @@ -113,4 +113,24 @@ class MastodonAccountApiController( return ResponseEntity.ok(block) } + + override suspend fun apiV1AccountsIdUnblockPost(id: String): ResponseEntity { + val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt + + val userid = principal.getClaim("uid").toLong() + + val unblock = accountApiService.unblock(userid, id.toLong()) + + return ResponseEntity.ok(unblock) + } + + override suspend fun apiV1AccountsIdUnfollowPost(id: String): ResponseEntity { + val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt + + val userid = principal.getClaim("uid").toLong() + + val unfollow = accountApiService.unfollow(userid, id.toLong()) + + return ResponseEntity.ok(unfollow) + } } diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/service/account/AccountApiService.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/service/account/AccountApiService.kt index 3a0f22bc..955603cb 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/service/account/AccountApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/service/account/AccountApiService.kt @@ -42,6 +42,8 @@ interface AccountApiService { * @return ブロック後のブロック対象ユーザーとの[Relationship] */ suspend fun block(userid: Long, target: Long): Relationship + suspend fun unblock(userid: Long, target: Long): Relationship + suspend fun unfollow(userid: Long, target: Long): Relationship } @Service @@ -101,10 +103,10 @@ class AccountApiServiceImpl( userService.createLocalUser(UserCreateDto(userCreateDto.name, userCreateDto.name, "", userCreateDto.password)) } - override suspend fun follow(loginUser: Long, followTargetUserId: Long): Relationship { + override suspend fun follow(loginUser: Long, followTargetUserId: Long): Relationship = transaction.transaction { relationshipService.followRequest(loginUser, followTargetUserId) - return fetchRelationship(loginUser, followTargetUserId) + return@transaction fetchRelationship(loginUser, followTargetUserId) } override suspend fun account(id: Long): Account = transaction.transaction { @@ -132,6 +134,18 @@ class AccountApiServiceImpl( fetchRelationship(userid, target) } + override suspend fun unblock(userid: Long, target: Long): Relationship = transaction.transaction { + relationshipService.unblock(userid, target) + + return@transaction fetchRelationship(userid, target) + } + + override suspend fun unfollow(userid: Long, target: Long): Relationship = transaction.transaction { + relationshipService.unfollow(userid, target) + + return@transaction fetchRelationship(userid, target) + } + private fun from(account: Account): CredentialAccount { return CredentialAccount( id = account.id, diff --git a/src/main/resources/openapi/mastodon.yaml b/src/main/resources/openapi/mastodon.yaml index 592726ae..fa3ea430 100644 --- a/src/main/resources/openapi/mastodon.yaml +++ b/src/main/resources/openapi/mastodon.yaml @@ -308,6 +308,49 @@ paths: schema: $ref: "#/components/schemas/Relationship" + /api/v1/accounts/{id}/unfollow: + post: + tags: + - account + security: + - OAuth2: + - "write:follows" + 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}/unblock: + 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: