Removes the need for an extra object for the tag array in the custom emoji nip

This commit is contained in:
Vitor Pamplona
2026-01-13 17:32:38 -05:00
parent 288fccd414
commit 6097a0b5b7
4 changed files with 10 additions and 12 deletions
@@ -375,7 +375,7 @@ fun CustomEmojiChecker(
onEmojiText: @Composable (ImmutableList<CustomEmoji.Renderable>) -> Unit,
) {
val mayContainEmoji by remember(text, tags) {
mutableStateOf(CustomEmoji.fastMightContainEmoji(text, tags))
mutableStateOf(CustomEmoji.fastMightContainEmoji(text, tags?.lists))
}
if (mayContainEmoji) {
@@ -385,7 +385,7 @@ fun CustomEmojiChecker(
}
LaunchedEffect(text, tags) {
val newEmojiList = CustomEmoji.assembleAnnotatedList(text, tags)
val newEmojiList = CustomEmoji.assembleAnnotatedList(text, tags?.lists)
if (newEmojiList != null) {
emojiList = newEmojiList
}
@@ -140,7 +140,7 @@ class RichTextParser {
val imageUrls = imagesForPager.filterValues { it is MediaUrlImage }.keys
val videoUrls = imagesForPager.filterValues { it is MediaUrlVideo }.keys
val emojiMap = CustomEmoji.createEmojiMap(tags)
val emojiMap = CustomEmoji.createEmojiMap(tags.lists)
val segments = findTextSegments(content, imageUrls, videoUrls, urlSet, emojiMap, tags)
@@ -21,7 +21,6 @@
package com.vitorpamplona.quartz.nip30CustomEmoji
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.nip01Core.core.ImmutableListOfLists
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
@@ -32,10 +31,10 @@ class CustomEmoji {
fun fastMightContainEmoji(
input: String,
allTags: ImmutableListOfLists<String>?,
allTags: Array<Array<String>>?,
): Boolean {
if (allTags == null) return false
if (allTags.lists.any { it.size > 2 && it[0] == EmojiUrlTag.TAG_NAME }) return true
if (allTags.any { it.size > 2 && it[0] == EmojiUrlTag.TAG_NAME }) return true
return input.contains(":")
}
@@ -47,7 +46,7 @@ class CustomEmoji {
return input.contains(":")
}
fun createEmojiMap(tags: ImmutableListOfLists<String>): Map<String, String> = tags.lists.filter { it.size > 2 && it[0] == EmojiUrlTag.TAG_NAME }.associate { ":${it[1]}:" to it[2] }
fun createEmojiMap(tags: Array<Array<String>>): Map<String, String> = tags.filter { it.size > 2 && it[0] == EmojiUrlTag.TAG_NAME }.associate { ":${it[1]}:" to it[2] }
fun findAllEmojis(input: String): List<String> {
val matcher = customEmojiPattern.findAll(input)
@@ -67,11 +66,11 @@ class CustomEmoji {
fun assembleAnnotatedList(
input: String,
allTags: ImmutableListOfLists<String>?,
tags: Array<Array<String>>?,
): ImmutableList<Renderable>? {
if (allTags == null || allTags.lists.isEmpty()) return null
if (tags == null || tags.isEmpty()) return null
val emojiPairs = createEmojiMap(allTags)
val emojiPairs = createEmojiMap(tags)
return assembleAnnotatedList(input, emojiPairs)
}
@@ -20,7 +20,6 @@
*/
package com.vitorpamplona.quartz.nip30CustomEmoji
import com.vitorpamplona.quartz.nip01Core.core.ImmutableListOfLists
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
@@ -150,7 +149,7 @@ class Nip30Test {
"#ioメシヨソイゲーム\n" +
"https://misskey.io/play/9g3qza4jow"
val result = CustomEmoji.assembleAnnotatedList(input, ImmutableListOfLists(tags))
val result = CustomEmoji.assembleAnnotatedList(input, tags)
assertEquals(9, result!!.size)