session-ios/SessionMessagingKit/Messages/Visible Messages/VisibleMessage+Profile.swift

73 lines
3.0 KiB
Swift
Raw Normal View History

2020-11-09 00:58:47 +01:00
import SessionUtilitiesKit
2020-11-05 04:16:45 +01:00
public extension VisibleMessage {
2020-11-05 23:17:05 +01:00
@objc(SNProfile)
class Profile : NSObject, NSCoding {
2020-11-06 05:53:08 +01:00
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
}
2020-11-05 04:16:45 +01:00
public required init?(coder: NSCoder) {
2020-11-06 05:53:08 +01:00
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 }
2020-11-05 04:16:45 +01:00
}
public func encode(with coder: NSCoder) {
2020-11-06 05:53:08 +01:00
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)
}
}
2020-11-06 06:01:51 +01:00
public func toProto() -> SNProtoDataMessage? {
2020-11-06 05:53:08 +01:00
guard let displayName = displayName else {
SNLog("Couldn't construct profile proto from: \(self).")
return nil
}
2020-11-06 06:01:51 +01:00
let dataMessageProto = SNProtoDataMessage.builder()
2020-11-06 05:53:08 +01:00
let profileProto = SNProtoDataMessageLokiProfile.builder()
profileProto.setDisplayName(displayName)
2020-11-06 06:01:51 +01:00
if let profileKey = profileKey, let profilePictureURL = profilePictureURL {
dataMessageProto.setProfileKey(profileKey)
profileProto.setProfilePicture(profilePictureURL)
}
2020-11-06 05:53:08 +01:00
do {
2020-11-06 06:01:51 +01:00
dataMessageProto.setProfile(try profileProto.build())
return try dataMessageProto.build()
2020-11-06 05:53:08 +01:00
} catch {
SNLog("Couldn't construct profile proto from: \(self).")
return nil
}
2020-11-05 04:16:45 +01:00
}
2020-12-07 05:11:49 +01:00
// MARK: Description
public override var description: String {
"""
Profile(
2021-05-03 07:13:18 +02:00
displayName: \(displayName ?? "null"),
profileKey: \(profileKey?.description ?? "null"),
2020-12-07 05:11:49 +01:00
profilePictureURL: \(profilePictureURL ?? "null")
)
"""
}
2020-11-05 04:16:45 +01:00
}
}