1818use OCA \DAV \CardDAV \CardDavBackend ;
1919use OCA \Google \AppInfo \Application ;
2020use OCP \Contacts \IManager as IContactManager ;
21+ use OCP \IConfig ;
2122use Psr \Log \LoggerInterface ;
2223use Sabre \VObject \Component \VCard ;
2324use Throwable ;
@@ -33,6 +34,7 @@ public function __construct(
3334 private IContactManager $ contactsManager ,
3435 private CardDavBackend $ cdBackend ,
3536 private GoogleAPIService $ googleApiService ,
37+ private IConfig $ config ,
3638 ) {
3739 }
3840
@@ -72,29 +74,41 @@ public function getContactGroupsById(string $userId): array {
7274 * @return array
7375 */
7476 public function getContactNumber (string $ userId ): array {
75- $ nbContacts = 0 ;
7677 $ params = [
7778 'personFields ' => implode (', ' , [
7879 'names ' ,
7980 ]),
80- 'pageSize ' => 100 ,
8181 ];
82- do {
83- $ result = $ this ->googleApiService ->request ($ userId , 'v1/people/me/connections ' , $ params , 'GET ' , 'https://people.googleapis.com/ ' );
84- if (isset ($ result ['error ' ])) {
85- return $ result ;
82+ $ result = [];
83+ $ contacts = $ this ->googleApiService ->request ($ userId , 'v1/people/me/connections ' , $ params , 'GET ' , 'https://people.googleapis.com/ ' );
84+ if (isset ($ contacts ['error ' ])) {
85+ return $ contacts ;
86+ }
87+ $ result ['nbContacts ' ] = $ contacts ['totalItems ' ] ?? 0 ;
88+ $ scopes = $ this ->config ->getUserValue ($ userId , Application::APP_ID , 'user_scopes ' , '{} ' );
89+ $ scopes = json_decode ($ scopes , true );
90+ if (isset ($ scopes ['can_access_other_contacts ' ]) && $ scopes ['can_access_other_contacts ' ] === 1 ) {
91+ $ params = [
92+ 'readMask ' => implode (', ' , [
93+ 'names ' ,
94+ 'emailAddresses ' ,
95+ ]),
96+ ];
97+ $ otherContacts = $ this ->googleApiService ->request ($ userId , 'v1/otherContacts ' , $ params , 'GET ' , 'https://people.googleapis.com/ ' );
98+ if (isset ($ otherContacts ['error ' ])) {
99+ return $ otherContacts ;
86100 }
87- $ nbContacts += count ($ result ['connections ' ] ?? []);
88- $ params ['pageToken ' ] = $ result ['nextPageToken ' ] ?? '' ;
89- } while (isset ($ result ['nextPageToken ' ]));
90- return ['nbContacts ' => $ nbContacts ];
101+ $ result ['nbOtherContacts ' ] = $ otherContacts ['totalSize ' ] ?? 0 ;
102+ }
103+ return $ result ;
91104 }
92105
93106 /**
94107 * @param string $userId
108+ * @param bool $otherContacts
95109 * @return Generator
96110 */
97- public function getContactList (string $ userId ): Generator {
111+ public function getContactList (string $ userId, bool $ otherContacts = false ): Generator {
98112 $ params = [
99113 'personFields ' => implode (', ' , [
100114 'addresses ' ,
@@ -127,6 +141,31 @@ public function getContactList(string $userId): Generator {
127141 }
128142 $ params ['pageToken ' ] = $ result ['nextPageToken ' ] ?? '' ;
129143 } while (isset ($ result ['nextPageToken ' ]));
144+ if ($ otherContacts ) {
145+ $ params = [
146+ 'readMask ' => implode (', ' , [
147+ 'emailAddresses ' ,
148+ 'metadata ' ,
149+ 'names ' ,
150+ 'phoneNumbers ' ,
151+ 'photos ' ,
152+ ]),
153+ 'sources ' => 'READ_SOURCE_TYPE_CONTACT ' ,
154+ 'pageSize ' => 100 ,
155+ ];
156+ do {
157+ $ result = $ this ->googleApiService ->request ($ userId , 'v1/otherContacts ' , $ params , 'GET ' , 'https://people.googleapis.com/ ' );
158+ if (isset ($ result ['error ' ])) {
159+ return $ result ;
160+ }
161+ if (isset ($ result ['otherContacts ' ]) && is_array ($ result ['otherContacts ' ])) {
162+ foreach ($ result ['otherContacts ' ] as $ contact ) {
163+ yield $ contact ;
164+ }
165+ }
166+ $ params ['pageToken ' ] = $ result ['nextPageToken ' ] ?? '' ;
167+ } while (isset ($ result ['nextPageToken ' ]));
168+ }
130169 return [];
131170 }
132171
@@ -166,9 +205,9 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
166205 }
167206 $ existingAddressBook = $ addressBook ;
168207 }
169-
208+ $ otherContacts = $ this -> config -> getUserValue ( $ userId , Application:: APP_ID , ' consider_other_contacts ' , ' 0 ' ) === ' 1 ' ;
170209 $ groupsById = $ this ->getContactGroupsById ($ userId );
171- $ contacts = $ this ->getContactList ($ userId );
210+ $ contacts = $ this ->getContactList ($ userId, $ otherContacts );
172211 $ nbAdded = 0 ;
173212 $ nbUpdated = 0 ;
174213 $ totalContactNumber = 0 ;
0 commit comments