Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NostrEventBuilding protocol to enable flexible composition of shared patterns across different kinds of NostrEvents #175

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example illustrates why using a builder pattern is powerful for something like Nostr. There are all kinds of shared event creation patterns across different Nostr event kinds. Each kind-specific NostrEvent.Builder subclass can easily integrate with the protocol and get the implementation for free. In the case for custom emojis, it can be used across three different event kinds -- I've only integrated with two of them (kind 0 metadata and kind 1 text notes).

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
Loading