status: click music urls to display in spotify
Changelog-Added: Click music statuses to display in spotify
This commit is contained in:
@@ -20,16 +20,18 @@ struct UserStatus {
|
|||||||
let expires_at: Date?
|
let expires_at: Date?
|
||||||
let content: String
|
let content: String
|
||||||
let created_at: UInt32
|
let created_at: UInt32
|
||||||
|
let url: URL?
|
||||||
|
|
||||||
func to_note(keypair: FullKeypair) -> NostrEvent? {
|
func to_note(keypair: FullKeypair) -> NostrEvent? {
|
||||||
return make_user_status_note(status: self, keypair: keypair)
|
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.type = type
|
||||||
self.expires_at = expires_at
|
self.expires_at = expires_at
|
||||||
self.content = content
|
self.content = content
|
||||||
self.created_at = created_at
|
self.created_at = created_at
|
||||||
|
self.url = url
|
||||||
}
|
}
|
||||||
|
|
||||||
func expired() -> Bool {
|
func expired() -> Bool {
|
||||||
@@ -51,6 +53,15 @@ struct UserStatus {
|
|||||||
return nil
|
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") }),
|
if let tag = ev.tags.first(where: { t in t.count >= 2 && t[0].matches_str("expiration") }),
|
||||||
tag.count == 2,
|
tag.count == 2,
|
||||||
let expires = UInt32(tag[1].string())
|
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))])
|
tags.append(["expiration", String(UInt32(expiry.timeIntervalSince1970))])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let url = status.url {
|
||||||
|
tags.append(["r", url.absoluteString])
|
||||||
|
}
|
||||||
|
|
||||||
let kind = NostrKind.status.rawValue
|
let kind = NostrKind.status.rawValue
|
||||||
guard let ev = NostrEvent(content: status.content, keypair: keypair.to_keypair(), kind: kind, tags: tags) else {
|
guard let ev = NostrEvent(content: status.content, keypair: keypair.to_keypair(), kind: kind, tags: tags) else {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ struct UserStatusView: View {
|
|||||||
var show_general: Bool
|
var show_general: Bool
|
||||||
var show_music: Bool
|
var show_music: Bool
|
||||||
|
|
||||||
|
@Environment(\.openURL) var openURL
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack(alignment: .leading, spacing: 2) {
|
VStack(alignment: .leading, spacing: 2) {
|
||||||
if show_general, let general = status.general {
|
if show_general, let general = status.general {
|
||||||
@@ -22,6 +24,11 @@ struct UserStatusView: View {
|
|||||||
.lineLimit(1)
|
.lineLimit(1)
|
||||||
.foregroundColor(.gray)
|
.foregroundColor(.gray)
|
||||||
.font(.callout.italic())
|
.font(.callout.italic())
|
||||||
|
.onTapGesture {
|
||||||
|
if let url = general.url {
|
||||||
|
openURL(url)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if show_music, let playing = status.music {
|
if show_music, let playing = status.music {
|
||||||
@@ -29,6 +36,11 @@ struct UserStatusView: View {
|
|||||||
.lineLimit(1)
|
.lineLimit(1)
|
||||||
.foregroundColor(.gray)
|
.foregroundColor(.gray)
|
||||||
.font(.callout.italic())
|
.font(.callout.italic())
|
||||||
|
.onTapGesture {
|
||||||
|
if let url = playing.url {
|
||||||
|
openURL(url)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -671,7 +671,12 @@ struct ContentView: View {
|
|||||||
|
|
||||||
let pdata = damus_state.profiles.profile_data(damus_state.pubkey)
|
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
|
pdata.status.music = music
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user