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:
committed by
William Casarin
parent
9847f12c95
commit
43017828e2
@@ -24,6 +24,9 @@ struct NewEventsBits: OptionSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class HomeModel: ObservableObject {
|
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 damus_state: DamusState
|
||||||
|
|
||||||
var has_event: [String: Set<String>] = [:]
|
var has_event: [String: Set<String>] = [:]
|
||||||
@@ -543,7 +546,8 @@ class HomeModel: ObservableObject {
|
|||||||
|
|
||||||
func got_new_dm(notifs: NewEventsBits, ev: NostrEvent) {
|
func got_new_dm(notifs: NewEventsBits, ev: NostrEvent) {
|
||||||
self.new_events = notifs
|
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 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)
|
let notify = LocalNotification(type: .dm, event: ev, target: ev, content: convo)
|
||||||
create_local_notification(profiles: damus_state.profiles, notify: notify)
|
create_local_notification(profiles: damus_state.profiles, notify: notify)
|
||||||
@@ -1112,6 +1116,11 @@ func process_local_notification(damus_state: DamusState, event ev: NostrEvent) {
|
|||||||
return
|
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 {
|
if type == .text && damus_state.settings.mention_notification {
|
||||||
let blocks = ev.blocks(damus_state.keypair.privkey)
|
let blocks = ev.blocks(damus_state.keypair.privkey)
|
||||||
for case .mention(let mention) in blocks where mention.ref.ref_id == damus_state.keypair.pubkey {
|
for case .mention(let mention) in blocks where mention.ref.ref_id == damus_state.keypair.pubkey {
|
||||||
|
|||||||
@@ -278,6 +278,11 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
|
|||||||
func sign(privkey: String) {
|
func sign(privkey: String) {
|
||||||
self.sig = sign_event(privkey: privkey, ev: self)
|
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 {
|
func sign_event(privkey: String, ev: NostrEvent) -> String {
|
||||||
|
|||||||
Reference in New Issue
Block a user