-
Notifications
You must be signed in to change notification settings - Fork 1
/
FriendProvider.kt
38 lines (30 loc) · 1.45 KB
/
FriendProvider.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.dluvian.voyage.data.provider
import com.dluvian.voyage.core.PubkeyHex
import com.dluvian.voyage.core.takeRandom
import com.dluvian.voyage.data.account.IPubkeyProvider
import com.dluvian.voyage.data.room.dao.FriendDao
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
class FriendProvider(
private val friendDao: FriendDao,
private val pubkeyProvider: IPubkeyProvider,
) {
private val scope = CoroutineScope(Dispatchers.IO)
private val friends = friendDao.getFriendsFlow()
.stateIn(scope, SharingStarted.Eagerly, emptyList())
fun getFriendPubkeys(maxPercentage: Float? = null): List<PubkeyHex> {
val adjustedMax = if (maxPercentage == null) null
else maxOf(1, (maxPercentage * friends.value.size).toInt())
return (friends.value - pubkeyProvider.getPubkeyHex())
.let { if (adjustedMax != null) it.takeRandom(adjustedMax) else it }
}
suspend fun getFriendsWithMissingContactList() = friendDao.getFriendsWithMissingContactList()
suspend fun getFriendsWithMissingNip65() = friendDao.getFriendsWithMissingNip65()
// Not named "getMaxCreatedAt" bc there should only be one createdAt available
suspend fun getCreatedAt() = friendDao.getMaxCreatedAt()
fun isFriend(pubkey: PubkeyHex): Boolean {
return getFriendPubkeys(maxPercentage = null).contains(pubkey)
}
}