2023-01-14 22:56:18 +00:00
|
|
|
package com.vitorpamplona.amethyst.service
|
|
|
|
|
|
|
|
import com.vitorpamplona.amethyst.model.Account
|
|
|
|
import com.vitorpamplona.amethyst.model.Note
|
2023-01-16 21:34:49 +00:00
|
|
|
import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent
|
|
|
|
import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent
|
|
|
|
import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent
|
2023-01-14 22:56:18 +00:00
|
|
|
import nostr.postr.JsonFilter
|
|
|
|
import nostr.postr.events.PrivateDmEvent
|
|
|
|
|
2023-01-16 02:52:59 +00:00
|
|
|
object NostrChatroomListDataSource: NostrDataSource<Note>("MailBoxFeed") {
|
2023-01-14 22:56:18 +00:00
|
|
|
lateinit var account: Account
|
|
|
|
|
|
|
|
fun createMessagesToMeFilter() = JsonFilter(
|
|
|
|
kinds = listOf(PrivateDmEvent.kind),
|
|
|
|
tags = mapOf("p" to listOf(account.userProfile().pubkeyHex))
|
|
|
|
)
|
|
|
|
|
|
|
|
fun createMessagesFromMeFilter() = JsonFilter(
|
|
|
|
kinds = listOf(PrivateDmEvent.kind),
|
|
|
|
authors = listOf(account.userProfile().pubkeyHex)
|
|
|
|
)
|
|
|
|
|
2023-01-16 21:34:49 +00:00
|
|
|
fun createMyChannelsFilter() = JsonFilter(
|
|
|
|
kinds = listOf(ChannelCreateEvent.kind),
|
|
|
|
ids = account.followingChannels.toList()
|
|
|
|
)
|
|
|
|
|
|
|
|
fun createMyChannelsInfoFilter() = JsonFilter(
|
|
|
|
kinds = listOf(ChannelMetadataEvent.kind),
|
|
|
|
tags = mapOf("e" to account.followingChannels.toList())
|
|
|
|
)
|
|
|
|
|
|
|
|
fun createMessagesToMyChannelsFilter() = JsonFilter(
|
|
|
|
kinds = listOf(ChannelMessageEvent.kind),
|
|
|
|
tags = mapOf("e" to account.followingChannels.toList()),
|
|
|
|
since = System.currentTimeMillis() / 1000 - (60 * 60 * 24 * 1), // 24 hours
|
|
|
|
)
|
|
|
|
|
2023-01-14 22:56:18 +00:00
|
|
|
val incomingChannel = requestNewChannel()
|
|
|
|
val outgoingChannel = requestNewChannel()
|
|
|
|
|
2023-01-16 21:34:49 +00:00
|
|
|
val myChannelsChannel = requestNewChannel()
|
|
|
|
val myChannelsInfoChannel = requestNewChannel()
|
|
|
|
val myChannelsMessagesChannel = requestNewChannel()
|
|
|
|
|
2023-01-14 22:56:18 +00:00
|
|
|
// returns the last Note of each user.
|
|
|
|
override fun feed(): List<Note> {
|
|
|
|
val messages = account.userProfile().messages
|
|
|
|
val messagingWith = messages.keys().toList()
|
|
|
|
|
2023-01-16 21:34:49 +00:00
|
|
|
val privateMessages = messagingWith.mapNotNull {
|
2023-01-17 13:42:00 +00:00
|
|
|
messages[it]?.sortedBy { it.event?.createdAt }?.lastOrNull { it.event != null }
|
2023-01-16 21:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val publicChannels = account.followingChannels().map {
|
2023-01-17 13:42:00 +00:00
|
|
|
it.notes.values.sortedBy { it.event?.createdAt }.lastOrNull { it.event != null }
|
2023-01-16 21:34:49 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 13:42:00 +00:00
|
|
|
return (privateMessages + publicChannels).filterNotNull().sortedBy { it.event?.createdAt }.reversed()
|
2023-01-14 22:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun updateChannelFilters() {
|
|
|
|
incomingChannel.filter = createMessagesToMeFilter()
|
|
|
|
outgoingChannel.filter = createMessagesFromMeFilter()
|
2023-01-16 21:34:49 +00:00
|
|
|
myChannelsChannel.filter = createMyChannelsFilter()
|
|
|
|
myChannelsInfoChannel.filter = createMyChannelsInfoFilter()
|
|
|
|
myChannelsMessagesChannel.filter = createMessagesToMyChannelsFilter()
|
2023-01-14 22:56:18 +00:00
|
|
|
}
|
|
|
|
}
|