2023-01-16 21:34:49 +00:00
|
|
|
package com.vitorpamplona.amethyst.service
|
|
|
|
|
2023-03-20 16:10:09 +00:00
|
|
|
import com.vitorpamplona.amethyst.model.Account
|
2023-02-18 18:06:53 +00:00
|
|
|
import com.vitorpamplona.amethyst.model.Channel
|
2023-06-19 22:34:52 +00:00
|
|
|
import com.vitorpamplona.amethyst.model.LiveActivitiesChannel
|
|
|
|
import com.vitorpamplona.amethyst.model.PublicChatChannel
|
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.ChannelMessageEvent
|
|
|
|
import com.vitorpamplona.quartz.events.LiveActivitiesChatMessageEvent
|
2023-01-16 21:34:49 +00:00
|
|
|
|
2023-03-07 18:46:44 +00:00
|
|
|
object NostrChannelDataSource : NostrDataSource("ChatroomFeed") {
|
2023-03-20 16:10:09 +00:00
|
|
|
var account: Account? = null
|
2023-03-07 18:46:44 +00:00
|
|
|
var channel: Channel? = null
|
2023-01-16 21:34:49 +00:00
|
|
|
|
2023-06-05 19:33:16 +00:00
|
|
|
fun loadMessagesBetween(account: Account, channel: Channel) {
|
2023-03-20 16:10:09 +00:00
|
|
|
this.account = account
|
2023-06-05 19:33:16 +00:00
|
|
|
this.channel = channel
|
2023-03-07 18:46:44 +00:00
|
|
|
resetFilters()
|
|
|
|
}
|
2023-01-16 21:34:49 +00:00
|
|
|
|
2023-03-20 16:10:09 +00:00
|
|
|
fun clear() {
|
|
|
|
account = null
|
|
|
|
channel = null
|
|
|
|
}
|
|
|
|
|
|
|
|
fun createMessagesByMeToChannelFilter(): TypedFilter? {
|
|
|
|
val myAccount = account ?: return null
|
|
|
|
|
2023-06-19 22:34:52 +00:00
|
|
|
if (channel is PublicChatChannel) {
|
2023-03-20 16:10:09 +00:00
|
|
|
// Brings on messages by the user from all other relays.
|
|
|
|
// Since we ship with write to public, read from private only
|
|
|
|
// this guarantees that messages from the author do not disappear.
|
|
|
|
return TypedFilter(
|
|
|
|
types = setOf(FeedType.FOLLOWS, FeedType.PRIVATE_DMS, FeedType.GLOBAL, FeedType.SEARCH),
|
|
|
|
filter = JsonFilter(
|
|
|
|
kinds = listOf(ChannelMessageEvent.kind),
|
|
|
|
authors = listOf(myAccount.userProfile().pubkeyHex),
|
|
|
|
limit = 50
|
|
|
|
)
|
|
|
|
)
|
2023-06-19 22:34:52 +00:00
|
|
|
} else if (channel is LiveActivitiesChannel) {
|
|
|
|
// Brings on messages by the user from all other relays.
|
|
|
|
// Since we ship with write to public, read from private only
|
|
|
|
// this guarantees that messages from the author do not disappear.
|
|
|
|
return TypedFilter(
|
|
|
|
types = setOf(FeedType.FOLLOWS, FeedType.PRIVATE_DMS, FeedType.GLOBAL, FeedType.SEARCH),
|
|
|
|
filter = JsonFilter(
|
|
|
|
kinds = listOf(LiveActivitiesChatMessageEvent.kind),
|
|
|
|
authors = listOf(myAccount.userProfile().pubkeyHex),
|
|
|
|
limit = 50
|
|
|
|
)
|
|
|
|
)
|
2023-03-20 16:10:09 +00:00
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2023-03-07 18:46:44 +00:00
|
|
|
fun createMessagesToChannelFilter(): TypedFilter? {
|
2023-06-19 22:34:52 +00:00
|
|
|
if (channel is PublicChatChannel) {
|
2023-03-07 18:46:44 +00:00
|
|
|
return TypedFilter(
|
|
|
|
types = setOf(FeedType.PUBLIC_CHATS),
|
|
|
|
filter = JsonFilter(
|
|
|
|
kinds = listOf(ChannelMessageEvent.kind),
|
|
|
|
tags = mapOf("e" to listOfNotNull(channel?.idHex)),
|
|
|
|
limit = 200
|
|
|
|
)
|
|
|
|
)
|
2023-06-19 22:34:52 +00:00
|
|
|
} else if (channel is LiveActivitiesChannel) {
|
|
|
|
return TypedFilter(
|
|
|
|
types = setOf(FeedType.PUBLIC_CHATS),
|
|
|
|
filter = JsonFilter(
|
|
|
|
kinds = listOf(LiveActivitiesChatMessageEvent.kind),
|
|
|
|
tags = mapOf("a" to listOfNotNull(channel?.idHex)),
|
|
|
|
limit = 200
|
|
|
|
)
|
|
|
|
)
|
2023-03-07 18:46:44 +00:00
|
|
|
}
|
|
|
|
return null
|
2023-01-18 23:59:18 +00:00
|
|
|
}
|
2023-01-16 21:34:49 +00:00
|
|
|
|
2023-03-07 18:46:44 +00:00
|
|
|
val messagesChannel = requestNewChannel()
|
2023-01-16 21:34:49 +00:00
|
|
|
|
2023-03-07 18:46:44 +00:00
|
|
|
override fun updateChannelFilters() {
|
2023-03-20 16:10:09 +00:00
|
|
|
messagesChannel.typedFilters = listOfNotNull(
|
|
|
|
createMessagesToChannelFilter(),
|
|
|
|
createMessagesByMeToChannelFilter()
|
|
|
|
).ifEmpty { null }
|
2023-03-07 18:46:44 +00:00
|
|
|
}
|
|
|
|
}
|