damus

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

DisplayName.swift (2577B)


      1 //
      2 //  DisplayName.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-03-14.
      6 //
      7 
      8 import Foundation
      9 
     10 enum DisplayName: Equatable {
     11     case both(username: String, displayName: String)
     12     case one(String)
     13 
     14     init (profile: Profile?, pubkey: Pubkey) {
     15         self = parse_display_name(name: profile?.name, display_name: profile?.display_name, pubkey: pubkey)
     16     }
     17 
     18     init (name: String?, display_name: String?, pubkey: Pubkey) {
     19         self = parse_display_name(name: name, display_name: display_name, pubkey: pubkey)
     20     }
     21 
     22     var displayName: String {
     23         switch self {
     24         case .one(let one):
     25             return one
     26         case .both(username: _, displayName: let displayName):
     27             return displayName
     28         }
     29     }
     30     
     31     var username: String {
     32         switch self {
     33         case .one(let one):
     34             return one
     35         case .both(username: let username, displayName: _):
     36             return username
     37         }
     38     }
     39 
     40     func nameComponents() -> PersonNameComponents {
     41         var components = PersonNameComponents()
     42         switch self {
     43         case .one(let one):
     44             components.nickname = one
     45             return components
     46         case .both(username: let username, displayName: let displayName):
     47             components.nickname = username
     48             let names = displayName.split(separator: " ")
     49             if let name = names.first {
     50                 components.givenName = String(name)
     51                 components.familyName = names.dropFirst().joined(separator: " ")
     52             }
     53             return components
     54         }
     55     }
     56 }
     57 
     58 
     59 func parse_display_name(name: String?, display_name: String?, pubkey: Pubkey) -> DisplayName {
     60     if pubkey == ANON_PUBKEY {
     61         return .one(NSLocalizedString("Anonymous", comment: "Placeholder display name of anonymous user."))
     62     }
     63 
     64     if name == nil && display_name == nil {
     65         return .one(abbrev_bech32_pubkey(pubkey: pubkey))
     66     }
     67 
     68     let name = name?.isEmpty == false ? name : nil
     69     let disp_name = display_name?.isEmpty == false ? display_name : nil
     70     
     71     if let name, let disp_name, name != disp_name {
     72         return .both(username: name, displayName: disp_name)
     73     }
     74     
     75     if let one = name ?? disp_name {
     76         return .one(one)
     77     }
     78     
     79     return .one(abbrev_bech32_pubkey(pubkey: pubkey))
     80 }
     81 
     82 func abbrev_bech32_pubkey(pubkey: Pubkey) -> String {
     83     return abbrev_identifier(String(pubkey.npub.dropFirst(4)))
     84 }
     85 
     86 func abbrev_identifier(_ pubkey: String, amount: Int = 8) -> String {
     87     return pubkey.prefix(amount) + ":" + pubkey.suffix(amount)
     88 }