Reuse local notification logic with push notifications

Testing
-------

Conditional pass

Device: iPhone 15 Pro simulator
iOS: 17.0.1
Damus: This commit
Coverage:

1. Mention notification works (local and push). PASS

2. Thread replies do not appear (but upon code inspection it seems like
   it was not supported before). PASS?

3. DM notification works with decryption (local and push). PASS

4. Zaps not yet implemented. Coming later.

Closes: https://github.com/damus-io/damus/issues/1702
Closes: https://github.com/damus-io/damus/issues/1703
Changelog-Changed: Improve push notification support to match local notification support
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
Daniel D’Aquino
2023-12-01 21:26:21 +00:00
committed by William Casarin
parent 5db22ae244
commit c4f0e833ff
8 changed files with 134 additions and 98 deletions

View File

@@ -16,24 +16,44 @@ class NotificationService: UNNotificationServiceExtension {
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
let ndb: Ndb? = try? Ndb(owns_db_file: false)
// Modify the notification content here...
guard let nostrEventJSON = request.content.userInfo["nostr_event"] as? String,
let nostrEvent = NdbNote.owned_from_json(json: nostrEventJSON)
guard let nostr_event_json = request.content.userInfo["nostr_event"] as? String,
let nostr_event = NdbNote.owned_from_json(json: nostr_event_json)
else {
// No nostr event detected. Just display the original notification
contentHandler(request.content)
return;
}
// Log that we got a push notification
if let txn = ndb?.lookup_profile(nostrEvent.pubkey) {
Log.debug("Got push notification from %s (%s)", for: .push_notifications, (txn.unsafeUnownedValue?.profile?.display_name ?? "Unknown"), nostrEvent.pubkey.hex())
Log.debug("Got nostr event push notification from pubkey %s", for: .push_notifications, nostr_event.pubkey.hex())
guard let state = NotificationExtensionState(),
let display_name = state.ndb.lookup_profile(nostr_event.pubkey).unsafeUnownedValue?.profile?.display_name // We are not holding the txn here.
else {
// Something failed to initialize so let's go for the next best thing
guard let improved_content = NotificationFormatter.shared.format_message(event: nostr_event) else {
// We cannot format this nostr event. Suppress notification.
contentHandler(UNNotificationContent())
return
}
contentHandler(improved_content)
return
}
if let improvedContent = NotificationFormatter.shared.format_message(event: nostrEvent, ndb: ndb) {
contentHandler(improvedContent)
guard should_display_notification(state: state, event: nostr_event) else {
// We should not display notification for this event. Suppress notification.
contentHandler(UNNotificationContent())
return
}
guard let notification_object = generate_local_notification_object(from: nostr_event, state: state) else {
// We could not process this notification. Probably an unsupported nostr event kind. Suppress.
contentHandler(UNNotificationContent())
return
}
let (improvedContent, _) = NotificationFormatter.shared.format_message(displayName: display_name, notify: notification_object)
contentHandler(improvedContent)
}
override func serviceExtensionTimeWillExpire() {