Removes Cited Posts from the replies (Fixes Quoted Notes)

This commit is contained in:
Vitor Pamplona
2023-02-18 19:09:49 -05:00
parent 6ff474376f
commit 54cdb5ae68

View File

@@ -23,11 +23,15 @@ import java.util.Collections
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import com.vitorpamplona.amethyst.service.relays.Relay import com.vitorpamplona.amethyst.service.relays.Relay
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
import kotlin.time.ExperimentalTime
import kotlin.time.measureTimedValue
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import nostr.postr.events.ContactListEvent import nostr.postr.events.ContactListEvent
import nostr.postr.events.DeletionEvent import nostr.postr.events.DeletionEvent
import nostr.postr.events.Event import nostr.postr.events.Event
@@ -115,7 +119,7 @@ object LocalCache {
if (note.event != null) return if (note.event != null) return
val mentions = event.mentions.map { getOrCreateUser(it) } val mentions = event.mentions.map { getOrCreateUser(it) }
val replyTo = event.replyTos.map { getOrCreateNote(it) } val replyTo = replyToWithoutCitations(event).map { getOrCreateNote(it) }
note.loadEvent(event, author, mentions, replyTo) note.loadEvent(event, author, mentions, replyTo)
@@ -140,44 +144,68 @@ object LocalCache {
refreshObservers() refreshObservers()
} }
private fun replyToWithoutCitations(event: TextNoteEvent): List<String> {
var citations = mutableSetOf<String>()
// Removes citations from replies:
val matcher = tagSearch.matcher(event.content)
while (matcher.find()) {
try {
val tag = matcher.group(1)?.let { event.tags[it.toInt()] }
if (tag != null && tag[0] == "e") {
citations.add(tag[1])
}
} catch (e: Exception) {
}
}
return event.replyTos.filter { it !in citations }
}
fun consume(event: RecommendRelayEvent) { fun consume(event: RecommendRelayEvent) {
//Log.d("RR", event.toJson()) //Log.d("RR", event.toJson())
} }
@OptIn(ExperimentalTime::class)
fun consume(event: ContactListEvent) { fun consume(event: ContactListEvent) {
val user = getOrCreateUser(event.pubKey.toHexKey()) val user = getOrCreateUser(event.pubKey.toHexKey())
if (event.createdAt > user.updatedFollowsAt) { if (event.createdAt > user.updatedFollowsAt) {
Log.d("CL", "AAA ${user.toBestDisplayName()} ${event.follows.size}") val (value, elapsed) = measureTimedValue {
user.updateFollows( user.updateFollows(
event.follows.map { event.follows.map {
try { try {
val pubKey = decodePublicKey(it.pubKeyHex) val pubKey = decodePublicKey(it.pubKeyHex)
getOrCreateUser(pubKey.toHexKey()) getOrCreateUser(pubKey.toHexKey())
} catch (e: Exception) { } catch (e: Exception) {
println("Could not parse Hex key: ${it.pubKeyHex}") println("Could not parse Hex key: ${it.pubKeyHex}")
println("UpdateFollows: " + event.toJson()) println("UpdateFollows: " + event.toJson())
e.printStackTrace() e.printStackTrace()
null null
} }
}.filterNotNull().toSet(), }.filterNotNull().toSet(),
event.createdAt event.createdAt
) )
try {
if (event.content.isNotEmpty()) {
val relays: Map<String, ContactListEvent.ReadWrite> =
Event.gson.fromJson(
event.content,
object : TypeToken<Map<String, ContactListEvent.ReadWrite>>() {}.type
)
user.updateRelays(relays)
}
} catch (e: Exception) {
e.printStackTrace()
} }
val (valueRelays, elapsedRelays) = measureTimedValue {
try {
if (event.content.isNotEmpty()) {
val relays: Map<String, ContactListEvent.ReadWrite> =
Event.gson.fromJson(
event.content,
object : TypeToken<Map<String, ContactListEvent.ReadWrite>>() {}.type
)
user.updateRelays(relays)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
Log.d("CL", "AAA ${user.toBestDisplayName()} ${event.follows.size} in ${elapsed} and \t${elapsedRelays}")
user.latestContactList = event user.latestContactList = event
} }
} }
@@ -524,9 +552,14 @@ class LocalCacheLiveData(val cache: LocalCache): LiveData<LocalCacheState>(Local
handlerWaiting.set(true) handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Main) val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch { scope.launch {
delay(100) try {
refresh() delay(100)
handlerWaiting.set(false) refresh()
} finally {
withContext(NonCancellable) {
handlerWaiting.set(false)
}
}
} }
} }