Local notifications for other events

Changelog-Added: Added local notifications for other events
This commit is contained in:
Swift
2023-03-29 19:19:41 -04:00
committed by William Casarin
parent f6133814e8
commit 61ef709d91
3 changed files with 147 additions and 14 deletions

View File

@@ -150,8 +150,10 @@ class HomeModel: ObservableObject {
// Generate zap vibration
zap_vibrate(zap_amount: zap.invoice.amount)
}
// Create in-app local notification for zap received.
create_in_app_zap_notification(profiles: profiles, zap: zap)
if damus_state.settings.zap_notification {
// Create in-app local notification for zap received.
create_in_app_zap_notification(profiles: profiles, zap: zap)
}
}
return
@@ -487,7 +489,40 @@ class HomeModel: ObservableObject {
return
}
handle_last_event(ev: ev, timeline: .notifications)
if handle_last_event(ev: ev, timeline: .notifications),
damus_state.contacts.follow_state(ev.pubkey) == .follows,
let type = ev.known_kind {
if type == .text,
damus_state.settings.mention_notification {
for block in ev.blocks(damus_state.keypair.privkey) {
if case .mention(let mention) = block, mention.ref.ref_id == damus_state.keypair.pubkey,
let displayName = damus_state.profiles.lookup(id: ev.pubkey)?.display_name {
let justContent = NSAttributedString(render_note_content(ev: ev, profiles: damus_state.profiles, privkey: damus_state.keypair.privkey).content).string
create_notification(displayName: displayName, conversation: justContent, type: type)
}
}
} else if type == .boost,
damus_state.settings.repost_notification,
let displayName = damus_state.profiles.lookup(id: ev.pubkey)?.display_name {
let justContent = NSAttributedString(render_note_content(ev: ev, profiles: damus_state.profiles, privkey: damus_state.keypair.privkey).content).string
if let jsonData = justContent.data(using: .utf8),
let jsonDict = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any],
let content = jsonDict["content"] as? String {
create_notification(displayName: displayName, conversation: content, type: type)
}
} else if type == .like,
damus_state.settings.like_notification,
let displayName = damus_state.profiles.lookup(id: ev.pubkey)?.display_name,
let eRef = ev.referenced_ids.eRefs.first?.ref_id,
let content = damus_state.events.lookup(eRef)?.content {
create_notification(displayName: displayName, conversation: content, type: type)
}
}
}
@discardableResult
@@ -506,6 +541,44 @@ class HomeModel: ObservableObject {
}
}
func create_notification(displayName: String, conversation: String, type: NostrKind) {
let content = UNMutableNotificationContent()
var title = ""
var identifier = ""
switch type {
case .text:
title = String(format: NSLocalizedString("Mentioned by %@", comment: "Mentioned by heading in local notification"), displayName)
identifier = "myMentionNotification"
case .boost:
title = String(format: NSLocalizedString("Reposted by %@", comment: "Reposted by heading in local notification"), displayName)
identifier = "myBoostNotification"
case .like:
title = String(format: NSLocalizedString("Liked by %@", comment: "Liked by heading in local notification"), displayName)
identifier = "myLikeNotification"
case .dm:
title = String(format: NSLocalizedString("DM by %@", comment: "DM by heading in local notification"), displayName)
identifier = "myDMNotification"
default:
break
}
content.title = title
content.body = conversation
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("Error: \(error)")
} else {
print("Local notification scheduled")
}
}
}
func handle_text_event(sub_id: String, _ ev: NostrEvent) {
guard should_show_event(contacts: damus_state.contacts, ev: ev) else {
return
@@ -536,9 +609,13 @@ class HomeModel: ObservableObject {
incoming_dms.append(ev)
dm_debouncer.debounce {
dm_debouncer.debounce { [self] in
if let notifs = handle_incoming_dms(prev_events: self.new_events, dms: self.dms, our_pubkey: self.damus_state.pubkey, evs: self.incoming_dms) {
self.new_events = notifs
if damus_state.settings.dm_notification,
let displayName = damus_state.profiles.lookup(id: self.incoming_dms.last!.pubkey)?.display_name {
create_notification(displayName: displayName, conversation: "You have received a direct message", type: .dm)
}
}
self.incoming_dms = []
}