mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.8 KiB
Swift
69 lines
2.8 KiB
Swift
5 years ago
|
|
||
|
@objc(LKUserDisplayNameUtilities)
|
||
|
public final class UserDisplayNameUtilities : NSObject {
|
||
|
|
||
|
override private init() { }
|
||
|
|
||
|
private static var userPublicKey: String {
|
||
|
return getUserHexEncodedPublicKey()
|
||
|
}
|
||
|
|
||
|
private static var userDisplayName: String? {
|
||
|
return SSKEnvironment.shared.profileManager.localProfileName()
|
||
|
}
|
||
|
|
||
|
// MARK: Sessions
|
||
|
@objc(getPrivateChatDisplayNameAvoidWriteTransaction:)
|
||
|
public static func getPrivateChatDisplayNameAvoidingWriteTransaction(for publicKey: String) -> String? {
|
||
|
if publicKey == userPublicKey {
|
||
|
return userDisplayName
|
||
|
} else {
|
||
|
return SSKEnvironment.shared.profileManager.profileNameForRecipient(withID: publicKey, avoidingWriteTransaction: true)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@objc public static func getPrivateChatDisplayName(for publicKey: String) -> String? {
|
||
|
if publicKey == userPublicKey {
|
||
|
return userDisplayName
|
||
|
} else {
|
||
|
return SSKEnvironment.shared.profileManager.profileNameForRecipient(withID: publicKey)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// MARK: Open Groups
|
||
|
@objc public static func getPublicChatDisplayName(for publicKey: String, in channel: UInt64, on server: String) -> String? {
|
||
|
var result: String?
|
||
|
OWSPrimaryStorage.shared().dbReadConnection.read { transaction in
|
||
|
result = getPublicChatDisplayName(for: publicKey, in: channel, on: server, using: transaction)
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
@objc public static func getPublicChatDisplayName(for publicKey: String, in channel: UInt64, on server: String, using transaction: YapDatabaseReadTransaction) -> String? {
|
||
|
if publicKey == userPublicKey {
|
||
|
return userDisplayName
|
||
|
} else {
|
||
|
let collection = "\(server).\(channel)"
|
||
|
return transaction.object(forKey: publicKey, inCollection: collection) as! String?
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@objc(LKGroupDisplayNameUtilities)
|
||
|
public final class GroupDisplayNameUtilities : NSObject {
|
||
|
|
||
|
override private init() { }
|
||
|
|
||
|
// MARK: Closed Groups
|
||
|
@objc public static func getDefaultDisplayName(for group: TSGroupThread) -> String {
|
||
|
let members = group.groupModel.groupMemberIds
|
||
|
let displayNames = members.map { hexEncodedPublicKey -> String in
|
||
|
guard let displayName = UserDisplayNameUtilities.getPrivateChatDisplayName(for: hexEncodedPublicKey) else { return hexEncodedPublicKey }
|
||
|
let regex = try! NSRegularExpression(pattern: ".* \\(\\.\\.\\.[0-9a-fA-F]*\\)")
|
||
|
guard regex.hasMatch(input: displayName) else { return displayName }
|
||
|
return String(displayName[displayName.startIndex..<(displayName.index(displayName.endIndex, offsetBy: -14))])
|
||
|
}.sorted()
|
||
|
return displayNames.joined(separator: ", ")
|
||
|
}
|
||
|
}
|