Skip to content

Commit 01b9bf5

Browse files
authored
Remove code that’s only used by unit tests
1 parent 4115382 commit 01b9bf5

File tree

6 files changed

+39
-143
lines changed

6 files changed

+39
-143
lines changed

Signal/test/util/GRDBFinderTest.swift

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -80,49 +80,19 @@ class GRDBFinderTest: SignalBaseTest {
8080
func testSignalAccountFinder() {
8181

8282
// We'll create SignalAccount for these...
83-
let address1 = SignalServiceAddress(phoneNumber: "+16505550101")
84-
let address2 = SignalServiceAddress(serviceId: Aci.randomForTesting(), phoneNumber: "+16505550102")
85-
let address3 = SignalServiceAddress(serviceId: Aci.randomForTesting(), phoneNumber: "+16505550103")
86-
let address4 = SignalServiceAddress.randomForTesting()
83+
let phoneNumber1 = "+16505550101"
8784
// ...but not these.
88-
let address5 = SignalServiceAddress(phoneNumber: "+16505550105")
89-
let address6 = SignalServiceAddress(serviceId: Aci.randomForTesting(), phoneNumber: "+16505550106")
90-
let address7 = SignalServiceAddress.randomForTesting()
85+
let phoneNumber5 = "+16505550105"
9186

9287
self.write { transaction in
93-
SignalAccount(address: address1).anyInsert(transaction: transaction)
94-
SignalAccount(address: address2).anyInsert(transaction: transaction)
95-
SignalAccount(address: address3).anyInsert(transaction: transaction)
96-
SignalAccount(address: address4).anyInsert(transaction: transaction)
88+
SignalAccount(phoneNumber: phoneNumber1).anyInsert(transaction: transaction)
9789
}
9890

9991
self.read { tx in
10092
// These should exist...
101-
XCTAssertNotNil(SignalAccountFinder().signalAccount(for: address1, tx: tx))
102-
// If we save a SignalAccount with just a phone number,
103-
// we should later be able to look it up using a UUID & phone number,
104-
XCTAssertNotNil(SignalAccountFinder().signalAccount(for: SignalServiceAddress(serviceId: Aci.randomForTesting(), phoneNumber: address1.phoneNumber!), tx: tx))
105-
XCTAssertNotNil(SignalAccountFinder().signalAccount(for: address2, tx: tx))
106-
// If we save a SignalAccount with just a phone number and UUID,
107-
// we should later be able to look it up using just a UUID.
108-
XCTAssertNotNil(SignalAccountFinder().signalAccount(for: SignalServiceAddress(address2.serviceId!), tx: tx))
109-
// If we save a SignalAccount with just a phone number and UUID,
110-
// we should later be able to look it up using just a phone number.
111-
XCTAssertNotNil(SignalAccountFinder().signalAccount(for: SignalServiceAddress(phoneNumber: address2.phoneNumber!), tx: tx))
112-
XCTAssertNotNil(SignalAccountFinder().signalAccount(for: address3, tx: tx))
113-
XCTAssertNotNil(SignalAccountFinder().signalAccount(for: SignalServiceAddress(address3.serviceId!), tx: tx))
114-
XCTAssertNotNil(SignalAccountFinder().signalAccount(for: SignalServiceAddress(phoneNumber: address3.phoneNumber!), tx: tx))
115-
XCTAssertNotNil(SignalAccountFinder().signalAccount(for: address4, tx: tx))
116-
// If we save a SignalAccount with just a UUID,
117-
// we should later be able to look it up using a UUID & phone number,
118-
XCTAssertNotNil(SignalAccountFinder().signalAccount(for: SignalServiceAddress(serviceId: address4.serviceId!, phoneNumber: "+16505550198"), tx: tx))
119-
93+
XCTAssertNotNil(SignalAccountFinder().signalAccount(for: phoneNumber1, tx: tx))
12094
// ...these don't.
121-
XCTAssertNil(SignalAccountFinder().signalAccount(for: address5, tx: tx))
122-
XCTAssertNil(SignalAccountFinder().signalAccount(for: address6, tx: tx))
123-
XCTAssertNil(SignalAccountFinder().signalAccount(for: SignalServiceAddress(address6.serviceId!), tx: tx))
124-
XCTAssertNil(SignalAccountFinder().signalAccount(for: SignalServiceAddress(phoneNumber: address6.phoneNumber!), tx: tx))
125-
XCTAssertNil(SignalAccountFinder().signalAccount(for: address7, tx: tx))
95+
XCTAssertNil(SignalAccountFinder().signalAccount(for: phoneNumber5, tx: tx))
12696
}
12797
}
12898

SignalServiceKit/Contacts/ContactSyncAttachmentBuilder.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,17 @@ enum ContactSyncAttachmentBuilder {
137137

138138
private static func localAccountToSync(localAddress: SignalServiceAddress) -> SignalAccount {
139139
// OWSContactsOutputStream requires all signalAccount to have a contact.
140-
return SignalAccount(address: localAddress)
140+
return SignalAccount(
141+
recipientPhoneNumber: localAddress.phoneNumber,
142+
recipientServiceId: localAddress.serviceId,
143+
multipleAccountLabelText: nil,
144+
cnContactId: nil,
145+
givenName: "",
146+
familyName: "",
147+
nickname: "",
148+
fullName: "",
149+
contactAvatarHash: nil,
150+
)
141151
}
142152
}
143153

SignalServiceKit/Contacts/SignalAccount.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,10 @@ extension SignalAccount {
176176
// MARK: - Convenience Inits
177177

178178
extension SignalAccount {
179-
@objc
180-
public convenience init(address: SignalServiceAddress) {
179+
public convenience init(phoneNumber: String) {
181180
self.init(
182-
recipientPhoneNumber: address.phoneNumber,
183-
recipientServiceId: address.serviceId,
181+
recipientPhoneNumber: phoneNumber,
182+
recipientServiceId: nil,
184183
multipleAccountLabelText: nil,
185184
cnContactId: nil,
186185
givenName: "",

SignalServiceKit/Contacts/SignalAccountFinder.swift

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,8 @@
66
import GRDB
77
import LibSignalClient
88

9-
public class SignalAccountFinder: NSObject {
10-
func signalAccount(
11-
for address: SignalServiceAddress,
12-
tx: DBReadTransaction
13-
) -> SignalAccount? {
14-
if
15-
let serviceId = address.serviceId,
16-
let serviceIdMatch = signalAccountWhere(
17-
column: SignalAccount.columnName(.recipientServiceId),
18-
matches: serviceId.serviceIdUppercaseString,
19-
tx: tx
20-
)
21-
{
22-
return serviceIdMatch
23-
}
24-
if
25-
let phoneNumber = address.phoneNumber,
26-
let phoneNumberMatch = signalAccount(for: phoneNumber, tx: tx)
27-
{
28-
return phoneNumberMatch
29-
}
30-
return nil
9+
public struct SignalAccountFinder {
10+
public init() {
3111
}
3212

3313
public func signalAccount(
@@ -48,54 +28,13 @@ public class SignalAccountFinder: NSObject {
4828
)
4929
}
5030

51-
func signalAccounts(
52-
for addresses: [SignalServiceAddress],
53-
tx: DBReadTransaction
54-
) -> [SignalAccount?] {
55-
return Refinery<SignalServiceAddress, SignalAccount>(addresses).refine { addresses in
56-
return signalAccountsForServiceIds(
57-
addresses.map { $0.serviceId },
58-
tx: tx
59-
)
60-
}.refine { addresses in
61-
return signalAccountsForPhoneNumbers(
62-
addresses.map { $0.phoneNumber },
63-
tx: tx
64-
)
65-
}.values
66-
}
67-
6831
func signalAccounts(
6932
for phoneNumbers: [String],
7033
tx: DBReadTransaction
7134
) -> [SignalAccount?] {
7235
return signalAccountsForPhoneNumbers(phoneNumbers, tx: tx)
7336
}
7437

75-
private func signalAccountsForServiceIds(
76-
_ serviceIds: [ServiceId?],
77-
tx: DBReadTransaction
78-
) -> [SignalAccount?] {
79-
let accounts = signalAccountsWhere(
80-
column: SignalAccount.columnName(.recipientServiceId),
81-
anyValueIn: serviceIds.compactMap { $0?.serviceIdUppercaseString },
82-
tx: tx
83-
)
84-
85-
let index: [ServiceId?: [SignalAccount?]] = Dictionary(grouping: accounts) { $0?.recipientServiceId }
86-
return serviceIds.map { maybeServiceId -> SignalAccount? in
87-
guard
88-
let serviceId = maybeServiceId,
89-
let accountsForServiceId = index[serviceId],
90-
let firstAccountForServiceId = accountsForServiceId.first
91-
else {
92-
return nil
93-
}
94-
95-
return firstAccountForServiceId
96-
}
97-
}
98-
9938
private func signalAccountsForPhoneNumbers(
10039
_ phoneNumbers: [String?],
10140
tx: DBReadTransaction
@@ -131,10 +70,6 @@ public class SignalAccountFinder: NSObject {
13170
let qms = Array(repeating: "?", count: values.count).joined(separator: ", ")
13271
let sql = "SELECT * FROM \(SignalAccount.databaseTableName) WHERE \(column) in (\(qms))"
13372

134-
/// Why did we use `allSignalAccounts` instead of `SignalAccount.anyFetchAll`?
135-
/// The reason is that the `SignalAccountReadCache` needs to have
136-
/// `didReadSignalAccount` called on it for each record we enumerate, and
137-
/// `SignalAccount.anyEnumerate` has this built in.
13873
return allSignalAccounts(
13974
tx: tx,
14075
sql: sql,
@@ -149,10 +84,6 @@ public class SignalAccountFinder: NSObject {
14984
) -> SignalAccount? {
15085
let sql = "SELECT * FROM \(SignalAccount.databaseTableName) WHERE \(column) = ? LIMIT 1"
15186

152-
/// Why did we use `allSignalAccounts` instead of `SignalAccount.anyFetchAll`?
153-
/// The reason is that the `SignalAccountReadCache` needs to have
154-
/// `didReadSignalAccount` called on it for each record we enumerate, and
155-
/// `SignalAccount.anyEnumerate` has this built in.
15687
return allSignalAccounts(
15788
tx: tx,
15889
sql: sql,

SignalServiceKit/tests/Account/SignalAccountFinderTest.swift

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,48 @@ class SignalAccountFinderTest: SSKBaseTest {
2020
}
2121
}
2222

23-
private func createAccount(serviceId: ServiceId, phoneNumber: E164?) -> SignalAccount {
23+
private func createAccount(phoneNumber: E164) -> SignalAccount {
2424
write {
25-
let account = SignalAccount(address: SignalServiceAddress(serviceId: serviceId, phoneNumber: phoneNumber?.stringValue))
25+
let account = SignalAccount(phoneNumber: phoneNumber.stringValue)
2626
account.anyInsert(transaction: $0)
2727
return account
2828
}
2929
}
3030

3131
func testFetchAccounts() {
32-
let aci1 = Aci.randomForTesting()
3332
let pn1 = E164("+16505550100")!
34-
let account1 = createAccount(serviceId: aci1, phoneNumber: pn1)
35-
36-
let aci2 = Aci.randomForTesting()
37-
let account2 = createAccount(serviceId: aci2, phoneNumber: nil)
33+
let account1 = createAccount(phoneNumber: pn1)
3834

3935
// Nothing prevents us from creating multiple accounts for the same recipient.
40-
let aci3 = Aci.randomForTesting()
4136
let pn3 = E164("+16505550101")!
42-
let account3 = createAccount(serviceId: aci3, phoneNumber: pn3)
43-
_ = createAccount(serviceId: aci3, phoneNumber: pn3)
37+
let account3 = createAccount(phoneNumber: pn3)
38+
_ = createAccount(phoneNumber: pn3)
4439

4540
// Create an account but don't fetch it.
46-
let aci4 = Aci.randomForTesting()
47-
_ = createAccount(serviceId: aci4, phoneNumber: nil)
48-
49-
// Create a ServiceId without an account.
50-
let aci5 = Aci.randomForTesting()
41+
let pn4 = E164("+16505550102")!
42+
_ = createAccount(phoneNumber: pn4)
5143

52-
// Create an account for a PNI-only contact
53-
let pni6 = Pni.randomForTesting()
54-
let pn6 = E164("+17735550155")!
55-
let account6 = createAccount(serviceId: pni6, phoneNumber: pn6)
44+
// Create a phone number without an account.
45+
let pn5 = E164("+16505550103")!
5646

57-
let addressesToFetch: [SignalServiceAddress] = [
58-
SignalServiceAddress(aci1),
59-
SignalServiceAddress(aci2),
60-
SignalServiceAddress(aci3),
61-
SignalServiceAddress(aci5),
62-
SignalServiceAddress(pni6),
47+
let phoneNumbersToFetch: [E164] = [
48+
pn1,
49+
pn3,
50+
pn5,
6351
]
6452

6553
let expectedAccounts: [SignalAccount?] = [
6654
account1,
67-
account2,
6855
account3,
6956
nil,
70-
account6,
7157
]
7258

7359
read { tx in
7460
let accountFinder = SignalAccountFinder()
75-
let actualAccounts = accountFinder.signalAccounts(for: addressesToFetch, tx: tx)
61+
let actualAccounts = accountFinder.signalAccounts(for: phoneNumbersToFetch.map(\.stringValue), tx: tx)
7662
XCTAssertEqual(
77-
actualAccounts.map { $0?.recipientAddress },
78-
expectedAccounts.map { $0?.recipientAddress }
63+
actualAccounts.map { $0?.recipientPhoneNumber },
64+
expectedAccounts.map { $0?.recipientPhoneNumber }
7965
)
8066
}
8167
}

SignalServiceKit/tests/Contacts/SignalRecipientTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class SignalRecipientTest: SSKBaseTest {
173173
)
174174
newPhoneNumberProfile.anyInsert(transaction: transaction)
175175

176-
let oldAccount = SignalAccount(address: oldAddress)
176+
let oldAccount = SignalAccount(phoneNumber: oldAddress.phoneNumber!)
177177
oldAccount.anyInsert(transaction: transaction)
178178

179179
mergeHighTrust(aci: aci, phoneNumber: oldPhoneNumber, transaction: transaction)
@@ -252,7 +252,7 @@ class SignalRecipientTest: SSKBaseTest {
252252
transaction: transaction
253253
)
254254

255-
let oldAccount = SignalAccount(address: oldAddress)
255+
let oldAccount = SignalAccount(phoneNumber: oldAddress.phoneNumber!)
256256
oldAccount.anyInsert(transaction: transaction)
257257

258258
mergeHighTrust(aci: oldAci, phoneNumber: phoneNumber, transaction: transaction)

0 commit comments

Comments
 (0)