feat: とりあえずacceptを返すように

This commit is contained in:
usbharu 2023-03-30 18:23:38 +09:00
parent 61ddbb5743
commit 9dcaddc108
4 changed files with 56 additions and 34 deletions

View File

@ -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<String> = emptyList(),
name: String,
`object`: Object?,
`object`: String?,
actor: String?
) : super(add(type,"Follow"), name) {
this.`object` = `object`

View File

@ -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

View File

@ -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<Follow>(bodyText)
activityPubUserService.receiveFollow(readValue)
}
ActivityPubService.ActivityType.Undo -> {
TODO()
}
}
}
}

View File

@ -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<Person>()
} 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()
))
}
}
}