2023-01-14 22:56:18 +00:00
|
|
|
package com.vitorpamplona.amethyst.service
|
|
|
|
|
|
|
|
import com.vitorpamplona.amethyst.model.Account
|
2023-08-10 22:04:23 +00:00
|
|
|
import com.vitorpamplona.amethyst.service.relays.EOSEAccount
|
2023-02-05 23:14:41 +00:00
|
|
|
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
|
2023-08-16 21:58:25 +00:00
|
|
|
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
|
2023-08-10 22:04:23 +00:00
|
|
|
private var withRoom: ChatroomKey? = null
|
2023-01-14 22:56:18 +00:00
|
|
|
|
2023-08-10 22:04:23 +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-02-03 01:17:53 +00:00
|
|
|
}
|
2023-03-07 18:46:44 +00:00
|
|
|
|
|
|
|
fun createMessagesToMeFilter(): TypedFilter? {
|
2023-08-10 22:04:23 +00:00
|
|
|
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? {
|
2023-08-10 22:04:23 +00:00
|
|
|
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),
|
2023-08-10 22:04:23 +00:00
|
|
|
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
|
|
|
|
2023-08-10 22:04:23 +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() {
|
2023-08-10 22:04:23 +00:00
|
|
|
inandoutChannel.typedFilters = listOfNotNull(
|
|
|
|
createMessagesToMeFilter(),
|
|
|
|
createMessagesFromMeFilter()
|
|
|
|
).ifEmpty { null }
|
2023-03-07 18:46:44 +00:00
|
|
|
}
|
|
|
|
}
|