Moves channels to commons and removes the dependency in the cache from Note
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.model
|
||||
|
||||
import androidx.compose.runtime.Stable
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||
import com.vitorpamplona.quartz.utils.cache.LargeCache
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
@Stable
|
||||
abstract class Channel : NotesGatherer {
|
||||
companion object {
|
||||
val DefaultFeedOrder: Comparator<Note> =
|
||||
compareByDescending<Note> { it.createdAt() }.thenBy { it.idHex }
|
||||
}
|
||||
|
||||
val notes = LargeCache<HexKey, Note>()
|
||||
var lastNote: Note? = null
|
||||
|
||||
private var relays = mapOf<NormalizedRelayUrl, Counter>()
|
||||
|
||||
private var changesFlow: WeakReference<MutableSharedFlow<ListChange<Note>>> = WeakReference(null)
|
||||
|
||||
fun changesFlow(): MutableSharedFlow<ListChange<Note>> {
|
||||
val current = changesFlow.get()
|
||||
if (current != null) return current
|
||||
val new = MutableSharedFlow<ListChange<Note>>(0, 10, BufferOverflow.DROP_OLDEST)
|
||||
changesFlow = WeakReference(new)
|
||||
return new
|
||||
}
|
||||
|
||||
open fun participatingAuthors(maxTimeLimit: Long) =
|
||||
notes.mapNotNull { key, value ->
|
||||
val createdAt = value.createdAt()
|
||||
if (createdAt != null && createdAt > maxTimeLimit) {
|
||||
value.author
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
abstract fun toBestDisplayName(): String
|
||||
|
||||
open fun relays(): Set<NormalizedRelayUrl> =
|
||||
relays.keys
|
||||
.toSortedSet { o1, o2 ->
|
||||
val o1Count = relays[o1]?.number ?: 0
|
||||
val o2Count = relays[o2]?.number ?: 0
|
||||
o2Count.compareTo(o1Count) // descending
|
||||
}
|
||||
|
||||
fun updateChannelInfo() {
|
||||
flowSet?.metadata?.invalidateData()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun addRelaySync(briefInfo: NormalizedRelayUrl) {
|
||||
if (briefInfo !in relays) {
|
||||
relays = relays + Pair(briefInfo, Counter(1))
|
||||
}
|
||||
}
|
||||
|
||||
fun addRelay(relay: NormalizedRelayUrl) {
|
||||
val counter = relays[relay]
|
||||
if (counter != null) {
|
||||
counter.number++
|
||||
} else {
|
||||
addRelaySync(relay)
|
||||
}
|
||||
}
|
||||
|
||||
fun addNote(
|
||||
note: Note,
|
||||
relay: NormalizedRelayUrl? = null,
|
||||
) {
|
||||
if (!notes.containsKey(note.idHex)) {
|
||||
notes.put(note.idHex, note)
|
||||
note.addGatherer(this)
|
||||
|
||||
if ((note.createdAt() ?: 0L) > (lastNote?.createdAt() ?: 0L)) {
|
||||
lastNote = note
|
||||
}
|
||||
|
||||
if (relay != null) {
|
||||
addRelay(relay)
|
||||
}
|
||||
|
||||
changesFlow.get()?.tryEmit(ListChange.Addition(note))
|
||||
|
||||
flowSet?.notes?.invalidateData()
|
||||
}
|
||||
}
|
||||
|
||||
override fun removeNote(note: Note) {
|
||||
if (notes.containsKey(note.idHex)) {
|
||||
notes.remove(note.idHex)
|
||||
note.removeGatherer(this)
|
||||
|
||||
if (note == lastNote) {
|
||||
lastNote = notes.values().sortedWith(DefaultFeedOrder).firstOrNull()
|
||||
}
|
||||
|
||||
changesFlow.get()?.tryEmit(ListChange.Deletion(note))
|
||||
|
||||
flowSet?.notes?.invalidateData()
|
||||
}
|
||||
}
|
||||
|
||||
fun pruneOldMessages(): Set<Note> {
|
||||
val important =
|
||||
notes
|
||||
.values()
|
||||
.sortedWith(DefaultFeedOrder)
|
||||
.take(500)
|
||||
.toSet()
|
||||
|
||||
val toBeRemoved = notes.filter { key, it -> it !in important }
|
||||
|
||||
toBeRemoved.forEach { notes.remove(it.idHex) }
|
||||
|
||||
changesFlow.get()?.tryEmit(ListChange.SetDeletion(toBeRemoved.toSet()))
|
||||
|
||||
flowSet?.notes?.invalidateData()
|
||||
|
||||
return toBeRemoved.toSet()
|
||||
}
|
||||
|
||||
fun pruneHiddenMessages(account: IAccount): Set<Note> {
|
||||
val hidden =
|
||||
notes
|
||||
.filter { key, it ->
|
||||
it.author?.let { author -> account.isHidden(author) } == true
|
||||
}.toSet()
|
||||
|
||||
hidden.forEach { notes.remove(it.idHex) }
|
||||
|
||||
changesFlow.get()?.tryEmit(ListChange.SetDeletion(hidden))
|
||||
|
||||
flowSet?.notes?.invalidateData()
|
||||
|
||||
return hidden.toSet()
|
||||
}
|
||||
|
||||
var flowSet: ChannelFlowSet? = null
|
||||
|
||||
@Synchronized
|
||||
fun createOrDestroyFlowSync(create: Boolean) {
|
||||
if (create) {
|
||||
if (flowSet == null) {
|
||||
flowSet = ChannelFlowSet(this)
|
||||
}
|
||||
} else {
|
||||
if (flowSet != null && flowSet?.isInUse() == false) {
|
||||
flowSet = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun flow(): ChannelFlowSet {
|
||||
if (flowSet == null) {
|
||||
createOrDestroyFlowSync(true)
|
||||
}
|
||||
return flowSet!!
|
||||
}
|
||||
|
||||
fun clearFlow() {
|
||||
if (flowSet != null && flowSet?.isInUse() == false) {
|
||||
createOrDestroyFlowSync(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class Counter(
|
||||
var number: Int = 0,
|
||||
)
|
||||
|
||||
@Stable
|
||||
class ChannelFlowSet(
|
||||
u: Channel,
|
||||
) {
|
||||
// Observers line up here.
|
||||
val metadata = ChannelFlow(u)
|
||||
val notes = ChannelFlow(u)
|
||||
|
||||
fun isInUse(): Boolean =
|
||||
metadata.hasObservers() ||
|
||||
notes.hasObservers()
|
||||
}
|
||||
|
||||
class ChannelFlow(
|
||||
val channel: Channel,
|
||||
) {
|
||||
val stateFlow = MutableStateFlow(ChannelState(channel))
|
||||
|
||||
fun invalidateData() {
|
||||
stateFlow.tryEmit(ChannelState(channel))
|
||||
}
|
||||
|
||||
fun hasObservers() = stateFlow.subscriptionCount.value > 0
|
||||
}
|
||||
|
||||
class ChannelState(
|
||||
val channel: Channel,
|
||||
)
|
||||
@@ -84,4 +84,6 @@ interface IAccount {
|
||||
|
||||
/** Set of followed user pubkeys (for feed ordering/highlighting) */
|
||||
fun followingKeySet(): Set<String>
|
||||
|
||||
fun isHidden(user: User): Boolean
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.model
|
||||
|
||||
sealed class ListChange<out T> {
|
||||
data class Addition<T>(
|
||||
val item: T,
|
||||
) : ListChange<T>()
|
||||
|
||||
data class Deletion<T>(
|
||||
val item: T,
|
||||
) : ListChange<T>()
|
||||
|
||||
data class SetAddition<T>(
|
||||
val item: Set<T>,
|
||||
) : ListChange<T>()
|
||||
|
||||
data class SetDeletion<T>(
|
||||
val item: Set<T>,
|
||||
) : ListChange<T>()
|
||||
}
|
||||
@@ -22,13 +22,13 @@ package com.vitorpamplona.amethyst.commons.model
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import androidx.compose.runtime.Stable
|
||||
import com.vitorpamplona.amethyst.commons.model.cache.ICacheProvider
|
||||
import com.vitorpamplona.amethyst.commons.threading.checkNotInMainThread
|
||||
import com.vitorpamplona.amethyst.commons.util.firstFullCharOrEmoji
|
||||
import com.vitorpamplona.amethyst.commons.util.replace
|
||||
import com.vitorpamplona.amethyst.commons.util.toShortDisplay
|
||||
import com.vitorpamplona.quartz.experimental.bounties.addedRewardValue
|
||||
import com.vitorpamplona.quartz.experimental.bounties.hasAdditionalReward
|
||||
import com.vitorpamplona.quartz.experimental.ephemChat.chat.EphemeralChatEvent
|
||||
import com.vitorpamplona.quartz.lightning.LnInvoiceUtil
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Address
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
@@ -57,6 +57,7 @@ import com.vitorpamplona.quartz.nip47WalletConnect.LnZapPaymentResponseEvent
|
||||
import com.vitorpamplona.quartz.nip47WalletConnect.PayInvoiceMethod
|
||||
import com.vitorpamplona.quartz.nip47WalletConnect.PayInvoiceSuccessResponse
|
||||
import com.vitorpamplona.quartz.nip53LiveActivities.chat.LiveActivitiesChatMessageEvent
|
||||
import com.vitorpamplona.quartz.nip53LiveActivities.streaming.LiveActivitiesEvent
|
||||
import com.vitorpamplona.quartz.nip56Reports.ReportEvent
|
||||
import com.vitorpamplona.quartz.nip56Reports.ReportType
|
||||
import com.vitorpamplona.quartz.nip57Zaps.LnZapEvent
|
||||
@@ -109,7 +110,6 @@ class AddressableNote(
|
||||
@Stable
|
||||
open class Note(
|
||||
val idHex: String,
|
||||
private val cacheProvider: ICacheProvider? = null,
|
||||
) : NotesGatherer {
|
||||
// These fields are only available after the Text Note event is received.
|
||||
// They are immutable after that.
|
||||
@@ -196,15 +196,28 @@ open class Note(
|
||||
}
|
||||
|
||||
fun relayHintUrl(): NormalizedRelayUrl? {
|
||||
val noteEvent = event
|
||||
val communityPostRelays =
|
||||
when (noteEvent) {
|
||||
is CommunityDefinitionEvent -> noteEvent.relayUrls().ifEmpty { null }?.toSet()
|
||||
is IsInPublicChatChannel -> cacheProvider?.getAnyChannel(this)?.relays()
|
||||
else -> null
|
||||
// checks Community Events first
|
||||
when (val noteEvent = event) {
|
||||
is CommunityDefinitionEvent -> noteEvent.relayUrls().firstOrNull()?.let { return it }
|
||||
is IsInPublicChatChannel -> {
|
||||
inGatherers?.forEach {
|
||||
if (it is com.vitorpamplona.amethyst.commons.model.Channel) {
|
||||
it.relays().firstOrNull()?.let { return it }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!communityPostRelays.isNullOrEmpty()) return (communityPostRelays as? Collection<NormalizedRelayUrl?>)?.firstOrNull()
|
||||
is LiveActivitiesEvent -> {
|
||||
noteEvent.relays().ifEmpty { null }?.toSet()
|
||||
}
|
||||
is LiveActivitiesChatMessageEvent -> {
|
||||
inGatherers?.forEach {
|
||||
if (it is com.vitorpamplona.amethyst.commons.model.Channel) {
|
||||
it.relays().firstOrNull()?.let { return it }
|
||||
}
|
||||
}
|
||||
}
|
||||
is EphemeralChatEvent -> noteEvent.roomId()?.let { return it.relayUrl }
|
||||
}
|
||||
|
||||
val currentOutbox = author?.outboxRelays()?.toSet()
|
||||
|
||||
|
||||
Vendored
+3
-14
@@ -20,6 +20,8 @@
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.model.cache
|
||||
|
||||
import com.vitorpamplona.amethyst.commons.model.Channel
|
||||
import com.vitorpamplona.amethyst.commons.model.Note
|
||||
import com.vitorpamplona.amethyst.commons.model.User
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
|
||||
@@ -43,7 +45,7 @@ interface ICacheProvider {
|
||||
* @param note The note to look up channel for
|
||||
* @return The channel if found, null otherwise
|
||||
*/
|
||||
fun getAnyChannel(note: Any?): IChannel?
|
||||
fun getAnyChannel(note: Note): Channel?
|
||||
|
||||
/**
|
||||
* Gets a User by public key hex.
|
||||
@@ -98,16 +100,3 @@ interface ICacheProvider {
|
||||
*/
|
||||
fun hasBeenDeleted(event: Any): Boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Minimal channel interface for relay resolution.
|
||||
* Full channel implementations (PublicChatChannel, LiveActivitiesChannel)
|
||||
* implement this interface.
|
||||
*/
|
||||
interface IChannel {
|
||||
/**
|
||||
* Gets the relay URLs for this channel.
|
||||
* @return List of relay URLs or null if none configured
|
||||
*/
|
||||
fun relays(): List<Any>?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user