mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-11 10:42:52 +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.R.attr.data
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import android.util.Log.e
|
||||||
import androidx.compose.runtime.Immutable
|
import androidx.compose.runtime.Immutable
|
||||||
import com.fasterxml.jackson.core.JsonParseException
|
import com.fasterxml.jackson.core.JsonParseException
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
|
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.EventHintProvider
|
||||||
import com.vitorpamplona.quartz.nip01Core.hints.types.EventIdHint
|
import com.vitorpamplona.quartz.nip01Core.hints.types.EventIdHint
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||||
@@ -46,18 +48,34 @@ class ChannelCreateEvent(
|
|||||||
sig: HexKey,
|
sig: HexKey,
|
||||||
) : Event(id, pubKey, createdAt, KIND, tags, content, sig),
|
) : Event(id, pubKey, createdAt, KIND, tags, content, sig),
|
||||||
EventHintProvider {
|
EventHintProvider {
|
||||||
|
@Transient
|
||||||
|
var cache: ChannelDataNorm? = null
|
||||||
|
|
||||||
override fun eventHints() = channelInfo().relays?.map { EventIdHint(id, it) } ?: emptyList()
|
override fun eventHints() = channelInfo().relays?.map { EventIdHint(id, it) } ?: emptyList()
|
||||||
|
|
||||||
override fun linkedEventIds() = listOf(id)
|
override fun linkedEventIds() = listOf(id)
|
||||||
|
|
||||||
fun channelInfo(): ChannelDataNorm =
|
fun isEncrypted() = tags.any { it.has(1) && it[0] == "encrypted" && it[1] == "true" }
|
||||||
|
|
||||||
|
fun channelInfo(): ChannelDataNorm {
|
||||||
|
cache?.let { return it }
|
||||||
|
|
||||||
|
val newInfo =
|
||||||
try {
|
try {
|
||||||
|
if (isEncrypted()) {
|
||||||
|
ChannelDataNorm()
|
||||||
|
} else {
|
||||||
ChannelData.parse(content)?.normalize() ?: ChannelDataNorm()
|
ChannelData.parse(content)?.normalize() ?: ChannelDataNorm()
|
||||||
|
}
|
||||||
} catch (e: JsonParseException) {
|
} catch (e: JsonParseException) {
|
||||||
Log.w("ChannelCreateEvent", "Failure to parse ${this.toJson()}", e)
|
Log.w("ChannelCreateEvent", "Failure to parse ${this.toJson()}", e)
|
||||||
ChannelDataNorm()
|
ChannelDataNorm()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cache = newInfo
|
||||||
|
return newInfo
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val KIND = 40
|
const val KIND = 40
|
||||||
|
|
||||||
|
@@ -25,6 +25,7 @@ import androidx.compose.runtime.Immutable
|
|||||||
import com.fasterxml.jackson.core.JsonParseException
|
import com.fasterxml.jackson.core.JsonParseException
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
|
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.EventHintBundle
|
||||||
import com.vitorpamplona.quartz.nip01Core.hints.EventHintProvider
|
import com.vitorpamplona.quartz.nip01Core.hints.EventHintProvider
|
||||||
import com.vitorpamplona.quartz.nip01Core.hints.types.EventIdHint
|
import com.vitorpamplona.quartz.nip01Core.hints.types.EventIdHint
|
||||||
@@ -48,6 +49,9 @@ class ChannelMetadataEvent(
|
|||||||
sig: HexKey,
|
sig: HexKey,
|
||||||
) : BasePublicChatEvent(id, pubKey, createdAt, KIND, tags, content, sig),
|
) : BasePublicChatEvent(id, pubKey, createdAt, KIND, tags, content, sig),
|
||||||
EventHintProvider {
|
EventHintProvider {
|
||||||
|
@Transient
|
||||||
|
var cache: ChannelDataNorm? = null
|
||||||
|
|
||||||
override fun eventHints() =
|
override fun eventHints() =
|
||||||
channelInfo().relays?.mapNotNull { relay ->
|
channelInfo().relays?.mapNotNull { relay ->
|
||||||
channelId()?.let { EventIdHint(it, relay) }
|
channelId()?.let { EventIdHint(it, relay) }
|
||||||
@@ -55,14 +59,27 @@ class ChannelMetadataEvent(
|
|||||||
|
|
||||||
override fun linkedEventIds() = channelId()?.let { listOf(it) } ?: emptyList()
|
override fun linkedEventIds() = channelId()?.let { listOf(it) } ?: emptyList()
|
||||||
|
|
||||||
fun channelInfo(): ChannelDataNorm =
|
fun isEncrypted() = tags.any { it.has(1) && it[0] == "encrypted" && it[1] == "true" }
|
||||||
|
|
||||||
|
fun channelInfo(): ChannelDataNorm {
|
||||||
|
cache?.let { return it }
|
||||||
|
|
||||||
|
val newInfo =
|
||||||
try {
|
try {
|
||||||
|
if (isEncrypted()) {
|
||||||
|
ChannelDataNorm()
|
||||||
|
} else {
|
||||||
ChannelData.parse(content)?.normalize() ?: ChannelDataNorm()
|
ChannelData.parse(content)?.normalize() ?: ChannelDataNorm()
|
||||||
|
}
|
||||||
} catch (e: JsonParseException) {
|
} catch (e: JsonParseException) {
|
||||||
Log.w("ChannelCreateEvent", "Failure to parse ${this.toJson()}", e)
|
Log.w("ChannelCreateEvent", "Failure to parse ${this.toJson()}", e)
|
||||||
ChannelDataNorm()
|
ChannelDataNorm()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cache = newInfo
|
||||||
|
return newInfo
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val KIND = 41
|
const val KIND = 41
|
||||||
const val ALT = "This is a public chat definition update"
|
const val ALT = "This is a public chat definition update"
|
||||||
|
Reference in New Issue
Block a user