diff --git a/src/main/kotlin/dev/usbharu/hideout/ap/Follow.kt b/src/main/kotlin/dev/usbharu/hideout/ap/Follow.kt index dca8de01..ef668610 100644 --- a/src/main/kotlin/dev/usbharu/hideout/ap/Follow.kt +++ b/src/main/kotlin/dev/usbharu/hideout/ap/Follow.kt @@ -1,13 +1,13 @@ package dev.usbharu.hideout.ap open class Follow : Object{ - public var `object`:Object? = null + public var `object`:String? = null public var actor:String? = null protected constructor() : super() constructor( type: List = emptyList(), name: String, - `object`: Object?, + `object`: String?, actor: String? ) : super(add(type,"Follow"), name) { this.`object` = `object` diff --git a/src/main/kotlin/dev/usbharu/hideout/ap/Person.kt b/src/main/kotlin/dev/usbharu/hideout/ap/Person.kt index 22651e5e..2a1f29f7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/ap/Person.kt +++ b/src/main/kotlin/dev/usbharu/hideout/ap/Person.kt @@ -4,7 +4,7 @@ open class Person : Object { private var id:String? = null var preferredUsername:String? = null var summary:String? = null - private var inbox:String? = null + var inbox:String? = null private var outbox:String? = null private var url:String? = null private var icon:Image? = null diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/userActivityPubRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/userActivityPubRouting.kt index 73188727..57dd3ce5 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/userActivityPubRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/userActivityPubRouting.kt @@ -1,6 +1,10 @@ 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.util.HttpUtil import io.ktor.http.* import io.ktor.server.application.* @@ -8,7 +12,7 @@ import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -fun Application.userActivityPubRouting(activityPubService: ActivityPubService) { +fun Application.userActivityPubRouting(activityPubService: ActivityPubService, activityPubUserService: ActivityPubUserService) { routing { route("/users/{name}") { route("/inbox") { @@ -21,7 +25,17 @@ fun Application.userActivityPubRouting(activityPubService: ActivityPubService) { } val bodyText = call.receiveText() println(bodyText) - activityPubService.switchApType(bodyText) + when (activityPubService.switchApType(bodyText)) { + ActivityPubService.ActivityType.Follow -> { + val readValue = Config.configData.objectMapper.readValue(bodyText) + activityPubUserService.receiveFollow(readValue) + + } + + ActivityPubService.ActivityType.Undo -> { + TODO() + } + } } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt index 3389514f..e1469bae 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ActivityPubUserService.kt @@ -1,8 +1,6 @@ package dev.usbharu.hideout.service -import dev.usbharu.hideout.ap.Image -import dev.usbharu.hideout.ap.Key -import dev.usbharu.hideout.ap.Person +import dev.usbharu.hideout.ap.* import dev.usbharu.hideout.config.Config import io.ktor.client.* import io.ktor.client.call.* @@ -11,40 +9,41 @@ import io.ktor.client.request.* import io.ktor.http.* class ActivityPubUserService( - private val httpClient:HttpClient, - private val userService: UserService, - private val userAuthService: IUserAuthService + private val httpClient: HttpClient, + private val userService: UserService, + private val userAuthService: IUserAuthService, + private val webFingerService: WebFingerService ) { suspend fun generateUserModel(name: String): Person { val userEntity = userService.findByName(name) val userAuthEntity = userAuthService.findByUserId(userEntity.id) val userUrl = "${Config.configData.url}/users/$name" return Person( - type = emptyList(), - name = userEntity.name, - id = userUrl, - preferredUsername = name, - summary = userEntity.description, - inbox = "$userUrl/inbox", - outbox = "$userUrl/outbox", - url = userUrl, - icon = Image( type = emptyList(), - name = "$userUrl/icon.png", - mediaType = "image/png", - url = "$userUrl/icon.png" - ), - publicKey = Key( - type = emptyList(), - name = "Public Key", - id = "$userUrl/pubkey", - owner = userUrl, - publicKeyPem = userAuthEntity.publicKey - ) + name = userEntity.name, + id = userUrl, + preferredUsername = name, + summary = userEntity.description, + inbox = "$userUrl/inbox", + outbox = "$userUrl/outbox", + url = userUrl, + icon = Image( + type = emptyList(), + name = "$userUrl/icon.png", + mediaType = "image/png", + url = "$userUrl/icon.png" + ), + publicKey = Key( + type = emptyList(), + name = "Public Key", + id = "$userUrl/pubkey", + owner = userUrl, + publicKeyPem = userAuthEntity.publicKey + ) ) } - suspend fun fetchUserModel(url:String):Person? { + suspend fun fetchUserModel(url: String): Person? { return try { httpClient.get(url).body() } catch (e: ResponseException) { @@ -55,7 +54,16 @@ class ActivityPubUserService( } } - suspend fun receiveFollow(){ - + suspend fun receiveFollow(follow: Follow) { + val actor = follow.actor ?: throw IllegalArgumentException("actor is null") + val person = fetchUserModel(actor) ?: throw IllegalArgumentException("actor is not found") + val inboxUrl = person.inbox ?: throw IllegalArgumentException("inbox is not found") + httpClient.post(inboxUrl) { + setBody(Accept( + name = "Follow", + `object` = follow, + actor = follow.`object`.orEmpty() + )) + } } }