diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Announce.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Announce.kt index 5c887259..513d4429 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Announce.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Announce.kt @@ -17,7 +17,7 @@ open class Announce @JsonCreator constructor( type = add(type, "Announce") ), HasActor, - HasId{ + HasId { override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false @@ -48,13 +48,13 @@ open class Announce @JsonCreator constructor( override fun toString(): String { return "Announce(" + - "apObject='$apObject', " + - "actor='$actor', " + - "id='$id', " + - "published='$published', " + - "to=$to, " + - "cc=$cc" + - ")" + - " ${super.toString()}" + "apObject='$apObject', " + + "actor='$actor', " + + "id='$id', " + + "published='$published', " + + "to=$to, " + + "cc=$cc" + + ")" + + " ${super.toString()}" } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Note.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Note.kt index 04910b09..cb51a489 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Note.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Note.kt @@ -6,7 +6,7 @@ import dev.usbharu.hideout.activitypub.domain.model.objects.Object import dev.usbharu.hideout.activitypub.domain.model.objects.ObjectDeserializer open class Note -@Suppress("LongParameterList") +@Suppress("LongParameterList", "CyclomaticComplexMethod") constructor( type: List = emptyList(), override val id: String, @@ -28,6 +28,8 @@ constructor( type = add(type, "Note") ), HasId { + + @Suppress("CyclomaticComplexMethod", "CognitiveComplexMethod") override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false @@ -52,6 +54,7 @@ constructor( return true } + @Suppress("CyclomaticComplexMethod") override fun hashCode(): Int { var result = super.hashCode() result = 31 * result + id.hashCode() diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/infrastructure/exposedquery/ExposedAnnounceQueryService.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/infrastructure/exposedquery/ExposedAnnounceQueryService.kt index 9051e61f..c89a6ce1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/infrastructure/exposedquery/ExposedAnnounceQueryService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/infrastructure/exposedquery/ExposedAnnounceQueryService.kt @@ -45,7 +45,7 @@ class ExposedAnnounceQueryService( ) return Announce( - emptyList(), + type = emptyList(), id = this[Posts.apId], apObject = repost, actor = this[Actors.url], @@ -63,4 +63,4 @@ class ExposedAnnounceQueryService( Visibility.DIRECT -> TODO() } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/infrastructure/exposedquery/NoteQueryServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/infrastructure/exposedquery/NoteQueryServiceImpl.kt index fa4b99f6..ffe2ee83 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/infrastructure/exposedquery/NoteQueryServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/infrastructure/exposedquery/NoteQueryServiceImpl.kt @@ -66,8 +66,7 @@ class NoteQueryServiceImpl(private val postRepository: PostRepository, private v logger.warn("Failed to get repostId: $repostId") } url - } - else { + } else { null } diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/query/AnnounceQueryService.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/query/AnnounceQueryService.kt index ed56ca6c..2e01f9ee 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/query/AnnounceQueryService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/query/AnnounceQueryService.kt @@ -8,4 +8,4 @@ import org.springframework.stereotype.Repository interface AnnounceQueryService { suspend fun findById(id: Long): Pair? suspend fun findByApId(apId: String): Pair? -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/announce/ApAnnounceProcessor.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/announce/ApAnnounceProcessor.kt index a4d74f66..99303538 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/announce/ApAnnounceProcessor.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/activity/announce/ApAnnounceProcessor.kt @@ -18,4 +18,4 @@ class ApAnnounceProcessor(transaction: Transaction, private val apNoteService: A override fun isSupported(activityType: ActivityType): Boolean = ActivityType.Announce == activityType override fun type(): Class = Announce::class.java -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/note/APNoteService.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/note/APNoteService.kt index df39051a..8cd1ad11 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/note/APNoteService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/objects/note/APNoteService.kt @@ -4,12 +4,14 @@ import dev.usbharu.hideout.activitypub.domain.exception.FailedToGetActivityPubRe import dev.usbharu.hideout.activitypub.domain.model.Announce import dev.usbharu.hideout.activitypub.domain.model.Emoji import dev.usbharu.hideout.activitypub.domain.model.Note +import dev.usbharu.hideout.activitypub.domain.model.Person import dev.usbharu.hideout.activitypub.query.AnnounceQueryService import dev.usbharu.hideout.activitypub.query.NoteQueryService import dev.usbharu.hideout.activitypub.service.common.APResourceResolveService import dev.usbharu.hideout.activitypub.service.common.resolve import dev.usbharu.hideout.activitypub.service.objects.emoji.EmojiService import dev.usbharu.hideout.activitypub.service.objects.user.APUserService +import dev.usbharu.hideout.core.domain.model.actor.Actor import dev.usbharu.hideout.core.domain.model.post.Post import dev.usbharu.hideout.core.domain.model.post.PostRepository import dev.usbharu.hideout.core.domain.model.post.Visibility @@ -68,7 +70,7 @@ class APNoteServiceImpl( ) throw FailedToGetActivityPubResourceException("Could not retrieve $url.", e) } - val savedNote = saveIfMissing(note, targetActor, url) + val savedNote = saveIfMissing(note, targetActor) logger.debug("SUCCESS Fetch Note url: {}", url) return savedNote } @@ -136,34 +138,22 @@ class APNoteServiceImpl( private suspend fun saveIfMissing( note: Note, - targetActor: String?, - url: String - ): Pair = noteQueryService.findByApid(note.id) ?: saveNote(note, targetActor, url) + targetActor: String? + ): Pair = noteQueryService.findByApid(note.id) ?: saveNote(note, targetActor) - private suspend fun saveNote(note: Note, targetActor: String?, url: String): Pair { + private suspend fun saveNote(note: Note, targetActor: String?): Pair { val person = apUserService.fetchPersonWithEntity( note.attributedTo, targetActor ) val post = postRepository.findByApId(note.id) - if (post != null) { return note to post } logger.debug("VISIBILITY url: {} to: {} cc: {}", note.id, note.to, note.cc) - - val visibility = if (note.to.contains(public)) { - Visibility.PUBLIC - } else if (note.to.contains(person.second.followers) && note.cc.contains(public)) { - Visibility.UNLISTED - } else if (note.to.contains(person.second.followers)) { - Visibility.FOLLOWERS - } else { - Visibility.DIRECT - } - + val visibility = visibility(note, person) logger.debug("VISIBILITY is {} url: {}", visibility.name, note.id) val reply = note.inReplyTo?.let { @@ -176,14 +166,7 @@ class APNoteServiceImpl( postRepository.findByUrl(it) } - val emojis = note.tag - .filterIsInstance() - .map { - emojiService.fetchEmoji(it).second - } - .map { - it.id - } + val emojis = buildEmojis(note) val mediaList = note.attachment.map { mediaService.uploadRemoteMedia( @@ -228,14 +211,37 @@ class APNoteServiceImpl( ) } - val createRemote = postService.createRemote( - createPost - ) + val createRemote = postService.createRemote(createPost) return note to createRemote } + private suspend fun buildEmojis(note: Note) = note.tag + .filterIsInstance() + .map { + emojiService.fetchEmoji(it).second + } + .map { + it.id + } + + private fun visibility( + note: Note, + person: Pair + ): Visibility { + val visibility = if (note.to.contains(public)) { + Visibility.PUBLIC + } else if (note.to.contains(person.second.followers) && note.cc.contains(public)) { + Visibility.UNLISTED + } else if (note.to.contains(person.second.followers)) { + Visibility.FOLLOWERS + } else { + Visibility.DIRECT + } + return visibility + } + override suspend fun fetchNote(note: Note, targetActor: String?): Note = - saveIfMissing(note, targetActor, note.id).first + saveIfMissing(note, targetActor).first companion object { const val public: String = "https://www.w3.org/ns/activitystreams#Public" diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt index c5d905e4..e9b73fe2 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt @@ -223,7 +223,11 @@ data class Post private constructor( } fun isPureRepost(): Boolean = - this.text.isEmpty() && this.content.isEmpty() && this.overview == null && this.replyId == null && this.repostId != null + this.text.isEmpty() && + this.content.isEmpty() && + this.overview == null && + this.replyId == null && + this.repostId != null fun delete(): Post { return Post( diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/relationship/RelationshipRepository.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/relationship/RelationshipRepository.kt index b08180a6..c934e18b 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/relationship/RelationshipRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/relationship/RelationshipRepository.kt @@ -36,6 +36,7 @@ interface RelationshipRepository { suspend fun findByTargetIdAndFollowing(targetId: Long, following: Boolean): List + @Suppress("FunctionMaxLength") suspend fun findByTargetIdAndFollowRequestAndIgnoreFollowRequest( targetId: Long, followRequest: Boolean, diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/domain/model/MastodonNotificationRepository.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/domain/model/MastodonNotificationRepository.kt index e1294843..7ee7342c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/domain/model/MastodonNotificationRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/domain/model/MastodonNotificationRepository.kt @@ -8,6 +8,7 @@ interface MastodonNotificationRepository { suspend fun deleteById(id: Long) suspend fun findById(id: Long): MastodonNotification? + @Suppress("FunctionMaxLength") suspend fun findByUserIdAndInTypesAndInSourceActorId( loginUser: Long, types: List, diff --git a/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt b/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt index f577f48d..16c5900f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiController.kt @@ -84,7 +84,7 @@ class MastodonAccountApiController( maxId?.toLongOrNull(), sinceId?.toLongOrNull(), minId?.toLongOrNull(), - limit.coerceIn(0, 80) ?: 40 + limit.coerceIn(0, 80) ) ) val httpHeader = statuses.toHttpHeader(