Merge branch 'main-upstream' into chess-enhancements-and-bug-fixes
This commit is contained in:
+2
-1
@@ -27,6 +27,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.commands.toRelay.Command
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.commands.toRelay.CountCmd
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.commands.toRelay.EventCmd
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.commands.toRelay.ReqCmd
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.server.policies.PolicyStack
|
||||
|
||||
/**
|
||||
* Defines custom behavior for this relay.
|
||||
@@ -76,7 +77,7 @@ interface IRelayPolicy {
|
||||
*/
|
||||
fun canSendToSession(event: Event): Boolean = true
|
||||
|
||||
operator fun plus(other: IRelayPolicy) = listOf(this, other)
|
||||
operator fun plus(other: IRelayPolicy): IRelayPolicy = PolicyStack(this, other)
|
||||
}
|
||||
|
||||
sealed interface PolicyResult<T : Command> {
|
||||
|
||||
+30
-3
@@ -41,7 +41,7 @@ class NostrServer(
|
||||
private val store: IEventStore,
|
||||
private val policyBuilder: () -> IRelayPolicy = { VerifyPolicy },
|
||||
private val parentContext: CoroutineContext = SupervisorJob(),
|
||||
) {
|
||||
) : AutoCloseable {
|
||||
private val subStore = LiveEventStore(store)
|
||||
|
||||
/** Scope for all subscriptions. */
|
||||
@@ -70,11 +70,38 @@ class NostrServer(
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts down the server, cancelling all subscriptions and sessions.
|
||||
* Registers a new client connection and serves it for the duration of
|
||||
* [incoming]. The session is automatically closed when [incoming] returns.
|
||||
*
|
||||
* @param send Callback the server uses to send JSON messages to this client.
|
||||
* @param incoming Suspend block that yields raw JSON strings from the client
|
||||
* (e.g., reading WebSocket text frames in a loop).
|
||||
*/
|
||||
fun shutdown() {
|
||||
suspend fun serve(
|
||||
send: (String) -> Unit,
|
||||
incoming: suspend (RelaySession) -> Unit,
|
||||
) {
|
||||
val session = connect(send)
|
||||
try {
|
||||
incoming(session)
|
||||
} finally {
|
||||
session.close()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts down the server, cancelling all subscriptions and closing the store.
|
||||
*/
|
||||
override fun close() {
|
||||
connections.forEach { _, session -> session.cancelAllSubscriptions() }
|
||||
connections.clear()
|
||||
scope.cancel()
|
||||
store.close()
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts down the server, cancelling all subscriptions and closing the store.
|
||||
*/
|
||||
@Deprecated("Use close() instead", replaceWith = ReplaceWith("close()"))
|
||||
fun shutdown() = close()
|
||||
}
|
||||
|
||||
+2
-2
@@ -23,7 +23,7 @@ package com.vitorpamplona.quartz.nip01Core.store
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
|
||||
|
||||
interface IEventStore {
|
||||
interface IEventStore : AutoCloseable {
|
||||
fun insert(event: Event)
|
||||
|
||||
interface ITransaction {
|
||||
@@ -56,5 +56,5 @@ interface IEventStore {
|
||||
|
||||
fun deleteExpiredEvents()
|
||||
|
||||
fun close()
|
||||
override fun close()
|
||||
}
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nip89AppHandlers.definition
|
||||
|
||||
fun AppDefinitionEvent.platformLinks() = tags.platformLinks()
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nip89AppHandlers.definition
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.TagArray
|
||||
import com.vitorpamplona.quartz.nip89AppHandlers.definition.tags.PlatformLinkTag
|
||||
|
||||
fun TagArray.platformLinks() = this.mapNotNull(PlatformLinkTag::parse)
|
||||
+1
-1
@@ -41,7 +41,7 @@ class PlatformLinkTag(
|
||||
}
|
||||
|
||||
fun parse(tag: Tag): PlatformLinkTag? {
|
||||
if (match(tag)) return PlatformLinkTag(tag[1], tag[2], tag.getOrNull(3))
|
||||
if (match(tag)) return PlatformLinkTag(tag[0], tag[1], tag.getOrNull(2))
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nip89AppHandlers.recommendation
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.TagArray
|
||||
import com.vitorpamplona.quartz.nip89AppHandlers.recommendation.tags.RecommendationTag
|
||||
|
||||
fun TagArray.recommendations() = this.mapNotNull(RecommendationTag::parse)
|
||||
|
||||
fun TagArray.recommendationAddresses() = this.mapNotNull(RecommendationTag::parseAddressId)
|
||||
+14
-14
@@ -142,7 +142,7 @@ class NostrServerAuthTest {
|
||||
assertEquals(1, collector.messages.size)
|
||||
assertTrue(collector.messages[0].contains("\"AUTH\""))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -167,7 +167,7 @@ class NostrServerAuthTest {
|
||||
assertTrue((session.policy as FullAuthPolicy).isAuthenticated())
|
||||
assertTrue(session.policy.authenticatedUsers.contains(pubkey))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -188,7 +188,7 @@ class NostrServerAuthTest {
|
||||
assertTrue(okMessages[0].contains("challenge"))
|
||||
assertFalse((session.policy as FullAuthPolicy).isAuthenticated())
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -213,7 +213,7 @@ class NostrServerAuthTest {
|
||||
assertTrue(okMessages[0].contains("relay url"))
|
||||
assertFalse((session.policy as FullAuthPolicy).isAuthenticated())
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -242,7 +242,7 @@ class NostrServerAuthTest {
|
||||
assertTrue(okMessages[0].contains("created_at"))
|
||||
assertFalse((session.policy as FullAuthPolicy).isAuthenticated())
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -281,7 +281,7 @@ class NostrServerAuthTest {
|
||||
assertTrue(okMessages[0].contains("could not parse message"))
|
||||
assertFalse((session.policy as FullAuthPolicy).isAuthenticated())
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -315,7 +315,7 @@ class NostrServerAuthTest {
|
||||
assertTrue(authedPubkeys.contains(pubkey))
|
||||
assertTrue(authedPubkeys.contains(pubkey2))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
// -- NIP-42: requireAuth ---------------------------------------------------
|
||||
@@ -337,7 +337,7 @@ class NostrServerAuthTest {
|
||||
assertTrue(okMessages[0].contains("\"false\""))
|
||||
assertTrue(okMessages[0].contains("auth-required:"))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -354,7 +354,7 @@ class NostrServerAuthTest {
|
||||
assertEquals(1, closedMessages.size)
|
||||
assertTrue(closedMessages[0].contains("auth-required:"))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -371,7 +371,7 @@ class NostrServerAuthTest {
|
||||
assertEquals(1, closedMessages.size)
|
||||
assertTrue(closedMessages[0].contains("auth-required:"))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -410,7 +410,7 @@ class NostrServerAuthTest {
|
||||
val eoseMessages = collector.rawMessagesContaining("EOSE")
|
||||
assertTrue(eoseMessages.isNotEmpty())
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -430,7 +430,7 @@ class NostrServerAuthTest {
|
||||
assertEquals(1, okMessages.size)
|
||||
assertTrue(okMessages[0].contains("\"true\""))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
// -- Custom AuthPolicy tests -----------------------------------------------
|
||||
@@ -471,7 +471,7 @@ class NostrServerAuthTest {
|
||||
assertTrue(okMessages[1].contains("\"false\""))
|
||||
assertTrue(okMessages[1].contains("auth-required:"))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -531,6 +531,6 @@ class NostrServerAuthTest {
|
||||
assertEquals(1, events.size)
|
||||
assertEquals(pubkey, events[0].event.pubKey)
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
}
|
||||
|
||||
+11
-11
@@ -115,7 +115,7 @@ class NostrServerTest {
|
||||
val stored = store.query<Event>(Filter(ids = listOf(event.id)))
|
||||
assertEquals(1, stored.size)
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -138,7 +138,7 @@ class NostrServerTest {
|
||||
assertTrue(okMessages[0].contains("\"true\""))
|
||||
assertTrue(okMessages[1].contains("\"false\""))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
// -- REQ command -----------------------------------------------------------
|
||||
@@ -172,7 +172,7 @@ class NostrServerTest {
|
||||
// Events should be newest first
|
||||
assertTrue(events[0].event.createdAt >= events[1].event.createdAt)
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -195,7 +195,7 @@ class NostrServerTest {
|
||||
val events = collector.parsedEventMessages().filterIsInstance<EventMessage>()
|
||||
assertEquals(3, events.size)
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
// -- Live subscription -----------------------------------------------------
|
||||
@@ -226,7 +226,7 @@ class NostrServerTest {
|
||||
assertTrue(newMessages.isNotEmpty())
|
||||
assertTrue(newMessages[0].contains("\"EVENT\""))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -251,7 +251,7 @@ class NostrServerTest {
|
||||
|
||||
assertEquals(countAfterEose, collector1.messages.size)
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
// -- CLOSE command ---------------------------------------------------------
|
||||
@@ -282,7 +282,7 @@ class NostrServerTest {
|
||||
|
||||
assertEquals(countAfterClose, collector1.messages.size)
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -318,7 +318,7 @@ class NostrServerTest {
|
||||
assertTrue(newMessages[0].contains("\"EVENT\""))
|
||||
assertTrue(newMessages[0].contains(hexId(2)))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
// -- COUNT command (NIP-45) ------------------------------------------------
|
||||
@@ -344,7 +344,7 @@ class NostrServerTest {
|
||||
assertEquals(1, countMessages.size)
|
||||
assertTrue(countMessages[0].contains("\"count\":2"))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
// -- Disconnect ------------------------------------------------------------
|
||||
@@ -374,7 +374,7 @@ class NostrServerTest {
|
||||
assertEquals(countAfterDisconnect, collector1.messages.size)
|
||||
assertEquals(2, collector2.messages.size)
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
|
||||
// -- Invalid messages ------------------------------------------------------
|
||||
@@ -392,6 +392,6 @@ class NostrServerTest {
|
||||
assertEquals(1, collector.messages.size)
|
||||
assertTrue(collector.messages[0].contains("NOTICE"))
|
||||
|
||||
server.shutdown()
|
||||
server.close()
|
||||
}
|
||||
}
|
||||
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nip89AppHandlers.definition
|
||||
|
||||
import com.vitorpamplona.quartz.nip89AppHandlers.definition.tags.PlatformLinkTag
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
|
||||
class PlatformLinkTagTest {
|
||||
@Test
|
||||
fun parsesWebPlatformLinkWithEntityType() {
|
||||
val tag = arrayOf("web", "https://example.com/a/<bech32>", "nevent")
|
||||
val result = PlatformLinkTag.parse(tag)
|
||||
|
||||
assertNotNull(result)
|
||||
assertEquals("web", result.platform)
|
||||
assertEquals("https://example.com/a/<bech32>", result.uri)
|
||||
assertEquals("nevent", result.entityType)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesAndroidPlatformLink() {
|
||||
val tag = arrayOf("android", "amethyst://note/<bech32>", "note")
|
||||
val result = PlatformLinkTag.parse(tag)
|
||||
|
||||
assertNotNull(result)
|
||||
assertEquals("android", result.platform)
|
||||
assertEquals("amethyst://note/<bech32>", result.uri)
|
||||
assertEquals("note", result.entityType)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesIosPlatformLink() {
|
||||
val tag = arrayOf("ios", "damus://note/<bech32>", "naddr")
|
||||
val result = PlatformLinkTag.parse(tag)
|
||||
|
||||
assertNotNull(result)
|
||||
assertEquals("ios", result.platform)
|
||||
assertEquals("damus://note/<bech32>", result.uri)
|
||||
assertEquals("naddr", result.entityType)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rejectsTagWithOnlyOneElement() {
|
||||
val tag = arrayOf("web")
|
||||
assertNull(PlatformLinkTag.parse(tag))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rejectsUnknownPlatform() {
|
||||
val tag = arrayOf("windows", "https://example.com", "note")
|
||||
assertNull(PlatformLinkTag.parse(tag))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun roundTripPreservesValues() {
|
||||
val original = PlatformLinkTag("web", "https://example.com/e/<bech32>", "nevent")
|
||||
val tagArray = original.toTagArray()
|
||||
val parsed = PlatformLinkTag.parse(tagArray)
|
||||
|
||||
assertNotNull(parsed)
|
||||
assertEquals(original.platform, parsed.platform)
|
||||
assertEquals(original.uri, parsed.uri)
|
||||
assertEquals(original.entityType, parsed.entityType)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun roundTripWithoutEntityType() {
|
||||
val original = PlatformLinkTag("android", "amethyst://open/<bech32>", null)
|
||||
val tagArray = original.toTagArray()
|
||||
val parsed = PlatformLinkTag.parse(tagArray)
|
||||
|
||||
// Without entityType, tag only has 2 elements, so match requires has(2) which means 3 elements
|
||||
// This is expected behavior per NIP-89 spec (entityType is specified)
|
||||
assertNull(parsed)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user