From 0d0edc736a5528fd7c43a9ef47f7f6d55ed1e831 Mon Sep 17 00:00:00 2001 From: Terry Yiu <963907+tyiu@users.noreply.github.com> Date: Sat, 20 Apr 2024 07:53:25 -0400 Subject: [PATCH] Add missing NostrEvent subclass mappings from the EventKind enum (#148) --- Sources/NostrSDK/EventKind.swift | 53 +++++++++++++------ Sources/NostrSDK/RelayResponse.swift | 10 +--- Tests/NostrSDKTests/EventKindTests.swift | 6 +++ .../RelayResponseDecodingTests.swift | 1 + 4 files changed, 46 insertions(+), 24 deletions(-) diff --git a/Sources/NostrSDK/EventKind.swift b/Sources/NostrSDK/EventKind.swift index a42baf5..8819dbb 100644 --- a/Sources/NostrSDK/EventKind.swift +++ b/Sources/NostrSDK/EventKind.swift @@ -126,23 +126,46 @@ public enum EventKind: RawRepresentable, CaseIterable, Codable, Equatable { public var rawValue: RawValue { switch self { - case .setMetadata: return 0 - case .textNote: return 1 - case .followList: return 3 - case .directMessage: return 4 - case .deletion: return 5 - case .repost: return 6 - case .reaction: return 7 - case .genericRepost: return 16 - case .report: return 1984 - case .muteList: return 10000 - case .bookmarksList: return 10003 - case .longformContent: return 30023 + case .setMetadata: return 0 + case .textNote: return 1 + case .followList: return 3 + case .directMessage: return 4 + case .deletion: return 5 + case .repost: return 6 + case .reaction: return 7 + case .genericRepost: return 16 + case .report: return 1984 + case .muteList: return 10000 + case .bookmarksList: return 10003 + case .longformContent: return 30023 case .dateBasedCalendarEvent: return 31922 case .timeBasedCalendarEvent: return 31923 - case .calendar: return 31924 - case .calendarEventRSVP: return 31925 - case let .unknown(value): return value + case .calendar: return 31924 + case .calendarEventRSVP: return 31925 + case let .unknown(value): return value + } + } + + /// The ``NostrEvent`` subclass associated with the kind. + public var classForKind: NostrEvent.Type { + switch self { + case .setMetadata: return SetMetadataEvent.self + case .textNote: return TextNoteEvent.self + case .followList: return FollowListEvent.self + case .directMessage: return DirectMessageEvent.self + case .deletion: return DeletionEvent.self + case .repost: return TextNoteRepostEvent.self + case .reaction: return ReactionEvent.self + case .genericRepost: return GenericRepostEvent.self + case .report: return ReportEvent.self + case .muteList: return MuteListEvent.self + case .bookmarksList: return BookmarksListEvent.self + case .longformContent: return LongformContentEvent.self + case .dateBasedCalendarEvent: return DateBasedCalendarEvent.self + case .timeBasedCalendarEvent: return TimeBasedCalendarEvent.self + case .calendar: return CalendarListEvent.self + case .calendarEventRSVP: return CalendarEventRSVP.self + case .unknown: return NostrEvent.self } } diff --git a/Sources/NostrSDK/RelayResponse.swift b/Sources/NostrSDK/RelayResponse.swift index 6eb509b..77cd1c5 100644 --- a/Sources/NostrSDK/RelayResponse.swift +++ b/Sources/NostrSDK/RelayResponse.swift @@ -17,15 +17,7 @@ fileprivate struct EventKindMapper: Decodable { // swiftlint:disable:this pr /// The ``NostrEvent`` subclass associated with the kind. var classForKind: NostrEvent.Type { - switch kind { - case .setMetadata: return SetMetadataEvent.self - case .textNote: return TextNoteEvent.self - case .followList: return FollowListEvent.self - case .directMessage: return DirectMessageEvent.self - case .repost: return TextNoteRepostEvent.self - case .genericRepost: return GenericRepostEvent.self - default: return NostrEvent.self - } + kind.classForKind } } diff --git a/Tests/NostrSDKTests/EventKindTests.swift b/Tests/NostrSDKTests/EventKindTests.swift index 6b7e899..5a38222 100644 --- a/Tests/NostrSDKTests/EventKindTests.swift +++ b/Tests/NostrSDKTests/EventKindTests.swift @@ -10,6 +10,12 @@ import XCTest final class EventKindTests: XCTestCase { + func testHasClassForKind() { + EventKind.allCases.forEach { kind in + XCTAssertTrue(kind.classForKind !== NostrEvent.self, "NostrEvent subclass for known event kind \"\(kind)\" was not defined.") + } + } + func testIsNonParameterizedReplaceable() throws { XCTAssertTrue(EventKind.setMetadata.isNonParameterizedReplaceable) XCTAssertTrue(EventKind.followList.isNonParameterizedReplaceable) diff --git a/Tests/NostrSDKTests/RelayResponseDecodingTests.swift b/Tests/NostrSDKTests/RelayResponseDecodingTests.swift index 890840c..1221f90 100644 --- a/Tests/NostrSDKTests/RelayResponseDecodingTests.swift +++ b/Tests/NostrSDKTests/RelayResponseDecodingTests.swift @@ -48,6 +48,7 @@ final class RelayResponseDecodingTests: XCTestCase, FixtureLoading { } XCTAssertEqual(subscriptionId, "some-subscription-id") XCTAssertNotNil(event) + XCTAssertTrue(event is TextNoteEvent) XCTAssertEqual(event.id, "fa5ed84fc8eeb959fd39ad8e48388cfc33075991ef8e50064cfcecfd918bb91b") } else { XCTFail("failed to decode")