damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 981d500c25a1f6ab01af0ff40e32bbdb842c23b0
parent d02fc9142dcf07a5c329f8bea27dd9d91c55feb1
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 23 Aug 2023 17:17:53 -0700

status: click music urls to display in spotify

Changelog-Added: Click music statuses to display in spotify

Diffstat:
Mdamus/Components/Status/UserStatus.swift | 17++++++++++++++++-
Mdamus/Components/Status/UserStatusView.swift | 12++++++++++++
Mdamus/ContentView.swift | 7++++++-
3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/damus/Components/Status/UserStatus.swift b/damus/Components/Status/UserStatus.swift @@ -20,16 +20,18 @@ struct UserStatus { let expires_at: Date? let content: String let created_at: UInt32 + let url: URL? func to_note(keypair: FullKeypair) -> NostrEvent? { return make_user_status_note(status: self, keypair: keypair) } - init(type: UserStatusType, expires_at: Date?, content: String, created_at: UInt32) { + init(type: UserStatusType, expires_at: Date?, content: String, created_at: UInt32, url: URL? = nil) { self.type = type self.expires_at = expires_at self.content = content self.created_at = created_at + self.url = url } func expired() -> Bool { @@ -51,6 +53,15 @@ struct UserStatus { return nil } + if let tag = ev.tags.first(where: { t in t.count >= 2 && t[0].matches_char("r") }), + tag.count >= 2, + let url = URL(string: tag[1].string()) + { + self.url = url + } else { + self.url = nil + } + if let tag = ev.tags.first(where: { t in t.count >= 2 && t[0].matches_str("expiration") }), tag.count == 2, let expires = UInt32(tag[1].string()) @@ -160,6 +171,10 @@ func make_user_status_note(status: UserStatus, keypair: FullKeypair, expiry: Dat tags.append(["expiration", String(UInt32(expiry.timeIntervalSince1970))]) } + if let url = status.url { + tags.append(["r", url.absoluteString]) + } + let kind = NostrKind.status.rawValue guard let ev = NostrEvent(content: status.content, keypair: keypair.to_keypair(), kind: kind, tags: tags) else { return nil diff --git a/damus/Components/Status/UserStatusView.swift b/damus/Components/Status/UserStatusView.swift @@ -15,6 +15,8 @@ struct UserStatusView: View { var show_general: Bool var show_music: Bool + @Environment(\.openURL) var openURL + var body: some View { VStack(alignment: .leading, spacing: 2) { if show_general, let general = status.general { @@ -22,6 +24,11 @@ struct UserStatusView: View { .lineLimit(1) .foregroundColor(.gray) .font(.callout.italic()) + .onTapGesture { + if let url = general.url { + openURL(url) + } + } } if show_music, let playing = status.music { @@ -29,6 +36,11 @@ struct UserStatusView: View { .lineLimit(1) .foregroundColor(.gray) .font(.callout.italic()) + .onTapGesture { + if let url = playing.url { + openURL(url) + } + } } } diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -671,7 +671,12 @@ struct ContentView: View { let pdata = damus_state.profiles.profile_data(damus_state.pubkey) - let music = UserStatus(type: .music, expires_at: Date.now.addingTimeInterval(song.playbackDuration), content: "\(song.title ?? "Unknown") - \(song.artist ?? "Unknown")", created_at: UInt32(Date.now.timeIntervalSince1970)) + let desc = "\(song.title ?? "Unknown") - \(song.artist ?? "Unknown")" + let encodedDesc = desc.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) + let url = encodedDesc.flatMap { enc in + URL(string: "spotify:search:\(enc)") + } + let music = UserStatus(type: .music, expires_at: Date.now.addingTimeInterval(song.playbackDuration), content: desc, created_at: UInt32(Date.now.timeIntervalSince1970), url: url) pdata.status.music = music