From d9a81ecb119e9691dd19a93389ed18d24650d4f9 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 4 Mar 2021 14:40:58 +1100 Subject: [PATCH 1/3] Fix profile handling --- .../MessageReceiver+Handling.swift | 98 ++++++++++++------- 1 file changed, 62 insertions(+), 36 deletions(-) diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index 313345165..ab8ebbdcc 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -182,30 +182,10 @@ extension MessageReceiver { SNLog("Configuration message received.") let storage = SNMessagingKitConfiguration.shared.storage let transaction = transaction as! YapDatabaseReadWriteTransaction - let userDefaults = UserDefaults.standard // Profile - let userProfile = storage.getUserProfile(using: transaction) - let user = storage.getUser() ?? Contact(sessionID: userPublicKey) - if let name = message.displayName { - let shouldUpdate = given(userDefaults[.lastDisplayNameUpdate]) { message.sentTimestamp! > UInt64($0.timeIntervalSince1970 * 1000) } ?? true - if shouldUpdate { - userProfile.profileName = name - user.name = name - userDefaults[.lastDisplayNameUpdate] = Date(timeIntervalSince1970: TimeInterval(message.sentTimestamp! / 1000)) - } - } - if let profilePictureURL = message.profilePictureURL, let profileKeyAsData = message.profileKey { - let shouldUpdate = given(userDefaults[.lastProfilePictureUpdate]) { message.sentTimestamp! > UInt64($0.timeIntervalSince1970 * 1000) } ?? true - if shouldUpdate { - userProfile.avatarUrlPath = profilePictureURL - user.profilePictureURL = profilePictureURL - userProfile.profileKey = OWSAES256Key(data: profileKeyAsData) - user.profilePictureEncryptionKey = OWSAES256Key(data: profileKeyAsData) - userDefaults[.lastProfilePictureUpdate] = Date(timeIntervalSince1970: TimeInterval(message.sentTimestamp! / 1000)) - } - } - userProfile.save(with: transaction) - Storage.shared.setContact(user, using: transaction) + let userProfile = SNMessagingKitConfiguration.shared.storage.getUserProfile(using: transaction) + updateProfileIfNeeded(publicKey: userPublicKey, name: message.displayName, profilePictureURL: message.profilePictureURL, + profileKey: given(message.profileKey) { OWSAES256Key(data: $0)! }, sentTimestamp: message.sentTimestamp!, transaction: transaction) transaction.addCompletionQueue(DispatchQueue.main) { SSKEnvironment.shared.profileManager.downloadAvatar(for: userProfile) } @@ -263,20 +243,9 @@ extension MessageReceiver { var attachmentsToDownload = attachmentIDs // Update profile if needed if let newProfile = message.profile { - let profileManager = SSKEnvironment.shared.profileManager let sessionID = message.sender! - let oldProfile = OWSUserProfile.fetch(uniqueId: sessionID, transaction: transaction) - let contact = Storage.shared.getContact(with: sessionID) ?? Contact(sessionID: sessionID) - if let displayName = newProfile.displayName, displayName != oldProfile?.profileName { - profileManager.updateProfileForContact(withID: sessionID, displayName: displayName, with: transaction) - contact.name = displayName - } - if let profileKey = newProfile.profileKey, let profilePictureURL = newProfile.profilePictureURL, profileKey.count == kAES256_KeyByteLength, - profileKey != oldProfile?.profileKey?.keyData { - profileManager.setProfileKeyData(profileKey, forRecipientId: sessionID, avatarURL: profilePictureURL) - contact.profilePictureURL = profilePictureURL - contact.profilePictureEncryptionKey = OWSAES256Key(data: profileKey) - } + updateProfileIfNeeded(publicKey: sessionID, name: newProfile.displayName, profilePictureURL: newProfile.profilePictureURL, + profileKey: given(newProfile.profileKey) { OWSAES256Key(data: $0)! }, sentTimestamp: message.sentTimestamp!, transaction: transaction) if let rawDisplayName = newProfile.displayName, let openGroupID = openGroupID { let endIndex = sessionID.endIndex let cutoffIndex = sessionID.index(endIndex, offsetBy: -8) @@ -329,6 +298,63 @@ extension MessageReceiver { SSKEnvironment.shared.notificationsManager!.notifyUser(for: tsIncomingMessage, in: thread, transaction: transaction) return tsMessageID } + + + + // MARK: - Profile Updating + private static func updateProfileIfNeeded(publicKey: String, name: String?, profilePictureURL: String?, + profileKey: OWSAES256Key?, sentTimestamp: UInt64, transaction: YapDatabaseReadWriteTransaction) { + let isCurrentUser = (publicKey == getUserHexEncodedPublicKey()) + let profileManager = SSKEnvironment.shared.profileManager + let userDefaults = UserDefaults.standard + let owsProfile = isCurrentUser ? SNMessagingKitConfiguration.shared.storage.getUserProfile(using: transaction) + : OWSUserProfile.fetch(uniqueId: publicKey, transaction: transaction) // Old API + let contact = Storage.shared.getContact(with: publicKey) ?? Contact(sessionID: publicKey) // New API + // Name + if let name = name, name != owsProfile?.profileName { + let shouldUpdate: Bool + if isCurrentUser { + shouldUpdate = given(userDefaults[.lastDisplayNameUpdate]) { sentTimestamp > UInt64($0.timeIntervalSince1970 * 1000) } ?? true + } else { + shouldUpdate = true + } + if shouldUpdate { + if isCurrentUser { + owsProfile?.profileName = name + userDefaults[.lastDisplayNameUpdate] = Date(timeIntervalSince1970: TimeInterval(sentTimestamp / 1000)) + } else { + profileManager.updateProfileForContact(withID: publicKey, displayName: name, with: transaction) + } + contact.name = name + } + } + // Profile picture & profile key + if let profileKey = profileKey, let profilePictureURL = profilePictureURL, profileKey.keyData.count == kAES256_KeyByteLength, + profileKey != owsProfile?.profileKey { + let shouldUpdate: Bool + if isCurrentUser { + shouldUpdate = given(userDefaults[.lastProfilePictureUpdate]) { sentTimestamp > UInt64($0.timeIntervalSince1970 * 1000) } ?? true + } else { + shouldUpdate = true + } + if shouldUpdate { + if isCurrentUser { + owsProfile?.avatarUrlPath = profilePictureURL + owsProfile?.profileKey = profileKey + userDefaults[.lastProfilePictureUpdate] = Date(timeIntervalSince1970: TimeInterval(sentTimestamp / 1000)) + } else { + profileManager.setProfileKeyData(profileKey.keyData, forRecipientId: publicKey, avatarURL: profilePictureURL) + } + contact.profilePictureURL = profilePictureURL + contact.profilePictureEncryptionKey = profileKey + } + } + // Persist changes + if isCurrentUser { + owsProfile?.save(with: transaction) + } + Storage.shared.setContact(contact, using: transaction) + } From b4e94c3f93343b46628f59b7ffcc546c9e8ae510 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 4 Mar 2021 14:45:17 +1100 Subject: [PATCH 2/3] Add comment --- .../Sending & Receiving/MessageReceiver+Handling.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index ab8ebbdcc..4ce89d936 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -350,7 +350,7 @@ extension MessageReceiver { } } // Persist changes - if isCurrentUser { + if isCurrentUser { // In the case where it's someone else the profile will already be saved (updateProfileForContact and setProfileKeyData to that internally) owsProfile?.save(with: transaction) } Storage.shared.setContact(contact, using: transaction) From 2b4e3a8b74073a8d9c06bbd815c7440ad12a463b Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 4 Mar 2021 14:53:13 +1100 Subject: [PATCH 3/3] Update version number --- Session.xcodeproj/project.pbxproj | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index 3f3d4ecda..d81ae483c 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -5169,7 +5169,7 @@ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 203; + CURRENT_PROJECT_VERSION = 204; DEBUG_INFORMATION_FORMAT = dwarf; DEVELOPMENT_TEAM = SUQ8J2PCT7; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; @@ -5190,7 +5190,7 @@ INFOPLIST_FILE = SessionShareExtension/Meta/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks"; - MARKETING_VERSION = 1.8.3; + MARKETING_VERSION = 1.9.0; MTL_ENABLE_DEBUG_INFO = YES; PRODUCT_BUNDLE_IDENTIFIER = "com.loki-project.loki-messenger.ShareExtension"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -5238,7 +5238,7 @@ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 203; + CURRENT_PROJECT_VERSION = 204; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_TEAM = SUQ8J2PCT7; ENABLE_NS_ASSERTIONS = NO; @@ -5264,7 +5264,7 @@ INFOPLIST_FILE = SessionShareExtension/Meta/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks"; - MARKETING_VERSION = 1.8.3; + MARKETING_VERSION = 1.9.0; MTL_ENABLE_DEBUG_INFO = NO; PRODUCT_BUNDLE_IDENTIFIER = "com.loki-project.loki-messenger.ShareExtension"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -5299,7 +5299,7 @@ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 203; + CURRENT_PROJECT_VERSION = 204; DEBUG_INFORMATION_FORMAT = dwarf; DEVELOPMENT_TEAM = SUQ8J2PCT7; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; @@ -5318,7 +5318,7 @@ INFOPLIST_FILE = SessionNotificationServiceExtension/Meta/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks"; - MARKETING_VERSION = 1.8.3; + MARKETING_VERSION = 1.9.0; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = "com.loki-project.loki-messenger.NotificationServiceExtension"; @@ -5369,7 +5369,7 @@ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 203; + CURRENT_PROJECT_VERSION = 204; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_TEAM = SUQ8J2PCT7; ENABLE_NS_ASSERTIONS = NO; @@ -5393,7 +5393,7 @@ INFOPLIST_FILE = SessionNotificationServiceExtension/Meta/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks"; - MARKETING_VERSION = 1.8.3; + MARKETING_VERSION = 1.9.0; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = "com.loki-project.loki-messenger.NotificationServiceExtension"; @@ -6254,7 +6254,7 @@ CODE_SIGN_ENTITLEMENTS = Session/Meta/Signal.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; - CURRENT_PROJECT_VERSION = 203; + CURRENT_PROJECT_VERSION = 204; DEVELOPMENT_TEAM = SUQ8J2PCT7; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -6290,7 +6290,7 @@ "$(SRCROOT)", ); LLVM_LTO = NO; - MARKETING_VERSION = 1.8.3; + MARKETING_VERSION = 1.9.0; OTHER_LDFLAGS = "$(inherited)"; OTHER_SWIFT_FLAGS = "$(inherited) \"-D\" \"COCOAPODS\" \"-DDEBUG\""; PRODUCT_BUNDLE_IDENTIFIER = "com.loki-project.loki-messenger"; @@ -6322,7 +6322,7 @@ CODE_SIGN_ENTITLEMENTS = Session/Meta/Signal.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; - CURRENT_PROJECT_VERSION = 203; + CURRENT_PROJECT_VERSION = 204; DEVELOPMENT_TEAM = SUQ8J2PCT7; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -6358,7 +6358,7 @@ "$(SRCROOT)", ); LLVM_LTO = NO; - MARKETING_VERSION = 1.8.3; + MARKETING_VERSION = 1.9.0; OTHER_LDFLAGS = "$(inherited)"; PRODUCT_BUNDLE_IDENTIFIER = "com.loki-project.loki-messenger"; PRODUCT_NAME = Session;