status: fix status events not expiring locally

Changelog-Fixed: Fix status events not expiring locally
This commit is contained in:
William Casarin
2023-08-23 16:11:48 -07:00
parent 042b7da315
commit 23a8d6fb6b
4 changed files with 38 additions and 4 deletions

View File

@@ -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 {

View File

@@ -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))
})
}

View File

@@ -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 }

View File

@@ -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) {