Skip to content

Commit

Permalink
Add missing NostrEvent subclass mappings from the EventKind enum (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiu authored Apr 20, 2024
1 parent 8850043 commit 0d0edc7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
53 changes: 38 additions & 15 deletions Sources/NostrSDK/EventKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
10 changes: 1 addition & 9 deletions Sources/NostrSDK/RelayResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
6 changes: 6 additions & 0 deletions Tests/NostrSDKTests/EventKindTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Tests/NostrSDKTests/RelayResponseDecodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 0d0edc7

Please sign in to comment.