feat: acceptを追加

This commit is contained in:
usbharu 2023-03-30 19:47:08 +09:00
parent e97a3e9e96
commit 5320915f7f
1 changed files with 13 additions and 11 deletions

View File

@ -12,8 +12,8 @@ import io.ktor.client.request.*
import io.ktor.http.* import io.ktor.http.*
class WebFingerService( class WebFingerService(
private val httpClient: HttpClient, private val httpClient: HttpClient,
private val userService: UserService private val userService: UserService
) : IWebFingerService { ) : IWebFingerService {
override suspend fun fetch(acct: String): WebFinger? { override suspend fun fetch(acct: String): WebFinger? {
@ -22,7 +22,7 @@ class WebFingerService(
return try { return try {
httpClient.get("https://$domain/.well-known/webfinger?resource=acct:$fullName") httpClient.get("https://$domain/.well-known/webfinger?resource=acct:$fullName")
.body<WebFinger>() .body<WebFinger>()
} catch (e: ResponseException) { } catch (e: ResponseException) {
if (e.response.status == HttpStatusCode.NotFound) { if (e.response.status == HttpStatusCode.NotFound) {
return null return null
@ -33,7 +33,9 @@ class WebFingerService(
override suspend fun fetchUserModel(url: String): Person? { override suspend fun fetchUserModel(url: String): Person? {
return try { return try {
httpClient.get(url).body<Person>() httpClient.get(url) {
header("Accept", "application/activity+json")
}.body<Person>()
} catch (e: ResponseException) { } catch (e: ResponseException) {
if (e.response.status == HttpStatusCode.NotFound) { if (e.response.status == HttpStatusCode.NotFound) {
return null return null
@ -46,9 +48,9 @@ class WebFingerService(
val link = webFinger.links.find { val link = webFinger.links.find {
it.rel == "self" && HttpUtil.isContentTypeOfActivityPub( it.rel == "self" && HttpUtil.isContentTypeOfActivityPub(
ContentType.parse( ContentType.parse(
it.type.orEmpty() it.type.orEmpty()
) )
) )
}?.href ?: throw Exception() }?.href ?: throw Exception()
@ -59,10 +61,10 @@ class WebFingerService(
val userModel = fetchUserModel(link) ?: throw Exception() val userModel = fetchUserModel(link) ?: throw Exception()
val user = User( val user = User(
userModel.preferredUsername ?: throw IllegalStateException(), userModel.preferredUsername ?: throw IllegalStateException(),
domain, domain,
userName, userName,
userModel.summary.orEmpty() userModel.summary.orEmpty()
) )
return userService.create(user) return userService.create(user)
} }