Skip to content

Commit

Permalink
Add CLOSED relay response
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiu committed May 2, 2024
1 parent 0c4224d commit 594a242
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
22 changes: 14 additions & 8 deletions Sources/NostrSDK/RelayResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,25 @@ enum RelayResponse: Decodable {
case event = "EVENT"
case ok = "OK"
case eose = "EOSE"
case closed = "CLOSED"
case notice = "NOTICE"
case auth = "AUTH"
case count = "COUNT"
}

struct OKMessage {
let type: OKMessageType
struct RelayResponseMessage {
let prefix: RelayResponseMessagePrefix
let message: String

init(rawMessage: String) {
let components = rawMessage.split(separator: ":", maxSplits: 1)
if let firstComponent = components.first {
type = OKMessageType(rawValue: String(firstComponent)) ?? .unknown
prefix = RelayResponseMessagePrefix(rawValue: String(firstComponent)) ?? .unknown
} else {
type = .unknown
prefix = .unknown
}

if type == .unknown {
if prefix == .unknown {
message = rawMessage.trimmingCharacters(in: .whitespacesAndNewlines)
} else if components.count >= 2 {
message = components[1].trimmingCharacters(in: .whitespacesAndNewlines)
Expand All @@ -58,7 +59,7 @@ enum RelayResponse: Decodable {
}
}

enum OKMessageType: String, Codable {
enum RelayResponseMessagePrefix: String, Codable {
case unknown
case duplicate
case pow
Expand All @@ -69,8 +70,9 @@ enum RelayResponse: Decodable {
}

case event(subscriptionId: String, event: NostrEvent)
case ok(eventId: String, success: Bool, message: OKMessage)
case ok(eventId: String, success: Bool, message: RelayResponseMessage)
case eose(subscriptionId: String)
case closed(subscriptionId: String, message: RelayResponseMessage)
case notice(message: String)
case auth(challenge: String)
case count(subscriptionId: String, count: Int)
Expand All @@ -95,10 +97,14 @@ enum RelayResponse: Decodable {
let eventId = try container.decode(String.self)
let success = try container.decode(Bool.self)
let message = try container.decode(String.self)
self = .ok(eventId: eventId, success: success, message: OKMessage(rawMessage: message))
self = .ok(eventId: eventId, success: success, message: RelayResponseMessage(rawMessage: message))
case .eose:
let subscriptionId = try container.decode(String.self)
self = .eose(subscriptionId: subscriptionId)
case .closed:
let subscriptionId = try container.decode(String.self)
let message = try container.decode(String.self)
self = .closed(subscriptionId: subscriptionId, message: RelayResponseMessage(rawMessage: message))
case .notice:
let message = try container.decode(String.self)
self = .notice(message: message)
Expand Down
1 change: 1 addition & 0 deletions Tests/NostrSDKTests/Fixtures/closed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["CLOSED", "some-subscription-id", "error: shutting down idle subscription"]
26 changes: 21 additions & 5 deletions Tests/NostrSDKTests/RelayResponseDecodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class RelayResponseDecodingTests: XCTestCase, FixtureLoading {
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.type, .unknown)
XCTAssertEqual(message.prefix, .unknown)
XCTAssertEqual(message.message, "")
} else {
XCTFail("failed to decode")
Expand All @@ -54,7 +54,7 @@ final class RelayResponseDecodingTests: XCTestCase, FixtureLoading {
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.type, .pow)
XCTAssertEqual(message.prefix, .pow)
XCTAssertEqual(message.message, "difficulty: 25>=24")
} else {
XCTFail("failed to decode")
Expand All @@ -71,7 +71,7 @@ final class RelayResponseDecodingTests: XCTestCase, FixtureLoading {
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.type, .pow)
XCTAssertEqual(message.prefix, .pow)
XCTAssertEqual(message.message, "")
} else {
XCTFail("failed to decode")
Expand All @@ -88,7 +88,7 @@ final class RelayResponseDecodingTests: XCTestCase, FixtureLoading {
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, true)
XCTAssertEqual(message.type, .unknown)
XCTAssertEqual(message.prefix, .unknown)
XCTAssertEqual(message.message, "unknown: reason: unknown")
} else {
XCTFail("failed to decode")
Expand All @@ -105,7 +105,7 @@ final class RelayResponseDecodingTests: XCTestCase, FixtureLoading {
}
XCTAssertEqual(eventId, "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30")
XCTAssertEqual(success, false)
XCTAssertEqual(message.type, .blocked)
XCTAssertEqual(message.prefix, .blocked)
XCTAssertEqual(message.message, "tor exit nodes not allowed")
} else {
XCTFail("failed to decode")
Expand All @@ -126,6 +126,22 @@ final class RelayResponseDecodingTests: XCTestCase, FixtureLoading {
}
}

func testDecodeClosedMessage() throws {
let data = try loadFixtureData("closed")

if let relayResponse = RelayResponse.decode(data: data) {
guard case .closed(let subscriptionId, let message) = relayResponse else {
XCTFail("incorrect type")
return
}
XCTAssertEqual(subscriptionId, "some-subscription-id")
XCTAssertEqual(message.prefix, .error)
XCTAssertEqual(message.message, "shutting down idle subscription")
} else {
XCTFail("failed to decode")
}
}

func testDecodeNoticeMessage() throws {
let data = try loadFixtureData("notice")

Expand Down

0 comments on commit 594a242

Please sign in to comment.