Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
Show correct follow info when no profile in db
Browse files Browse the repository at this point in the history
  • Loading branch information
dluvian committed Feb 5, 2024
1 parent 3efdbfe commit 5adadbd
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.dluvian.nozzle.data.subscriber.INozzleSubscriber
import com.dluvian.nozzle.data.utils.LONG_DEBOUNCE
import com.dluvian.nozzle.data.utils.firstThenDistinctDebounce
import com.dluvian.nozzle.model.ProfileWithMeta
import com.dluvian.nozzle.model.Pubkey
import com.dluvian.nozzle.model.PubkeyVariations
import com.dluvian.nozzle.model.nostr.Metadata
import kotlinx.coroutines.flow.Flow
Expand All @@ -40,9 +41,6 @@ class ProfileWithMetaProvider(

val pubkey = profileIdToNostrId(profileId)?.hex ?: profileId

val profileExtendedFlow = profileDao.getProfileEntityExtendedFlow(pubkey = pubkey)
.distinctUntilChanged()

// TODO: SQL join (?)
val seenInRelaysFlow = eventRelayDao.listUsedRelaysFlow(pubkey)
.firstThenDistinctDebounce(LONG_DEBOUNCE)
Expand All @@ -66,7 +64,7 @@ class ProfileWithMetaProvider(

return getFinalFlow(
pubkeyVariations = PubkeyVariations.fromPubkey(pubkey),
profileFlow = profileExtendedFlow,
profileFlow = getProfileExtendedFlow(pubkey = pubkey),
seenInRelaysFlow = seenInRelaysFlow,
nip65Flow = nip65Flow,
nprofileFlow = nprofileFlow,
Expand All @@ -76,7 +74,7 @@ class ProfileWithMetaProvider(

private fun getFinalFlow(
pubkeyVariations: PubkeyVariations,
profileFlow: Flow<ProfileEntityExtended?>,
profileFlow: Flow<ProfileEntityExtended>,
seenInRelaysFlow: Flow<List<String>>,
nip65Flow: Flow<List<Nip65Relay>>,
nprofileFlow: Flow<String?>,
Expand All @@ -92,17 +90,26 @@ class ProfileWithMetaProvider(
ProfileWithMeta(
pubkey = pubkeyVariations.pubkey,
nprofile = nprofile ?: pubkeyVariations.npub,
metadata = profile?.profileEntity?.metadata
metadata = profile.profileEntity?.metadata
?: Metadata(name = pubkeyVariations.shortenedNpub),
numOfFollowing = profile?.numOfFollowing ?: 0,
numOfFollowers = profile?.numOfFollowers ?: 0,
numOfFollowing = profile.followInfo.numOfFollowing,
numOfFollowers = profile.followInfo.numOfFollowers,
seenInRelays = seenInRelays,
writesInRelays = nip65s.filter { it.isWrite }.map { it.url },
readsInRelays = nip65s.filter { it.isRead }.map { it.url },
isOneself = pubkeyProvider.isOneself(pubkeyVariations.pubkey), // TODO: Handle in SQL
followsYou = profile?.followsYou ?: false,
followsYou = profile.followInfo.followsYou,
trustScore = trustScore,
)
}
}

// Not in a single SQL query because it would need 7 subqueries to build the profileEntity
private fun getProfileExtendedFlow(pubkey: Pubkey): Flow<ProfileEntityExtended> {
val profileFlow = profileDao.getProfileFlow(pubkey = pubkey).distinctUntilChanged()
val followInfoFlow = contactDao.getFollowInfoFlow(pubkey = pubkey).distinctUntilChanged()
return combine(profileFlow, followInfoFlow) { profile, followInfo ->
ProfileEntityExtended(profileEntity = profile, followInfo = followInfo)
}.distinctUntilChanged()
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/dluvian/nozzle/data/room/dao/ContactDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.room.Query
import androidx.room.Transaction
import com.dluvian.nozzle.data.TRUST_SCORE_BOOST
import com.dluvian.nozzle.data.room.entity.ContactEntity
import com.dluvian.nozzle.data.room.helper.FollowInfo
import com.dluvian.nozzle.model.Pubkey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
Expand Down Expand Up @@ -276,4 +277,17 @@ interface ContactDao {
"LIMIT :limit"
)
fun listFriendsOfFriendsFlow(limit: Int): Flow<List<String>>

@Query(
"SELECT " +
// SELECT numOfFollowing
"(SELECT COUNT(contactPubkey) FROM contact WHERE pubkey = :pubkey) AS numOfFollowing, " +
// SELECT numOfFollowers
"(SELECT COUNT(pubkey) FROM contact WHERE contactPubkey = :pubkey) AS numOfFollowers, " +
// SELECT followsYou
"(SELECT EXISTS(SELECT * FROM contact WHERE pubkey = :pubkey " +
"AND contactPubkey = (SELECT pubkey FROM account WHERE isActive = 1)" +
")) AS followsYou "
)
fun getFollowInfoFlow(pubkey: Pubkey): Flow<FollowInfo>
}
20 changes: 3 additions & 17 deletions app/src/main/java/com/dluvian/nozzle/data/room/dao/ProfileDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.room.Query
import androidx.room.RewriteQueriesToDropUnusedColumns
import androidx.room.Transaction
import com.dluvian.nozzle.data.room.entity.ProfileEntity
import com.dluvian.nozzle.data.room.helper.extended.ProfileEntityExtended
import com.dluvian.nozzle.data.utils.escapeSQLPercentChars
import com.dluvian.nozzle.model.Pubkey
import com.dluvian.nozzle.model.nostr.Metadata
Expand All @@ -20,28 +19,15 @@ interface ProfileDao {
@Query("SELECT * FROM profile WHERE pubkey = :pubkey")
suspend fun getProfile(pubkey: String): ProfileEntity?

@Query("SELECT * FROM profile WHERE pubkey = :pubkey")
fun getProfileFlow(pubkey: String): Flow<ProfileEntity?>

@Query("SELECT * FROM profile WHERE pubkey IN (:pubkeys)")
fun getProfilesFlow(pubkeys: Collection<Pubkey>): Flow<List<ProfileEntity>>

@Query("SELECT * FROM profile WHERE pubkey IN (:pubkeys)")
suspend fun getProfiles(pubkeys: Collection<Pubkey>): List<ProfileEntity>

@Query(
// SELECT metadata
"SELECT mainProfile.*, " +
// SELECT numOfFollowing
"(SELECT COUNT(contactPubkey) FROM contact WHERE pubkey = :pubkey) AS numOfFollowing, " +
// SELECT numOfFollowers
"(SELECT COUNT(pubkey) FROM contact WHERE contactPubkey = :pubkey) AS numOfFollowers, " +
// SELECT followsYou
"(SELECT EXISTS(SELECT * FROM contact WHERE pubkey = :pubkey " +
"AND contactPubkey = (SELECT pubkey FROM account WHERE isActive = 1)" +
")) AS followsYou " +
"FROM profile AS mainProfile " +
"WHERE mainProfile.pubkey = :pubkey"
)
fun getProfileEntityExtendedFlow(pubkey: String): Flow<ProfileEntityExtended?>

@RewriteQueriesToDropUnusedColumns
@Query("SELECT * FROM profile WHERE pubkey = (SELECT pubkey FROM account WHERE isActive = 1)")
fun getActiveMetadata(): Flow<Metadata?>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dluvian.nozzle.data.room.helper

data class FollowInfo(
val numOfFollowing: Int,
val numOfFollowers: Int,
val followsYou: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package com.dluvian.nozzle.data.room.helper.extended

import androidx.room.Embedded
import com.dluvian.nozzle.data.room.entity.ProfileEntity
import com.dluvian.nozzle.data.room.helper.FollowInfo

data class ProfileEntityExtended(
@Embedded
val profileEntity: ProfileEntity,
val numOfFollowing: Int,
val numOfFollowers: Int,
val followsYou: Boolean,
val profileEntity: ProfileEntity?,
val followInfo: FollowInfo
)

0 comments on commit 5adadbd

Please sign in to comment.