Merge branch 'main' into main

This commit is contained in:
greenart7c3
2024-03-25 07:08:08 -03:00
committed by GitHub
10 changed files with 96 additions and 76 deletions
@@ -208,7 +208,7 @@ class Account(
val saveable: AccountLiveData = AccountLiveData(this)
@Immutable
data class LiveFollowLists(
class LiveFollowLists(
val users: ImmutableSet<String> = persistentSetOf(),
val hashtags: ImmutableSet<String> = persistentSetOf(),
val geotags: ImmutableSet<String> = persistentSetOf(),
@@ -108,10 +108,9 @@ class LiveActivitiesChannel(val address: ATag) : Channel(address.toTag()) {
@Stable
abstract class Channel(val idHex: String) {
var creator: User? = null
var updatedMetadataAt: Long = 0
val notes = LargeCache<HexKey, Note>()
var lastNoteCreatedAt: Long = 0
open fun id() = Hex.decode(idHex)
@@ -147,6 +146,10 @@ abstract class Channel(val idHex: String) {
fun addNote(note: Note) {
notes.put(note.idHex, note)
if ((note.createdAt() ?: 0) > lastNoteCreatedAt) {
lastNoteCreatedAt = note.createdAt() ?: 0
}
}
fun removeNote(note: Note) {
@@ -130,7 +130,7 @@ object LocalCache {
val notes = LargeCache<HexKey, Note>()
val addressables = LargeCache<String, AddressableNote>()
val drafts = ConcurrentHashMap<String, MutableList<Drafts>>()
val channels = ConcurrentHashMap<HexKey, Channel>()
val channels = LargeCache<HexKey, Channel>()
val awaitingPaymentRequests = ConcurrentHashMap<HexKey, Pair<Note?, (LnZapPaymentResponseEvent) -> Unit>>(10)
fun checkGetOrCreateUser(key: String): User? {
@@ -193,7 +193,7 @@ object LocalCache {
}
fun getChannelIfExists(key: String): Channel? {
return channels[key]
return channels.get(key)
}
fun checkGetOrCreateNote(key: String): Note? {
@@ -246,15 +246,24 @@ object LocalCache {
}
}
fun getOrCreateChannel(
key: String,
channelFactory: (String) -> Channel,
): Channel {
checkNotInMainThread()
return channels.getOrCreate(key, channelFactory)
}
fun checkGetOrCreateChannel(key: String): Channel? {
checkNotInMainThread()
if (isValidHex(key)) {
return getOrCreateChannel(key) { PublicChatChannel(key) }
return channels.getOrCreate(key) { PublicChatChannel(key) }
}
val aTag = ATag.parse(key, null)
if (aTag != null) {
return getOrCreateChannel(aTag.toTag()) { LiveActivitiesChannel(aTag) }
return channels.getOrCreate(aTag.toTag()) { LiveActivitiesChannel(aTag) }
}
return null
}
@@ -266,19 +275,6 @@ object LocalCache {
return HexValidator.isHex(key)
}
fun getOrCreateChannel(
key: String,
channelFactory: (String) -> Channel,
): Channel {
checkNotInMainThread()
return channels[key]
?: run {
val newObject = channelFactory(key)
channels.putIfAbsent(key, newObject) ?: newObject
}
}
fun checkGetOrCreateAddressableNote(key: String): AddressableNote? {
return try {
val addr = ATag.parse(key, null) // relay doesn't matter for the index.
@@ -970,10 +966,10 @@ object LocalCache {
masterNote.removeReport(deleteNote)
}
deleteNote.channelHex()?.let { channels[it]?.removeNote(deleteNote) }
deleteNote.channelHex()?.let { getChannelIfExists(it)?.removeNote(deleteNote) }
(deleteNote.event as? LiveActivitiesChatMessageEvent)?.activity()?.let {
channels[it.toTag()]?.removeNote(deleteNote)
getChannelIfExists(it.toTag())?.removeNote(deleteNote)
}
if (deleteNote.event is PrivateDmEvent) {
@@ -1710,14 +1706,14 @@ object LocalCache {
checkNotInMainThread()
val key = decodeEventIdAsHexOrNull(text)
if (key != null && channels[key] != null) {
return listOfNotNull(channels[key])
if (key != null && getChannelIfExists(key) != null) {
return listOfNotNull(getChannelIfExists(key))
}
return channels.values.filter {
it.anyNameStartsWith(text) ||
it.idHex.startsWith(text, true) ||
it.idNote().startsWith(text, true)
return channels.filter { _, channel ->
channel.anyNameStartsWith(text) ||
channel.idHex.startsWith(text, true) ||
channel.idNote().startsWith(text, true)
}
}
@@ -1796,8 +1792,8 @@ object LocalCache {
fun pruneOldAndHiddenMessages(account: Account) {
checkNotInMainThread()
channels.forEach { it ->
val toBeRemoved = it.value.pruneOldAndHiddenMessages(account)
channels.forEach { _, channel ->
val toBeRemoved = channel.pruneOldAndHiddenMessages(account)
val childrenToBeRemoved = mutableListOf<Note>()
@@ -1809,9 +1805,9 @@ object LocalCache {
removeFromCache(childrenToBeRemoved)
if (toBeRemoved.size > 100 || it.value.notes.size() > 100) {
if (toBeRemoved.size > 100 || channel.notes.size() > 100) {
println(
"PRUNE: ${toBeRemoved.size} messages removed from ${it.value.toBestDisplayName()}. ${it.value.notes.size()} kept",
"PRUNE: ${toBeRemoved.size} messages removed from ${channel.toBestDisplayName()}. ${channel.notes.size()} kept",
)
}
}
@@ -178,9 +178,8 @@ object NostrDiscoveryDataSource : NostrDataSource("DiscoveryFeed") {
filter =
JsonFilter(
authors = follows,
kinds =
listOf(ChannelCreateEvent.KIND, ChannelMetadataEvent.KIND, ChannelMessageEvent.KIND),
limit = 300,
kinds = listOf(ChannelMessageEvent.KIND),
limit = 500,
since =
latestEOSEs.users[account.userProfile()]
?.followList
@@ -194,7 +193,7 @@ object NostrDiscoveryDataSource : NostrDataSource("DiscoveryFeed") {
filter =
JsonFilter(
ids = followChats,
kinds = listOf(ChannelCreateEvent.KIND),
kinds = listOf(ChannelCreateEvent.KIND, ChannelMessageEvent.KIND),
limit = 300,
since =
latestEOSEs.users[account.userProfile()]
@@ -23,7 +23,7 @@ package com.vitorpamplona.amethyst.ui.dal
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.ParticipantListBuilder
import com.vitorpamplona.amethyst.model.PublicChatChannel
import com.vitorpamplona.quartz.events.ChannelCreateEvent
import com.vitorpamplona.quartz.events.IsInPublicChatChannel
import com.vitorpamplona.quartz.events.MuteListEvent
@@ -42,12 +42,25 @@ open class DiscoverChatFeedFilter(val account: Account) : AdditiveFeedFilter<Not
}
override fun feed(): List<Note> {
val params = buildFilterParams(account)
val allChannelNotes =
LocalCache.channels.values.mapNotNull { LocalCache.getNoteIfExists(it.idHex) }
LocalCache.channels.mapNotNullIntoSet { _, channel ->
if (channel is PublicChatChannel) {
val note = LocalCache.getNoteIfExists(channel.idHex)
val noteEvent = note?.event
val notes = innerApplyFilter(allChannelNotes)
if (noteEvent == null || params.match(noteEvent)) {
note
} else {
null
}
} else {
null
}
}
return sort(notes)
return sort(allChannelNotes)
}
override fun applyFilter(collection: Set<Note>): Set<Note> {
@@ -70,11 +83,21 @@ open class DiscoverChatFeedFilter(val account: Account) : AdditiveFeedFilter<Not
// note event here will never be null
val noteEvent = note.event
if (noteEvent is ChannelCreateEvent && params.match(noteEvent)) {
note
if ((LocalCache.getChannelIfExists(noteEvent.id)?.notes?.size() ?: 0) > 0) {
note
} else {
null
}
} else if (noteEvent is IsInPublicChatChannel) {
val channel = noteEvent.channel()?.let { LocalCache.checkGetOrCreateNote(it) }
if (channel != null && (channel.event == null || params.match(channel.event))) {
channel
if (channel != null &&
(channel.event == null || (channel.event is ChannelCreateEvent && params.match(channel.event)))
) {
if ((LocalCache.getChannelIfExists(channel.idHex)?.notes?.size() ?: 0) > 0) {
channel
} else {
null
}
} else {
null
}
@@ -85,17 +108,15 @@ open class DiscoverChatFeedFilter(val account: Account) : AdditiveFeedFilter<Not
}
override fun sort(collection: Set<Note>): List<Note> {
val followingKeySet =
account.liveDiscoveryFollowLists.value?.users ?: account.liveKind3Follows.value.users
val counter = ParticipantListBuilder()
val participantCounts =
collection.associate { it to counter.countFollowsThatParticipateOn(it, followingKeySet) }
val lastNote =
collection.associateWith { note ->
LocalCache.getChannelIfExists(note.idHex)?.lastNoteCreatedAt ?: 0
}
return collection
.sortedWith(
compareBy(
{ participantCounts[it] },
{ lastNote[it] },
{ it.createdAt() },
{ it.idHex },
),
@@ -23,7 +23,6 @@ package com.vitorpamplona.amethyst.ui.dal
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.ParticipantListBuilder
import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.events.CommunityDefinitionEvent
import com.vitorpamplona.quartz.events.CommunityPostApprovalEvent
@@ -112,21 +111,15 @@ open class DiscoverCommunityFeedFilter(val account: Account) : AdditiveFeedFilte
) = aTag != null && aTag.kind == CommunityDefinitionEvent.KIND && params.match(aTag)
override fun sort(collection: Set<Note>): List<Note> {
val followingKeySet =
account.liveDiscoveryFollowLists.value?.users ?: account.liveKind3Follows.value.users
val counter = ParticipantListBuilder()
val participantCounts =
collection.associate { it to counter.countFollowsThatParticipateOn(it, followingKeySet) }
val allParticipants =
collection.associate { it to counter.countFollowsThatParticipateOn(it, null) }
val lastNote =
collection.associateWith { note ->
note.boosts.maxOfOrNull { it.createdAt() ?: 0 } ?: 0
}
return collection
.sortedWith(
compareBy(
{ participantCounts[it] },
{ allParticipants[it] },
{ lastNote[it] },
{ it.createdAt() },
{ it.idHex },
),
@@ -48,8 +48,8 @@ open class DiscoverLiveFeedFilter(
}
override fun feed(): List<Note> {
val allChannelNotes = LocalCache.channels.values.mapNotNull { LocalCache.getNoteIfExists(it.idHex) }
val allMessageNotes = LocalCache.channels.values.map { it.notes.filter { key, it -> it.event is LiveActivitiesEvent } }.flatten()
val allChannelNotes = LocalCache.channels.mapNotNull { _, channel -> LocalCache.getNoteIfExists(channel.idHex) }
val allMessageNotes = LocalCache.channels.map { _, channel -> channel.notes.filter { key, it -> it.event is LiveActivitiesEvent } }.flatten()
val notes = innerApplyFilter(allChannelNotes + allMessageNotes)
@@ -316,7 +316,7 @@ fun WatchAccountForDiscoveryScreen(
discoveryChatFeedViewModel: NostrDiscoverChatFeedViewModel,
accountViewModel: AccountViewModel,
) {
val listState by accountViewModel.account.liveStoriesFollowLists.collectAsStateWithLifecycle()
val listState by accountViewModel.account.liveDiscoveryFollowLists.collectAsStateWithLifecycle()
LaunchedEffect(accountViewModel, listState) {
NostrDiscoveryDataSource.resetFilters()
@@ -348,7 +348,7 @@ private fun DiscoverFeedLoaded(
ChannelCardCompose(
baseNote = item,
routeForLastRead = routeForLastRead,
modifier = Modifier,
modifier = Modifier.fillMaxWidth(),
forceEventKind = forceEventKind,
accountViewModel = accountViewModel,
nav = nav,
@@ -441,6 +441,8 @@
<string name="connectivity_type_always">Siempre</string>
<string name="connectivity_type_wifi_only">Solo Wi-Fi</string>
<string name="connectivity_type_never">Nunca</string>
<string name="ui_feature_set_type_complete">Completo</string>
<string name="ui_feature_set_type_simplified">Simplificado</string>
<string name="system">Sistema</string>
<string name="light">Claro</string>
<string name="dark">Oscuro</string>
@@ -452,6 +454,8 @@
<string name="automatically_show_url_preview">Vista previa de URL</string>
<string name="automatically_hide_nav_bars">Desplazamiento inmersivo</string>
<string name="automatically_hide_nav_bars_description">Ocultar barras de navegación al desplazarse</string>
<string name="ui_style">Modo de interfaz</string>
<string name="ui_style_description">Elegir el estilo de publicación</string>
<string name="load_image">Cargar imagen</string>
<string name="spamming_users">Spammers</string>
<string name="muted_button">Silenciado. Hacer clic para reactivar el sonido.</string>