Sync profile updates
This commit is contained in:
parent
713b781def
commit
10e6d114a0
|
@ -132,28 +132,13 @@ final class LinkDeviceVC : BaseVC, UIPageViewControllerDataSource, UIPageViewCon
|
|||
}
|
||||
|
||||
@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
|
||||
let profileManager = OWSProfileManager.shared()
|
||||
var userProfile: OWSUserProfile!
|
||||
Storage.write(with: { transaction in
|
||||
userProfile = profileManager.getLocalUserProfile(with: transaction)
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,11 +14,17 @@ enum Onboarding {
|
|||
case .register:
|
||||
userDefaults[.hasViewedSeed] = false
|
||||
restorationTime = 0
|
||||
userDefaults[.hasSyncedConfiguration] = true
|
||||
userDefaults[.hasSyncedInitialConfiguration] = true
|
||||
case .recover, .link:
|
||||
userDefaults[.hasViewedSeed] = true
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -296,10 +296,19 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate {
|
|||
}
|
||||
|
||||
private func updateProfile(isUpdatingDisplayName: Bool, isUpdatingProfilePicture: Bool) {
|
||||
let userDefaults = UserDefaults.standard
|
||||
let displayName = displayNameToBeUploaded ?? OWSProfileManager.shared().profileNameForRecipient(withID: 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: {
|
||||
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 {
|
||||
modalActivityIndicator.dismiss {
|
||||
guard let self = self else { return }
|
||||
|
|
|
@ -143,12 +143,36 @@ extension MessageReceiver {
|
|||
}
|
||||
|
||||
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 transaction = transaction as! YapDatabaseReadWriteTransaction
|
||||
let userDefaults = UserDefaults.standard
|
||||
// Profile
|
||||
let userProfile = storage.getUserProfile(using: transaction)
|
||||
if let displayName = message.displayName {
|
||||
let shouldUpdate = given(userDefaults[.lastDisplayNameUpdate]) { message.sentTimestamp! > UInt64($0.timeIntervalSince1970 * 1000) } ?? true
|
||||
if shouldUpdate {
|
||||
userProfile.profileName = displayName
|
||||
userDefaults[.lastDisplayNameUpdate] = Date()
|
||||
}
|
||||
}
|
||||
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
|
||||
userProfile.profileKey = OWSAES256Key(data: profileKeyAsData)
|
||||
userDefaults[.lastProfilePictureUpdate] = Date()
|
||||
}
|
||||
}
|
||||
userProfile.save(with: transaction)
|
||||
transaction.addCompletionQueue(DispatchQueue.main) {
|
||||
SSKEnvironment.shared.profileManager.downloadAvatar(for: userProfile)
|
||||
}
|
||||
// Notification
|
||||
UserDefaults.standard[.hasSyncedConfiguration] = true
|
||||
let profile: [String:Any?] = [ "displayName" : message.displayName, "profilePictureURL" : message.profilePictureURL, "profileKey" : message.profileKey ]
|
||||
NotificationCenter.default.post(name: .configurationMessageReceived, object: profile)
|
||||
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 {
|
||||
|
@ -163,6 +187,7 @@ extension MessageReceiver {
|
|||
OpenGroupManager.shared.add(with: openGroupURL, using: transaction).retainUntilComplete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public static func handleVisibleMessage(_ message: VisibleMessage, associatedWithProto proto: SNProtoContent, openGroupID: String?, isBackgroundPoll: Bool, using transaction: Any) throws -> String {
|
||||
|
|
|
@ -19,6 +19,7 @@ public protocol SessionMessagingKitStorageProtocol {
|
|||
func getUserDisplayName() -> String?
|
||||
func getUserProfileKey() -> Data?
|
||||
func getUserProfilePictureURL() -> String?
|
||||
func getUserProfile(using transaction: Any) -> OWSUserProfile
|
||||
|
||||
// MARK: - Closed Groups
|
||||
|
||||
|
|
|
@ -29,6 +29,8 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
- (void)ensureLocalProfileCached;
|
||||
- (void)ensureProfileCachedForContactWithID:(NSString *)contactID with:(YapDatabaseReadWriteTransaction *)transaction;
|
||||
|
||||
- (void)downloadAvatarForUserProfile:(OWSUserProfile *)userProfile;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
|
|
@ -3,7 +3,7 @@ import Foundation
|
|||
public enum SNUserDefaults {
|
||||
|
||||
public enum Bool : Swift.String {
|
||||
case hasSyncedConfiguration
|
||||
case hasSyncedInitialConfiguration = "hasSyncedConfiguration"
|
||||
case hasViewedSeed
|
||||
case hasSeenLinkPreviewSuggestion
|
||||
case isUsingFullAPNs
|
||||
|
@ -13,6 +13,8 @@ public enum SNUserDefaults {
|
|||
public enum Date : Swift.String {
|
||||
case lastProfilePictureUpload
|
||||
case lastConfigurationSync
|
||||
case lastDisplayNameUpdate
|
||||
case lastProfilePictureUpdate
|
||||
}
|
||||
|
||||
public enum Double : Swift.String {
|
||||
|
|
|
@ -5,4 +5,8 @@ extension Storage : SessionMessagingKitStorageProtocol, SessionSnodeKitStoragePr
|
|||
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
||||
OWSPrimaryStorage.shared().updateMessageIDCollectionByPruningMessagesWithIDs(messageIDs, in: transaction)
|
||||
}
|
||||
|
||||
public func getUserProfile(using transaction: Any) -> OWSUserProfile {
|
||||
return OWSProfileManager.shared().getLocalUserProfile(with: transaction as! YapDatabaseReadWriteTransaction)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue