Glen
12/20/2022, 5:05 PMprivate val eventsCache: MutableSet<TextNoteEvent> = mutableSetOf() private val profiles: MutableSet<MetadataEvent> = mutableSetOf()
private val postCache: MutableSet<Post> = mutableSetOf() val eventsByPubkey = eventsCache
.distinctBy { it.id.toHex() }
.associateBy { it.pubKey.toHex() }
val posts = profiles
//.filter { eventsByPubkey[it.pubKey.toHex()] != null }
.map { metadataEvent ->
val textEvent = eventsByPubkey[metadataEvent.pubKey.toHex()]
if (textEvent != null){
if (textEvent.pubKey.toHex() == metadataEvent.pubKey.toHex()){
val post = Post(
user = User(
username = metadataEvent
.contactMetaData.name ?: textEvent.pubKey.toNpub().take(9),
pubKey = textEvent.pubKey.toHex(),
bio = metadataEvent.contactMetaData.about ?: "",
image = metadataEvent.contactMetaData.picture ?: ""
),
postId = textEvent.id.toHex(),
timestamp = textEvent.createdAt,
textContent = textEvent.content,
imageLinks = textEvent.content.urlsInText()
)
postCache.add(post)
} else {
val post = Post(
user = User(
username = textEvent.pubKey.toNpub().take(9),
pubKey = textEvent.pubKey.toHex(),
bio = metadataEvent.contactMetaData.about ?: "",
image = metadataEvent.contactMetaData.picture ?: ""
),
postId = textEvent.id.toHex(),
timestamp = textEvent.createdAt,
textContent = textEvent.content,
imageLinks = textEvent.content.urlsInText()
)
postCache.add(post)
}
}
}
Paul Griffith
12/20/2022, 6:12 PMif
is an expression in Kotlin, you can put the only condition logic directly inline in the assignmentGlen
12/20/2022, 6:16 PMGlen
12/20/2022, 6:17 PMPaul Griffith
12/20/2022, 6:17 PMmapNotNullTo
to make the collection to the final output more explicit without the ’cache` variable)Glen
12/20/2022, 6:18 PMGlen
12/20/2022, 6:19 PM