Skip to content

Commit

Permalink
chore: refactor nexcloud plugin- related to NC-addressbook
Browse files Browse the repository at this point in the history
  • Loading branch information
Fahim Salam Chowdhury committed Sep 3, 2024
1 parent 2e3fbe0 commit a10e93c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 51 deletions.
64 changes: 40 additions & 24 deletions plugins/nextcloud/NextcloudAddressBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ function __construct(string $defaultUri = 'webmail', string $defaultName = 'WebM
$this->GetSavedAddressBookKey();
}

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

return $this->contactsManager;
}

private function GetSavedAddressBookKey(): string
{
if ($this->ignoreSystemAddressBook) {
foreach ($this->contactsManager->getUserAddressBooks() as $addressBook) {
foreach ($this->getContactsManager()->getUserAddressBooks() as $addressBook) {
if ($addressBook->isSystemAddressBook()) {
$this->contactsManager->unregisterAddressBook($addressBook);
$this->getContactsManager()->unregisterAddressBook($addressBook);
}
}
}
Expand Down Expand Up @@ -60,7 +67,8 @@ public function IsSupported(): bool
return true;
}

public function SetEmail(string $sEmail) : bool {
public function SetEmail(string $sEmail): bool
{
return true;
}

Expand Down Expand Up @@ -139,32 +147,40 @@ public function IncFrec(array $aEmails, bool $bCreateAuto = true): bool
if (!count($aEmailsObjects)) {
return false;
}
foreach ($aEmailsObjects as $oEmail) {
if ('' === \trim($oEmail->GetEmail())) {
continue;
}
$sEmail = \trim($oEmail->GetEmail(true));
$existingResults = $this->contactsManager->search($sEmail, ['EMAIL'], ['strict_search' => true]);

if (!empty($existingResults)) {
continue;
}

$properties = [
'EMAIL' => $sEmail,
'FN' => $sEmail
];

if ('' !== \trim($oEmail->GetDisplayName())) {
$properties['FN'] = $oEmail->GetDisplayName();
}
$this->contactsManager->createOrUpdate($properties, $this->GetSavedAddressBookKey());
foreach ($aEmailsObjects as $oEmail) {
$this->createOrUpdateContact($oEmail);
}

return true;
}

return false;
}

private function createOrUpdateContact($oEmail)
{
if ('' === \trim($oEmail->GetEmail())) {
return;
}
$sEmail = \trim($oEmail->GetEmail(true));
$existingResults = $this->getContactsManager()->search($sEmail, ['EMAIL'], ['strict_search' => true]);

if (!empty($existingResults)) {
return;
}

$properties = [
'EMAIL' => $sEmail,
'FN' => $sEmail
];

if ('' !== \trim($oEmail->GetDisplayName())) {
$properties['FN'] = $oEmail->GetDisplayName();
}
$this->getContactsManager()->createOrUpdate($properties, $this->GetSavedAddressBookKey());
}

public function Test(): string
{
return '';
Expand Down
58 changes: 36 additions & 22 deletions plugins/nextcloud/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,24 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
DESCRIPTION = 'Integrate with Nextcloud v20+',
REQUIRED = '2.36.2';

private const IGNORE_SYSTEM_ADDRESSBOOK_KEY = 'ignoreSystemAddressbook';
private const IGNORE_SYSTEM_ADDRESSBOOK_DEFAULT_VALUE = true;

private const ENABLE_NC_ADDRESSBOOK_KEY = 'enableNcAddressbook';
private const ENABLE_NC_ADDRESSBOOK_DEFAULT_VALUE = false;

private const DISABLE_INHOUSE_ADDRESSBOOK_KEY = 'disableInhouseAddressbook';
private const DISABLE_INHOUSE_ADDRESSBOOK_DEFAULT_VALUE = false;

private const DEFAULT_ADDRESSBOOK_URI_KEY = 'defaultNCAddressbookUri';
private const DEFAULT_ADDRESSBOOK_URI = 'webmail';

private const DEFAULT_ADDRESSBOOK_NAME_KEY = 'defaultNCAddressbookName';
private const DEFAULT_ADDRESSBOOK_NAME = 'WebMail';

private const DEFAULT_ADDRESSBOOK_DESCRIPTION_KEY = 'defaultNCAddressbookDescription';
private const DEFAULT_ADDRESSBOOK_DESCRIPTION = 'Recipients from snappymail';
private const DEFAULT_ADDRESSBOOK_URI = 'webmail';

private const ADDRESSBOOK_SETTINGS_KEY = 'nextCloudAddressBookUri';

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

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

if ($this->Config()->Get('plugin', 'disableInhouseAddressbook', false)) {
if ($this->Config()->Get('plugin', self::DISABLE_INHOUSE_ADDRESSBOOK_KEY, self::DISABLE_INHOUSE_ADDRESSBOOK_DEFAULT_VALUE)) {
$this->addJs('js/hideInhouseAddressbook.js');
}
} else {
Expand All @@ -70,9 +84,9 @@ public function GetAddressBooks()

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

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

foreach ($contactsManager->getUserAddressBooks() as $addressBook) {
if ($ignoreSystemAddressbook && $addressBook->isSystemAddressBook()) {
Expand Down Expand Up @@ -429,20 +443,20 @@ public function MainFabrica(string $sName, &$mResult)
}
include_once __DIR__ . '/NextcloudContactsSuggestions.php';
$mResult[] = new NextcloudContactsSuggestions(
$this->Config()->Get('plugin', 'ignoreSystemAddressbook', true)
$this->Config()->Get('plugin', self::IGNORE_SYSTEM_ADDRESSBOOK_KEY, self::IGNORE_SYSTEM_ADDRESSBOOK_DEFAULT_VALUE)
);
}

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

$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);
$ignoreSystemAddressbook = $this->Config()->Get('plugin', self::IGNORE_SYSTEM_ADDRESSBOOK_KEY, self::IGNORE_SYSTEM_ADDRESSBOOK_DEFAULT_VALUE);
$defaultName = $this->Config()->Get('plugin', self::DEFAULT_ADDRESSBOOK_NAME_KEY, self::DEFAULT_ADDRESSBOOK_NAME);
$defaultDescription = $this->Config()->Get('plugin', self::DEFAULT_ADDRESSBOOK_DESCRIPTION_KEY, self::DEFAULT_ADDRESSBOOK_DESCRIPTION);
$defaultUri = $this->Config()->Get('plugin', self::DEFAULT_ADDRESSBOOK_URI_KEY, self::DEFAULT_ADDRESSBOOK_URI);

$mResult = new NextcloudAddressBook(
$defaultUri,
Expand All @@ -451,7 +465,7 @@ public function MainFabrica(string $sName, &$mResult)
$ignoreSystemAddressbook
);
}
/*
/*
if ($this->Config()->Get('plugin', 'storage', false) && ('storage' === $sName || 'storage-local' === $sName)) {
require_once __DIR__ . '/storage.php';
$oDriver = new \NextcloudStorage(APP_PRIVATE_DATA.'storage', $sName === 'storage-local');
Expand All @@ -466,9 +480,9 @@ protected function configMapping(): array
\RainLoop\Plugins\Property::NewInstance('suggestions')->SetLabel('Suggestions')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(true),
\RainLoop\Plugins\Property::NewInstance('ignoreSystemAddressbook')->SetLabel('Ignore system addressbook')
\RainLoop\Plugins\Property::NewInstance(self::IGNORE_SYSTEM_ADDRESSBOOK_KEY)->SetLabel('Ignore system addressbook')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(true),
->SetDefaultValue(self::IGNORE_SYSTEM_ADDRESSBOOK_DEFAULT_VALUE),
/*
\RainLoop\Plugins\Property::NewInstance('storage')->SetLabel('Use Nextcloud user ID in config storage path')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
Expand All @@ -478,25 +492,25 @@ protected function configMapping(): array
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(false),

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

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

\RainLoop\Plugins\Property::NewInstance('defaultNCAddressbookName')->SetLabel('Default nextcloud addressbook Name for recipinets')
\RainLoop\Plugins\Property::NewInstance(self::DEFAULT_ADDRESSBOOK_NAME_KEY)->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')
\RainLoop\Plugins\Property::NewInstance(self::DEFAULT_ADDRESSBOOK_DESCRIPTION_KEY)->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.')
\RainLoop\Plugins\Property::NewInstance(self::DISABLE_INHOUSE_ADDRESSBOOK_KEY)->SetLabel('Disable SnappyMail internal addressbook. This is recomended if nextcloud addressbook is being used.')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(false)
->SetDefaultValue(self::DISABLE_INHOUSE_ADDRESSBOOK_DEFAULT_VALUE)
);
}

Expand Down
10 changes: 5 additions & 5 deletions plugins/nextcloud/js/addressbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

const selectElement = Element.fromHTML('<select></select>');

const books = JSON.parse(oData.Result.addressbooks);
books.forEach(book => {
if (book.selected) {
selectElement.append(Element.fromHTML('<option value="' + book.uri + '" selected="selected">' + book.name + '</option>'));
const addressbooks = JSON.parse(oData.Result.addressbooks);
addressbooks.forEach(addressbook => {
if (addressbook.selected) {
selectElement.append(Element.fromHTML('<option value="' + addressbook.uri + '" selected="selected">' + addressbook.name + '</option>'));
} else {
selectElement.append(Element.fromHTML('<option value="' + book.uri + '">' + book.name + '</option>'));
selectElement.append(Element.fromHTML('<option value="' + addressbook.uri + '">' + addressbook.name + '</option>'));
}
});

Expand Down

0 comments on commit a10e93c

Please sign in to comment.