Skip to content

Commit

Permalink
Add NostrEventBuilding protocol to enable flexible composition of sha…
Browse files Browse the repository at this point in the history
…red patterns across different kinds of NostrEvents (#175)
  • Loading branch information
tyiu authored Sep 4, 2024
1 parent e18ebda commit 4e65dfa
Show file tree
Hide file tree
Showing 28 changed files with 820 additions and 110 deletions.
10 changes: 10 additions & 0 deletions Sources/NostrSDK/CustomEmoji.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public class CustomEmoji: CustomEmojiValidating, Equatable {
}
}

/// Builder that adds ``CustomEmoji``s to a ``NostrEvent``.
public protocol CustomEmojiBuilding: NostrEventBuilding {}
public extension CustomEmojiBuilding {
/// Adds a list of ``CustomEmoji``.
@discardableResult
func customEmojis(_ customEmojis: [CustomEmoji]) -> Self {
appendTags(contentsOf: customEmojis.map { $0.tag })
}
}

public protocol CustomEmojiInterpreting: NostrEvent, CustomEmojiValidating {}
public extension CustomEmojiInterpreting {
/// Returns the list of well-formatted custom emojis derived from NostrEvent tags.
Expand Down
12 changes: 11 additions & 1 deletion Sources/NostrSDK/Events/AuthenticationEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ public final class AuthenticationEvent: NostrEvent, RelayProviding {
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: .authentication, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
14 changes: 12 additions & 2 deletions Sources/NostrSDK/Events/BookmarksListEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ public final class BookmarksListEvent: NostrEvent, HashtagInterpreting, PrivateT
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}


@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: .bookmarksList, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
12 changes: 11 additions & 1 deletion Sources/NostrSDK/Events/Calendars/CalendarEventRSVP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ public final class CalendarEventRSVP: NostrEvent, ParameterizedReplaceableEvent
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

public init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: .calendarEventRSVP, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
12 changes: 11 additions & 1 deletion Sources/NostrSDK/Events/Calendars/CalendarListEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@ public final class CalendarListEvent: NostrEvent, ParameterizedReplaceableEvent,
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

public init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: .calendar, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
12 changes: 11 additions & 1 deletion Sources/NostrSDK/Events/Calendars/DateBasedCalendarEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ public final class DateBasedCalendarEvent: NostrEvent, CalendarEventInterpreting
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

public init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: .dateBasedCalendarEvent, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
12 changes: 11 additions & 1 deletion Sources/NostrSDK/Events/Calendars/TimeBasedCalendarEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ public final class TimeBasedCalendarEvent: NostrEvent, CalendarEventInterpreting
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

public init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: .timeBasedCalendarEvent, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
14 changes: 12 additions & 2 deletions Sources/NostrSDK/Events/DeletionEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@ public final class DeletionEvent: NostrEvent, EventCoordinatesTagInterpreting {
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}


@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: .deletion, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
14 changes: 12 additions & 2 deletions Sources/NostrSDK/Events/FollowListEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ public final class FollowListEvent: NostrEvent, NonParameterizedReplaceableEvent
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}


@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

init(tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: .followList, content: "", tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
14 changes: 12 additions & 2 deletions Sources/NostrSDK/Events/GenericRepostEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ public class GenericRepostEvent: NostrEvent {
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}


@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

init(content: String, tags: [Tag], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: Self.kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
12 changes: 11 additions & 1 deletion Sources/NostrSDK/Events/GiftWrap/GiftWrapEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ public final class GiftWrapEvent: NostrEvent, NIP44v2Encrypting {
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

public init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970 - TimeInterval.random(in: 0...172800)), signedBy keypair: Keypair) throws {
try super.init(kind: .giftWrap, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
12 changes: 11 additions & 1 deletion Sources/NostrSDK/Events/GiftWrap/SealEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ public final class SealEvent: NostrEvent, NIP44v2Encrypting {
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

public init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970 - TimeInterval.random(in: 0...172800)), signedBy keypair: Keypair) throws {
try super.init(kind: .seal, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
14 changes: 12 additions & 2 deletions Sources/NostrSDK/Events/LegacyEncryptedDirectMessageEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ public final class LegacyEncryptedDirectMessageEvent: NostrEvent, LegacyDirectMe
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}


@available(*, unavailable, message: "This initializer is unavailable for this class.")
required init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), pubkey: String) {
super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, pubkey: pubkey)
}

@available(*, unavailable, message: "This initializer is unavailable for this class.")
override init(id: String, pubkey: String, createdAt: Int64, kind: EventKind, tags: [Tag], content: String, signature: String?) {
super.init(id: id, pubkey: pubkey, createdAt: createdAt, kind: kind, tags: tags, content: content, signature: signature)
}

init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws {
try super.init(kind: .legacyEncryptedDirectMessage, content: content, tags: tags, createdAt: createdAt, signedBy: keypair)
}
Expand Down
Loading

0 comments on commit 4e65dfa

Please sign in to comment.