diff --git a/src/main/kotlin/dev/usbharu/hideout/config/HttpClientConfig.kt b/src/main/kotlin/dev/usbharu/hideout/config/HttpClientConfig.kt index 454a4de6..324f1e93 100644 --- a/src/main/kotlin/dev/usbharu/hideout/config/HttpClientConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/config/HttpClientConfig.kt @@ -6,6 +6,7 @@ import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.service.core.Transaction import io.ktor.client.* import io.ktor.client.engine.cio.* +import io.ktor.client.plugins.cache.* import io.ktor.client.plugins.logging.* import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration @@ -22,6 +23,9 @@ class HttpClientConfig { logger = Logger.DEFAULT level = LogLevel.INFO + } + install(HttpCache) { + } expectSuccess = true } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt index c6173127..83849581 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APLikeService.kt @@ -28,7 +28,7 @@ class APLikeServiceImpl( like.`object` ?: throw IllegalActivityPubObjectException("object is null") transaction.transaction { val person = apUserService.fetchPersonWithEntity(actor) - apNoteService.fetchNote(like.`object` ?: return@transaction) + apNoteService.fetchNoteAsync(like.`object` ?: return@transaction).await() val post = postQueryService.findByUrl(like.`object` ?: return@transaction) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt index 663b2d57..b41282db 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APNoteService.kt @@ -24,8 +24,14 @@ import dev.usbharu.hideout.service.post.PostService import io.ktor.client.* import io.ktor.client.statement.* import kjob.core.job.JobProps +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Qualifier +import org.springframework.cache.annotation.Cacheable import org.springframework.stereotype.Service import java.time.Instant @@ -34,6 +40,15 @@ interface APNoteService { suspend fun createNote(post: Post) suspend fun createNoteJob(props: JobProps) + @Cacheable("fetchNote") + fun fetchNoteAsync(url: String, targetActor: String? = null): Deferred { + return CoroutineScope(Dispatchers.IO).async { + newSuspendedTransaction { + fetchNote(url, targetActor) + } + } + } + suspend fun fetchNote(url: String, targetActor: String? = null): Note suspend fun fetchNote(note: Note, targetActor: String? = null): Note } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt index 312c182b..07519bcc 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/reaction/ReactionServiceImpl.kt @@ -4,6 +4,7 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Reaction import dev.usbharu.hideout.query.ReactionQueryService import dev.usbharu.hideout.repository.ReactionRepository import dev.usbharu.hideout.service.ap.APReactionService +import org.slf4j.LoggerFactory import org.springframework.stereotype.Service @Service @@ -14,9 +15,14 @@ class ReactionServiceImpl( ) : ReactionService { override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) { if (reactionQueryService.reactionAlreadyExist(postId, userId, 0).not()) { - reactionRepository.save( - Reaction(reactionRepository.generateId(), 0, postId, userId) - ) + + try { + reactionRepository.save( + Reaction(reactionRepository.generateId(), 0, postId, userId) + ) + } catch (_: Exception) { + LOGGER.warn("FAILED Failure to persist reaction information.") + } } } @@ -34,4 +40,8 @@ class ReactionServiceImpl( override suspend fun removeReaction(userId: Long, postId: Long) { reactionQueryService.deleteByPostIdAndUserId(postId, userId) } + + companion object { + val LOGGER = LoggerFactory.getLogger(ReactionServiceImpl::class.java) + } }