mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-08-26 02:29:20 +03:00
chore: refactor nexcloud plugin- related to NC-addressbook
This commit is contained in:
parent
2e3fbe0a12
commit
552a91acc2
5 changed files with 200 additions and 163 deletions
|
|
@ -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 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -53,63 +60,63 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
|
|||
return $addressBookId['id'];
|
||||
}
|
||||
|
||||
public function IsSupported(): bool
|
||||
public function IsSupported() : bool
|
||||
{
|
||||
// Maybe just return true, contacts app is just a frontend
|
||||
//return \OC::$server->getAppManager()->isEnabledForUser('contacts');
|
||||
return \OC::$server->getAppManager()->isEnabledForUser('contacts');
|
||||
}
|
||||
|
||||
public function SetEmail(string $sEmail) : bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function SetEmail(string $sEmail) : bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function Sync(): bool
|
||||
public function Sync() : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function Export(string $sType = 'vcf'): bool
|
||||
public function Export(string $sType = 'vcf') : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function ContactSave(Contact $oContact): bool
|
||||
public function ContactSave(Contact $oContact) : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function DeleteContacts(array $aContactIds): bool
|
||||
public function DeleteContacts(array $aContactIds) : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function DeleteAllContacts(string $sEmail): bool
|
||||
public function DeleteAllContacts(string $sEmail) : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function GetContacts(int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0): array
|
||||
public function GetContacts(int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0) : array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function GetContactByEmail(string $sEmail): ?Contact
|
||||
public function GetContactByEmail(string $sEmail) : ?Contact
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function GetContactByID($mID, bool $bIsStrID = false): ?Contact
|
||||
public function GetContactByID($mID, bool $bIsStrID = false) : ?Contact
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function GetSuggestions(string $sSearch, int $iLimit = 20): array
|
||||
public function GetSuggestions(string $sSearch, int $iLimit = 20) : array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
private function GetEmailObjects(array $aEmails): array
|
||||
private function GetEmailObjects(array $aEmails) : array
|
||||
{
|
||||
$aEmailsObjects = \array_map(function ($mItem) {
|
||||
$oResult = null;
|
||||
|
|
@ -131,7 +138,7 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
|
|||
* Add/increment email address usage
|
||||
* Handy for "most used" sorting suggestions in PdoAddressBook
|
||||
*/
|
||||
public function IncFrec(array $aEmails, bool $bCreateAuto = true): bool
|
||||
public function IncFrec(array $aEmails, bool $bCreateAuto = true) : bool
|
||||
{
|
||||
if ($bCreateAuto) {
|
||||
$aEmailsObjects = $this->GetEmailObjects($aEmails);
|
||||
|
|
@ -139,53 +146,61 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
|
|||
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());
|
||||
$this->createOrUpdateContact($oEmail);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function Test(): string
|
||||
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 '';
|
||||
}
|
||||
|
||||
private function Account(): \RainLoop\Model\Account
|
||||
private function Account() : \RainLoop\Model\Account
|
||||
{
|
||||
return \RainLoop\Api::Actions()->getAccountFromToken();
|
||||
}
|
||||
|
||||
private function SettingsProvider(): \RainLoop\Providers\Settings
|
||||
private function SettingsProvider() : \RainLoop\Providers\Settings
|
||||
{
|
||||
return \RainLoop\Api::Actions()->SettingsProvider(true);
|
||||
}
|
||||
|
||||
private function Settings(): \RainLoop\Settings
|
||||
private function Settings() : \RainLoop\Settings
|
||||
{
|
||||
return $this->SettingsProvider()->Load($this->Account());
|
||||
}
|
||||
|
||||
private function GetSavedUri(): string
|
||||
private function GetSavedUri() : string
|
||||
{
|
||||
return $this->Settings()->GetConf(self::SETTINGS_KEY, $this->defaultUri);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue