Skip to content

Commit

Permalink
Rename SetMetadataEvent to MetadataEvent to conform to current NIPs r…
Browse files Browse the repository at this point in the history
…epository
  • Loading branch information
tyiu committed May 29, 2024
1 parent 78aeb3b commit b93ba66
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
10 changes: 5 additions & 5 deletions Sources/NostrSDK/EventKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public enum EventKind: RawRepresentable, CaseIterable, Codable, Equatable, Hasha

public typealias RawValue = Int

/// The content is set to a stringified JSON object `{name: <username>, about: <string>, picture: <url, string>}` describing the user who created the event. A relay may delete past `set_metadata` events once it gets a new one for the same pubkey.
/// The content is set to a stringified JSON object `{name: <username>, about: <string>, picture: <url, string>}` describing the user who created the event. A relay may delete past `metadata` events once it gets a new one for the same pubkey.
///
/// See [NIP-01](https://github.com/nostr-protocol/nips/blob/master/01.md)
case setMetadata
case metadata

/// The content is set to the plaintext content of a note (anything the user wants to say). Content that must be parsed, such as Markdown and HTML, should not be used. Clients should also not parse content as those.
///
Expand Down Expand Up @@ -126,7 +126,7 @@ public enum EventKind: RawRepresentable, CaseIterable, Codable, Equatable, Hasha

/// List of all event kinds except for `unknown`.
static public let allCases: AllCases = [
.setMetadata,
.metadata,
.textNote,
.followList,
.legacyEncryptedDirectMessage,
Expand Down Expand Up @@ -158,7 +158,7 @@ public enum EventKind: RawRepresentable, CaseIterable, Codable, Equatable, Hasha

public var rawValue: RawValue {
switch self {
case .setMetadata: return 0
case .metadata: return 0
case .textNote: return 1
case .followList: return 3
case .legacyEncryptedDirectMessage: return 4
Expand All @@ -184,7 +184,7 @@ public enum EventKind: RawRepresentable, CaseIterable, Codable, Equatable, Hasha
/// The ``NostrEvent`` subclass associated with the kind.
public var classForKind: NostrEvent.Type {
switch self {
case .setMetadata: return SetMetadataEvent.self
case .metadata: return MetadataEvent.self
case .textNote: return TextNoteEvent.self
case .followList: return FollowListEvent.self
case .legacyEncryptedDirectMessage: return LegacyEncryptedDirectMessageEvent.self
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SetMetadataEvent.swift
// MetadataEvent.swift
//
//
// Created by Bryan Montz on 7/22/23.
Expand Down Expand Up @@ -67,7 +67,7 @@ public struct UserMetadata: Codable {
/// An event that contains a user profile.
///
/// > Note: [NIP-01 Specification](https://github.com/nostr-protocol/nips/blob/b503f8a92b22be3037b8115fe3e644865a4fa155/01.md#basic-event-kinds)
public final class SetMetadataEvent: NostrEvent, CustomEmojiInterpreting, NonParameterizedReplaceableEvent {
public final class MetadataEvent: NostrEvent, CustomEmojiInterpreting, NonParameterizedReplaceableEvent {

public required init(from decoder: Decoder) throws {
try super.init(from: decoder)
Expand All @@ -79,7 +79,7 @@ public final class SetMetadataEvent: NostrEvent, CustomEmojiInterpreting, NonPar
}

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

/// A dictionary containing all of the properties in the `content` field of the ``NostrEvent``.
Expand All @@ -102,20 +102,20 @@ public final class SetMetadataEvent: NostrEvent, CustomEmojiInterpreting, NonPar

public extension EventCreating {

/// Creates a ``SetMetadataEvent`` (kind 0) and signs it with the provided ``Keypair``.
/// Creates a ``MetadataEvent`` (kind 0) and signs it with the provided ``Keypair``.
/// - Parameters:
/// - userMetadata: The metadata to set.
/// - customEmojis: The custom emojis to emojify with if the matching shortcodes are found in the name or about fields.
/// - keypair: The Keypair to sign with.
/// - Returns: The signed ``SetMetadataEvent``.
/// - Returns: The signed ``MetadataEvent``.
///
/// See [NIP-01](https://github.com/nostr-protocol/nips/blob/master/01.md)
func setMetadataEvent(withUserMetadata userMetadata: UserMetadata, customEmojis: [CustomEmoji] = [], signedBy keypair: Keypair) throws -> SetMetadataEvent {
func metadataEvent(withUserMetadata userMetadata: UserMetadata, customEmojis: [CustomEmoji] = [], signedBy keypair: Keypair) throws -> MetadataEvent {
let metadataAsData = try JSONEncoder().encode(userMetadata)
guard let metadataAsString = String(data: metadataAsData, encoding: .utf8) else {
throw EventCreatingError.invalidInput
}
let customEmojiTags = customEmojis.map { $0.tag }
return try SetMetadataEvent(content: metadataAsString, tags: customEmojiTags, signedBy: keypair)
return try MetadataEvent(content: metadataAsString, tags: customEmojiTags, signedBy: keypair)
}
}
2 changes: 1 addition & 1 deletion Tests/NostrSDKTests/EventCoordinatesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class EventCoordinatesTests: XCTestCase {
func testInitFailsOnNonParameterizedReplaceableEventWithIdentifier() throws {
XCTAssertThrowsError(
try EventCoordinates(
kind: .setMetadata,
kind: .metadata,
pubkey: Keypair.test.publicKey,
identifier: "should-fail"
)
Expand Down
2 changes: 1 addition & 1 deletion Tests/NostrSDKTests/EventKindTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class EventKindTests: XCTestCase {
}

func testIsNonParameterizedReplaceable() throws {
XCTAssertTrue(EventKind.setMetadata.isNonParameterizedReplaceable)
XCTAssertTrue(EventKind.metadata.isNonParameterizedReplaceable)
XCTAssertTrue(EventKind.followList.isNonParameterizedReplaceable)
XCTAssertTrue(EventKind.muteList.isNonParameterizedReplaceable)
XCTAssertTrue(EventKind(rawValue: 19999).isNonParameterizedReplaceable)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SetMetadataEventTests.swift
// MetadataEventTests.swift
//
//
// Created by Terry Yiu on 4/14/24.
Expand All @@ -8,9 +8,9 @@
@testable import NostrSDK
import XCTest

final class SetMetadataEventTests: XCTestCase, EventCreating, EventVerifying, FixtureLoading {
final class MetadataEventTests: XCTestCase, EventCreating, EventVerifying, FixtureLoading {

func testCreateSetMetadataEvent() throws {
func testCreateMetadataEvent() throws {
let meta = UserMetadata(name: "Nostr SDK Test :ostrich:",
displayName: "Nostr SDK Display Name",
about: "I'm a test account. I'm used to test the Nostr SDK for Apple platforms. :apple:",
Expand All @@ -31,8 +31,8 @@ final class SetMetadataEventTests: XCTestCase, EventCreating, EventVerifying, Fi
Tag(name: .emoji, value: "apple", otherParameters: ["https://nostrsdk.com/apple.png"])
]

let event = try setMetadataEvent(withUserMetadata: meta, customEmojis: customEmojis, signedBy: Keypair.test)
let expectedReplaceableEventCoordinates = try XCTUnwrap(EventCoordinates(kind: .setMetadata, pubkey: Keypair.test.publicKey))
let event = try metadataEvent(withUserMetadata: meta, customEmojis: customEmojis, signedBy: Keypair.test)
let expectedReplaceableEventCoordinates = try XCTUnwrap(EventCoordinates(kind: .metadata, pubkey: Keypair.test.publicKey))

XCTAssertEqual(event.userMetadata?.name, "Nostr SDK Test :ostrich:")
XCTAssertEqual(event.userMetadata?.displayName, "Nostr SDK Display Name")
Expand All @@ -48,14 +48,14 @@ final class SetMetadataEventTests: XCTestCase, EventCreating, EventVerifying, Fi
try verifyEvent(event)
}

func testDecodeSetMetadata() throws {
func testDecodeMetadata() throws {

let event: SetMetadataEvent = try decodeFixture(filename: "set_metadata")
let event: MetadataEvent = try decodeFixture(filename: "metadata")

XCTAssertEqual(event.id, "d214c914b0ab49ec919fa5f60fabf746f421e432d96f941bd2573e4d22e36b51")
XCTAssertEqual(event.pubkey, "00000000827ffaa94bfea288c3dfce4422c794fbb96625b6b31e9049f729d700")
XCTAssertEqual(event.createdAt, 1684370291)
XCTAssertEqual(event.kind, .setMetadata)
XCTAssertEqual(event.kind, .metadata)
XCTAssertEqual(event.tags, [])
XCTAssertTrue(event.content.hasPrefix("{\"banner\":\"https://nostr.build/i/nostr.build"))
XCTAssertEqual(event.signature, "7bb7f031fbf41f49eeb44fdfb061bc8d143197d33fae8d29b017709adf2b17c76e78ccb2ee128ee93d0661cad4c626a747d48a178745c94944a693ff31ea7619")
Expand All @@ -79,14 +79,14 @@ final class SetMetadataEventTests: XCTestCase, EventCreating, EventVerifying, Fi
XCTAssertEqual(userMetadata.bannerPictureURL, URL(string: "https://nostr.build/i/nostr.build_90a51a2e50c9f42288260d01b3a2a4a1c7a9df085423abad7809e76429da7cdc.gif"))
}

func testDecodeSetMetadataWithEmptyWebsite() throws {
func testDecodeMetadataWithEmptyWebsite() throws {

let event: SetMetadataEvent = try decodeFixture(filename: "set_metadata_alternate")
let event: MetadataEvent = try decodeFixture(filename: "metadata_alternate")

XCTAssertEqual(event.id, "2883f411daaef3370f87dc4456fbe1184ab50ec97013249d7cdda4b8938d0b0a")
XCTAssertEqual(event.pubkey, "58c741aa630c2da35a56a77c1d05381908bd10504fdd2d8b43f725efa6d23196")
XCTAssertEqual(event.createdAt, 1676405699)
XCTAssertEqual(event.kind, .setMetadata)
XCTAssertEqual(event.kind, .metadata)
XCTAssertEqual(event.tags, [])
XCTAssertTrue(event.content.hasPrefix("{\"website\":\"\",\"nip05\":"))
XCTAssertEqual(event.signature, "6f12e0090940bf923d96e9c1dce134c1c16c5fdb1e79efff3ed791bb6ff985b4dda609dc85e1ad15c752c6c5f4cbbf8949068731e1b881ac13b2eb1ce59fc578")
Expand All @@ -104,14 +104,14 @@ final class SetMetadataEventTests: XCTestCase, EventCreating, EventVerifying, Fi
XCTAssertEqual(userMetadata.nostrAddress, "[email protected]")
}

func testDecodeSetMetadataWithCustomEmojis() throws {
func testDecodeMetadataWithCustomEmojis() throws {

let event: SetMetadataEvent = try decodeFixture(filename: "set_metadata_custom_emojis")
let event: MetadataEvent = try decodeFixture(filename: "metadata_custom_emojis")

XCTAssertEqual(event.id, "290e0e02411669d8c6f31b95259020458bb1ad43cec8a4fdf87e5c8628ab3e54")
XCTAssertEqual(event.pubkey, "9947f9659dd80c3682402b612f5447e28249997fb3709500c32a585eb0977340")
XCTAssertEqual(event.createdAt, 1699769361)
XCTAssertEqual(event.kind, .setMetadata)
XCTAssertEqual(event.kind, .metadata)
XCTAssertEqual(event.tags, [Tag(name: .emoji, value: "ostrich", otherParameters: ["https://nostrsdk.com/ostrich.png"]), Tag(name: .emoji, value: "apple", otherParameters: ["https://nostrsdk.com/apple.png"])])
XCTAssertTrue(event.content.hasPrefix("{\"banner\":\"https://nostrsdk.com/banner.png"))
XCTAssertEqual(event.signature, "9039249cca3b29208fade093a96c7929fa944dfe566ae77933efe738de75852d67e93cbe3c9321dbe95cabb705435071a5ce3116adadc135e493f5939e2e664c")
Expand Down
File renamed without changes.

0 comments on commit b93ba66

Please sign in to comment.