Skip to content

Commit

Permalink
fixup! feat: default contact
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Jan 20, 2025
1 parent fa9ad1d commit 27d815c
Showing 1 changed file with 50 additions and 52 deletions.
102 changes: 50 additions & 52 deletions lib/Controller/DefaultContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
namespace OCA\Contacts\Controller;

use OCA\Contacts\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\App\IAppManager;
use OCP\Files\NotFoundException;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IRequest;

Expand All @@ -21,8 +21,8 @@ class DefaultContactController extends ApiController {
public function __construct(

Check warning on line 21 in lib/Controller/DefaultContactController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DefaultContactController.php#L21

Added line #L21 was not covered by tests
IRequest $request,
private IConfig $config,
private IAppData $appData,
private IAppManager $appManager
private IAppData $appData,
private IAppManager $appManager,
) {
parent::__construct(Application::APP_ID, $request);

Check warning on line 27 in lib/Controller/DefaultContactController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DefaultContactController.php#L27

Added line #L27 was not covered by tests
}
Expand All @@ -36,58 +36,56 @@ public function __construct(
* @return JSONResponse an empty JSONResponse with respective http status code
*/
public function setAppConfig($allow) {
$key ='enableDefaultContact';
if($allow ==='yes' && !$this->defaultContactExists()){
$this->setInitialDefaultContact();
}
$key = 'enableDefaultContact';
if ($allow === 'yes' && !$this->defaultContactExists()) {
$this->setInitialDefaultContact();

Check warning on line 41 in lib/Controller/DefaultContactController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DefaultContactController.php#L38-L41

Added lines #L38 - L41 were not covered by tests
}
$this->config->setAppValue(Application::APP_ID, $key, $allow);
return new JSONResponse([], Http::STATUS_OK);

Check warning on line 44 in lib/Controller/DefaultContactController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DefaultContactController.php#L43-L44

Added lines #L43 - L44 were not covered by tests
}

public function setDefaultContact($contactData){
if(!$this->config->getAppValue(Application::APP_ID, 'enableDefaultContact', 'yes')){
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
try{
$folder = $this->appData->getFolder('defaultContact');
}
catch(NotFoundException $e){
$folder = $this->appData->newFolder('defaultContact');
}
$file = (!$folder->fileExists('defaultContact.vcf')) ? $folder->newFile('defaultContact.vcf') : $folder->getFile('defaultContact.vcf');
$file->putContent($contactData);
return new JSONResponse([], Http::STATUS_OK);
}
public function setDefaultContact($contactData) {
if (!$this->config->getAppValue(Application::APP_ID, 'enableDefaultContact', 'yes')) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);

Check warning on line 49 in lib/Controller/DefaultContactController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DefaultContactController.php#L47-L49

Added lines #L47 - L49 were not covered by tests
}
try {
$folder = $this->appData->getFolder('defaultContact');
} catch (NotFoundException $e) {
$folder = $this->appData->newFolder('defaultContact');

Check warning on line 54 in lib/Controller/DefaultContactController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DefaultContactController.php#L52-L54

Added lines #L52 - L54 were not covered by tests
}
$file = (!$folder->fileExists('defaultContact.vcf')) ? $folder->newFile('defaultContact.vcf') : $folder->getFile('defaultContact.vcf');
$file->putContent($contactData);
return new JSONResponse([], Http::STATUS_OK);

Check warning on line 58 in lib/Controller/DefaultContactController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DefaultContactController.php#L56-L58

Added lines #L56 - L58 were not covered by tests
}

private function setInitialDefaultContact(){
$cardData = 'BEGIN:VCARD' . PHP_EOL .
'VERSION:3.0' . PHP_EOL .
'PRODID:-//Nextcloud Contacts v' . $this->appManager->getAppVersion('contacts') . PHP_EOL .
'UID: janeDoe' . PHP_EOL .
'FN:Jane Doe' . PHP_EOL .
'ADR;TYPE=HOME:;;123 Street Street;City;State;;Country' . PHP_EOL .
'EMAIL;TYPE=WORK:[email protected]' . PHP_EOL .
'TEL;TYPE=HOME,VOICE:+999999999999' . PHP_EOL .
'TITLE:Manager' . PHP_EOL .
'ORG:Company' . PHP_EOL .
'BDAY;VALUE=DATE:20000101' . PHP_EOL .
'URL;VALUE=URI:https://example.com/' . PHP_EOL .
'REV;VALUE=DATE-AND-OR-TIME:20241227T144820Z' . PHP_EOL .
'END:VCARD';
$folder = $this->appData->getFolder('defaultContact');
$file = $folder->newFile('defaultContact.vcf');
$file->putContent($cardData);
}
private function setInitialDefaultContact() {
$cardData = 'BEGIN:VCARD' . PHP_EOL .
'VERSION:3.0' . PHP_EOL .
'PRODID:-//Nextcloud Contacts v' . $this->appManager->getAppVersion('contacts') . PHP_EOL .
'UID: janeDoe' . PHP_EOL .
'FN:Jane Doe' . PHP_EOL .
'ADR;TYPE=HOME:;;123 Street Street;City;State;;Country' . PHP_EOL .
'EMAIL;TYPE=WORK:[email protected]' . PHP_EOL .
'TEL;TYPE=HOME,VOICE:+999999999999' . PHP_EOL .
'TITLE:Manager' . PHP_EOL .
'ORG:Company' . PHP_EOL .
'BDAY;VALUE=DATE:20000101' . PHP_EOL .
'URL;VALUE=URI:https://example.com/' . PHP_EOL .
'REV;VALUE=DATE-AND-OR-TIME:20241227T144820Z' . PHP_EOL .
'END:VCARD';
$folder = $this->appData->getFolder('defaultContact');
$file = $folder->newFile('defaultContact.vcf');
$file->putContent($cardData);

Check warning on line 78 in lib/Controller/DefaultContactController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DefaultContactController.php#L61-L78

Added lines #L61 - L78 were not covered by tests
}

private function defaultContactExists(): bool {
try{
$folder = $this->appData->getFolder('defaultContact');
}
catch(NotFoundException $e){
$this->appData->newFolder('defaultContact');
return false;
}
return $folder->fileExists('defaultContact.vcf');
}
private function defaultContactExists(): bool {

Check warning on line 81 in lib/Controller/DefaultContactController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DefaultContactController.php#L81

Added line #L81 was not covered by tests
try {
$folder = $this->appData->getFolder('defaultContact');
} catch (NotFoundException $e) {
$this->appData->newFolder('defaultContact');
return false;

Check warning on line 86 in lib/Controller/DefaultContactController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DefaultContactController.php#L83-L86

Added lines #L83 - L86 were not covered by tests
}
return $folder->fileExists('defaultContact.vcf');

Check warning on line 88 in lib/Controller/DefaultContactController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DefaultContactController.php#L88

Added line #L88 was not covered by tests
}

}
}

0 comments on commit 27d815c

Please sign in to comment.