Skip to content

Commit

Permalink
Fix parsing referenced articles
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandarIlic committed Aug 2, 2024
1 parent 820a05a commit 7cf204e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ suspend fun ArticleResponse.persistToDatabaseAsTransaction(userId: String, datab
database.withTransaction {
database.profiles().upsertAll(data = profiles)
database.posts().upsertAll(data = allNotes + referencedNotes)
database.articles().upsertAll(list = allPrimalArticles + allArticles + referencedArticles)
database.articles().upsertAll(list = allPrimalArticles + referencedArticles + allArticles)
database.eventStats().upsertAll(data = eventStats)
database.eventUserStats().upsertAll(data = eventUserStats)
database.eventZaps().upsertAll(data = eventZaps)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@ import timber.log.Timber
fun List<PrimalEvent>.mapNotNullReferencedEventsAsArticleDataPO(
wordsCountMap: Map<String, Int>,
cdnResources: Map<String, CdnResource>,
) = this.mapNotNull { it.takeContentOrNull<NostrEvent>() }
.filter { event -> event.kind == NostrEventKind.LongFormContent.value }
.mapNotNull { event ->
event.asArticleData(
wordsCount = wordsCountMap[event.id],
cdnResources = cdnResources,
)
}
): List<ArticleData> {
val mappedFromNostrEvents = this.mapNotNull { it.takeContentOrNull<NostrEvent>() }
.filter { event -> event.kind == NostrEventKind.LongFormContent.value }
.mapNotNull { event ->
event.asArticleData(
wordsCount = wordsCountMap[event.id],
cdnResources = cdnResources,
)
}

val mappedFromPrimalEvents = this.mapNotNull { it.takeContentOrNull<PrimalEvent>() }
.filter { event -> event.kind == NostrEventKind.PrimalLongFormContent.value }
.mapNotNull { event ->
event.asArticleData(
wordsCount = wordsCountMap[event.id],
cdnResources = cdnResources,
)
}

return mappedFromNostrEvents + mappedFromPrimalEvents
}

fun List<PrimalEvent>.mapNotNullPrimalEventAsArticleDataPO(
wordsCountMap: Map<String, Int> = emptyMap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package net.primal.android.nostr.ext
import kotlinx.serialization.json.decodeFromJsonElement
import net.primal.android.core.serialization.json.NostrJson
import net.primal.android.nostr.model.primal.PrimalEvent
import timber.log.Timber

inline fun <reified T> PrimalEvent?.takeContentOrNull(): T? {
if (this == null) return null
Expand All @@ -13,7 +12,6 @@ inline fun <reified T> PrimalEvent?.takeContentOrNull(): T? {
NostrJson.parseToJsonElement(this.content),
)
} catch (error: IllegalArgumentException) {
Timber.w(error)
null
}
}

0 comments on commit 7cf204e

Please sign in to comment.