Keeps cache of the content inside Public chat forums

This commit is contained in:
Vitor Pamplona
2025-07-10 18:08:32 -04:00
parent f3e08bd297
commit dc61c5a8e4
2 changed files with 49 additions and 14 deletions

View File

@@ -22,11 +22,13 @@ package com.vitorpamplona.quartz.nip28PublicChat.admin
import android.R.attr.data
import android.util.Log
import android.util.Log.e
import androidx.compose.runtime.Immutable
import com.fasterxml.jackson.core.JsonParseException
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
import com.vitorpamplona.quartz.nip01Core.core.has
import com.vitorpamplona.quartz.nip01Core.hints.EventHintProvider
import com.vitorpamplona.quartz.nip01Core.hints.types.EventIdHint
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
@@ -46,17 +48,33 @@ class ChannelCreateEvent(
sig: HexKey,
) : Event(id, pubKey, createdAt, KIND, tags, content, sig),
EventHintProvider {
@Transient
var cache: ChannelDataNorm? = null
override fun eventHints() = channelInfo().relays?.map { EventIdHint(id, it) } ?: emptyList()
override fun linkedEventIds() = listOf(id)
fun channelInfo(): ChannelDataNorm =
try {
ChannelData.parse(content)?.normalize() ?: ChannelDataNorm()
} catch (e: JsonParseException) {
Log.w("ChannelCreateEvent", "Failure to parse ${this.toJson()}", e)
ChannelDataNorm()
}
fun isEncrypted() = tags.any { it.has(1) && it[0] == "encrypted" && it[1] == "true" }
fun channelInfo(): ChannelDataNorm {
cache?.let { return it }
val newInfo =
try {
if (isEncrypted()) {
ChannelDataNorm()
} else {
ChannelData.parse(content)?.normalize() ?: ChannelDataNorm()
}
} catch (e: JsonParseException) {
Log.w("ChannelCreateEvent", "Failure to parse ${this.toJson()}", e)
ChannelDataNorm()
}
cache = newInfo
return newInfo
}
companion object {
const val KIND = 40

View File

@@ -25,6 +25,7 @@ import androidx.compose.runtime.Immutable
import com.fasterxml.jackson.core.JsonParseException
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
import com.vitorpamplona.quartz.nip01Core.core.has
import com.vitorpamplona.quartz.nip01Core.hints.EventHintBundle
import com.vitorpamplona.quartz.nip01Core.hints.EventHintProvider
import com.vitorpamplona.quartz.nip01Core.hints.types.EventIdHint
@@ -48,6 +49,9 @@ class ChannelMetadataEvent(
sig: HexKey,
) : BasePublicChatEvent(id, pubKey, createdAt, KIND, tags, content, sig),
EventHintProvider {
@Transient
var cache: ChannelDataNorm? = null
override fun eventHints() =
channelInfo().relays?.mapNotNull { relay ->
channelId()?.let { EventIdHint(it, relay) }
@@ -55,13 +59,26 @@ class ChannelMetadataEvent(
override fun linkedEventIds() = channelId()?.let { listOf(it) } ?: emptyList()
fun channelInfo(): ChannelDataNorm =
try {
ChannelData.parse(content)?.normalize() ?: ChannelDataNorm()
} catch (e: JsonParseException) {
Log.w("ChannelCreateEvent", "Failure to parse ${this.toJson()}", e)
ChannelDataNorm()
}
fun isEncrypted() = tags.any { it.has(1) && it[0] == "encrypted" && it[1] == "true" }
fun channelInfo(): ChannelDataNorm {
cache?.let { return it }
val newInfo =
try {
if (isEncrypted()) {
ChannelDataNorm()
} else {
ChannelData.parse(content)?.normalize() ?: ChannelDataNorm()
}
} catch (e: JsonParseException) {
Log.w("ChannelCreateEvent", "Failure to parse ${this.toJson()}", e)
ChannelDataNorm()
}
cache = newInfo
return newInfo
}
companion object {
const val KIND = 41