Skip to content

Commit

Permalink
Handle Invalid URLs when Parsing UserMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
joelklabo committed Aug 21, 2023
1 parent 797f285 commit 717f97d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Sources/NostrSDK/Events/SetMetadataEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public struct UserMetadata: Codable {
self.pictureURL = pictureURL
self.bannerPictureURL = bannerPictureURL
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decodeIfPresent(String.self, forKey: .name)
displayName = try container.decodeIfPresent(String.self, forKey: .displayName)
about = try container.decodeIfPresent(String.self, forKey: .about)
website = try? container.decodeIfPresent(URL.self, forKey: .website)
nostrAddress = try container.decodeIfPresent(String.self, forKey: .nostrAddress)
pictureURL = try? container.decodeIfPresent(URL.self, forKey: .pictureURL)
bannerPictureURL = try? container.decodeIfPresent(URL.self, forKey: .bannerPictureURL)
}
}

/// An event that contains a user profile.
Expand Down
25 changes: 25 additions & 0 deletions Tests/NostrSDKTests/EventDecodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ final class EventDecodingTests: XCTestCase, FixtureLoading {
XCTAssertEqual(userMetadata.bannerPictureURL, URL(string: "https://nostr.build/i/nostr.build_90a51a2e50c9f42288260d01b3a2a4a1c7a9df085423abad7809e76429da7cdc.gif"))
}

func testDecodeSetMetadataWithEmptyWebsite() throws {

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

XCTAssertEqual(event.id, "2883f411daaef3370f87dc4456fbe1184ab50ec97013249d7cdda4b8938d0b0a")
XCTAssertEqual(event.pubkey, "58c741aa630c2da35a56a77c1d05381908bd10504fdd2d8b43f725efa6d23196")
XCTAssertEqual(event.createdAt, 1676405699)
XCTAssertEqual(event.kind, .setMetadata)
XCTAssertEqual(event.tags, [])
XCTAssertTrue(event.content.hasPrefix("{\"website\":\"\",\"nip05\":"))
XCTAssertEqual(event.signature, "6f12e0090940bf923d96e9c1dce134c1c16c5fdb1e79efff3ed791bb6ff985b4dda609dc85e1ad15c752c6c5f4cbbf8949068731e1b881ac13b2eb1ce59fc578")

// access metadata properties from raw dictionary
XCTAssertEqual(event.rawUserMetadata["name"] as? String, "gladstein")
XCTAssertEqual(event.rawUserMetadata["display_name"] as? String, "gladstein")
XCTAssertEqual(event.rawUserMetadata["nip05"] as? String, "[email protected]")

// access metadata properties from decoded object
let userMetadata = try XCTUnwrap(event.userMetadata)
XCTAssertEqual(userMetadata.name, "gladstein")
XCTAssertEqual(userMetadata.about, "")
XCTAssertNil(userMetadata.website)
XCTAssertEqual(userMetadata.nostrAddress, "[email protected]")
}

func testDecodeTextNote() throws {

let event: TextNoteEvent = try decodeFixture(filename: "text_note")
Expand Down
9 changes: 9 additions & 0 deletions Tests/NostrSDKTests/Fixtures/set_metadata_alternate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"content": "{\"website\":\"\",\"nip05\":\"[email protected]\",\"picture\":\"https://sps.columbia.edu/sites/default/files/styles/card_square_270x270_medium_1x/public/2022-06/1576737889853.jpg?h=5316ccab&itok=0JSGy3MH\",\"lud16\":\"[email protected]\",\"display_name\":\"gladstein\",\"about\":\"\",\"name\":\"gladstein\"}",
"created_at": 1676405699,
"id": "2883f411daaef3370f87dc4456fbe1184ab50ec97013249d7cdda4b8938d0b0a",
"kind": 0,
"pubkey": "58c741aa630c2da35a56a77c1d05381908bd10504fdd2d8b43f725efa6d23196",
"sig": "6f12e0090940bf923d96e9c1dce134c1c16c5fdb1e79efff3ed791bb6ff985b4dda609dc85e1ad15c752c6c5f4cbbf8949068731e1b881ac13b2eb1ce59fc578",
"tags": []
}

0 comments on commit 717f97d

Please sign in to comment.