1
0
mirror of git://jb55.com/damus synced 2024-09-19 19:46:51 +00:00

fix user notifications from old events immediately shown on install and login

Changelog-Fixed: Fix user notifications from old events immediately shown on install and login
Closes: #1106
This commit is contained in:
Bryan Montz 2023-05-08 06:44:58 -05:00 committed by William Casarin
parent 9847f12c95
commit 43017828e2
2 changed files with 15 additions and 1 deletions

View File

@ -24,6 +24,9 @@ struct NewEventsBits: OptionSet {
}
class HomeModel: ObservableObject {
// Don't trigger a user notification for events older than a certain age
static let event_max_age_for_notification: TimeInterval = 12 * 60 * 60
var damus_state: DamusState
var has_event: [String: Set<String>] = [:]
@ -543,7 +546,8 @@ class HomeModel: ObservableObject {
func got_new_dm(notifs: NewEventsBits, ev: NostrEvent) {
self.new_events = notifs
if damus_state.settings.dm_notification {
if damus_state.settings.dm_notification && ev.age < HomeModel.event_max_age_for_notification {
let convo = ev.decrypted(privkey: self.damus_state.keypair.privkey) ?? NSLocalizedString("New encrypted direct message", comment: "Notification that the user has received a new direct message")
let notify = LocalNotification(type: .dm, event: ev, target: ev, content: convo)
create_local_notification(profiles: damus_state.profiles, notify: notify)
@ -1112,6 +1116,11 @@ func process_local_notification(damus_state: DamusState, event ev: NostrEvent) {
return
}
// Don't show notifications for old events
guard ev.age < HomeModel.event_max_age_for_notification else {
return
}
if type == .text && damus_state.settings.mention_notification {
let blocks = ev.blocks(damus_state.keypair.privkey)
for case .mention(let mention) in blocks where mention.ref.ref_id == damus_state.keypair.pubkey {

View File

@ -278,6 +278,11 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
func sign(privkey: String) {
self.sig = sign_event(privkey: privkey, ev: self)
}
var age: TimeInterval {
let event_date = Date(timeIntervalSince1970: TimeInterval(created_at))
return Date.now.timeIntervalSince(event_date)
}
}
func sign_event(privkey: String, ev: NostrEvent) -> String {