@objc(SNContact) public class Contact : NSObject, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility @objc public let sessionID: String /// The URL from which to fetch the contact's profile picture. @objc public var profilePictureURL: String? /// The file name of the contact's profile picture on local storage. @objc public var profilePictureFileName: String? /// The key with which the profile is encrypted. @objc public var profileEncryptionKey: OWSAES256Key? /// The ID of the thread associated with this contact. @objc public var threadID: String? /// This flag is used to determine whether we should auto-download files sent by this contact. @objc public var isTrusted = false // MARK: Name /// The name of the contact. Use this whenever you need the "real", underlying name of a user (e.g. when sending a message). @objc public var name: String? /// The contact's nickname, if the user set one. @objc public var nickname: String? /// The name to display in the UI. For local use only. @objc public func displayName(for context: Context) -> String? { if let nickname = nickname { return nickname } switch context { case .regular: return name case .openGroup: // In open groups, where it's more likely that multiple users have the same name, we display a bit of the Session ID after // a user's display name for added context. guard let name = name else { return nil } let endIndex = sessionID.endIndex let cutoffIndex = sessionID.index(endIndex, offsetBy: -8) return "\(name) (...\(sessionID[cutoffIndex.. Bool { guard let other = other as? Contact else { return false } return sessionID == other.sessionID } // MARK: Hashing override public var hash: Int { // Override NSObject.hash and not Hashable.hashValue or Hashable.hash(into:) return sessionID.hash } // MARK: Description override public var description: String { nickname ?? name ?? sessionID } // MARK: Convenience @objc(contextForThread:) public static func context(for thread: TSThread) -> Context { return ((thread as? TSGroupThread)?.isOpenGroup == true) ? .openGroup : .regular } }