feat: 絵文字リアクションの受信ができるように

This commit is contained in:
usbharu 2023-12-22 15:13:39 +09:00
parent 8af489c93c
commit 51837ea379
4 changed files with 29 additions and 12 deletions

View File

@ -9,7 +9,7 @@ import dev.usbharu.hideout.activitypub.service.common.ExtendedActivityVocabulary
class ObjectDeserializer : JsonDeserializer<Object>() {
@Suppress("LongMethod", "CyclomaticComplexMethod")
override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): Object {
override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): Object? {
requireNotNull(p)
val treeNode: JsonNode = requireNotNull(p.codec?.readTree(p))
if (treeNode.isValueNode) {
@ -24,7 +24,7 @@ class ObjectDeserializer : JsonDeserializer<Object>() {
ExtendedActivityVocabulary.values().firstOrNull { it.name.equals(jsonNode.asText(), true) }
}
} else if (type.isValueNode) {
ExtendedActivityVocabulary.values().first { it.name.equals(type.asText(), true) }
ExtendedActivityVocabulary.values().firstOrNull() { it.name.equals(type.asText(), true) }
} else {
TODO()
}
@ -85,6 +85,7 @@ class ObjectDeserializer : JsonDeserializer<Object>() {
ExtendedActivityVocabulary.Video -> TODO()
ExtendedActivityVocabulary.Mention -> TODO()
ExtendedActivityVocabulary.Emoji -> p.codec.treeToValue(treeNode, Emoji::class.java)
null -> null
}
} else {
TODO()

View File

@ -1,13 +1,16 @@
package dev.usbharu.hideout.activitypub.service.activity.like
import dev.usbharu.hideout.activitypub.domain.exception.FailedToGetActivityPubResourceException
import dev.usbharu.hideout.activitypub.domain.model.Emoji
import dev.usbharu.hideout.activitypub.domain.model.Like
import dev.usbharu.hideout.activitypub.service.common.AbstractActivityPubProcessor
import dev.usbharu.hideout.activitypub.service.common.ActivityPubProcessContext
import dev.usbharu.hideout.activitypub.service.common.ActivityType
import dev.usbharu.hideout.activitypub.service.objects.emoji.EmojiService
import dev.usbharu.hideout.activitypub.service.objects.note.APNoteService
import dev.usbharu.hideout.activitypub.service.objects.user.APUserService
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.domain.model.emoji.UnicodeEmoji
import dev.usbharu.hideout.core.service.reaction.ReactionService
import org.springframework.stereotype.Service
@ -16,7 +19,8 @@ class APLikeProcessor(
transaction: Transaction,
private val apUserService: APUserService,
private val apNoteService: APNoteService,
private val reactionService: ReactionService
private val reactionService: ReactionService,
private val emojiService: EmojiService
) :
AbstractActivityPubProcessor<Like>(transaction) {
override suspend fun internalProcess(activity: ActivityPubProcessContext<Like>) {
@ -29,9 +33,16 @@ class APLikeProcessor(
try {
val post = apNoteService.fetchNoteWithEntity(target).second
val emoji = if (content.startsWith(":")) {
val tag = activity.activity.tag
(tag.firstOrNull { it is Emoji } as Emoji?)?.let { emojiService.fetchEmoji(it).second }
} else {
UnicodeEmoji(content)
}
reactionService.receiveReaction(
content,
actor.substringAfter("://").substringBefore("/"),
emoji ?: UnicodeEmoji(""),
personWithEntity.second.id,
post.id
)

View File

@ -1,11 +1,12 @@
package dev.usbharu.hideout.core.service.reaction
import dev.usbharu.hideout.core.domain.model.emoji.Emoji
import org.springframework.stereotype.Service
@Service
interface ReactionService {
suspend fun receiveReaction(name: String, domain: String, actorId: Long, postId: Long)
suspend fun receiveReaction(emoji: Emoji, actorId: Long, postId: Long)
suspend fun receiveRemoveReaction(actorId: Long, postId: Long)
suspend fun sendReaction(name: String, actorId: Long, postId: Long)
suspend fun sendReaction(emoji: Emoji, actorId: Long, postId: Long)
suspend fun removeReaction(actorId: Long, postId: Long)
}

View File

@ -1,7 +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.emoji.Emoji
import dev.usbharu.hideout.core.domain.model.reaction.Reaction
import dev.usbharu.hideout.core.domain.model.reaction.ReactionRepository
import org.jetbrains.exposed.exceptions.ExposedSQLException
@ -14,11 +14,15 @@ class ReactionServiceImpl(
private val reactionRepository: ReactionRepository,
private val apReactionService: APReactionService
) : ReactionService {
override suspend fun receiveReaction(name: String, domain: String, actorId: Long, postId: Long) {
override suspend fun receiveReaction(
emoji: Emoji,
actorId: Long,
postId: Long
) {
if (reactionRepository.existByPostIdAndActorIdAndEmojiId(postId, actorId, 0).not()) {
try {
reactionRepository.save(
Reaction(reactionRepository.generateId(), UnicodeEmoji(""), postId, actorId)
Reaction(reactionRepository.generateId(), emoji, postId, actorId)
)
} catch (_: ExposedSQLException) {
}
@ -34,7 +38,7 @@ class ReactionServiceImpl(
reactionRepository.delete(reaction)
}
override suspend fun sendReaction(name: String, actorId: Long, postId: Long) {
override suspend fun sendReaction(emoji: Emoji, actorId: Long, postId: Long) {
val findByPostIdAndUserIdAndEmojiId =
reactionRepository.findByPostIdAndActorIdAndEmojiId(postId, actorId, 0)
@ -43,7 +47,7 @@ class ReactionServiceImpl(
reactionRepository.delete(findByPostIdAndUserIdAndEmojiId)
}
val reaction = Reaction(reactionRepository.generateId(), UnicodeEmoji(""), postId, actorId)
val reaction = Reaction(reactionRepository.generateId(), emoji, postId, actorId)
reactionRepository.save(reaction)
apReactionService.reaction(reaction)
}