Skip to content

Commit

Permalink
Fix local SignalRecipient if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
max-signal committed Sep 14, 2023
1 parent 48f2c42 commit 1094d86
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ public class LegacyChangePhoneNumber: NSObject {
let recipientMerger = DependenciesBridge.shared.recipientMerger
let localRecipient = recipientMerger.applyMergeForLocalAccount(
aci: serviceAci,
pni: servicePni,
phoneNumber: serviceE164,
pni: servicePni,
tx: transaction.asV2Write
)
localRecipient.markAsRegisteredAndSave(tx: transaction)
Expand Down
2 changes: 1 addition & 1 deletion SignalServiceKit/src/Account/TSAccountManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ public extension TSAccountManager {

let localRecipient = DependenciesBridge.shared.recipientMerger.applyMergeForLocalAccount(
aci: newAci.wrappedAciValue,
pni: newPni?.wrappedPniValue,
phoneNumber: newLocalNumber.wrappedValue,
pni: newPni?.wrappedPniValue,
tx: transaction.asV2Write
)
localRecipient.markAsRegisteredAndSave(tx: transaction)
Expand Down
4 changes: 2 additions & 2 deletions SignalServiceKit/src/Contacts/RecipientMerger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public protocol RecipientMerger {
/// time we're allowed to "merge" the identifiers for our own account.
func applyMergeForLocalAccount(
aci: Aci,
pni: Pni?,
phoneNumber: E164,
pni: Pni?,
tx: DBWriteTransaction
) -> SignalRecipient

Expand Down Expand Up @@ -160,8 +160,8 @@ class RecipientMergerImpl: RecipientMerger {

func applyMergeForLocalAccount(
aci: Aci,
pni: Pni?,
phoneNumber: E164,
pni: Pni?,
tx: DBWriteTransaction
) -> SignalRecipient {
return mergeAlways(aci: aci, phoneNumber: phoneNumber, isLocalRecipient: true, tx: tx)
Expand Down
13 changes: 0 additions & 13 deletions SignalServiceKit/src/Contacts/SignalServiceAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -493,19 +493,6 @@ public class SignalServiceAddressCache: NSObject {
owsAssertDebug(GRDBSchemaMigrator.areMigrationsComplete)

databaseStorage.read { transaction in
if let localIdentifiers = tsAccountManager.localIdentifiers(transaction: transaction) {
updateRecipient(
aci: localIdentifiers.aci,
// PNI TODO: Fetch our own PNI once it's stored on our SignalRecipient.
//
// (Even though our own PNI may be available at this point, we should have
// a recipient for ourselves, so we'd immediately overwrite it during the
// `anyEnumerate` below.)
pni: nil,
phoneNumber: localIdentifiers.phoneNumber
)
}

SignalRecipient.anyEnumerate(transaction: transaction) { recipient, _ in
self.updateRecipient(recipient)
}
Expand Down
24 changes: 24 additions & 0 deletions SignalServiceKit/src/SSKEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public class SSKEnvironment: NSObject {
}
warmCachesForObject("signalProxy", SignalProxy.warmCaches)
warmCachesForObject("tsAccountManager", tsAccountManager.warmCaches)
warmCachesForObject("fixLocalRecipient", fixLocalRecipientIfNeeded)
warmCachesForObject("signalServiceAddressCache", signalServiceAddressCache.warmCaches)
warmCachesForObject("signalService", signalService.warmCaches)
warmCachesForObject("remoteConfigManager", remoteConfigManager.warmCaches)
Expand All @@ -251,6 +252,29 @@ public class SSKEnvironment: NSObject {
NotificationCenter.default.post(name: SSKEnvironment.warmCachesNotification, object: nil)
}

/// Ensures the local SignalRecipient is correct.
///
/// This primarily serves to ensure the local SignalRecipient has its own
/// Pni (a one-time migration), but it also helps ensure that the value is
/// always consistent with TSAccountManager's values.
private func fixLocalRecipientIfNeeded() {
databaseStorage.write { tx in
guard let localIdentifiers = tsAccountManager.localIdentifiers(transaction: tx) else {
return // Not registered yet.
}
guard let phoneNumber = E164(localIdentifiers.phoneNumber) else {
return // Registered with an invalid phone number.
}
let recipientMerger = DependenciesBridge.shared.recipientMerger
_ = recipientMerger.applyMergeForLocalAccount(
aci: localIdentifiers.aci,
phoneNumber: phoneNumber,
pni: localIdentifiers.pni,
tx: tx.asV2Write
)
}
}

#if TESTABLE_BUILD

public func setContactsManagerForUnitTests(_ contactsManager: ContactsManagerProtocol) {
Expand Down

0 comments on commit 1094d86

Please sign in to comment.