Sync profile updates

This commit is contained in:
Niels Andriesse 2021-02-23 16:01:06 +11:00
parent 713b781def
commit 10e6d114a0
8 changed files with 74 additions and 40 deletions

View File

@ -132,28 +132,13 @@ final class LinkDeviceVC : BaseVC, UIPageViewControllerDataSource, UIPageViewCon
} }
@objc private func handleConfigurationMessageReceived(_ notification: Notification) { @objc private func handleConfigurationMessageReceived(_ notification: Notification) {
guard let profile = notification.object as? [String:Any?], let displayName = profile["displayName"] as? String else { return }
let profilePictureURL = profile["profilePictureURL"] as? String
let profileKeyAsData = profile["profileKey"] as? NSData
let profileKey = given(profileKeyAsData) { OWSAES256Key(data: $0 as Data)! }
TSAccountManager.sharedInstance().phoneNumberAwaitingVerification = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey TSAccountManager.sharedInstance().phoneNumberAwaitingVerification = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey
let profileManager = OWSProfileManager.shared() DispatchQueue.main.async {
var userProfile: OWSUserProfile! self.navigationController!.dismiss(animated: true) {
Storage.write(with: { transaction in let pnModeVC = PNModeVC()
userProfile = profileManager.getLocalUserProfile(with: transaction) self.navigationController!.setViewControllers([ pnModeVC ], animated: true)
userProfile.profileName = displayName
userProfile.avatarUrlPath = profilePictureURL
userProfile.profileKey = profileKey
userProfile.save(with: transaction)
}, completion: {
profileManager.downloadAvatar(for: userProfile)
DispatchQueue.main.async {
self.navigationController!.dismiss(animated: true) {
let pnModeVC = PNModeVC()
self.navigationController!.setViewControllers([ pnModeVC ], animated: true)
}
} }
}) }
} }
} }

View File

@ -14,11 +14,17 @@ enum Onboarding {
case .register: case .register:
userDefaults[.hasViewedSeed] = false userDefaults[.hasViewedSeed] = false
restorationTime = 0 restorationTime = 0
userDefaults[.hasSyncedConfiguration] = true userDefaults[.hasSyncedInitialConfiguration] = true
case .recover, .link: case .recover, .link:
userDefaults[.hasViewedSeed] = true userDefaults[.hasViewedSeed] = true
restorationTime = Date().timeIntervalSince1970 restorationTime = Date().timeIntervalSince1970
userDefaults[.hasSyncedConfiguration] = false userDefaults[.hasSyncedInitialConfiguration] = false
}
switch self {
case .register, .recover:
userDefaults[.lastDisplayNameUpdate] = Date()
userDefaults[.lastProfilePictureUpdate] = Date()
case .link: break
} }
OWSPrimaryStorage.shared().setRestorationTime(restorationTime) OWSPrimaryStorage.shared().setRestorationTime(restorationTime)
} }

View File

@ -296,10 +296,19 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate {
} }
private func updateProfile(isUpdatingDisplayName: Bool, isUpdatingProfilePicture: Bool) { private func updateProfile(isUpdatingDisplayName: Bool, isUpdatingProfilePicture: Bool) {
let userDefaults = UserDefaults.standard
let displayName = displayNameToBeUploaded ?? OWSProfileManager.shared().profileNameForRecipient(withID: getUserHexEncodedPublicKey()) let displayName = displayNameToBeUploaded ?? OWSProfileManager.shared().profileNameForRecipient(withID: getUserHexEncodedPublicKey())
let profilePicture = profilePictureToBeUploaded ?? OWSProfileManager.shared().profileAvatar(forRecipientId: getUserHexEncodedPublicKey()) let profilePicture = profilePictureToBeUploaded ?? OWSProfileManager.shared().profileAvatar(forRecipientId: getUserHexEncodedPublicKey())
ModalActivityIndicatorViewController.present(fromViewController: navigationController!, canCancel: false) { [weak self] modalActivityIndicator in ModalActivityIndicatorViewController.present(fromViewController: navigationController!, canCancel: false) { [weak self, displayNameToBeUploaded, profilePictureToBeUploaded] modalActivityIndicator in
OWSProfileManager.shared().updateLocalProfileName(displayName, avatarImage: profilePicture, success: { OWSProfileManager.shared().updateLocalProfileName(displayName, avatarImage: profilePicture, success: {
if displayNameToBeUploaded != nil {
userDefaults[.lastDisplayNameUpdate] = Date()
}
if profilePictureToBeUploaded != nil {
userDefaults[.lastProfilePictureUpdate] = Date()
}
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.forceSyncConfigurationNowIfNeeded().retainUntilComplete()
DispatchQueue.main.async { DispatchQueue.main.async {
modalActivityIndicator.dismiss { modalActivityIndicator.dismiss {
guard let self = self else { return } guard let self = self else { return }

View File

@ -143,24 +143,49 @@ extension MessageReceiver {
} }
private static func handleConfigurationMessage(_ message: ConfigurationMessage, using transaction: Any) { private static func handleConfigurationMessage(_ message: ConfigurationMessage, using transaction: Any) {
guard message.sender == getUserHexEncodedPublicKey(), !UserDefaults.standard[.hasSyncedConfiguration] else { return } guard message.sender == getUserHexEncodedPublicKey() else { return }
let storage = SNMessagingKitConfiguration.shared.storage let storage = SNMessagingKitConfiguration.shared.storage
// Notification let transaction = transaction as! YapDatabaseReadWriteTransaction
UserDefaults.standard[.hasSyncedConfiguration] = true let userDefaults = UserDefaults.standard
let profile: [String:Any?] = [ "displayName" : message.displayName, "profilePictureURL" : message.profilePictureURL, "profileKey" : message.profileKey ] // Profile
NotificationCenter.default.post(name: .configurationMessageReceived, object: profile) let userProfile = storage.getUserProfile(using: transaction)
// Closed groups if let displayName = message.displayName {
let allClosedGroupPublicKeys = storage.getUserClosedGroupPublicKeys() let shouldUpdate = given(userDefaults[.lastDisplayNameUpdate]) { message.sentTimestamp! > UInt64($0.timeIntervalSince1970 * 1000) } ?? true
for closedGroup in message.closedGroups { if shouldUpdate {
guard !allClosedGroupPublicKeys.contains(closedGroup.publicKey) else { continue } userProfile.profileName = displayName
handleNewClosedGroup(groupPublicKey: closedGroup.publicKey, name: closedGroup.name, encryptionKeyPair: closedGroup.encryptionKeyPair, userDefaults[.lastDisplayNameUpdate] = Date()
members: [String](closedGroup.members), admins: [String](closedGroup.admins), messageSentTimestamp: message.sentTimestamp!, using: transaction) }
} }
// Open groups if let profilePictureURL = message.profilePictureURL, let profileKeyAsData = message.profileKey {
let allOpenGroups = Set(storage.getAllUserOpenGroups().keys) let shouldUpdate = given(userDefaults[.lastProfilePictureUpdate]) { message.sentTimestamp! > UInt64($0.timeIntervalSince1970 * 1000) } ?? true
for openGroupURL in message.openGroups { if shouldUpdate {
guard !allOpenGroups.contains(openGroupURL) else { continue } userProfile.avatarUrlPath = profilePictureURL
OpenGroupManager.shared.add(with: openGroupURL, using: transaction).retainUntilComplete() userProfile.profileKey = OWSAES256Key(data: profileKeyAsData)
userDefaults[.lastProfilePictureUpdate] = Date()
}
}
userProfile.save(with: transaction)
transaction.addCompletionQueue(DispatchQueue.main) {
SSKEnvironment.shared.profileManager.downloadAvatar(for: userProfile)
}
// Notification
NotificationCenter.default.post(name: .configurationMessageReceived, object: nil)
// Initial configuration sync
if !UserDefaults.standard[.hasSyncedInitialConfiguration] {
UserDefaults.standard[.hasSyncedInitialConfiguration] = true
// Closed groups
let allClosedGroupPublicKeys = storage.getUserClosedGroupPublicKeys()
for closedGroup in message.closedGroups {
guard !allClosedGroupPublicKeys.contains(closedGroup.publicKey) else { continue }
handleNewClosedGroup(groupPublicKey: closedGroup.publicKey, name: closedGroup.name, encryptionKeyPair: closedGroup.encryptionKeyPair,
members: [String](closedGroup.members), admins: [String](closedGroup.admins), messageSentTimestamp: message.sentTimestamp!, using: transaction)
}
// Open groups
let allOpenGroups = Set(storage.getAllUserOpenGroups().keys)
for openGroupURL in message.openGroups {
guard !allOpenGroups.contains(openGroupURL) else { continue }
OpenGroupManager.shared.add(with: openGroupURL, using: transaction).retainUntilComplete()
}
} }
} }

View File

@ -19,6 +19,7 @@ public protocol SessionMessagingKitStorageProtocol {
func getUserDisplayName() -> String? func getUserDisplayName() -> String?
func getUserProfileKey() -> Data? func getUserProfileKey() -> Data?
func getUserProfilePictureURL() -> String? func getUserProfilePictureURL() -> String?
func getUserProfile(using transaction: Any) -> OWSUserProfile
// MARK: - Closed Groups // MARK: - Closed Groups

View File

@ -29,6 +29,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)ensureLocalProfileCached; - (void)ensureLocalProfileCached;
- (void)ensureProfileCachedForContactWithID:(NSString *)contactID with:(YapDatabaseReadWriteTransaction *)transaction; - (void)ensureProfileCachedForContactWithID:(NSString *)contactID with:(YapDatabaseReadWriteTransaction *)transaction;
- (void)downloadAvatarForUserProfile:(OWSUserProfile *)userProfile;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@ -3,7 +3,7 @@ import Foundation
public enum SNUserDefaults { public enum SNUserDefaults {
public enum Bool : Swift.String { public enum Bool : Swift.String {
case hasSyncedConfiguration case hasSyncedInitialConfiguration = "hasSyncedConfiguration"
case hasViewedSeed case hasViewedSeed
case hasSeenLinkPreviewSuggestion case hasSeenLinkPreviewSuggestion
case isUsingFullAPNs case isUsingFullAPNs
@ -13,6 +13,8 @@ public enum SNUserDefaults {
public enum Date : Swift.String { public enum Date : Swift.String {
case lastProfilePictureUpload case lastProfilePictureUpload
case lastConfigurationSync case lastConfigurationSync
case lastDisplayNameUpdate
case lastProfilePictureUpdate
} }
public enum Double : Swift.String { public enum Double : Swift.String {

View File

@ -5,4 +5,8 @@ extension Storage : SessionMessagingKitStorageProtocol, SessionSnodeKitStoragePr
let transaction = transaction as! YapDatabaseReadWriteTransaction let transaction = transaction as! YapDatabaseReadWriteTransaction
OWSPrimaryStorage.shared().updateMessageIDCollectionByPruningMessagesWithIDs(messageIDs, in: transaction) OWSPrimaryStorage.shared().updateMessageIDCollectionByPruningMessagesWithIDs(messageIDs, in: transaction)
} }
public func getUserProfile(using transaction: Any) -> OWSUserProfile {
return OWSProfileManager.shared().getLocalUserProfile(with: transaction as! YapDatabaseReadWriteTransaction)
}
} }