amethyst/app/src/main/java/com/vitorpamplona/amethyst/service/NostrChatroomDataSource.kt

61 wiersze
1.9 KiB
Kotlin
Czysty Zwykły widok Historia

2023-01-14 22:56:18 +00:00
package com.vitorpamplona.amethyst.service
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.User
2023-03-08 22:07:56 +00:00
import com.vitorpamplona.amethyst.service.model.PrivateDmEvent
import com.vitorpamplona.amethyst.service.relays.FeedType
2023-03-05 00:11:49 +00:00
import com.vitorpamplona.amethyst.service.relays.JsonFilter
2023-03-08 22:07:56 +00:00
import com.vitorpamplona.amethyst.service.relays.TypedFilter
object NostrChatroomDataSource : NostrDataSource("ChatroomFeed") {
lateinit var account: Account
var withUser: User? = null
2023-01-14 22:56:18 +00:00
2023-03-08 22:07:56 +00:00
fun loadMessagesBetween(accountIn: Account, userId: String) {
account = accountIn
withUser = LocalCache.users[userId]
resetFilters()
}
2023-03-08 22:07:56 +00:00
fun createMessagesToMeFilter(): TypedFilter? {
val myPeer = withUser
return if (myPeer != null) {
TypedFilter(
types = setOf(FeedType.PRIVATE_DMS),
filter = JsonFilter(
kinds = listOf(PrivateDmEvent.kind),
authors = listOf(myPeer.pubkeyHex),
tags = mapOf("p" to listOf(account.userProfile().pubkeyHex))
)
)
} else {
null
}
}
2023-01-14 22:56:18 +00:00
2023-03-08 22:07:56 +00:00
fun createMessagesFromMeFilter(): TypedFilter? {
val myPeer = withUser
return if (myPeer != null) {
TypedFilter(
types = setOf(FeedType.PRIVATE_DMS),
filter = JsonFilter(
kinds = listOf(PrivateDmEvent.kind),
authors = listOf(account.userProfile().pubkeyHex),
tags = mapOf("p" to listOf(myPeer.pubkeyHex))
)
)
} else {
null
}
}
2023-01-14 22:56:18 +00:00
2023-03-08 22:07:56 +00:00
val inandoutChannel = requestNewChannel()
override fun updateChannelFilters() {
inandoutChannel.typedFilters = listOfNotNull(createMessagesToMeFilter(), createMessagesFromMeFilter()).ifEmpty { null }
}
}