@@ -54,24 +54,12 @@ public function __construct(
5454 */
5555 #[NoAdminRequired]
5656 #[ApiRoute('POST ' , '/api/v1/import ' )]
57- public function import (
58- int $ fileId ,
59- ?string $ addressBookKey = null ,
60- ?string $ addressBookUri = null ,
61- ): DataResponse {
62- // This is a negated xor in disguise (both or neither is empty)
63- if (empty ($ addressBookKey ) === empty ($ addressBookUri )) {
64- return new DataResponse (
65- 'Expected one of addressBookKey or addressBookUri ' ,
66- Http::STATUS_BAD_REQUEST ,
67- );
68- }
69-
57+ public function import (int $ fileId , ?string $ addressBookUri = null ): DataResponse {
7058 if ($ this ->userId === null ) {
7159 return new DataResponse ('Not logged in ' , Http::STATUS_UNAUTHORIZED );
7260 }
7361
74- $ addressBook = $ this ->findUserAddressBook ($ addressBookKey , $ addressBookUri );
62+ $ addressBook = $ this ->findUserAddressBook ($ addressBookUri );
7563 if ($ addressBook === null ) {
7664 return new DataResponse ('Address book not found ' , Http::STATUS_NOT_FOUND );
7765 }
@@ -168,23 +156,25 @@ public function import(
168156 ]);
169157 }
170158
171- private function findUserAddressBook (?string $ key , ?string $ uri ): ?ICreateContactFromString {
172- $ addressBooks = $ this ->contactsManager ->getUserAddressBooks ();
159+ private function findUserAddressBook (?string $ uri ): ?ICreateContactFromString {
160+ /** @var ICreateContactFromString[] $addressBooks */
161+ $ addressBooks = array_filter (
162+ $ this ->contactsManager ->getUserAddressBooks (),
163+ static fn ($ addressBook ) => $ addressBook instanceof ICreateContactFromString,
164+ );
173165
174- // Try the given address book first
175- if ($ key !== null || $ uri !== null ) {
166+ // Try the given address book by URI first
167+ if ($ uri !== null ) {
176168 foreach ($ addressBooks as $ addressBook ) {
177- if (!($ addressBook instanceof ICreateContactFromString)) {
178- continue ;
179- }
180-
181- if ($ addressBook ->getKey () === $ key || $ addressBook ->getUri () === $ uri ) {
169+ if ($ addressBook ->getUri () === $ uri ) {
182170 return $ addressBook ;
183171 }
184172 }
173+
174+ return null ;
185175 }
186176
187- // Try to find the default address book (named contacts)
177+ // Try to find the user's default address book
188178 foreach ($ addressBooks as $ addressBook ) {
189179 if ($ addressBook ->getUri () === CardDavBackend::PERSONAL_ADDRESSBOOK_URI ) {
190180 return $ addressBook ;
0 commit comments