Skip to content

Commit

Permalink
fixup! feat: add example contact on first login
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Jan 24, 2025
1 parent 979db68 commit b6cd5b0
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 52 deletions.
4 changes: 2 additions & 2 deletions apps/dav/lib/Controller/ExampleContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public function __construct(
*/
public function setAppConfig($key, $allow) {
if ($allow === 'yes' && $key === 'enableDefaultContact' && !$this->defaultContactExists()) {
$this->setInitialDefaultContact();
$this->setCard();
}
$this->config->setAppValue(Application::APP_ID, $key, $allow);
return new JSONResponse([], Http::STATUS_OK);
}

public function setDefaultContact($contactData) {
public function setDefaultContact(?string $contactData = null) {
if (!$this->config->getAppValue(Application::APP_ID, 'enableDefaultContact', 'no')) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
Expand Down
8 changes: 7 additions & 1 deletion apps/dav/lib/HookManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\CardDAV\SyncService;
use OCA\DAV\Service\DefaultContactService;
use OCP\Defaults;
use OCP\IUser;
use OCP\IUserManager;
Expand All @@ -36,6 +37,7 @@ public function __construct(
private CalDavBackend $calDav,
private CardDavBackend $cardDav,
private Defaults $themingDefaults,
private DefaultContactService $defaultContactService,
) {
}

Expand Down Expand Up @@ -143,13 +145,17 @@ public function firstLogin(?IUser $user = null) {
}
if ($this->cardDav->getAddressBooksForUserCount($principal) === 0) {
try {
$this->cardDav->createAddressBook($principal, CardDavBackend::PERSONAL_ADDRESSBOOK_URI, [
$addressBookId = $this->cardDav->createAddressBook($principal, CardDavBackend::PERSONAL_ADDRESSBOOK_URI, [
'{DAV:}displayname' => CardDavBackend::PERSONAL_ADDRESSBOOK_NAME,
]);
} catch (\Exception $e) {
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
}
}
if ($addressBookId) {
$this->defaultContactService->createDefaultContact($addressBookId);
}

}
}
}
6 changes: 0 additions & 6 deletions apps/dav/lib/Listener/AddressbookListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
namespace OCA\DAV\Listener;

use OCA\DAV\CardDAV\Activity\Backend as ActivityBackend;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\Events\AddressBookCreatedEvent;
use OCA\DAV\Events\AddressBookDeletedEvent;
use OCA\DAV\Events\AddressBookShareUpdatedEvent;
use OCA\DAV\Events\AddressBookUpdatedEvent;
use OCA\DAV\Service\DefaultContactService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use Psr\Log\LoggerInterface;
Expand All @@ -26,7 +24,6 @@ class AddressbookListener implements IEventListener {
public function __construct(
private ActivityBackend $activityBackend,
private LoggerInterface $logger,
private DefaultContactService $defaultContactService,
) {
}

Expand All @@ -36,9 +33,6 @@ public function handle(Event $event): void {
$this->activityBackend->onAddressbookCreate(
$event->getAddressBookData()
);
if ($event->getAddressBookData()['uri'] === CardDavBackend::PERSONAL_ADDRESSBOOK_URI) {
$this->defaultContactService->createDefaultContact((string)$event->getAddressBookId());
}

$this->logger->debug(
sprintf('Activity generated for new addressbook %d', $event->getAddressBookId())
Expand Down
12 changes: 10 additions & 2 deletions apps/dav/lib/Service/DefaultContactService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@ public function createDefaultContact(string $addressBookId): void {
$newUid = Uuid::v4()->toRfc4122();
$newRev = date('Ymd\THis\Z');
$vcard = \Sabre\VObject\Reader::read($data, \Sabre\VObject\Reader::OPTION_FORGIVING);
$vcard->UID->setValue($newUid);
$vcard->REV->setValue($newRev);
if ($vcard->UID) {
$vcard->UID->setValue($newUid);
} else {
$vcard->createProperty('UID', $newUid);
}
if ($vcard->REV) {
$vcard->REV->setValue($newRev);
} else {
$vcard->createProperty('REV', $newRev);
}

// Level 3 means that the document is invalid
// https://sabre.io/vobject/vcard/#validating-vcard
Expand Down
4 changes: 0 additions & 4 deletions apps/dav/lib/Settings/ExampleContentSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,4 @@ public function getPriority() {
return 10;
}

public function getName(): ?string {
return 'Example Content';
}

}
87 changes: 53 additions & 34 deletions apps/dav/src/views/ExampleContactSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,44 @@
<NcSettingsSection id="exmaple-content"
:name="$t('dav', 'Example Content')"
:description="$t('dav', 'Set example content to be created on new user first login.')">
<p>
<div>
<input id="enable-default-contact"
v-model="enableDefaultContact"
type="checkbox"
class="checkbox"
@change="updateEnableDefaultContact">
<label for="enable-default-contact"> {{ $t('dav',"Default contact is added to the user's own address book on user's first login.") }} </label>
<NcButton v-if="enableDefaultContact"
class="import-button"
type="primary"
@click="toggleModal">
<template #icon>
<IconUpload :size="20" />
</template>
{{ $t('dav', 'Import contact') }}
</NcButton>
<NcDialog :open.sync="isModalOpen"
:name="$t('dav', 'Import contacts')"
:buttons="buttons">
<div>
<p>{{ $t('dav', 'Importing a new .vcf file will delete the existing default contact and replace it with the new one. Do you want to continue?') }}</p>
</div>
</NcDialog>
<input id="example-contact-import"
ref="exampleContactImportInput"
:disabled="loading"
type="file"
accept=".vcf"
class="hidden-visually"
@change="processFile">
</p>
<div v-if="enableDefaultContact" class="buttons">
<NcButton type="primary"
@click="toggleModal">
<template #icon>
<IconUpload :size="20" />
</template>
{{ $t('dav', 'Import contact') }}
</NcButton>
<NcButton type="secondary"
@click="resetContact">
<template #icon>
<IconRestore :size="20" />
</template>
{{ $t('dav', 'Reset to default contact') }}
</NcButton>
</div>
</div>
<NcDialog :open.sync="isModalOpen"
:name="$t('dav', 'Import contacts')"
:buttons="buttons">
<div>
<p>{{ $t('dav', 'Importing a new .vcf file will delete the existing default contact and replace it with the new one. Do you want to continue?') }}</p>
</div>
</NcDialog>
<input id="example-contact-import"
ref="exampleContactImportInput"
:disabled="loading"
type="file"
accept=".vcf"
class="hidden-visually"
@change="processFile">
</NcSettingsSection>
</template>
<script>
Expand All @@ -47,6 +54,7 @@ import { loadState } from '@nextcloud/initial-state'
import { NcDialog, NcButton, NcSettingsSection } from '@nextcloud/vue'
import { showError, showSuccess } from '@nextcloud/dialogs'
import IconUpload from 'vue-material-design-icons/Upload.vue'
import IconRestore from 'vue-material-design-icons/Restore.vue'
import IconCancel from '@mdi/svg/svg/cancel.svg?raw'
import IconCheck from '@mdi/svg/svg/check.svg?raw'

Expand All @@ -59,6 +67,7 @@ export default {
NcButton,
NcSettingsSection,
IconUpload,
IconRestore,
},
data() {
return {
Expand Down Expand Up @@ -95,18 +104,27 @@ export default {
clickImportInput() {
this.$refs.exampleContactImportInput.click()
},
resetContact() {
this.loading = true
axios.put(generateUrl('/apps/dav/api/defaultcontact/contact'))
.then(() => {
showSuccess(this.$t('dav', 'Contact reset successfully'))
})
.catch((error) => {
console.error('Error importing contact:', error)
showError(this.$t('dav', 'Error while resetting contact'))
})
.finally(() => {
this.loading = false
})
},
processFile(event) {
console.log('processFile triggered')
this.loading = true
console.log('Event:', event)

const file = event.target.files[0]
console.log('Selected file:', file)

const reader = new FileReader()

reader.onload = () => {
console.log('File content:', reader.result)
this.isModalOpen = false
axios.put(generateUrl('/apps/dav/api/defaultcontact/contact'), { contactData: reader.result })
.then(() => {
Expand All @@ -127,10 +145,11 @@ export default {
}
</script>
<style lang="scss" scoped>
.import-button {
.buttons {
margin-top: 1rem;
}
.hidden-visually {
display: none;
display: flex;
& > * {
margin-inline-end: 5px;
}
}
</style>
4 changes: 2 additions & 2 deletions dist/dav-settings-example-content.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/dav-settings-example-content.js.map

Large diffs are not rendered by default.

0 comments on commit b6cd5b0

Please sign in to comment.