Fixes leaving and joining channels

This commit is contained in:
Vitor Pamplona
2025-07-23 08:44:41 -04:00
parent 95f463b0aa
commit 064a732ec8
8 changed files with 105 additions and 23 deletions
@@ -41,5 +41,5 @@ data class RoomId(
}
}
fun toTagArray() = RoomIdTag.assemble(id, relayUrl)
fun toTagArray() = RoomIdTag.assemble(this)
}
@@ -23,6 +23,8 @@ package com.vitorpamplona.quartz.experimental.ephemChat.list
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.experimental.ephemChat.chat.RoomId
import com.vitorpamplona.quartz.experimental.ephemChat.list.rooms
import com.vitorpamplona.quartz.experimental.ephemChat.list.tags.RoomIdTag
import com.vitorpamplona.quartz.experimental.ephemChat.list.tags.RoomIdTag.Companion.parse
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.TagArray
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
@@ -35,7 +37,7 @@ import com.vitorpamplona.quartz.nip31Alts.AltTag
import com.vitorpamplona.quartz.nip31Alts.alt
import com.vitorpamplona.quartz.nip51Lists.PrivateTagArrayEvent
import com.vitorpamplona.quartz.nip51Lists.encryption.PrivateTagsInContent
import com.vitorpamplona.quartz.nip51Lists.remove
import com.vitorpamplona.quartz.nip51Lists.removeParsing
import com.vitorpamplona.quartz.utils.TimeUtils
import java.lang.reflect.Modifier.isPrivate
@@ -48,6 +50,10 @@ class EphemeralChatListEvent(
content: String,
sig: HexKey,
) : PrivateTagArrayEvent(id, pubKey, createdAt, KIND, tags, content, sig) {
fun publicRooms() = tags.rooms()
fun publicRoomSet() = tags.roomSet()
companion object {
const val KIND = 10023
const val ALT = "Ephemeral Chat List"
@@ -109,8 +115,8 @@ class EphemeralChatListEvent(
): EphemeralChatListEvent {
val privateTags = earlierVersion.privateTags(signer) ?: throw SignerExceptions.UnauthorizedDecryptionException()
return resign(
privateTags = privateTags.remove(room.toTagArray()),
tags = earlierVersion.tags.remove(room.toTagArray()),
privateTags = privateTags.removeParsing(RoomIdTag::parse, room),
tags = earlierVersion.tags.removeParsing(RoomIdTag::parse, room),
signer = signer,
createdAt = createdAt,
)
@@ -37,8 +37,8 @@ import com.vitorpamplona.quartz.nip31Alts.alt
import com.vitorpamplona.quartz.nip51Lists.PrivateTagArrayEvent
import com.vitorpamplona.quartz.nip51Lists.encryption.PrivateTagsInContent
import com.vitorpamplona.quartz.nip51Lists.encryption.signNip51List
import com.vitorpamplona.quartz.nip51Lists.remove
import com.vitorpamplona.quartz.nip51Lists.removeAny
import com.vitorpamplona.quartz.nip51Lists.removeParsing
import com.vitorpamplona.quartz.utils.TimeUtils
@Immutable
@@ -142,8 +142,8 @@ class ChannelListEvent(
): ChannelListEvent {
val privateTags = earlierVersion.privateTags(signer) ?: throw SignerExceptions.UnauthorizedDecryptionException()
return resign(
privateTags = privateTags.remove(channel.toTagArray()),
tags = earlierVersion.tags.remove(channel.toTagArray()),
privateTags = privateTags.removeParsing(ChannelTag::parseId, channel.eventId),
tags = earlierVersion.tags.removeParsing(ChannelTag::parseId, channel.eventId),
signer = signer,
createdAt = createdAt,
)
@@ -20,9 +20,11 @@
*/
package com.vitorpamplona.quartz.nip51Lists
import com.vitorpamplona.quartz.nip01Core.core.Tag
import com.vitorpamplona.quartz.nip01Core.core.TagArray
import com.vitorpamplona.quartz.utils.startsWith
import com.vitorpamplona.quartz.utils.startsWithAny
import kotlin.collections.ArrayList
inline fun TagArray.filterToArray(predicate: (Array<String>) -> Boolean): TagArray = filterTo(ArrayList(), predicate).toTypedArray()
@@ -30,6 +32,17 @@ inline fun TagArray.remove(predicate: (Array<String>) -> Boolean): TagArray = fi
fun TagArray.remove(startsWith: Array<String>): TagArray = filterNotTo(ArrayList(this.size), { it.startsWith(startsWith) }).toTypedArray()
fun <R> TagArray.removeParsing(
transform: (Tag) -> R,
equalsTo: R,
): TagArray =
filterNotTo(
destination = ArrayList(this.size),
predicate = {
transform(it) == equalsTo
},
).toTypedArray()
fun TagArray.removeAny(startsWith: List<Array<String>>): TagArray =
filterNotTo(
ArrayList(this.size),