From ec2d784c205aa255816c75e110be3f6246e9a617 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 4 Jan 2021 10:36:18 +1100 Subject: [PATCH] Update contact model when legacy update methods are invoked --- SessionMessagingKit/Contacts/Contact.swift | 14 +++--- .../Database/Storage+Contacts.swift | 2 + SessionMessagingKit/To Do/OWSUserProfile.m | 49 +++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/SessionMessagingKit/Contacts/Contact.swift b/SessionMessagingKit/Contacts/Contact.swift index f29e74c7f..769a4c67e 100644 --- a/SessionMessagingKit/Contacts/Contact.swift +++ b/SessionMessagingKit/Contacts/Contact.swift @@ -1,19 +1,19 @@ @objc(SNContact) public class Contact : NSObject, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility - public let sessionID: String + @objc public let sessionID: String /// The display name of the contact. /// /// - Note: In open groups use `openGroupDisplayName`. - public var displayName: String? + @objc public var displayName: String? /// The URL from which to fetch the contact's profile picture. - public var profilePictureURL: String? + @objc public var profilePictureURL: String? /// The file name of the contact's profile picture on local storage. - public var profilePictureFileName: String? + @objc public var profilePictureFileName: String? /// The key with which the profile picture is encrypted. - public var profilePictureEncryptionKey: OWSAES256Key? + @objc public var profilePictureEncryptionKey: OWSAES256Key? /// The ID of the thread associated with this contact. - public var threadID: String? + @objc public var threadID: String? /// 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. @@ -25,7 +25,7 @@ public class Contact : NSObject, NSCoding { // NSObject/NSCoding conformance is } // MARK: Initialization - public init(sessionID: String) { + @objc public init(sessionID: String) { self.sessionID = sessionID super.init() } diff --git a/SessionMessagingKit/Database/Storage+Contacts.swift b/SessionMessagingKit/Database/Storage+Contacts.swift index 7d6404e51..9a41ea138 100644 --- a/SessionMessagingKit/Database/Storage+Contacts.swift +++ b/SessionMessagingKit/Database/Storage+Contacts.swift @@ -4,6 +4,7 @@ extension Storage { private static let contactCollection = "LokiContactCollection" + @objc(getContactWithSessionID:) public func getContact(with sessionID: String) -> Contact? { var result: Contact? Storage.read { transaction in @@ -12,6 +13,7 @@ extension Storage { return result } + @objc(setContact:usingTransaction:) public func setContact(_ contact: Contact, using transaction: Any) { (transaction as! YapDatabaseReadWriteTransaction).setObject(contact, forKey: contact.sessionID, inCollection: Storage.contactCollection) } diff --git a/SessionMessagingKit/To Do/OWSUserProfile.m b/SessionMessagingKit/To Do/OWSUserProfile.m index 7e18a8d3b..0440d6cfa 100644 --- a/SessionMessagingKit/To Do/OWSUserProfile.m +++ b/SessionMessagingKit/To Do/OWSUserProfile.m @@ -13,6 +13,8 @@ #import #import #import +#import +#import NS_ASSUME_NONNULL_BEGIN @@ -117,6 +119,15 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; #pragma mark - +- (NSString *)sessionID +{ + if ([self.recipientId isEqual:kLocalProfileUniqueId]) { + return OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey; + } else { + return self.recipientId; + } +} + - (nullable NSString *)avatarUrlPath { @synchronized(self) @@ -270,6 +281,12 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; functionName:__PRETTY_FUNCTION__ transaction:transaction completion:completion]; + + SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID]; + contact.displayName = [profileName ows_stripped]; + contact.profilePictureURL = avatarUrlPath; + contact.profilePictureFileName = avatarFileName; + [LKStorage.shared setContact:contact usingTransaction:transaction]; } - (void)updateWithProfileName:(nullable NSString *)profileName @@ -288,6 +305,14 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; functionName:__PRETTY_FUNCTION__ dbConnection:dbConnection completion:completion]; + + SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID]; + contact.displayName = [profileName ows_stripped]; + contact.profilePictureURL = avatarUrlPath; + contact.profilePictureFileName = avatarFileName; + [LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [LKStorage.shared setContact:contact usingTransaction:transaction]; + }]; } - (void)updateWithProfileName:(nullable NSString *)profileName @@ -302,6 +327,13 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; functionName:__PRETTY_FUNCTION__ dbConnection:dbConnection completion:completion]; + + SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID]; + contact.displayName = [profileName ows_stripped]; + contact.profilePictureURL = avatarUrlPath; + [LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [LKStorage.shared setContact:contact usingTransaction:transaction]; + }]; } - (void)updateWithAvatarUrlPath:(nullable NSString *)avatarUrlPath @@ -318,6 +350,13 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; functionName:__PRETTY_FUNCTION__ dbConnection:dbConnection completion:completion]; + + SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID]; + contact.profilePictureURL = avatarUrlPath; + contact.profilePictureFileName = avatarFileName; + [LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [LKStorage.shared setContact:contact usingTransaction:transaction]; + }]; } - (void)updateWithAvatarFileName:(nullable NSString *)avatarFileName @@ -330,6 +369,12 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; functionName:__PRETTY_FUNCTION__ dbConnection:dbConnection completion:completion]; + + SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID]; + contact.profilePictureFileName = avatarFileName; + [LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [LKStorage.shared setContact:contact usingTransaction:transaction]; + }]; } - (void)clearWithProfileKey:(OWSAES256Key *)profileKey @@ -368,6 +413,10 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; functionName:__PRETTY_FUNCTION__ transaction:transaction completion:completion]; + + SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID]; + contact.profilePictureEncryptionKey = profileKey; + [LKStorage.shared setContact:contact usingTransaction:transaction]; } #pragma mark - Database Connection Accessors