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

70 wiersze
2.4 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.service.relays.EOSEAccount
import com.vitorpamplona.amethyst.service.relays.FeedType
2023-03-05 00:11:49 +00:00
import com.vitorpamplona.amethyst.service.relays.JsonFilter
2023-03-07 18:46:44 +00:00
import com.vitorpamplona.amethyst.service.relays.TypedFilter
import com.vitorpamplona.quartz.events.ChatroomKey
import com.vitorpamplona.quartz.events.PrivateDmEvent
2023-03-07 18:46:44 +00:00
object NostrChatroomDataSource : NostrDataSource("ChatroomFeed") {
lateinit var account: Account
private var withRoom: ChatroomKey? = null
2023-01-14 22:56:18 +00:00
private val latestEOSEs = EOSEAccount()
fun loadMessagesBetween(accountIn: Account, withRoom: ChatroomKey) {
this.account = accountIn
this.withRoom = withRoom
2023-03-07 18:46:44 +00:00
resetFilters()
}
2023-03-07 18:46:44 +00:00
fun createMessagesToMeFilter(): TypedFilter? {
val myPeer = withRoom
return if (myPeer != null) {
TypedFilter(
types = setOf(FeedType.PRIVATE_DMS),
filter = JsonFilter(
kinds = listOf(PrivateDmEvent.kind),
authors = myPeer.users.map { it },
tags = mapOf("p" to listOf(account.userProfile().pubkeyHex)),
since = latestEOSEs.users[account.userProfile()]?.followList?.get(withRoom.hashCode().toString())?.relayList
)
)
} else {
null
}
}
2023-03-07 18:46:44 +00:00
fun createMessagesFromMeFilter(): TypedFilter? {
val myPeer = withRoom
2023-03-07 18:46:44 +00:00
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 myPeer.users.map { it }),
since = latestEOSEs.users[account.userProfile()]?.followList?.get(withRoom.hashCode().toString())?.relayList
2023-03-07 18:46:44 +00:00
)
)
} else {
null
}
}
2023-01-14 22:56:18 +00:00
val inandoutChannel = requestNewChannel { time, relayUrl ->
latestEOSEs.addOrUpdate(account.userProfile(), withRoom.hashCode().toString(), relayUrl, time)
}
2023-03-07 18:46:44 +00:00
override fun updateChannelFilters() {
inandoutChannel.typedFilters = listOfNotNull(
createMessagesToMeFilter(),
createMessagesFromMeFilter()
).ifEmpty { null }
2023-03-07 18:46:44 +00:00
}
}