From 7cc2825d898cd9b61b53e3f77b0459006ce26417 Mon Sep 17 00:00:00 2001 From: kernelkind Date: Mon, 29 Jan 2024 18:51:18 -0500 Subject: [PATCH] nip19: fix shared nevents that are too long There comes a point when the sharing a Nip19 mention becomes unwieldy when there is too much TLV data. This patch features a naive approach to making sure the relay portion of the TLV data doesn't contribute too much data to the mention. It adds a maximum number of relays that should be shared in the mention, right now it is set to four. Changelog-Fixed: Fix shared nevents that are too long Lightning-address: kernelkind@getalby.com Signed-off-by: kernelkind Reviewed-by: William Casarin Signed-off-by: William Casarin --- damus/Models/ProfileModel.swift | 6 ++++++ damus/Views/ActionBar/ShareAction.swift | 2 +- damus/Views/Events/EventMenu.swift | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/damus/Models/ProfileModel.swift b/damus/Models/ProfileModel.swift index 8b932884..c86ae2dd 100644 --- a/damus/Models/ProfileModel.swift +++ b/damus/Models/ProfileModel.swift @@ -13,6 +13,8 @@ class ProfileModel: ObservableObject, Equatable { @Published var relays: [String: RelayInfo]? = nil @Published var progress: Int = 0 + private let MAX_SHARE_RELAYS = 4 + var events: EventHolder let pubkey: Pubkey let damus: DamusState @@ -161,6 +163,10 @@ class ProfileModel: ObservableObject, Equatable { func getRelayStrings() -> [String] { return relays?.keys.map {$0} ?? [] } + + func getCappedRelayStrings() -> [String] { + return relays?.keys.prefix(MAX_SHARE_RELAYS).map { $0 } ?? [] + } } diff --git a/damus/Views/ActionBar/ShareAction.swift b/damus/Views/ActionBar/ShareAction.swift index 64fe1c26..49605de3 100644 --- a/damus/Views/ActionBar/ShareAction.swift +++ b/damus/Views/ActionBar/ShareAction.swift @@ -40,7 +40,7 @@ struct ShareAction: View { ShareActionButton(img: "link", text: NSLocalizedString("Copy Link", comment: "Button to copy link to note")) { dismiss() - UIPasteboard.general.string = "https://damus.io/" + Bech32Object.encode(.nevent(NEvent(noteid: event.id, relays: userProfile.getRelayStrings()))) + UIPasteboard.general.string = "https://damus.io/" + Bech32Object.encode(.nevent(NEvent(noteid: event.id, relays: userProfile.getCappedRelayStrings()))) } let bookmarkImg = isBookmarked ? "bookmark.fill" : "bookmark" diff --git a/damus/Views/Events/EventMenu.swift b/damus/Views/Events/EventMenu.swift index 8728c9a6..e56ccbee 100644 --- a/damus/Views/Events/EventMenu.swift +++ b/damus/Views/Events/EventMenu.swift @@ -86,7 +86,7 @@ struct MenuItems: View { } Button { - UIPasteboard.general.string = Bech32Object.encode(.nprofile(NProfile(author: target_pubkey, relays: profileModel.getRelayStrings()))) + UIPasteboard.general.string = Bech32Object.encode(.nprofile(NProfile(author: target_pubkey, relays: profileModel.getCappedRelayStrings()))) } label: { Label(NSLocalizedString("Copy user public key", comment: "Context menu option for copying the ID of the user who created the note."), image: "user") }