fix: ActivityPubのidが重複した際の処理を修正

This commit is contained in:
usbharu 2023-06-04 00:47:09 +09:00
parent d3d91fc2bc
commit 1a63ae104c
3 changed files with 73 additions and 58 deletions

View File

@ -20,4 +20,5 @@ interface IPostRepository {
userId: Long?): List<Post>
suspend fun findByUserId(idOrNull: Long, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List<Post>
suspend fun findByApId(id: String): Post?
}

View File

@ -89,6 +89,12 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe
override suspend fun findByUserId(idOrNull: Long, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List<Post> {
TODO("Not yet implemented")
}
override suspend fun findByApId(id: String): Post? {
return query {
Posts.select { Posts.apId eq id }.singleOrNull()?.toPost()
}
}
}
object Posts : Table() {

View File

@ -72,6 +72,17 @@ class ActivityPubNoteServiceImpl(
override suspend fun fetchNote(url: String, targetActor: String?): Note {
val post = postRepository.findByUrl(url)
if (post != null) {
return postToNote(post)
}
val response = httpClient.getAp(
url,
"$targetActor#pubkey"
)
val note = response.body<Note>()
return note(note, targetActor, url)
}
private suspend fun postToNote(post: Post): Note {
val user = userService.findById(post.userId)
val reply = post.replyId?.let { postRepository.findOneById(it) }
return Note(
@ -86,19 +97,16 @@ class ActivityPubNoteServiceImpl(
inReplyTo = reply?.url
)
}
val response = httpClient.getAp(
url,
"$targetActor#pubkey"
)
val note = response.body<Note>()
return note(note, targetActor, url)
}
private suspend fun ActivityPubNoteServiceImpl.note(
note: Note,
targetActor: String?,
url: String
): Note {
val findByApId = postRepository.findByApId(url)
if (findByApId != null) {
return postToNote(findByApId)
}
val person = activityPubUserService.fetchPerson(
note.attributedTo ?: throw IllegalActivityPubObjectException("note.attributedTo is null"),
targetActor