Skip to content

Commit

Permalink
Implement proper primal legend profile updates (#241)
Browse files Browse the repository at this point in the history
---------

Approved-by: Aleksandar Ilic <[email protected]>
  • Loading branch information
markocic authored Dec 3, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent e2b2753 commit 3548e0c
Showing 13 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ suspend fun ArticleResponse.persistToDatabaseAsTransaction(userId: String, datab
val eventUserStats = this.primalEventUserStats.mapNotNullAsEventUserStatsPO(userId = userId)

database.withTransaction {
database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)
database.posts().upsertAll(data = allNotes + referencedNotes)
database.articles().upsertAll(list = allArticles)
database.eventStats().upsertAll(data = eventStats)
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ class ExploreRepository @Inject constructor(
)

database.withTransaction {
database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)
database.eventZaps().upsertAll(data = eventZaps)
}

@@ -115,7 +115,7 @@ class ExploreRepository @Inject constructor(
val userFollowCount = response.usersFollowCount?.takeContentAsPrimalUserFollowersCountsOrNull()

database.withTransaction {
database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)
}

profiles.map {
@@ -166,7 +166,7 @@ class ExploreRepository @Inject constructor(
}.sortedByDescending { it.score }

database.withTransaction {
database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)
database.profileStats().insertOrIgnore(
data = result.map {
ProfileStats(profileId = it.metadata.ownerId, followers = it.followersCount)
Original file line number Diff line number Diff line change
@@ -171,7 +171,7 @@ class FeedsRepository @Inject constructor(
.fold(emptyMap<String, Float>()) { acc, map -> acc + map }

withContext(dispatcherProvider.io()) {
database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)
database.eventStats().upsertAll(data = eventStatsMap.values.map { it.asEventStatsPO() })
database.eventUserStats().upsertAll(data = userStats.values.map { it.asEventUserStatsPO(userId = userId) })
}
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ class MessagesProcessor @Inject constructor(
val attachments = messageDataList.flatMapMessagesAsNoteAttachmentPO()

database.withTransaction {
database.profiles().upsertAll(
database.profiles().insertOrUpdateAll(
data = profileMetadata.mapAsProfileDataPO(
cdnResources = cdnResources,
primalUserNames = primalUserNamesMap,
@@ -112,7 +112,7 @@ class MessagesProcessor @Inject constructor(
primalUserNames = primalUserNames,
primalLegendProfiles = primalLegendProfiles,
)
database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)
profiles
} catch (error: WssException) {
Timber.w(error)
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ suspend fun FeedResponse.persistToDatabaseAsTransaction(userId: String, database
val userPostStats = primalEventUserStats.mapNotNullAsEventUserStatsPO(userId = userId)

database.withTransaction {
database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)
database.posts().upsertAll(data = allPosts)
database.attachments().upsertAllNoteAttachments(data = noteAttachments)
database.attachments().upsertAllNostrUris(data = noteNostrUris)
Original file line number Diff line number Diff line change
@@ -10,8 +10,20 @@ import kotlinx.coroutines.flow.Flow
@Dao
interface ProfileDataDao {

@Transaction
fun insertOrUpdateAll(data: List<ProfileData>) {
val existingProfiles = findProfileData(data.map { it.ownerId }).associateBy { it.ownerId }
insertOrReplaceAll(
data.map {
it.copy(
primalLegendProfile = it.primalLegendProfile ?: existingProfiles[it.ownerId]?.primalLegendProfile,
)
},
)
}

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun upsertAll(data: List<ProfileData>)
fun insertOrReplaceAll(data: List<ProfileData>)

@Transaction
@Query("SELECT * FROM ProfileData WHERE ownerId = :profileId")
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ class ProfileRepository @Inject constructor(
primalUserNames = primalUserNames,
primalLegendProfiles = primalLegendProfiles,
)
database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)
profiles
}

@@ -101,7 +101,7 @@ class ProfileRepository @Inject constructor(

database.withTransaction {
if (profileMetadata != null) {
database.profiles().upsertAll(data = listOf(profileMetadata))
database.profiles().insertOrUpdateAll(data = listOf(profileMetadata))
}

if (profileStats != null) {
@@ -208,7 +208,7 @@ class ProfileRepository @Inject constructor(
)
val followersCountsMap = response.followerCounts?.takeContentAsPrimalUserFollowersCountsOrNull()

database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)

profiles.map {
val score = followersCountsMap?.get(it.ownerId)
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ class MutedUserRepository @Inject constructor(
)
}

database.profiles().upsertAll(data = profileData)
database.profiles().insertOrUpdateAll(data = profileData)
return muteList
.map { mutedUserId ->
mutedUserId.asMutedAccountPO(
Original file line number Diff line number Diff line change
@@ -133,7 +133,7 @@ class EventRepository @Inject constructor(
primalLegendProfiles = primalLegendProfiles,
)
database.withTransaction {
database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)
}

val userScoresMap = response.userScores?.takeContentAsPrimalUserScoresOrNull()
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ suspend fun EventZapsResponse.persistToDatabaseAsTransaction(database: PrimalDat
)
val eventZaps = this.zaps.mapAsEventZapDO(profilesMap = profiles.associateBy { it.ownerId })
database.withTransaction {
database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)
database.eventZaps().upsertAll(data = eventZaps)
}
}
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ class UserAccountFetcher @Inject constructor(

withContext(dispatcherProvider.io()) {
primalDatabase.withTransaction {
primalDatabase.profiles().upsertAll(data = listOf(profileData))
primalDatabase.profiles().insertOrUpdateAll(data = listOf(profileData))
profileStats?.let(primalDatabase.profileStats()::upsert)
}
}
Original file line number Diff line number Diff line change
@@ -158,7 +158,7 @@ class UserRepository @Inject constructor(
primalUserNames = emptyMap(),
primalLegendProfiles = emptyMap(),
)
database.profiles().upsertAll(data = listOf(profileData))
database.profiles().insertOrUpdateAll(data = listOf(profileData))

accountsStore.getAndUpdateAccount(userId = userId) {
this.copy(
Original file line number Diff line number Diff line change
@@ -123,7 +123,7 @@ class WalletTransactionsMediator(
}

database.withTransaction {
database.profiles().upsertAll(data = profiles)
database.profiles().insertOrUpdateAll(data = profiles)
database.walletTransactions().upsertAll(data = transactions)
}
}

0 comments on commit 3548e0c

Please sign in to comment.