diff --git a/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift b/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift index 80c954466..b35c590a6 100644 --- a/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift +++ b/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift @@ -190,8 +190,11 @@ final class DeviceLinkingModal : Modal, DeviceLinkingSessionDelegate { let thread = TSContactThread.getOrCreateThread(withContactId: slaveHexEncodedPublicKey, transaction: transaction) thread.save(with: transaction) } - let _ = SSKEnvironment.shared.syncManager.syncAllContacts() - let _ = SSKEnvironment.shared.syncManager.syncAllGroups() + let _ = SSKEnvironment.shared.syncManager.syncAllGroups().ensure { + // Closed groups first because we prefer the session request mechanism + // to the AFR mechanism + let _ = SSKEnvironment.shared.syncManager.syncAllContacts() + } let _ = SSKEnvironment.shared.syncManager.syncAllOpenGroups() storage.dbReadWriteConnection.readWrite { transaction in storage.setFriendRequestStatus(.friends, for: slaveHexEncodedPublicKey, transaction: transaction) diff --git a/SignalMessaging/contacts/OWSSyncManager.m b/SignalMessaging/contacts/OWSSyncManager.m index ef9f5f31d..50954e223 100644 --- a/SignalMessaging/contacts/OWSSyncManager.m +++ b/SignalMessaging/contacts/OWSSyncManager.m @@ -270,8 +270,7 @@ NSString *const kSyncManagerLastContactSyncKey = @"kTSStorageManagerOWSSyncManag - (AnyPromise *)syncContact:(NSString *)hexEncodedPubKey transaction:(YapDatabaseReadTransaction *)transaction { - [LKSyncMessagesProtocol syncContactWithHexEncodedPublicKey:hexEncodedPubKey in:transaction]; - return [AnyPromise promiseWithValue:@1]; + return [LKSyncMessagesProtocol syncContactWithHexEncodedPublicKey:hexEncodedPubKey in:transaction]; } - (AnyPromise *)syncAllContacts diff --git a/SignalServiceKit/src/Contacts/SignalRecipient.h b/SignalServiceKit/src/Contacts/SignalRecipient.h index 6dcfb51a0..3fd396f73 100644 --- a/SignalServiceKit/src/Contacts/SignalRecipient.h +++ b/SignalServiceKit/src/Contacts/SignalRecipient.h @@ -42,6 +42,7 @@ NS_ASSUME_NONNULL_BEGIN + (void)markRecipientAsRegistered:(NSString *)recipientId deviceId:(UInt32)deviceId transaction:(YapDatabaseReadWriteTransaction *)transaction; + + (void)markRecipientAsUnregistered:(NSString *)recipientId transaction:(YapDatabaseReadWriteTransaction *)transaction; @end diff --git a/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift index 8b7ec256a..04a468b8c 100644 --- a/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift @@ -180,6 +180,7 @@ public final class SessionManagementProtocol : NSObject { storage.setPreKeyBundle(preKeyBundle, forContact: hexEncodedPublicKey, transaction: transaction) // If we received a friend request (i.e. also a new pre key bundle), but we were already friends with the other user, reset the session. // The envelope type is set during UD decryption. + // TODO: Should this ignore session requests? if envelope.type == .friendRequest, let thread = TSContactThread.getWithContactId(hexEncodedPublicKey, transaction: transaction), // TODO: Should this be getOrCreate? thread.isContactFriend { diff --git a/SignalServiceKit/src/Loki/Protocol/Sync Messages/SyncMessagesProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Sync Messages/SyncMessagesProtocol.swift index 38547a26e..a5ef9e71c 100644 --- a/SignalServiceKit/src/Loki/Protocol/Sync Messages/SyncMessagesProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Sync Messages/SyncMessagesProtocol.swift @@ -37,14 +37,22 @@ public final class SyncMessagesProtocol : NSObject { } public static func syncAllContacts() -> Promise { - var friends: [SignalAccount] = [] + // Collect all master devices with which a session has been established. Note that + // this is not the same as all master devices with which we're friends. + var hepks: Set = [] TSContactThread.enumerateCollectionObjects { object, _ in guard let thread = object as? TSContactThread else { return } let hexEncodedPublicKey = thread.contactIdentifier() guard thread.isContactFriend && thread.shouldThreadBeVisible && !thread.isSlaveThread else { return } - friends.append(SignalAccount(recipientId: hexEncodedPublicKey)) + hepks.insert(hexEncodedPublicKey) } - friends.append(SignalAccount(recipientId: getUserHexEncodedPublicKey())) // TODO: Are we sure about this? + TSGroupThread.enumerateCollectionObjects { object, _ in + guard let group = object as? TSGroupThread, group.groupModel.groupType == .closedGroup, + group.shouldThreadBeVisible else { return } + hepks.formUnion(group.groupModel.groupMemberIds) + } + hepks.insert(getUserHexEncodedPublicKey()) // TODO: Are we sure about this? + let friends = hepks.map { SignalAccount(recipientId: $0) } let syncManager = SSKEnvironment.shared.syncManager let promises = friends.chunked(by: 3).map { friends -> Promise in // TODO: Does this always fit? return Promise(syncManager.syncContacts(for: friends)).map { _ in }