mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-09 21:42:30 +02:00
Keeps cache of the content inside Public chat forums
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user