mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-28 22:03:01 +02:00
Adds a filter for messages by the author in public chats from all relays.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.vitorpamplona.amethyst.service
|
package com.vitorpamplona.amethyst.service
|
||||||
|
|
||||||
|
import com.vitorpamplona.amethyst.model.Account
|
||||||
import com.vitorpamplona.amethyst.model.Channel
|
import com.vitorpamplona.amethyst.model.Channel
|
||||||
import com.vitorpamplona.amethyst.model.LocalCache
|
import com.vitorpamplona.amethyst.model.LocalCache
|
||||||
import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent
|
import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent
|
||||||
@@ -8,13 +9,39 @@ import com.vitorpamplona.amethyst.service.relays.JsonFilter
|
|||||||
import com.vitorpamplona.amethyst.service.relays.TypedFilter
|
import com.vitorpamplona.amethyst.service.relays.TypedFilter
|
||||||
|
|
||||||
object NostrChannelDataSource : NostrDataSource("ChatroomFeed") {
|
object NostrChannelDataSource : NostrDataSource("ChatroomFeed") {
|
||||||
|
var account: Account? = null
|
||||||
var channel: Channel? = null
|
var channel: Channel? = null
|
||||||
|
|
||||||
fun loadMessagesBetween(channelId: String) {
|
fun loadMessagesBetween(account: Account, channelId: String) {
|
||||||
|
this.account = account
|
||||||
channel = LocalCache.getOrCreateChannel(channelId)
|
channel = LocalCache.getOrCreateChannel(channelId)
|
||||||
resetFilters()
|
resetFilters()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun clear() {
|
||||||
|
account = null
|
||||||
|
channel = null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createMessagesByMeToChannelFilter(): TypedFilter? {
|
||||||
|
val myAccount = account ?: return null
|
||||||
|
|
||||||
|
if (channel != null) {
|
||||||
|
// 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
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
fun createMessagesToChannelFilter(): TypedFilter? {
|
fun createMessagesToChannelFilter(): TypedFilter? {
|
||||||
if (channel != null) {
|
if (channel != null) {
|
||||||
return TypedFilter(
|
return TypedFilter(
|
||||||
@@ -32,6 +59,9 @@ object NostrChannelDataSource : NostrDataSource("ChatroomFeed") {
|
|||||||
val messagesChannel = requestNewChannel()
|
val messagesChannel = requestNewChannel()
|
||||||
|
|
||||||
override fun updateChannelFilters() {
|
override fun updateChannelFilters() {
|
||||||
messagesChannel.typedFilters = listOfNotNull(createMessagesToChannelFilter()).ifEmpty { null }
|
messagesChannel.typedFilters = listOfNotNull(
|
||||||
|
createMessagesToChannelFilter(),
|
||||||
|
createMessagesByMeToChannelFilter()
|
||||||
|
).ifEmpty { null }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -91,9 +91,6 @@ fun ChannelScreen(
|
|||||||
if (account != null && channelId != null) {
|
if (account != null && channelId != null) {
|
||||||
val replyTo = remember { mutableStateOf<Note?>(null) }
|
val replyTo = remember { mutableStateOf<Note?>(null) }
|
||||||
|
|
||||||
ChannelFeedFilter.loadMessagesBetween(account, channelId)
|
|
||||||
NostrChannelDataSource.loadMessagesBetween(channelId)
|
|
||||||
|
|
||||||
val channelState by NostrChannelDataSource.channel!!.live.observeAsState()
|
val channelState by NostrChannelDataSource.channel!!.live.observeAsState()
|
||||||
val channel = channelState?.channel ?: return
|
val channel = channelState?.channel ?: return
|
||||||
|
|
||||||
@@ -101,6 +98,9 @@ fun ChannelScreen(
|
|||||||
val lifeCycleOwner = LocalLifecycleOwner.current
|
val lifeCycleOwner = LocalLifecycleOwner.current
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
|
ChannelFeedFilter.loadMessagesBetween(account, channelId)
|
||||||
|
NostrChannelDataSource.loadMessagesBetween(account, channelId)
|
||||||
|
|
||||||
feedViewModel.invalidateData()
|
feedViewModel.invalidateData()
|
||||||
channelScreenModel.imageUploadingError.collect { error ->
|
channelScreenModel.imageUploadingError.collect { error ->
|
||||||
Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
|
||||||
@@ -111,11 +111,15 @@ fun ChannelScreen(
|
|||||||
val observer = LifecycleEventObserver { _, event ->
|
val observer = LifecycleEventObserver { _, event ->
|
||||||
if (event == Lifecycle.Event.ON_RESUME) {
|
if (event == Lifecycle.Event.ON_RESUME) {
|
||||||
println("Channel Start")
|
println("Channel Start")
|
||||||
NostrChannelDataSource.start()
|
ChannelFeedFilter.loadMessagesBetween(account, channelId)
|
||||||
|
NostrChannelDataSource.loadMessagesBetween(account, channelId)
|
||||||
|
|
||||||
feedViewModel.invalidateData()
|
feedViewModel.invalidateData()
|
||||||
}
|
}
|
||||||
if (event == Lifecycle.Event.ON_PAUSE) {
|
if (event == Lifecycle.Event.ON_PAUSE) {
|
||||||
println("Channel Stop")
|
println("Channel Stop")
|
||||||
|
|
||||||
|
NostrChannelDataSource.clear()
|
||||||
NostrChannelDataSource.stop()
|
NostrChannelDataSource.stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user