Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private extension GoogleScope {
case .userInfo: return "User Info"
case .mail: return "Gmail"
case .contacts: return "Contacts"
case .otherContacts: return "Other Contacts"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@ final class UserContactsProvider {
return service
}

private enum QueryType {
case contacts, other

func query(searchString: String) -> GTLRPeopleServiceQuery {
switch self {
case .contacts:
let query = GTLRPeopleServiceQuery_PeopleSearchContacts.query()
query.readMask = readMask
query.query = searchString
return query
case .other:
let query = GTLRPeopleServiceQuery_OtherContactsSearch.query()
query.readMask = readMask
query.query = searchString
return query
}
}

var readMask: String {
switch self {
case .contacts, .other: return "emailAddresses"
}
}
}

init(userService: GoogleUserServiceType = GoogleUserService()) {
self.userService = userService

Expand All @@ -43,19 +68,31 @@ final class UserContactsProvider {
private func runWarmupQuery() {
Task {
// Warmup query for google contacts cache
_ = try? await searchContacts(query: "")
_ = await searchContacts(query: "")
}
}
}

extension UserContactsProvider: CloudContactsProvider {
func searchContacts(query: String) async throws -> [String] {
let searchQuery = GTLRPeopleServiceQuery_PeopleSearchContacts.query()
searchQuery.readMask = "names,emailAddresses"
searchQuery.query = query
func searchContacts(query: String) async -> [String] {
let contacts = await searchUserContacts(query: query, type: .contacts)
let otherContacts = await searchUserContacts(query: query, type: .other)
let emails = Set(contacts + otherContacts)
return Array(emails).sorted(by: >)
}
}

extension UserContactsProvider {
private func searchUserContacts(query: String, type: QueryType) async -> [String] {
let query = type.query(searchString: query)

guard let emails = try? await perform(query: query) else { return [] }
return emails
}

return try await withCheckedThrowingContinuation { continuation in
self.peopleService.executeQuery(searchQuery) { _, data, error in
private func perform(query: GTLRPeopleServiceQuery) async throws -> [String] {
try await withCheckedThrowingContinuation { continuation in
self.peopleService.executeQuery(query) { _, data, error in
if let error = error {
continuation.resume(throwing: CloudContactsProviderError.providerError(error))
return
Expand Down
3 changes: 2 additions & 1 deletion FlowCrypt/Functionality/Services/GeneralConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ enum GeneralConstants {
}

enum GoogleScope: CaseIterable {
case userInfo, mail, contacts
case userInfo, mail, contacts, otherContacts

var value: String {
switch self {
case .userInfo: return "https://www.googleapis.com/auth/userinfo.profile"
case .mail: return "https://mail.google.com/"
case .contacts: return "https://www.googleapis.com/auth/contacts"
case .otherContacts: return "https://www.googleapis.com/auth/contacts.other.readonly"
}
}
}
3 changes: 2 additions & 1 deletion FlowCryptAppTests/GeneralConstantsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class GeneralConstantsTest: XCTestCase {
let expectedScope = Set([
"https://www.googleapis.com/auth/userinfo.profile",
"https://mail.google.com/",
"https://www.googleapis.com/auth/contacts"
"https://www.googleapis.com/auth/contacts",
"https://www.googleapis.com/auth/contacts.other.readonly"
])
XCTAssert(currentScope == expectedScope)

Expand Down