Skip to content

Commit

Permalink
Refactor duplicate code for the shareableEventCoordinates function fo…
Browse files Browse the repository at this point in the history
…r ReplaceableEvents
  • Loading branch information
tyiu committed Jun 30, 2024
1 parent 34e98a4 commit 225552c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
14 changes: 1 addition & 13 deletions Sources/NostrSDK/Events/NonParameterizedReplaceableEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ public extension NonParameterizedReplaceableEvent {
}

func shareableEventCoordinates(relayURLStrings: [String]? = nil, includeAuthor: Bool = true, includeKind: Bool = true) throws -> String {
let validatedRelayURLStrings = try relayURLStrings?.map {
try validateRelayURLString($0)
}.map { $0.absoluteString }

var metadata = Metadata(relays: validatedRelayURLStrings, identifier: "")
if includeAuthor {
metadata.pubkey = pubkey
}
if includeKind {
metadata.kind = UInt32(kind.rawValue)
}

return try encodedIdentifier(with: metadata, identifierType: .address)
try shareableEventCoordinates(relayURLStrings: relayURLStrings, includeAuthor: includeAuthor, includeKind: includeKind, identifier: "")
}
}
14 changes: 1 addition & 13 deletions Sources/NostrSDK/Events/ParameterizedReplaceableEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ public extension ParameterizedReplaceableEvent {
}

func shareableEventCoordinates(relayURLStrings: [String]? = nil, includeAuthor: Bool = true, includeKind: Bool = true) throws -> String {
let validatedRelayURLStrings = try relayURLStrings?.map {
try validateRelayURLString($0)
}.map { $0.absoluteString }

var metadata = Metadata(relays: validatedRelayURLStrings, identifier: identifier)
if includeAuthor {
metadata.pubkey = pubkey
}
if includeKind {
metadata.kind = UInt32(kind.rawValue)
}

return try encodedIdentifier(with: metadata, identifierType: .address)
try shareableEventCoordinates(relayURLStrings: relayURLStrings, includeAuthor: includeAuthor, includeKind: includeKind, identifier: identifier ?? "")
}
}
18 changes: 18 additions & 0 deletions Sources/NostrSDK/Events/ReplaceableEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,21 @@ public protocol ReplaceableEvent: NostrEvent {
/// > Note: [NIP-19 bech32-encoded entities](https://github.com/nostr-protocol/nips/blob/master/19.md)
func shareableEventCoordinates(relayURLStrings: [String]?, includeAuthor: Bool, includeKind: Bool) throws -> String
}

extension ReplaceableEvent {
func shareableEventCoordinates(relayURLStrings: [String]?, includeAuthor: Bool, includeKind: Bool, identifier: String) throws -> String {
let validatedRelayURLStrings = try relayURLStrings?.map {
try validateRelayURLString($0)
}.map { $0.absoluteString }

var metadata = Metadata(relays: validatedRelayURLStrings, identifier: identifier)
if includeAuthor {
metadata.pubkey = pubkey
}
if includeKind {
metadata.kind = UInt32(kind.rawValue)
}

return try encodedIdentifier(with: metadata, identifierType: .address)
}
}

0 comments on commit 225552c

Please sign in to comment.