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 552a91a
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 160 deletions.
105 changes: 60 additions & 45 deletions plugins/nextcloud/NextcloudAddressBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
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)
{
Expand All @@ -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 All @@ -53,63 +60,63 @@ private function GetSavedAddressBookKey(): string
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 true;
return \OC::$server->getAppManager()->isEnabledForUser('contacts');
}

public function SetEmail(string $sEmail) : bool {
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;
Expand All @@ -131,61 +138,69 @@ private function GetEmailObjects(array $aEmails): array
* 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);

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;
}

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);
}
Expand Down
Loading

0 comments on commit 552a91a

Please sign in to comment.