Skip to content

Commit

Permalink
status: fix status events not expiring locally
Browse files Browse the repository at this point in the history
Changelog-Fixed: Fix status events not expiring locally
  • Loading branch information
jb55 committed Aug 23, 2023
1 parent 042b7da commit 23a8d6f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
20 changes: 19 additions & 1 deletion damus/Components/Status/UserStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -54,6 +61,7 @@ struct UserStatus {
}

self.content = ev.content
self.created_at = ev.created_at
}

}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion damus/Components/Status/UserStatusSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}

Expand Down
4 changes: 3 additions & 1 deletion damus/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 }
Expand Down
16 changes: 15 additions & 1 deletion damus/Models/HomeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 23a8d6f

Please sign in to comment.