Skip to content

Commit

Permalink
Separates Typed Relay class from a SimpleRelay class to prepare to mo…
Browse files Browse the repository at this point in the history
…ve to Quartz
  • Loading branch information
vitorpamplona committed Jan 14, 2025
1 parent 9b0f24b commit e3e9022
Show file tree
Hide file tree
Showing 32 changed files with 982 additions and 616 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import kotlin.time.measureTimedValue
class Amethyst : Application() {
val applicationIOScope = CoroutineScope(Dispatchers.IO + SupervisorJob())

val client: NostrClient = NostrClient(OkHttpWebSocket.Builder())
val client: NostrClient = NostrClient(OkHttpWebSocket.BuilderFactory())

// Service Manager is only active when the activity is active.
val serviceManager = ServiceManager(client, applicationIOScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2456,9 +2456,9 @@ object LocalCache {

if (event is AddressableEvent && relay != null) {
// updates relay with a new event.
getAddressableNoteIfExists(event.addressTag())?.let {
it.event?.let { existingEvent ->
if (existingEvent.createdAt > event.createdAt) {
getAddressableNoteIfExists(event.addressTag())?.let { note ->
note.event?.let { existingEvent ->
if (existingEvent.createdAt > event.createdAt && !note.hasRelay(relay)) {
Log.d("LocalCache", "Updating ${relay.url} with a new version of ${event.toJson()} to ${existingEvent.toJson()}")
relay.send(existingEvent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ open class Note(
}
}

fun hasRelay(relay: Relay) = relay.brief !in relays

fun addRelay(relay: Relay) {
if (relay.brief !in relays) {
addRelaySync(relay.brief)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ class User(
relay: Relay,
eventTime: Long,
) {
val here = relaysBeingUsed[relay.url]
val here = relaysBeingUsed[relay.brief.url]
if (here == null) {
relaysBeingUsed = relaysBeingUsed + Pair(relay.url, RelayInfo(relay.url, eventTime, 1))
relaysBeingUsed = relaysBeingUsed + Pair(relay.brief.url, RelayInfo(relay.brief.url, eventTime, 1))
} else {
if (eventTime > here.lastEvent) {
here.lastEvent = eventTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
*/
package com.vitorpamplona.amethyst.service.okhttp

import com.vitorpamplona.ammolite.sockets.WebSocket
import com.vitorpamplona.ammolite.sockets.WebSocketListener
import com.vitorpamplona.ammolite.sockets.WebsocketBuilder
import com.vitorpamplona.ammolite.relays.relays.sockets.WebSocket
import com.vitorpamplona.ammolite.relays.relays.sockets.WebSocketListener
import com.vitorpamplona.ammolite.relays.relays.sockets.WebsocketBuilder
import com.vitorpamplona.ammolite.relays.relays.sockets.WebsocketBuilderFactory
import okhttp3.Request
import okhttp3.Response

Expand Down Expand Up @@ -73,14 +74,19 @@ class OkHttpWebSocket(
) = out.onFailure(t, response?.message)
}

class Builder : WebsocketBuilder {
class Builder(
val forceProxy: Boolean,
) : WebsocketBuilder {
override fun build(
url: String,
forceProxy: Boolean,
out: WebSocketListener,
) = OkHttpWebSocket(url, forceProxy, out)
}

class BuilderFactory : WebsocketBuilderFactory {
override fun build(forceProxy: Boolean) = Builder(forceProxy)
}

override fun cancel() {
socket?.cancel()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.StdHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
import com.vitorpamplona.amethyst.ui.theme.imageModifier
import com.vitorpamplona.ammolite.relays.RelayStat
import com.vitorpamplona.ammolite.relays.relays.RelayStat

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.StdHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
import com.vitorpamplona.amethyst.ui.theme.imageModifier
import com.vitorpamplona.ammolite.relays.RelayStat
import com.vitorpamplona.ammolite.relays.relays.RelayStat

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import com.vitorpamplona.amethyst.ui.theme.RowColSpacing
import com.vitorpamplona.amethyst.ui.theme.StdHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.grayText
import com.vitorpamplona.ammolite.relays.Constants
import com.vitorpamplona.ammolite.relays.RelayStat
import com.vitorpamplona.ammolite.relays.relays.RelayStat

@Composable
fun AllRelayListView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package com.vitorpamplona.amethyst.ui.actions.relays
import androidx.compose.runtime.Immutable
import com.vitorpamplona.ammolite.relays.FeedType
import com.vitorpamplona.ammolite.relays.RelayBriefInfoCache
import com.vitorpamplona.ammolite.relays.RelayStat
import com.vitorpamplona.ammolite.relays.relays.RelayStat
import com.vitorpamplona.quartz.nip01Core.HexKey

@Immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ import com.vitorpamplona.ammolite.relays.Constants
import com.vitorpamplona.ammolite.relays.Constants.activeTypesGlobalChats
import com.vitorpamplona.ammolite.relays.FeedType
import com.vitorpamplona.ammolite.relays.RelayBriefInfoCache
import com.vitorpamplona.ammolite.relays.RelayStat
import com.vitorpamplona.ammolite.relays.relays.RelayStat
import com.vitorpamplona.quartz.nip65RelayList.RelayUrlFormatter
import kotlinx.coroutines.launch

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import com.vitorpamplona.amethyst.ui.theme.ThemeComparisonColumn
import com.vitorpamplona.amethyst.ui.theme.allGoodColor
import com.vitorpamplona.amethyst.ui.theme.largeRelayIconModifier
import com.vitorpamplona.ammolite.relays.COMMON_FEED_TYPES
import com.vitorpamplona.ammolite.relays.RelayStat
import com.vitorpamplona.ammolite.relays.relays.RelayStat

@OptIn(ExperimentalLayoutApi::class)
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.ButtonBorder
import com.vitorpamplona.amethyst.ui.theme.Size10dp
import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.ammolite.relays.RelayStat
import com.vitorpamplona.ammolite.relays.relays.RelayStat
import com.vitorpamplona.quartz.nip65RelayList.RelayUrlFormatter

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package com.vitorpamplona.ammolite.relays
import com.vitorpamplona.quartz.nip65RelayList.RelayUrlFormatter

object Constants {
val activeTypes = setOf(FeedType.FOLLOWS, FeedType.PRIVATE_DMS)
val activeTypesFollows = setOf(FeedType.FOLLOWS, FeedType.PRIVATE_DMS)
val activeTypesChats = setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS, FeedType.PRIVATE_DMS)
val activeTypesGlobalChats = setOf(FeedType.FOLLOWS, FeedType.PUBLIC_CHATS, FeedType.PRIVATE_DMS, FeedType.GLOBAL)
val activeTypesSearch = setOf(FeedType.SEARCH)
Expand All @@ -35,7 +35,7 @@ object Constants {
RelaySetupInfo(RelayUrlFormatter.normalize("wss://relay.nostr.bg"), read = true, write = true, feedTypes = activeTypesChats),
RelaySetupInfo(RelayUrlFormatter.normalize("wss://nostr.oxtr.dev"), read = true, write = true, feedTypes = activeTypesChats),
RelaySetupInfo(RelayUrlFormatter.normalize("wss://nostr.fmt.wiz.biz"), read = true, write = false, feedTypes = activeTypesChats),
RelaySetupInfo(RelayUrlFormatter.normalize("wss://relay.damus.io"), read = true, write = true, feedTypes = activeTypes),
RelaySetupInfo(RelayUrlFormatter.normalize("wss://relay.damus.io"), read = true, write = true, feedTypes = activeTypesFollows),
// Global
RelaySetupInfo(RelayUrlFormatter.normalize("wss://nostr.mom"), read = true, write = true, feedTypes = activeTypesGlobalChats),
RelaySetupInfo(RelayUrlFormatter.normalize("wss://nos.lol"), read = true, write = true, feedTypes = activeTypesGlobalChats),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
package com.vitorpamplona.ammolite.relays

import android.util.Log
import com.vitorpamplona.ammolite.relays.relays.RelayState
import com.vitorpamplona.ammolite.relays.relays.sockets.WebsocketBuilderFactory
import com.vitorpamplona.ammolite.service.checkNotInMainThread
import com.vitorpamplona.ammolite.sockets.WebsocketBuilder
import com.vitorpamplona.quartz.nip01Core.core.Event
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
Expand All @@ -38,7 +39,7 @@ import java.util.concurrent.TimeUnit
* published through multiple relays. Events are stored with their respective persona.
*/
class NostrClient(
private val websocketBuilder: WebsocketBuilder,
private val websocketBuilder: WebsocketBuilderFactory,
) : RelayPool.Listener {
private val relayPool: RelayPool = RelayPool()
private val subscriptions: MutableSubscriptionManager = MutableSubscriptionManager()
Expand Down Expand Up @@ -174,17 +175,24 @@ class NostrClient(
Log.d("sendAndWaitForResponse", "onError Error from relay ${relay.url} count: ${latch.count} error: $error")
}

override fun onEOSE(
relay: Relay,
subscriptionId: String,
) {
latch.countDown()
Log.d("sendAndWaitForResponse", "onEOSE relay ${relay.url} count: ${latch.count}")
}

override fun onRelayStateChange(
type: Relay.StateType,
type: RelayState,
relay: Relay,
subscriptionId: String?,
) {
if (type == Relay.StateType.DISCONNECT || type == Relay.StateType.EOSE) {
if (type == RelayState.DISCONNECTED) {
latch.countDown()
}
if (type == Relay.StateType.CONNECT) {
if (type == RelayState.CONNECTED) {
Log.d("sendAndWaitForResponse", "${type.name} Sending event to relay ${relay.url} count: ${latch.count}")
relay.sendOverride(signedEvent)
relay.send(signedEvent)
}
Log.d("sendAndWaitForResponse", "onRelayStateChange ${type.name} from relay ${relay.url} count: ${latch.count}")
}
Expand Down Expand Up @@ -312,16 +320,18 @@ class NostrClient(
}
}

override fun onEOSE(
relay: Relay,
subscriptionId: String,
) {
listeners.forEach { it.onEOSE(relay, subscriptionId) }
}

override fun onRelayStateChange(
type: Relay.StateType,
type: RelayState,
relay: Relay,
channel: String?,
) {
// Releases the Web thread for the new payload.
// May need to add a processing queue if processing new events become too costly.
// GlobalScope.launch(Dispatchers.Default) {
listeners.forEach { it.onRelayStateChange(type, relay, channel) }
// }
listeners.forEach { it.onRelayStateChange(type, relay) }
}

@OptIn(DelicateCoroutinesApi::class)
Expand Down Expand Up @@ -419,11 +429,16 @@ class NostrClient(
afterEOSE: Boolean,
) = Unit

/** Connected to or disconnected from a relay */
open fun onEOSE(
relay: Relay,
subscriptionId: String,
) = Unit

/** Connected to or disconnected from a relay */
open fun onRelayStateChange(
type: Relay.StateType,
type: RelayState,
relay: Relay,
subscriptionId: String?,
) = Unit

/** When an relay saves or rejects a new event. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.vitorpamplona.ammolite.relays

import android.util.Log
import com.vitorpamplona.ammolite.relays.relays.RelayState
import com.vitorpamplona.ammolite.service.checkNotInMainThread
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.utils.TimeUtils
Expand Down Expand Up @@ -93,25 +94,20 @@ abstract class NostrDataSource(
}
}

override fun onRelayStateChange(
type: Relay.StateType,
override fun onEOSE(
relay: Relay,
subscriptionId: String?,
subscriptionId: String,
) {
// if (subscriptions.containsKey(subscriptionId)) {
// Log.d([email protected], "Relay ${relay.url} ${subscriptionId}
// ${type.name}")
// }

if (
type == Relay.StateType.EOSE &&
subscriptionId != null &&
subscriptions.containsKey(subscriptionId)
) {
if (subscriptions.containsKey(subscriptionId)) {
markAsEOSE(subscriptionId, relay)
}
}

override fun onRelayStateChange(
type: RelayState,
relay: Relay,
) {}

override fun onSendResponse(
eventId: String,
success: Boolean,
Expand Down
Loading

0 comments on commit e3e9022

Please sign in to comment.