damus

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

commit 23a8d6fb6b84503a86a1f13d785c9aa2cb1e30c9
parent 042b7da31593c15595c7b35f5cc005f85b93e4e6
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 23 Aug 2023 16:11:48 -0700

status: fix status events not expiring locally

Changelog-Fixed: Fix status events not expiring locally

Diffstat:
Mdamus/Components/Status/UserStatus.swift | 20+++++++++++++++++++-
Mdamus/Components/Status/UserStatusSheet.swift | 2+-
Mdamus/ContentView.swift | 4+++-
Mdamus/Models/HomeModel.swift | 16+++++++++++++++-
4 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/damus/Components/Status/UserStatus.swift b/damus/Components/Status/UserStatus.swift @@ -19,15 +19,22 @@ struct UserStatus { let type: UserStatusType let expires_at: Date? let content: String + let created_at: UInt32 func to_note(keypair: FullKeypair) -> NostrEvent? { return make_user_status_note(status: self, keypair: keypair) } - init(type: UserStatusType, expires_at: Date?, content: String) { + init(type: UserStatusType, expires_at: Date?, content: String, created_at: UInt32) { self.type = type self.expires_at = expires_at self.content = content + self.created_at = created_at + } + + func expired() -> Bool { + guard let expires_at else { return false } + return Date.now >= expires_at } init?(ev: NostrEvent) { @@ -54,6 +61,7 @@ struct UserStatus { } self.content = ev.content + self.created_at = ev.created_at } } @@ -77,6 +85,16 @@ class UserStatusModel: ObservableObject { } } + func try_expire() { + if let general, general.expired() { + self.general = nil + } + + if let music, music.expired() { + self.music = nil + } + } + var _playing_enabled: Bool var playing_enabled: Bool { set { diff --git a/damus/Components/Status/UserStatusSheet.swift b/damus/Components/Status/UserStatusSheet.swift @@ -45,7 +45,7 @@ struct UserStatusSheet: View { Binding(get: { status.general?.content ?? "" }, set: { v in - status.general = UserStatus(type: .general, expires_at: duration.expiration, content: v) + status.general = UserStatus(type: .general, expires_at: duration.expiration, content: v, created_at: UInt32(Date.now.timeIntervalSince1970)) }) } diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -359,6 +359,7 @@ struct ContentView: View { } .onReceive(timer) { n in self.damus_state?.postbox.try_flushing_events() + self.damus_state!.profiles.profile_data(self.damus_state!.pubkey).status.try_expire() } .onReceive(handle_notify(.report)) { target in self.active_sheet = .report(target) @@ -670,7 +671,8 @@ 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")") + 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)) + pdata.status.music = music guard let ev = music.to_note(keypair: kp) else { return } diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -200,11 +200,25 @@ class HomeModel { return } + // don't process expired events if let expires = st.expires_at, Date.now >= expires { return } - damus_state.profiles.profile_data(ev.pubkey).status.update_status(st) + let pdata = damus_state.profiles.profile_data(ev.pubkey) + + // don't use old events + if st.type == .music, + let music = pdata.status.music, + ev.created_at < music.created_at { + return + } else if st.type == .general, + let general = pdata.status.general, + ev.created_at < general.created_at { + return + } + + pdata.status.update_status(st) } func handle_nwc_response(_ ev: NostrEvent, relay: String) {