diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/emoji/EmojiService.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/emoji/EmojiService.kt new file mode 100644 index 00000000..4dd6e2d1 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/emoji/EmojiService.kt @@ -0,0 +1,9 @@ +package dev.usbharu.hideout.activitypub.service.objects.emoji + +import dev.usbharu.hideout.activitypub.domain.model.Emoji +import dev.usbharu.hideout.core.domain.model.emoji.CustomEmoji + +interface EmojiService { + suspend fun fetchEmoji(url: String): Pair + suspend fun fetchEmoji(emoji: Emoji): Pair +} diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/emoji/EmojiServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/emoji/EmojiServiceImpl.kt new file mode 100644 index 00000000..3c990b59 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/emoji/EmojiServiceImpl.kt @@ -0,0 +1,63 @@ +package dev.usbharu.hideout.activitypub.service.objects.emoji + +import dev.usbharu.hideout.activitypub.domain.model.Emoji +import dev.usbharu.hideout.activitypub.service.common.APResourceResolveServiceImpl +import dev.usbharu.hideout.activitypub.service.common.resolve +import dev.usbharu.hideout.core.domain.model.emoji.CustomEmoji +import dev.usbharu.hideout.core.domain.model.emoji.CustomEmojiRepository +import dev.usbharu.hideout.core.service.instance.InstanceService +import dev.usbharu.hideout.core.service.media.MediaService +import dev.usbharu.hideout.core.service.media.RemoteMedia +import org.springframework.stereotype.Service +import java.net.URL +import java.time.Instant + +@Service +class EmojiServiceImpl( + private val customEmojiRepository: CustomEmojiRepository, + private val instanceService: InstanceService, + private val mediaService: MediaService, + private val apResourceResolveServiceImpl: APResourceResolveServiceImpl +) : EmojiService { + override suspend fun fetchEmoji(url: String): Pair { + val emoji = apResourceResolveServiceImpl.resolve(url, null as Long?) + return fetchEmoji(emoji) + } + + override suspend fun fetchEmoji(emoji: Emoji): Pair { + return emoji to save(emoji) + } + + private suspend fun save(emoji: Emoji): CustomEmoji { + val domain = URL(emoji.id).host + val name = emoji.name.trim(':') + val customEmoji = customEmojiRepository.findByNameAndDomain(name, domain) + + if (customEmoji != null) { + return customEmoji + } + + val instance = instanceService.fetchInstance(emoji.id) + + val media = mediaService.uploadRemoteMedia( + RemoteMedia( + emoji.name, + emoji.icon.url, + emoji.icon.mediaType.orEmpty(), + null + ) + ) + + val customEmoji1 = CustomEmoji( + customEmojiRepository.generateId(), + name, + domain, + instance.id, + media.url, + null, + Instant.now() + ) + + return customEmojiRepository.save(customEmoji1) + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/CustomEmojiRepository.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/CustomEmojiRepository.kt index 59aa34aa..df7e87f3 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/CustomEmojiRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/CustomEmojiRepository.kt @@ -1,7 +1,9 @@ package dev.usbharu.hideout.core.domain.model.emoji interface CustomEmojiRepository { + suspend fun generateId(): Long suspend fun save(customEmoji: CustomEmoji): CustomEmoji suspend fun findById(id: Long): CustomEmoji? suspend fun delete(customEmoji: CustomEmoji) + suspend fun findByNameAndDomain(name: String, domain: String): CustomEmoji? } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/CustomEmojiRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/CustomEmojiRepositoryImpl.kt index 3296f5d5..0df58975 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/CustomEmojiRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/CustomEmojiRepositoryImpl.kt @@ -1,5 +1,6 @@ package dev.usbharu.hideout.core.infrastructure.exposedrepository +import dev.usbharu.hideout.application.service.id.IdGenerateService import dev.usbharu.hideout.core.domain.model.emoji.CustomEmoji import dev.usbharu.hideout.core.domain.model.emoji.CustomEmojiRepository import org.jetbrains.exposed.sql.* @@ -8,8 +9,13 @@ import org.jetbrains.exposed.sql.javatime.CurrentTimestamp import org.jetbrains.exposed.sql.javatime.timestamp import org.slf4j.Logger import org.slf4j.LoggerFactory +import org.springframework.stereotype.Repository + +@Repository +class CustomEmojiRepositoryImpl(private val idGenerateService: IdGenerateService) : CustomEmojiRepository, + AbstractRepository() { + override suspend fun generateId(): Long = idGenerateService.generateId() -class CustomEmojiRepositoryImpl : CustomEmojiRepository, AbstractRepository() { override suspend fun save(customEmoji: CustomEmoji): CustomEmoji = query { val singleOrNull = CustomEmojis.select { CustomEmojis.id eq customEmoji.id }.forUpdate().singleOrNull() if (singleOrNull == null) { @@ -43,6 +49,13 @@ class CustomEmojiRepositoryImpl : CustomEmojiRepository, AbstractRepository() { CustomEmojis.deleteWhere { CustomEmojis.id eq customEmoji.id } } + override suspend fun findByNameAndDomain(name: String, domain: String): CustomEmoji? = query { + return@query CustomEmojis + .select { CustomEmojis.name eq name and (CustomEmojis.domain eq domain) } + .singleOrNull() + ?.toCustomEmoji() + } + override val logger: Logger get() = Companion.logger diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/reaction/ReactionServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/reaction/ReactionServiceImpl.kt index 2a731029..a6ffeb3b 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/reaction/ReactionServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/reaction/ReactionServiceImpl.kt @@ -1,6 +1,7 @@ package dev.usbharu.hideout.core.service.reaction import dev.usbharu.hideout.activitypub.service.activity.like.APReactionService +import dev.usbharu.hideout.core.domain.model.emoji.UnicodeEmoji import dev.usbharu.hideout.core.domain.model.reaction.Reaction import dev.usbharu.hideout.core.domain.model.reaction.ReactionRepository import org.jetbrains.exposed.exceptions.ExposedSQLException @@ -17,7 +18,7 @@ class ReactionServiceImpl( if (reactionRepository.existByPostIdAndActorIdAndEmojiId(postId, actorId, 0).not()) { try { reactionRepository.save( - Reaction(reactionRepository.generateId(), 0, postId, actorId) + Reaction(reactionRepository.generateId(), UnicodeEmoji("❤"), postId, actorId) ) } catch (_: ExposedSQLException) { } @@ -42,7 +43,7 @@ class ReactionServiceImpl( reactionRepository.delete(findByPostIdAndUserIdAndEmojiId) } - val reaction = Reaction(reactionRepository.generateId(), 0, postId, actorId) + val reaction = Reaction(reactionRepository.generateId(), UnicodeEmoji("❤"), postId, actorId) reactionRepository.save(reaction) apReactionService.reaction(reaction) }