From d39a3da3b728aa06ac96a2dec2f071050fa6e3f1 Mon Sep 17 00:00:00 2001 From: Suhail Saqan Date: Thu, 1 Jun 2023 14:49:56 -0500 Subject: [PATCH] util: add separate_images and separate_invoices --- damus/Nostr/NostrEvent.swift | 23 +++++++++++++++++++++++ damus/Views/FollowingView.swift | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift index 9768991e..7be20ad4 100644 --- a/damus/Nostr/NostrEvent.swift +++ b/damus/Nostr/NostrEvent.swift @@ -949,6 +949,29 @@ func first_eref_mention(ev: NostrEvent, keypair: Keypair) -> Mention? { return nil } +func separate_images(ev: NostrEvent, keypair: Keypair) -> [MediaUrl]? { + let urlBlocks: [URL] = ev.blocks(keypair).blocks.reduce(into: []) { urls, block in + guard case .url(let url) = block else { + return + } + if classify_url(url).is_img != nil { + urls.append(url) + } + } + let mediaUrls = urlBlocks.map { MediaUrl.image($0) } + return mediaUrls.isEmpty ? nil : mediaUrls +} + +func separate_invoices(ev: NostrEvent, keypair: Keypair) -> [Invoice]? { + let invoiceBlocks: [Invoice] = ev.blocks(keypair).blocks.reduce(into: []) { invoices, block in + guard case .invoice(let invoice) = block else { + return + } + invoices.append(invoice) + } + return invoiceBlocks.isEmpty ? nil : invoiceBlocks +} + /** Transforms a `NostrEvent` of known kind `NostrKind.like`to a human-readable emoji. If the known kind is not a `NostrKind.like`, it will return `nil`. diff --git a/damus/Views/FollowingView.swift b/damus/Views/FollowingView.swift index bdb5724e..2f2693f6 100644 --- a/damus/Views/FollowingView.swift +++ b/damus/Views/FollowingView.swift @@ -93,7 +93,7 @@ struct FollowingView: View { /* struct FollowingView_Previews: PreviewProvider { static var previews: some View { - FollowingView(contact: <#NostrEvent#>, damus_state: <#DamusState#>) + FollowingView(damus_state: test_damus_state, following: test_following_model) } } */