From dd408f86dbc6ffa1dad64295bd0d2ca077fc651a Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 5 Apr 2023 16:59:59 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=A9=E7=94=A8=E3=81=97?= =?UTF-8?q?=E3=81=AA=E3=81=84=E4=BA=88=E5=AE=9A=E3=81=AE=E3=82=AF=E3=83=A9?= =?UTF-8?q?=E3=82=B9=E3=82=92=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/dev/usbharu/hideout/Application.kt | 1 + .../usbharu/hideout/plugins/ActivityPub.kt | 3 +- .../usbharu/hideout/routing/UserRouting.kt | 5 +-- .../hideout/routing/WellKnownRouting.kt | 2 +- .../hideout/routing/userActivityPubRouting.kt | 4 +- .../service/activitypub/ActivityPubService.kt | 39 +++++++++++++++++++ .../service/{ => impl}/ActivityPubService.kt | 5 +-- .../{ => impl}/ActivityPubUserService.kt | 4 +- .../service/{ => impl}/HttpSignService.kt | 2 +- .../service/{ => impl}/UserAuthService.kt | 4 +- .../hideout/service/{ => impl}/UserService.kt | 2 +- .../service/{ => impl}/WebFingerService.kt | 3 +- .../hideout/plugins/ActivityPubKtTest.kt | 5 +-- .../usbharu/hideout/plugins/KtorKeyMapTest.kt | 5 +-- 14 files changed, 61 insertions(+), 23 deletions(-) create mode 100644 src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt rename src/main/kotlin/dev/usbharu/hideout/service/{ => impl}/ActivityPubService.kt (74%) rename src/main/kotlin/dev/usbharu/hideout/service/{ => impl}/ActivityPubUserService.kt (93%) rename src/main/kotlin/dev/usbharu/hideout/service/{ => impl}/HttpSignService.kt (68%) rename src/main/kotlin/dev/usbharu/hideout/service/{ => impl}/UserAuthService.kt (97%) rename src/main/kotlin/dev/usbharu/hideout/service/{ => impl}/UserService.kt (96%) rename src/main/kotlin/dev/usbharu/hideout/service/{ => impl}/WebFingerService.kt (96%) diff --git a/src/main/kotlin/dev/usbharu/hideout/Application.kt b/src/main/kotlin/dev/usbharu/hideout/Application.kt index 56f3f1d8..e99bef82 100644 --- a/src/main/kotlin/dev/usbharu/hideout/Application.kt +++ b/src/main/kotlin/dev/usbharu/hideout/Application.kt @@ -12,6 +12,7 @@ import dev.usbharu.hideout.repository.UserAuthRepository import dev.usbharu.hideout.repository.UserRepository import dev.usbharu.hideout.routing.* import dev.usbharu.hideout.service.* +import dev.usbharu.hideout.service.impl.* import io.ktor.client.* import io.ktor.client.engine.cio.* import io.ktor.client.plugins.contentnegotiation.* diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt index 7d0a41d2..2c8d1d52 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/ActivityPub.kt @@ -3,7 +3,7 @@ package dev.usbharu.hideout.plugins import dev.usbharu.hideout.ap.JsonLd import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.service.IUserAuthService -import dev.usbharu.hideout.service.UserAuthService +import dev.usbharu.hideout.service.impl.UserAuthService import dev.usbharu.hideout.util.HttpUtil.Activity import io.ktor.client.* import io.ktor.client.plugins.api.* @@ -12,7 +12,6 @@ import io.ktor.client.statement.* import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.response.* -import io.ktor.util.* import kotlinx.coroutines.runBlocking import tech.barbero.http.message.signing.HttpMessage import tech.barbero.http.message.signing.HttpMessageSigner diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/UserRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/UserRouting.kt index d73c8ad0..d43b070d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/UserRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/UserRouting.kt @@ -4,8 +4,8 @@ import dev.usbharu.hideout.domain.model.User import dev.usbharu.hideout.plugins.UserSession import dev.usbharu.hideout.plugins.respondAp import dev.usbharu.hideout.plugins.tokenAuth -import dev.usbharu.hideout.service.ActivityPubUserService -import dev.usbharu.hideout.service.UserService +import dev.usbharu.hideout.service.impl.ActivityPubUserService +import dev.usbharu.hideout.service.impl.UserService import dev.usbharu.hideout.util.HttpUtil import io.ktor.http.* import io.ktor.server.application.* @@ -13,7 +13,6 @@ import io.ktor.server.auth.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -import io.ktor.server.sessions.* @Suppress("unused") fun Application.user(userService: UserService, activityPubUserService: ActivityPubUserService) { diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/WellKnownRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/WellKnownRouting.kt index 41fc6fd1..f1e6a7a6 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/WellKnownRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/WellKnownRouting.kt @@ -1,7 +1,7 @@ package dev.usbharu.hideout.routing import dev.usbharu.hideout.config.Config -import dev.usbharu.hideout.service.UserService +import dev.usbharu.hideout.service.impl.UserService import dev.usbharu.hideout.util.HttpUtil.Activity import io.ktor.http.* import io.ktor.server.application.* diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/userActivityPubRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/userActivityPubRouting.kt index 8993ecda..73d54fa8 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/userActivityPubRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/userActivityPubRouting.kt @@ -3,8 +3,8 @@ package dev.usbharu.hideout.routing import com.fasterxml.jackson.module.kotlin.readValue import dev.usbharu.hideout.ap.Follow import dev.usbharu.hideout.config.Config -import dev.usbharu.hideout.service.ActivityPubService -import dev.usbharu.hideout.service.ActivityPubUserService +import dev.usbharu.hideout.service.impl.ActivityPubService +import dev.usbharu.hideout.service.impl.ActivityPubUserService import dev.usbharu.hideout.util.HttpUtil import io.ktor.http.* import io.ktor.server.application.* diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt new file mode 100644 index 00000000..070046a3 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubService.kt @@ -0,0 +1,39 @@ +package dev.usbharu.hideout.service.activitypub + +interface ActivityPubService { + fun parseActivity(json:String): ActivityType + + fun processActivity(json:String, type: ActivityType) +} + +enum class ActivityType { + Accept, + Add, + Announce, + Arrive, + Block, + Create, + Delete, + Dislike, + Flag, + Follow, + Ignore, + Invite, + Join, + Leave, + Like, + Listen, + Move, + Offer, + Question, + Reject, + Read, + Remove, + TentativeReject, + TentativeAccept, + Travel, + Undo, + Update, + View, + Other +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubService.kt similarity index 74% rename from src/main/kotlin/dev/usbharu/hideout/service/ActivityPubService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubService.kt index bc6c9f17..e693bd57 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubService.kt @@ -1,6 +1,5 @@ -package dev.usbharu.hideout.service +package dev.usbharu.hideout.service.impl -import com.fasterxml.jackson.databind.ObjectMapper import dev.usbharu.hideout.config.Config class ActivityPubService() { @@ -10,7 +9,7 @@ class ActivityPubService() { Undo } - fun switchApType(json:String):ActivityType{ + fun switchApType(json:String): ActivityType { val typeAsText = Config.configData.objectMapper.readTree(json).get("type").asText() return when(typeAsText){ "Follow" -> ActivityType.Follow diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubUserService.kt similarity index 93% rename from src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubUserService.kt index 4c2b0f82..fd26123c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/ActivityPubUserService.kt @@ -1,8 +1,10 @@ -package dev.usbharu.hideout.service +package dev.usbharu.hideout.service.impl import dev.usbharu.hideout.ap.* import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.plugins.postAp +import dev.usbharu.hideout.service.IUserAuthService +import dev.usbharu.hideout.service.IWebFingerService import io.ktor.client.* class ActivityPubUserService( diff --git a/src/main/kotlin/dev/usbharu/hideout/service/HttpSignService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/HttpSignService.kt similarity index 68% rename from src/main/kotlin/dev/usbharu/hideout/service/HttpSignService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/impl/HttpSignService.kt index 440ee426..07cf8941 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/HttpSignService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/HttpSignService.kt @@ -1,4 +1,4 @@ -package dev.usbharu.hideout.service +package dev.usbharu.hideout.service.impl import java.security.PrivateKey diff --git a/src/main/kotlin/dev/usbharu/hideout/service/UserAuthService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserAuthService.kt similarity index 97% rename from src/main/kotlin/dev/usbharu/hideout/service/UserAuthService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/impl/UserAuthService.kt index ac60a847..cf5b0a48 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/UserAuthService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserAuthService.kt @@ -1,13 +1,13 @@ -package dev.usbharu.hideout.service +package dev.usbharu.hideout.service.impl import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.domain.model.User import dev.usbharu.hideout.domain.model.UserAuthentication import dev.usbharu.hideout.domain.model.UserAuthenticationEntity -import dev.usbharu.hideout.domain.model.Users.screenName import dev.usbharu.hideout.exception.UserNotFoundException import dev.usbharu.hideout.repository.IUserAuthRepository import dev.usbharu.hideout.repository.IUserRepository +import dev.usbharu.hideout.service.IUserAuthService import io.ktor.util.* import java.security.KeyPair import java.security.KeyPairGenerator diff --git a/src/main/kotlin/dev/usbharu/hideout/service/UserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt similarity index 96% rename from src/main/kotlin/dev/usbharu/hideout/service/UserService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt index 3183b212..4736338a 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/UserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt @@ -1,4 +1,4 @@ -package dev.usbharu.hideout.service +package dev.usbharu.hideout.service.impl import dev.usbharu.hideout.domain.model.User import dev.usbharu.hideout.domain.model.UserEntity diff --git a/src/main/kotlin/dev/usbharu/hideout/service/WebFingerService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/WebFingerService.kt similarity index 96% rename from src/main/kotlin/dev/usbharu/hideout/service/WebFingerService.kt rename to src/main/kotlin/dev/usbharu/hideout/service/impl/WebFingerService.kt index 9cc2cf52..8bf1c420 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/WebFingerService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/WebFingerService.kt @@ -1,8 +1,9 @@ -package dev.usbharu.hideout.service +package dev.usbharu.hideout.service.impl import dev.usbharu.hideout.ap.Person import dev.usbharu.hideout.domain.model.User import dev.usbharu.hideout.domain.model.UserEntity +import dev.usbharu.hideout.service.IWebFingerService import dev.usbharu.hideout.util.HttpUtil import dev.usbharu.hideout.webfinger.WebFinger import io.ktor.client.* diff --git a/src/test/kotlin/dev/usbharu/hideout/plugins/ActivityPubKtTest.kt b/src/test/kotlin/dev/usbharu/hideout/plugins/ActivityPubKtTest.kt index 8c7e7d76..36cefbbd 100644 --- a/src/test/kotlin/dev/usbharu/hideout/plugins/ActivityPubKtTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/plugins/ActivityPubKtTest.kt @@ -7,8 +7,8 @@ import dev.usbharu.hideout.domain.model.UserAuthenticationEntity import dev.usbharu.hideout.domain.model.UserEntity import dev.usbharu.hideout.repository.IUserAuthRepository import dev.usbharu.hideout.repository.IUserRepository -import dev.usbharu.hideout.service.UserAuthService -import dev.usbharu.hideout.service.toPem +import dev.usbharu.hideout.service.impl.UserAuthService +import dev.usbharu.hideout.service.impl.toPem import io.ktor.client.* import io.ktor.client.engine.mock.* import io.ktor.client.plugins.logging.* @@ -17,7 +17,6 @@ import org.junit.jupiter.api.Test import java.security.KeyPairGenerator import java.security.interfaces.RSAPrivateKey import java.security.interfaces.RSAPublicKey -import java.util.* class ActivityPubKtTest { @Test diff --git a/src/test/kotlin/dev/usbharu/hideout/plugins/KtorKeyMapTest.kt b/src/test/kotlin/dev/usbharu/hideout/plugins/KtorKeyMapTest.kt index 9f9a7cfe..3f56dd92 100644 --- a/src/test/kotlin/dev/usbharu/hideout/plugins/KtorKeyMapTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/plugins/KtorKeyMapTest.kt @@ -6,13 +6,12 @@ import dev.usbharu.hideout.domain.model.UserAuthenticationEntity import dev.usbharu.hideout.domain.model.UserEntity import dev.usbharu.hideout.repository.IUserAuthRepository import dev.usbharu.hideout.repository.IUserRepository -import dev.usbharu.hideout.service.UserAuthService -import dev.usbharu.hideout.service.toPem +import dev.usbharu.hideout.service.impl.UserAuthService +import dev.usbharu.hideout.service.impl.toPem import org.junit.jupiter.api.Test import java.security.KeyPairGenerator import java.security.interfaces.RSAPrivateKey import java.security.interfaces.RSAPublicKey -import java.util.* class KtorKeyMapTest {