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.
62 lines
2.7 KiB
Swift
62 lines
2.7 KiB
Swift
5 years ago
|
import SessionUtilitiesKit
|
||
5 years ago
|
|
||
|
public extension VisibleMessage {
|
||
|
|
||
5 years ago
|
@objc(SNProfile)
|
||
|
class Profile : NSObject, NSCoding {
|
||
5 years ago
|
public var displayName: String?
|
||
|
public var profileKey: Data?
|
||
|
public var profilePictureURL: String?
|
||
|
|
||
|
internal init(displayName: String, profileKey: Data? = nil, profilePictureURL: String? = nil) {
|
||
|
self.displayName = displayName
|
||
|
self.profileKey = profileKey
|
||
|
self.profilePictureURL = profilePictureURL
|
||
|
}
|
||
5 years ago
|
|
||
|
public required init?(coder: NSCoder) {
|
||
5 years ago
|
if let displayName = coder.decodeObject(forKey: "displayName") as! String? { self.displayName = displayName }
|
||
|
if let profileKey = coder.decodeObject(forKey: "profileKey") as! Data? { self.profileKey = profileKey }
|
||
|
if let profilePictureURL = coder.decodeObject(forKey: "profilePictureURL") as! String? { self.profilePictureURL = profilePictureURL }
|
||
5 years ago
|
}
|
||
|
|
||
|
public func encode(with coder: NSCoder) {
|
||
5 years ago
|
coder.encode(displayName, forKey: "displayName")
|
||
|
coder.encode(profileKey, forKey: "profileKey")
|
||
|
coder.encode(profilePictureURL, forKey: "profilePictureURL")
|
||
|
}
|
||
|
|
||
|
public static func fromProto(_ proto: SNProtoDataMessage) -> Profile? {
|
||
|
guard let profileProto = proto.profile, let displayName = profileProto.displayName else { return nil }
|
||
|
let profileKey = proto.profileKey
|
||
|
let profilePictureURL = profileProto.profilePicture
|
||
|
if let profileKey = profileKey, let profilePictureURL = profilePictureURL {
|
||
|
return Profile(displayName: displayName, profileKey: profileKey, profilePictureURL: profilePictureURL)
|
||
|
} else {
|
||
|
return Profile(displayName: displayName)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
public func toProto() -> SNProtoDataMessage? {
|
||
5 years ago
|
guard let displayName = displayName else {
|
||
|
SNLog("Couldn't construct profile proto from: \(self).")
|
||
|
return nil
|
||
|
}
|
||
5 years ago
|
let dataMessageProto = SNProtoDataMessage.builder()
|
||
5 years ago
|
let profileProto = SNProtoDataMessageLokiProfile.builder()
|
||
|
profileProto.setDisplayName(displayName)
|
||
5 years ago
|
if let profileKey = profileKey, let profilePictureURL = profilePictureURL {
|
||
|
dataMessageProto.setProfileKey(profileKey)
|
||
|
profileProto.setProfilePicture(profilePictureURL)
|
||
|
}
|
||
5 years ago
|
do {
|
||
5 years ago
|
dataMessageProto.setProfile(try profileProto.build())
|
||
|
return try dataMessageProto.build()
|
||
5 years ago
|
} catch {
|
||
|
SNLog("Couldn't construct profile proto from: \(self).")
|
||
|
return nil
|
||
|
}
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|