Keep only incfrec

This commit is contained in:
Akhil 2023-08-04 02:52:23 +05:30
parent 2f7d121fef
commit 147ba65a35
No known key found for this signature in database
GPG key ID: A8AB680DBB7F3D45

View file

@ -4,16 +4,21 @@ use Sabre\VObject\Component\VCard;
class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInterface class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInterface
{ {
use Rainloop\Providers\AddressBook\CardDAV;
private const URI = 'webmail'; private const URI = 'webmail';
private $contactsManager; private $contactsManager;
private $key;
function __construct() function __construct()
{ {
$this->contactsManager = \OC::$server->getContactsManager(); $this->contactsManager = \OC::$server->getContactsManager();
foreach ($this->contactsManager->getAddressBooks() as $addressbook) { foreach ($this->contactsManager->getUserAddressBooks() as $addressbook) {
if ($addressbook->getKey() !== self::URI) { if ($addressbook->getUri() !== self::URI) {
$this->contactsManager->unregisterAddressBook($addressbook); $this->contactsManager->unregisterAddressBook($addressbook);
} }
else {
$this->key = $addressbook->getKey();
}
} }
} }
@ -36,9 +41,6 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
} }
public function ContactSave(Contact $oContact) : bool { public function ContactSave(Contact $oContact) : bool {
if ($this->contactsManager->createOrUpdate($oContact->vCard->jsonSerialize(), self::URI)) {
return true;
}
return false; return false;
} }
@ -51,14 +53,7 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
} }
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 {
$options = [ return [];
'offset' => $iOffset,
'limit' => $iLimit
];
$results = $this->contactsManager->search($sSearch, [], $options);
$iResultCount = count($results);
return $this->convertResultsToSnappymailContacts($results);
} }
public function GetContactByEmail(string $sEmail) : ?Contact { public function GetContactByEmail(string $sEmail) : ?Contact {
@ -70,11 +65,7 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
} }
public function GetSuggestions(string $sSearch, int $iLimit = 20) : array { public function GetSuggestions(string $sSearch, int $iLimit = 20) : array {
$options = [ return [];
'limit' => $iLimit
];
$results = $this->contactsManager->search($sSearch, [], $options);
return $this->convertResultsToSnappymailContacts($results);
} }
/** /**
@ -83,26 +74,14 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
*/ */
public function IncFrec(array $aEmails, bool $bCreateAuto = true) : bool { public function IncFrec(array $aEmails, bool $bCreateAuto = true) : bool {
if ($bCreateAuto) { if ($bCreateAuto) {
$properties = [];
foreach ($aEmails as $sEmail => $sAddress) { foreach ($aEmails as $sEmail => $sAddress) {
$oVCard = new VCard; $properties['EMAIL'] = $sAddress;
$oVCard->add('EMAIL', $sEmail);
$sFullName = \trim(\MailSo\Mime\Email::Parse(\trim($sAddress))->GetDisplayName()); $sFullName = \trim(\MailSo\Mime\Email::Parse(\trim($sAddress))->GetDisplayName());
if ('' !== $sFullName) { if ('' !== $sFullName) {
$sFirst = $sLast = ''; $properties['FN'] = $sFullName;
if (false !== \strpos($sFullName, ' ')) {
$aNames = \explode(' ', $sFullName, 2);
$sFirst = isset($aNames[0]) ? $aNames[0] : '';
$sLast = isset($aNames[1]) ? $aNames[1] : '';
} else {
$sFirst = $sFullName;
}
if (\strlen($sFirst) || \strlen($sLast)) {
$oVCard->N = array($sLast, $sFirst, '', '', '');
}
} }
$oContact = new Contact(); $this->contactsManager->createOrUpdate($properties, $this->key);
$oContact->setVCard($oVCard);
$this->ContactSave($oContact);
} }
return true; return true;
} }
@ -113,15 +92,5 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
return ''; return '';
} }
private function convertResultsToSnappymailContacts(array $results = []) {
$contacts = [];
foreach($results as $result) {
$vCard = \Sabre\VObject\Reader::readJson($result);
$contact = new Contact();
$contact->setVCard($vCard);
$contacts[] = $contact;
}
return $contacts;
}
} }