Skip to content

Commit

Permalink
feat: add admin settings for nextcloudAddressbook related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fahim Salam Chowdhury committed Sep 3, 2024
1 parent bda1c28 commit 19ddcfb
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 17 deletions.
28 changes: 20 additions & 8 deletions plugins/nextcloud/NextcloudAddressBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,33 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
use RainLoop\Providers\AddressBook\CardDAV;
private $contactsManager;

private const DEFAULT_URI = 'webmail';
private const SETTINGS_KEY = 'nextCloudAddressBookUri';

function __construct()
private string $defaultUri = 'webmail';
private string $defaultName = 'WebMail';
private string $defaultDescription = 'Recipients from snappymail';
private bool $ignoreSystemAddressBook = true;


function __construct(string $defaultUri = 'webmail', string $defaultName = 'WebMail', string $defaultDescription = 'Recipients from snappymail', bool $ignoreSystemAddressBook = true)
{
$this->defaultUri = $defaultUri;
$this->defaultName = $defaultName;
$this->defaultDescription = $defaultDescription;
$this->ignoreSystemAddressBook = $ignoreSystemAddressBook;

$this->GetSavedAddressBookKey();
}

private function GetSavedAddressBookKey(): string
{
$this->contactsManager = \OC::$server->getContactsManager();

foreach ($this->contactsManager->getUserAddressBooks() as $addressBook) {
if ($addressBook->isSystemAddressBook()) {
$this->contactsManager->unregisterAddressBook($addressBook);
if ($this->ignoreSystemAddressBook) {
foreach ($this->contactsManager->getUserAddressBooks() as $addressBook) {
if ($addressBook->isSystemAddressBook()) {
$this->contactsManager->unregisterAddressBook($addressBook);
}
}
}

Expand All @@ -33,8 +45,8 @@ private function GetSavedAddressBookKey(): string

if ($addressBookId === null) {
return $cardDavBackend->createAddressBook($principalUri, $uri, array_filter([
'{DAV:}displayname' => 'WebMail',
'{urn:ietf:params:xml:ns:carddav}addressbook-description' => 'Recipients from snappymail',
'{DAV:}displayname' => $this->defaultName,
'{urn:ietf:params:xml:ns:carddav}addressbook-description' => $this->defaultDescription,
]));
}

Expand Down Expand Up @@ -181,6 +193,6 @@ private function Settings(): \RainLoop\Settings

private function GetSavedUri(): string
{
return $this->Settings()->GetConf(self::SETTINGS_KEY, self::DEFAULT_URI);
return $this->Settings()->GetConf(self::SETTINGS_KEY, $this->defaultUri);
}
}
60 changes: 51 additions & 9 deletions plugins/nextcloud/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
REQUIRED = '2.36.2';


private const DEFAULT_ADDRESSBOOK_NAME = 'WebMail';
private const DEFAULT_ADDRESSBOOK_DESCRIPTION = 'Recipients from snappymail';
private const DEFAULT_ADDRESSBOOK_URI = 'webmail';
private const ADDRESSBOOK_SETTINGS_KEY = 'nextCloudAddressBookUri';

Expand Down Expand Up @@ -46,9 +48,15 @@ public function Init(): void
$this->addHook('smtp.before-login', 'beforeLogin');
$this->addHook('sieve.before-login', 'beforeLogin');

$this->addJs('js/addressbook.js');
$this->addJsonHook('NextcloudGetAddressBooks', 'GetAddressBooks');
$this->addJsonHook('NextcloudUpdateAddressBook', 'UpdateAddressBook');
if ($this->Config()->Get('plugin', 'enableNcAddressbook', false)) {
$this->addJs('js/addressbook.js');
$this->addJsonHook('NextcloudGetAddressBooks', 'GetAddressBooks');
$this->addJsonHook('NextcloudUpdateAddressBook', 'UpdateAddressBook');
}

if ($this->Config()->Get('plugin', 'disableInhouseAddressbook', false)) {
$this->addJs('js/hideInhouseAddressbook.js');
}
} else {
\SnappyMail\Log::debug('Nextcloud', 'NOT integrated');
// \OC::$server->getConfig()->getAppValue('snappymail', 'snappymail-no-embed');
Expand All @@ -62,10 +70,12 @@ public function GetAddressBooks()

$contactsManager = \OC::$server->getContactsManager();

$selectedUri = $this->Settings()->GetConf(self::ADDRESSBOOK_SETTINGS_KEY, self::DEFAULT_ADDRESSBOOK_URI);
$defaultUri = $this->Config()->Get('plugin', 'defaultNCAddressbookUri', self::DEFAULT_ADDRESSBOOK_URI);
$selectedUri = $this->UserSettings()->GetConf(self::ADDRESSBOOK_SETTINGS_KEY, $defaultUri);
$ignoreSystemAddressbook = $this->Config()->Get('plugin', 'ignoreSystemAddressbook', true);

foreach ($contactsManager->getUserAddressBooks() as $addressBook) {
if ($addressBook->isSystemAddressBook()) {
if ($ignoreSystemAddressbook && $addressBook->isSystemAddressBook()) {
$contactsManager->unregisterAddressBook($addressBook);
continue;
}
Expand All @@ -89,7 +99,7 @@ public function GetAddressBooks()
public function UpdateAddressBook(): array
{
$uri = $this->jsonParam('uri');
$oSettings = $this->Settings();
$oSettings = $this->UserSettings();
if (\is_string($uri)) {
$oSettings->SetConf(self::ADDRESSBOOK_SETTINGS_KEY, $uri);
$this->SettingsProvider()->Save($this->Account(), $oSettings);
Expand Down Expand Up @@ -422,12 +432,24 @@ public function MainFabrica(string $sName, &$mResult)
$this->Config()->Get('plugin', 'ignoreSystemAddressbook', true)
);
}
if ('address-book' === $sName) {

if ('address-book' === $sName && $this->Config()->Get('plugin', 'enableNcAddressbook', false)) {
if (!\is_array($mResult)) {
$mResult = array();
}
include_once __DIR__ . '/NextcloudAddressBook.php';
$mResult = new NextcloudAddressBook();

$ignoreSystemAddressbook = $this->Config()->Get('plugin', 'ignoreSystemAddressbook', true);
$defaultName = $this->Config()->Get('plugin', 'defaultNCAddressbookName', self::DEFAULT_ADDRESSBOOK_NAME);
$defaultDescription = $this->Config()->Get('plugin', 'defaultNCAddressbookDescription', self::DEFAULT_ADDRESSBOOK_DESCRIPTION);
$defaultUri = $this->Config()->Get('plugin', 'defaultNCAddressbookUri', self::DEFAULT_ADDRESSBOOK_URI);

$mResult = new NextcloudAddressBook(
$defaultUri,
$defaultName,
$defaultDescription,
$ignoreSystemAddressbook
);
}
/*
if ($this->Config()->Get('plugin', 'storage', false) && ('storage' === $sName || 'storage-local' === $sName)) {
Expand All @@ -453,6 +475,26 @@ protected function configMapping(): array
->SetDefaultValue(false)
*/
\RainLoop\Plugins\Property::NewInstance('calendar')->SetLabel('Enable "Put ICS in calendar"')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(false),

\RainLoop\Plugins\Property::NewInstance('enableNcAddressbook')->SetLabel('Enable User to choose Nextcloud addressbook for recipients')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(false),

\RainLoop\Plugins\Property::NewInstance('defaultNCAddressbookUri')->SetLabel('Default nextcloud addressbook URI for recipinets')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING)
->SetDefaultValue('webmail'),

\RainLoop\Plugins\Property::NewInstance('defaultNCAddressbookName')->SetLabel('Default nextcloud addressbook Name for recipinets')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING)
->SetDefaultValue(self::DEFAULT_ADDRESSBOOK_NAME),

\RainLoop\Plugins\Property::NewInstance('defaultNCAddressbookDescription')->SetLabel('Default nextcloud addressbook description for recipinets')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING)
->SetDefaultValue(self::DEFAULT_ADDRESSBOOK_DESCRIPTION),

\RainLoop\Plugins\Property::NewInstance('disableInhouseAddressbook')->SetLabel('Disable SnappyMail internal addressbook. This is recomended if nextcloud addressbook is being used.')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(false)
);
Expand Down Expand Up @@ -496,7 +538,7 @@ private function SettingsProvider(): \RainLoop\Providers\Settings
return \RainLoop\Api::Actions()->SettingsProvider(true);
}

private function Settings(): \RainLoop\Settings
private function UserSettings(): \RainLoop\Settings
{
return $this->SettingsProvider()->Load($this->Account());
}
Expand Down
45 changes: 45 additions & 0 deletions plugins/nextcloud/js/hideInhouseAddressbook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(rl => {
if (rl) {
addEventListener('rl-view-model', e => {
if ('MailFolderList' === e.detail.viewModelTemplateID) {
const container = e.detail.viewModelDom.querySelector('.buttonContacts');
if (container) {
container.remove();
}
}
});


addEventListener('rl-view-model', e => {
if ('SystemDropDown' === e.detail.viewModelTemplateID) {
const container = e.detail.viewModelDom.querySelector('.dropdown-menu');
if (container) {
for (i = 0; i < container.children.length; i++) {
const element = container.children[i];
const attr = element.getAttribute("data-bind");
if (attr && attr.includes("visible: allowContacts")) {
element.remove();
break;
}
}
}
}
});

addEventListener('rl-view-model', e => {
if ('PopupsCompose' === e.detail.viewModelTemplateID) {
const container = e.detail.viewModelDom.querySelector('.pull-right');
if (container) {
for (i = 0; i < container.children.length; i++) {
const element = container.children[i];
const attr = element.getAttribute("data-bind");
if (attr && attr.includes("visible: allowContacts")) {
element.remove();
break;
}
}
}
}
});
}
})(window.rl);

0 comments on commit 19ddcfb

Please sign in to comment.