chore: refactor nexcloud plugin- related to NC-addressbook

This commit is contained in:
Fahim Salam Chowdhury 2024-09-03 12:37:37 +06:00
parent 2e3fbe0a12
commit 552a91acc2
5 changed files with 200 additions and 163 deletions

View file

@ -5,15 +5,15 @@ use RainLoop\Providers\AddressBook\Classes\Contact;
class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInterface
{
use RainLoop\Providers\AddressBook\CardDAV;
private $contactsManager;
private const SETTINGS_KEY = 'nextCloudAddressBookUri';
private const SETTINGS_KEY = 'nextcloudAddressBookUri';
private string $defaultUri = 'webmail';
private string $defaultName = 'WebMail';
private string $defaultDescription = 'Recipients from snappymail';
private bool $ignoreSystemAddressBook = true;
private $contactsManager;
function __construct(string $defaultUri = 'webmail', string $defaultName = 'WebMail', string $defaultDescription = 'Recipients from snappymail', bool $ignoreSystemAddressBook = true)
{
@ -25,14 +25,21 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
$this->GetSavedAddressBookKey();
}
private function getContactsManager()
{
if ($this->contactsManager == null) {
$this->contactsManager = \OC::$server->getContactsManager();
}
return $this->contactsManager;
}
private function GetSavedAddressBookKey() : string
{
$this->contactsManager = \OC::$server->getContactsManager();
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);
}
}
}
@ -56,11 +63,11 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
public function IsSupported() : bool
{
// Maybe just return true, contacts app is just a frontend
//return \OC::$server->getAppManager()->isEnabledForUser('contacts');
return true;
return \OC::$server->getAppManager()->isEnabledForUser('contacts');
}
public function SetEmail(string $sEmail) : bool {
public function SetEmail(string $sEmail) : bool
{
return true;
}
@ -139,15 +146,27 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
if (!count($aEmailsObjects)) {
return false;
}
foreach ($aEmailsObjects as $oEmail) {
$this->createOrUpdateContact($oEmail);
}
return true;
}
return false;
}
private function createOrUpdateContact($oEmail)
{
if ('' === \trim($oEmail->GetEmail())) {
continue;
return;
}
$sEmail = \trim($oEmail->GetEmail(true));
$existingResults = $this->contactsManager->search($sEmail, ['EMAIL'], ['strict_search' => true]);
$existingResults = $this->getContactsManager()->search($sEmail, ['EMAIL'], ['strict_search' => true]);
if (!empty($existingResults)) {
continue;
return;
}
$properties = [
@ -158,11 +177,7 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
if ('' !== \trim($oEmail->GetDisplayName())) {
$properties['FN'] = $oEmail->GetDisplayName();
}
$this->contactsManager->createOrUpdate($properties, $this->GetSavedAddressBookKey());
}
return true;
}
return false;
$this->getContactsManager()->createOrUpdate($properties, $this->GetSavedAddressBookKey());
}
public function Test() : string

View file

@ -10,11 +10,25 @@ 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 DEFAULT_ADDRESSBOOK_NAME = 'WebMail';
private const DEFAULT_ADDRESSBOOK_DESCRIPTION = 'Recipients from snappymail';
private const ENABLE_NC_ADDRESSBOOK_KEY = 'enableNcAddressbook';
private const ENABLE_NC_ADDRESSBOOK_DEFAULT_VALUE = false;
private const DISABLE_INHOUSE_ADDRESSBOOK_KEY = 'disableSnappymailContactsUI';
private const DISABLE_INHOUSE_ADDRESSBOOK_DEFAULT_VALUE = false;
private const DEFAULT_ADDRESSBOOK_URI_KEY = 'defaultNCAddressbookUri';
private const DEFAULT_ADDRESSBOOK_URI = 'webmail';
private const ADDRESSBOOK_SETTINGS_KEY = 'nextCloudAddressBookUri';
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 ADDRESSBOOK_SETTINGS_KEY = 'nextcloudAddressBookUri';
public function Init() : void
{
@ -48,13 +62,13 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
$this->addHook('smtp.before-login', 'beforeLogin');
$this->addHook('sieve.before-login', 'beforeLogin');
if ($this->Config()->Get('plugin', 'enableNcAddressbook', false)) {
$this->addJs('js/addressbook.js');
if ($this->Config()->Get('plugin', self::ENABLE_NC_ADDRESSBOOK_KEY, self::ENABLE_NC_ADDRESSBOOK_DEFAULT_VALUE)) {
$this->addJs('js/nextcloudAddressbook.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 {
@ -70,9 +84,9 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
$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()) {
@ -164,8 +178,7 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
// Only login with OIDC access token if
// it is enabled in config, the user is currently logged in with OIDC,
// the current snappymail account is the OIDC account and no account defined explicitly
if (
\OC::$server->getConfig()->getAppValue('snappymail', 'snappymail-autologin-oidc', false)
if (\OC::$server->getConfig()->getAppValue('snappymail', 'snappymail-autologin-oidc', false)
&& \OC::$server->getSession()->get('is_oidc')
&& $sNcEmail === $oSettings->username
&& !$bAccountDefinedExplicitly
@ -429,20 +442,20 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
}
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,
@ -466,9 +479,9 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
\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)
@ -478,25 +491,25 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
->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)
);
}
@ -517,7 +530,8 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
$sFilePathNew = $aFileInfo['dirname'].'/'.
\preg_replace('/\(\d{1,2}\)$/', '', $aFileInfo['filename']).
' ('.$iIndex.')'.
(empty($aFileInfo['extension']) ? '' : '.' . $aFileInfo['extension']);
(empty($aFileInfo['extension']) ? '' : '.'.$aFileInfo['extension'])
;
if (!$oFiles->file_exists($sFilePathNew)) {
return $sFilePathNew;
}

View file

@ -1,38 +0,0 @@
(rl => {
if (rl) {
addEventListener('rl-view-model', e => {
if ('SettingsContacts' === e.detail.viewModelTemplateID) {
const container = e.detail.viewModelDom.querySelector('.form-horizontal');
if (container) {
rl.pluginRemoteRequest((iError, oData) => {
if (!iError) {
const mainDivElement = Element.fromHTML('<div class="control-group">'
+ '<label>' + rl.i18n("NEXTCLOUD/ADDRESS_BOOK") + '</label>'
+ '</div>');
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>'));
} else {
selectElement.append(Element.fromHTML('<option value="' + book.uri + '">' + book.name + '</option>'));
}
});
selectElement.onchange = function() {
rl.pluginRemoteRequest(() => { }, 'NextcloudUpdateAddressBook', {
uri: selectElement.value
});
}
mainDivElement.append(selectElement);
container.append(mainDivElement);
}
}, "NextcloudGetAddressBooks");
}
}
});
}
})(window.rl);

View file

@ -13,7 +13,10 @@
addEventListener('rl-view-model', e => {
if ('SystemDropDown' === e.detail.viewModelTemplateID) {
const container = e.detail.viewModelDom.querySelector('.dropdown-menu');
if (container) {
if (!container) {
return;
}
for (i = 0; i < container.children.length; i++) {
const element = container.children[i];
const attr = element.getAttribute("data-bind");
@ -23,13 +26,15 @@
}
}
}
}
});
addEventListener('rl-view-model', e => {
if ('PopupsCompose' === e.detail.viewModelTemplateID) {
const container = e.detail.viewModelDom.querySelector('.pull-right');
if (container) {
if(!container) {
return;
}
for (i = 0; i < container.children.length; i++) {
const element = container.children[i];
const attr = element.getAttribute("data-bind");
@ -39,7 +44,6 @@
}
}
}
}
});
}
})(window.rl);

View file

@ -0,0 +1,42 @@
(rl => {
if (rl) {
addEventListener('rl-view-model', e => {
if ('SettingsContacts' === e.detail.viewModelTemplateID) {
const container = e.detail.viewModelDom.querySelector('.form-horizontal');
if (!container) {
return;
}
rl.pluginRemoteRequest((iError, oData) => {
if (iError) {
return;
}
const mainDivElement = Element.fromHTML('<div class="control-group">'
+ '<label>' + rl.i18n("NEXTCLOUD/ADDRESS_BOOK") + '</label>'
+ '</div>');
const selectElement = Element.fromHTML('<select></select>');
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="' + addressbook.uri + '">' + addressbook.name + '</option>'));
}
});
selectElement.onchange = function() {
rl.pluginRemoteRequest(() => { }, 'NextcloudUpdateAddressBook', {
uri: selectElement.value
});
}
mainDivElement.append(selectElement);
container.append(mainDivElement);
}, "NextcloudGetAddressBooks");
}
});
}
})(window.rl);