damus

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

LocalNotification.swift (2758B)


      1 //
      2 //  LocalNotification.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-04-15.
      6 //
      7 
      8 import Foundation
      9 
     10 let NDB_NOTE_JSON_USER_INFO_KEY = "ndb_note_json"
     11 
     12 struct LossyLocalNotification {
     13     let type: LocalNotificationType
     14     let mention: MentionRef
     15 
     16     func to_user_info() -> [AnyHashable: Any] {
     17         return [
     18             "type": self.type.rawValue,
     19             "id": self.mention.bech32
     20         ]
     21     }
     22     
     23     static func from_user_info(user_info: [AnyHashable: Any]) -> LossyLocalNotification? {
     24         if let encoded_ndb_note = user_info[NDB_NOTE_JSON_USER_INFO_KEY] as? String {
     25             return self.from(json_encoded_ndb_note: encoded_ndb_note)
     26         }
     27         guard let id = user_info["id"] as? String,
     28               let target_id = MentionRef.from_bech32(str: id) else {
     29             return nil
     30         }
     31         let typestr = user_info["type"] as! String
     32         let type = LocalNotificationType(rawValue: typestr)!
     33         
     34         return LossyLocalNotification(type: type, mention: target_id)
     35     }
     36     
     37     static func from(json_encoded_ndb_note: String) -> LossyLocalNotification? {
     38         guard let ndb_note = NdbNote.owned_from_json(json: json_encoded_ndb_note) else {
     39             return nil
     40         }
     41         return self.from(ndb_note: ndb_note)
     42     }
     43     
     44     static func from(ndb_note: NdbNote) -> LossyLocalNotification? {
     45         guard let known_kind = ndb_note.known_kind, let type = LocalNotificationType.from(nostr_kind: known_kind) else { return nil }
     46         let target: MentionRef = .note(ndb_note.id)
     47         return LossyLocalNotification(type: type, mention: target)
     48     }
     49 }
     50 
     51 enum NotificationTarget {
     52     case note(NostrEvent)
     53     case note_id(NoteId)
     54 
     55     var id: NoteId {
     56         switch self {
     57         case .note(let note):
     58             return note.id
     59         case .note_id(let id):
     60             return id
     61         }
     62     }
     63 }
     64 
     65 struct LocalNotification {
     66     let type: LocalNotificationType
     67     let event: NostrEvent
     68     let target: NotificationTarget
     69     let content: String
     70     
     71     func to_lossy() -> LossyLocalNotification {
     72         return LossyLocalNotification(type: self.type, mention: .note(self.target.id))
     73     }
     74 }
     75 
     76 enum LocalNotificationType: String {
     77     case dm
     78     case like
     79     case mention
     80     case reply
     81     case tagged
     82     case repost
     83     case zap
     84     case profile_zap
     85     
     86     static func from(nostr_kind: NostrKind) -> Self? {
     87         switch nostr_kind {
     88             case .text:
     89                 return .mention
     90             case .dm:
     91                 return .dm
     92             case .like:
     93                 return .like
     94             case .longform:
     95                 return .mention
     96             case .zap:
     97                 return .zap
     98             default:
     99                 return nil
    100         }
    101     }
    102 }