-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor out common non-NIP-01 tags out of NostrEvent file into separ…
…ate protocols for better code separation (#197)
- Loading branch information
Showing
9 changed files
with
181 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// AlternativeSummaryTag.swift | ||
// NostrSDK | ||
// | ||
// Created by Terry Yiu on 11/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Interprets the "alt" alternative summary tag. | ||
/// | ||
/// See [NIP-31 - Dealing with unknown event kinds](https://github.com/nostr-protocol/nips/blob/master/31.md). | ||
public protocol AlternativeSummaryTagInterpreting: NostrEvent {} | ||
public extension AlternativeSummaryTagInterpreting { | ||
/// A short human-readable plaintext summary of what the event is about | ||
/// when the event kind is part of a custom protocol and isn't meant to be read as text (like kind:1). | ||
var alternativeSummary: String? { | ||
firstValueForTagName(.alternativeSummary) | ||
} | ||
} | ||
|
||
/// Builder that adds an "alt" alternative summary tag to an event. | ||
/// | ||
/// See [NIP-31 - Dealing with unknown event kinds](https://github.com/nostr-protocol/nips/blob/master/31.md). | ||
public protocol AlternativeSummaryTagBuilding: NostrEventBuilding {} | ||
public extension AlternativeSummaryTagBuilding { | ||
/// Specifies a short human-readable plaintext summary of what the event is about | ||
/// when the event kind is part of a custom protocol and isn't meant to be read as text (like kind:1). | ||
@discardableResult | ||
func alternativeSummary(_ alternativeSummary: String) -> Self { | ||
appendTags(Tag(name: .alternativeSummary, value: alternativeSummary)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// ExpirationTag.swift | ||
// NostrSDK | ||
// | ||
// Created by Terry Yiu on 11/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Interprets the expiration tag. | ||
/// | ||
/// See [NIP-40 - Expiration Timestamp](https://github.com/nostr-protocol/nips/blob/master/40.md). | ||
public protocol ExpirationTagInterpreting: NostrEvent {} | ||
public extension ExpirationTagInterpreting { | ||
/// Unix timestamp at which the message SHOULD be considered expired (by relays and clients) and SHOULD be deleted by relays. | ||
var expiration: Int64? { | ||
if let expiration = firstValueForTagName(.expiration) { | ||
return Int64(expiration) | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
/// Whether the message SHOULD be considered expired (by relays and clients) and SHOULD be deleted by relays. | ||
var isExpired: Bool { | ||
if let expiration { | ||
return Int64(Date.now.timeIntervalSince1970) >= expiration | ||
} else { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
/// Builder that adds an expiration to an event. | ||
/// | ||
/// See [NIP-40 - Expiration Timestamp](https://github.com/nostr-protocol/nips/blob/master/40.md). | ||
public protocol ExpirationTagBuilding: NostrEventBuilding {} | ||
public extension ExpirationTagBuilding { | ||
/// Specifies a unix timestamp at which the message SHOULD be considered expired (by relays and clients) and SHOULD be deleted by relays. | ||
@discardableResult | ||
func expiration(_ expiration: Int64) -> Self { | ||
appendTags(Tag(name: .expiration, value: String(expiration))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Tests/NostrSDKTests/Events/Tags/AlternativeSummaryTagTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// AlternativeSummaryTagTests.swift | ||
// NostrSDK | ||
// | ||
// Created by Terry Yiu on 11/3/24. | ||
// | ||
|
||
@testable import NostrSDK | ||
import XCTest | ||
|
||
final class AlternativeSummaryTagTests: XCTestCase, FixtureLoading { | ||
|
||
func testAlternativeSummary() throws { | ||
let alternativeSummary = "Alternative summary to display for clients that do not support this event kind." | ||
let customEvent = try NostrEvent.Builder(kind: EventKind(rawValue: 23456)) | ||
.alternativeSummary(alternativeSummary) | ||
.build(signedBy: .test) | ||
XCTAssertEqual(customEvent.alternativeSummary, alternativeSummary) | ||
|
||
let decodedCustomEventWithAltTag: NostrEvent = try decodeFixture(filename: "custom_event_alt_tag") | ||
XCTAssertEqual(decodedCustomEventWithAltTag.alternativeSummary, alternativeSummary) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// ExpirationTagTests.swift | ||
// NostrSDK | ||
// | ||
// Created by Terry Yiu on 11/3/24. | ||
// | ||
|
||
@testable import NostrSDK | ||
import XCTest | ||
|
||
final class ExpirationTagTests: XCTestCase, FixtureLoading { | ||
|
||
func testExpiration() throws { | ||
let futureExpiration = Int64(Date.now.timeIntervalSince1970 + 10000) | ||
let futureExpirationEvent = try NostrEvent.Builder(kind: .textNote) | ||
.expiration(futureExpiration) | ||
.build(signedBy: .test) | ||
XCTAssertEqual(futureExpirationEvent.expiration, futureExpiration) | ||
XCTAssertFalse(futureExpirationEvent.isExpired) | ||
|
||
let pastExpiration = Int64(Date.now.timeIntervalSince1970 - 1) | ||
let pastExpirationEvent = try NostrEvent.Builder(kind: .textNote) | ||
.expiration(pastExpiration) | ||
.build(signedBy: .test) | ||
XCTAssertEqual(pastExpirationEvent.expiration, pastExpiration) | ||
XCTAssertTrue(pastExpirationEvent.isExpired) | ||
|
||
let decodedExpiredEvent: NostrEvent = try decodeFixture(filename: "test_event_expired") | ||
XCTAssertEqual(decodedExpiredEvent.expiration, 1697090842) | ||
XCTAssertTrue(decodedExpiredEvent.isExpired) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// LabelTagTests.swift | ||
// NostrSDK | ||
// | ||
// Created by Terry Yiu on 11/3/24. | ||
// | ||
|
||
@testable import NostrSDK | ||
import XCTest | ||
|
||
final class LabelTagTests: XCTestCase, EventVerifying { | ||
|
||
func testLabels() throws { | ||
let event = try TextNoteEvent.Builder() | ||
.appendLabels("IT-MI", "US-CA", namespace: "ISO-3166-2") | ||
.appendLabels("en", namespace: "ISO-639-1") | ||
.appendLabels("Milan", "San Francisco", mark: "cities") | ||
.appendLabels("Italy", "United States of America") | ||
.content("It's beautiful here in Milan and wonderful there in San Francisco!") | ||
.build(signedBy: .test) | ||
|
||
XCTAssertEqual(event.labels(for: "ISO-3166-2"), ["IT-MI", "US-CA"]) | ||
XCTAssertEqual(event.labels(for: "ISO-639-1"), ["en"]) | ||
XCTAssertEqual(event.labels(for: "cities"), ["Milan", "San Francisco"]) | ||
XCTAssertEqual(event.labels(for: nil), ["Italy", "United States of America"]) | ||
XCTAssertEqual(event.labels(for: "ugc"), ["Italy", "United States of America"]) | ||
XCTAssertEqual(event.labels(for: "doesnotexist"), []) | ||
|
||
XCTAssertEqual(event.labelNamespaces, ["ISO-3166-2", "ISO-639-1"]) | ||
|
||
let labels = event.labels | ||
XCTAssertEqual(labels["ISO-3166-2"], ["IT-MI", "US-CA"]) | ||
XCTAssertEqual(labels["ISO-639-1"], ["en"]) | ||
XCTAssertEqual(labels["cities"], ["Milan", "San Francisco"]) | ||
XCTAssertEqual(labels["ugc"], ["Italy", "United States of America"]) | ||
XCTAssertEqual(labels["doesnotexist"], nil) | ||
|
||
try verifyEvent(event) | ||
} | ||
|
||
} |