Skip to content

Commit

Permalink
Add image and title tags to CalendarListEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiu committed Aug 17, 2024
1 parent ad38266 commit 9ec53aa
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
10 changes: 7 additions & 3 deletions Sources/NostrSDK/Events/Calendars/CalendarListEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Foundation
/// One may create a calendar to segment calendar events for specific purposes. e.g., personal, work, travel, meetups, and conferences.
///
/// See [NIP-52 - Calendar](https://github.com/nostr-protocol/nips/blob/master/52.md#calendar).
public final class CalendarListEvent: NostrEvent, ParameterizedReplaceableEvent, TitleTagInterpreting {
public final class CalendarListEvent: NostrEvent, ParameterizedReplaceableEvent, ImageTagInterpreting, TitleTagInterpreting {
public required init(from decoder: Decoder) throws {
try super.init(from: decoder)
}
Expand Down Expand Up @@ -45,7 +45,7 @@ public extension EventCreating {
/// - Returns: The signed ``CalendarListEvent``.
///
/// See [NIP-52](https://github.com/nostr-protocol/nips/blob/master/52.md).
func calendarListEvent(withIdentifier identifier: String = UUID().uuidString, title: String, description: String = "", calendarEventsCoordinates: [EventCoordinates], signedBy keypair: Keypair) throws -> CalendarListEvent {
func calendarListEvent(withIdentifier identifier: String = UUID().uuidString, title: String, description: String = "", calendarEventsCoordinates: [EventCoordinates], imageURL: URL? = nil, signedBy keypair: Keypair) throws -> CalendarListEvent {
guard calendarEventsCoordinates.allSatisfy({ $0.kind == .dateBasedCalendarEvent || $0.kind == .timeBasedCalendarEvent }) else {
throw EventCreatingError.invalidInput
}
Expand All @@ -54,7 +54,11 @@ public extension EventCreating {
Tag(name: .identifier, value: identifier),
Tag(name: .title, value: title)
]


if let imageURL {
tags.append(Tag(name: .image, value: imageURL.absoluteString))
}

calendarEventsCoordinates
.filter { $0.kind == .dateBasedCalendarEvent || $0.kind == .timeBasedCalendarEvent }
.forEach { tags.append($0.tag) }
Expand Down
2 changes: 1 addition & 1 deletion Sources/NostrSDK/Events/Tags/ImageTagInterpreting.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// ImageTagInterpreting.swift
//
//
//
// Created by Terry Yiu on 8/4/24.
//
Expand Down
2 changes: 1 addition & 1 deletion Sources/NostrSDK/Events/Tags/SummaryTagInterpreting.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SummaryTagInterpreting.swift
//
//
//
// Created by Terry Yiu on 8/4/24.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ final class CalendarListEventTests: XCTestCase, EventCreating, EventVerifying, F
let identifier = "family-calendar"
let title = "Family Calendar"
let description = "All family events."
let calendar = try calendarListEvent(withIdentifier: identifier, title: title, description: description, calendarEventsCoordinates: [dateBasedCalendarEventCoordinates, timeBasedCalendarEventCoordinates], signedBy: Keypair.test)
let imageString = "https://nostrsdk.com/image.png"
let calendar = try calendarListEvent(withIdentifier: identifier, title: title, description: description, calendarEventsCoordinates: [dateBasedCalendarEventCoordinates, timeBasedCalendarEventCoordinates], imageURL: URL(string: imageString), signedBy: Keypair.test)

XCTAssertEqual(calendar.identifier, identifier)
XCTAssertEqual(calendar.title, title)
XCTAssertEqual(calendar.content, description)
XCTAssertEqual(calendar.imageURL?.absoluteString, imageString)
XCTAssertEqual(calendar.calendarEventCoordinateList, [dateBasedCalendarEventCoordinates, timeBasedCalendarEventCoordinates])

let expectedReplaceableEventCoordinates = try XCTUnwrap(EventCoordinates(kind: .calendar, pubkey: Keypair.test.publicKey, identifier: identifier))
Expand Down Expand Up @@ -72,6 +74,8 @@ final class CalendarListEventTests: XCTestCase, EventCreating, EventVerifying, F
XCTAssertEqual(event.title, "Family Calendar")
XCTAssertEqual(event.content, "All family events.")

XCTAssertEqual(event.imageURL?.absoluteString, "https://nostrsdk.com/image.png")

let pubkey = try XCTUnwrap(PublicKey(hex: "9947f9659dd80c3682402b612f5447e28249997fb3709500c32a585eb0977340"))
let dateBasedCalendarEventCoordinates = try XCTUnwrap(EventCoordinates(kind: .dateBasedCalendarEvent, pubkey: pubkey, identifier: "D5EB0A5A-0B36-44DB-95C3-DB51799894E6"))
let timeBasedCalendarEventCoordinates = try XCTUnwrap(EventCoordinates(kind: .timeBasedCalendarEvent, pubkey: pubkey, identifier: "1D355ED3-A45D-41A9-B3A5-709211794EFB"))
Expand Down
4 changes: 4 additions & 0 deletions Tests/NostrSDKTests/Fixtures/calendar.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"title",
"Family Calendar"
],
[
"image",
"https://nostrsdk.com/image.png"
],
[
"a",
"31922:9947f9659dd80c3682402b612f5447e28249997fb3709500c32a585eb0977340:D5EB0A5A-0B36-44DB-95C3-DB51799894E6"
Expand Down

0 comments on commit 9ec53aa

Please sign in to comment.