This change includes several source files related to NostrDB into the extension target as well, so that we can use it from that context (and thus enable more advanced push notification formatting and suppression) To make this change possible, I had to split some source files as well as to move some functions to different files, to ensure we don't have to pull too much unnecessary code into the extension. Testing ------- PASS Device: iPhone 15 Pro simulator iOS: 17.0.1 Damus: This commit Test steps: 1. Build DamusNotificationService. Should succeed. PASS 2. Build Damus (the app). PASS 3. Run app, scroll around some notes, go to a few different views, post a note. Should work as normal. PASS
64 lines
1.6 KiB
Swift
64 lines
1.6 KiB
Swift
//
|
|
// DisplayName.swift
|
|
// damus
|
|
//
|
|
// Created by William Casarin on 2023-03-14.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
enum DisplayName: Equatable {
|
|
case both(username: String, displayName: String)
|
|
case one(String)
|
|
|
|
var displayName: String {
|
|
switch self {
|
|
case .one(let one):
|
|
return one
|
|
case .both(username: _, displayName: let displayName):
|
|
return displayName
|
|
}
|
|
}
|
|
|
|
var username: String {
|
|
switch self {
|
|
case .one(let one):
|
|
return one
|
|
case .both(username: let username, displayName: _):
|
|
return username
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func parse_display_name(profile: Profile?, pubkey: Pubkey) -> DisplayName {
|
|
if pubkey == ANON_PUBKEY {
|
|
return .one(NSLocalizedString("Anonymous", comment: "Placeholder display name of anonymous user."))
|
|
}
|
|
|
|
guard let profile else {
|
|
return .one(abbrev_bech32_pubkey(pubkey: pubkey))
|
|
}
|
|
|
|
let name = profile.name?.isEmpty == false ? profile.name : nil
|
|
let disp_name = profile.display_name?.isEmpty == false ? profile.display_name : nil
|
|
|
|
if let name, let disp_name, name != disp_name {
|
|
return .both(username: name, displayName: disp_name)
|
|
}
|
|
|
|
if let one = name ?? disp_name {
|
|
return .one(one)
|
|
}
|
|
|
|
return .one(abbrev_bech32_pubkey(pubkey: pubkey))
|
|
}
|
|
|
|
func abbrev_bech32_pubkey(pubkey: Pubkey) -> String {
|
|
return abbrev_pubkey(String(pubkey.npub.dropFirst(4)))
|
|
}
|
|
|
|
func abbrev_pubkey(_ pubkey: String, amount: Int = 8) -> String {
|
|
return pubkey.prefix(amount) + ":" + pubkey.suffix(amount)
|
|
}
|