damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

NotificationFormatter.swift (6895B)


      1 //
      2 //  NotificationFormatter.swift
      3 //  DamusNotificationService
      4 //
      5 //  Created by Daniel D’Aquino on 2023-11-13.
      6 //
      7 
      8 import Foundation
      9 import UserNotifications
     10 
     11 struct NotificationFormatter {
     12     static var shared = NotificationFormatter()
     13     
     14     // MARK: - Formatting with NdbNote
     15     
     16     func format_message(event: NdbNote) -> UNMutableNotificationContent? {
     17         let content = UNMutableNotificationContent()
     18         if let event_json_data = try? JSONEncoder().encode(event),  // Must be encoded, as the notification completion handler requires this object to conform to `NSSecureCoding`
     19            let event_json_string = String(data: event_json_data, encoding: .utf8) {
     20             content.userInfo = [
     21                 NDB_NOTE_JSON_USER_INFO_KEY: event_json_string
     22             ]
     23         }
     24         switch event.known_kind {
     25             case .text:
     26                 content.title = NSLocalizedString("Someone posted a note", comment: "Title label for push notification where someone posted a note")
     27                 content.body = event.content
     28                 break
     29             case .dm:
     30                 content.title = NSLocalizedString("New message", comment: "Title label for push notifications where a direct message was sent to the user")
     31                 content.body = NSLocalizedString("(Contents are encrypted)", comment: "Label on push notification indicating that the contents of the message are encrypted")
     32                 break
     33             case .like:
     34                 guard let reactionEmoji = to_reaction_emoji(ev: event) else {
     35                     content.title = NSLocalizedString("Someone reacted to your note", comment: "Generic title label for push notifications where someone reacted to the user's post")
     36                     break
     37                 }
     38                 content.title = NSLocalizedString("New note reaction", comment: "Title label for push notifications where someone reacted to the user's post with a specific emoji")
     39                 content.body = String(format: NSLocalizedString("Someone reacted to your note with %@", comment: "Body label for push notifications where someone reacted to the user's post with a specific emoji"), reactionEmoji)
     40                 break
     41             case .zap:
     42                 content.title = NSLocalizedString("Someone zapped you ⚡️", comment: "Title label for a push notification where someone zapped the user")
     43                 break
     44             default:
     45                 return nil
     46         }
     47         return content
     48     }
     49     
     50     // MARK: - Formatting with LocalNotification
     51     
     52     func format_message(displayName: String, notify: LocalNotification) -> (content: UNMutableNotificationContent, identifier: String)? {
     53         let content = UNMutableNotificationContent()
     54         var title = ""
     55         var identifier = ""
     56         
     57         switch notify.type {
     58             case .mention:
     59                 title = String(format: NSLocalizedString("Mentioned by %@", comment: "Mentioned by heading in local notification"), displayName)
     60                 identifier = "myMentionNotification"
     61             case .repost:
     62                 title = String(format: NSLocalizedString("Reposted by %@", comment: "Reposted by heading in local notification"), displayName)
     63                 identifier = "myBoostNotification"
     64             case .like:
     65                 title = String(format: NSLocalizedString("%@ reacted with %@", comment: "Reacted by heading in local notification"), displayName, to_reaction_emoji(ev: notify.event) ?? "")
     66                 identifier = "myLikeNotification"
     67             case .dm:
     68                 title = displayName
     69                 identifier = "myDMNotification"
     70             case .zap, .profile_zap:
     71                 // not handled here. Try `format_message(displayName: String, notify: LocalNotification, state: HeadlessDamusState) async -> (content: UNMutableNotificationContent, identifier: String)?`
     72                 return nil
     73         }
     74         content.title = title
     75         content.body = notify.content
     76         content.sound = UNNotificationSound.default
     77         content.userInfo = notify.to_lossy().to_user_info()
     78 
     79         return (content, identifier)
     80     }
     81     
     82     func format_message(displayName: String, notify: LocalNotification, state: HeadlessDamusState) async -> (content: UNMutableNotificationContent, identifier: String)? {
     83         // Try sync method first and return if it works
     84         if let sync_formatted_message = self.format_message(displayName: displayName, notify: notify) {
     85             return sync_formatted_message
     86         }
     87         
     88         // If it does not work, try async formatting methods
     89         let content = UNMutableNotificationContent()
     90         
     91         switch notify.type {
     92             case .zap, .profile_zap:
     93                 guard let zap = await get_zap(from: notify.event, state: state) else {
     94                     return nil
     95                 }
     96                 content.title = Self.zap_notification_title(zap)
     97                 content.body = Self.zap_notification_body(profiles: state.profiles, zap: zap)
     98                 content.sound = UNNotificationSound.default
     99                 content.userInfo = LossyLocalNotification(type: .zap, mention: .note(notify.event.id)).to_user_info()
    100                 return (content, "myZapNotification")
    101             default:
    102                 // The sync method should have taken care of this.
    103                 return nil
    104         }
    105     }
    106     
    107     // MARK: - Formatting zap utility notifications
    108 
    109     static func zap_notification_title(_ zap: Zap) -> String {
    110         if zap.private_request != nil {
    111             return NSLocalizedString("Private Zap", comment: "Title of notification when a private zap is received.")
    112         } else {
    113             return NSLocalizedString("Zap", comment: "Title of notification when a non-private zap is received.")
    114         }
    115     }
    116 
    117     static func zap_notification_body(profiles: Profiles, zap: Zap, locale: Locale = Locale.current) -> String {
    118         let src = zap.request.ev
    119         let pk = zap.is_anon ? ANON_PUBKEY : src.pubkey
    120 
    121         let profile_txn = profiles.lookup(id: pk)
    122         let profile = profile_txn?.unsafeUnownedValue
    123         let name = Profile.displayName(profile: profile, pubkey: pk).displayName.truncate(maxLength: 50)
    124 
    125         let sats = NSNumber(value: (Double(zap.invoice.amount) / 1000.0))
    126         let formattedSats = format_msats_abbrev(zap.invoice.amount)
    127 
    128         if src.content.isEmpty {
    129             let format = localizedStringFormat(key: "zap_notification_no_message", locale: locale)
    130             return String(format: format, locale: locale, sats.decimalValue as NSDecimalNumber, formattedSats, name)
    131         } else {
    132             let format = localizedStringFormat(key: "zap_notification_with_message", locale: locale)
    133             return String(format: format, locale: locale, sats.decimalValue as NSDecimalNumber, formattedSats, name, src.content)
    134         }
    135     }
    136 }