mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-08-26 02:29:20 +03:00
feat: add addressBook user settings section for nextcloud plugin
This commit is contained in:
parent
957654c746
commit
500b997f91
10 changed files with 232 additions and 73 deletions
|
|
@ -1,38 +1,44 @@
|
|||
<?php
|
||||
|
||||
use RainLoop\Providers\AddressBook\Classes\Contact;
|
||||
|
||||
class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInterface
|
||||
{
|
||||
use RainLoop\Providers\AddressBook\CardDAV;
|
||||
private const URI = 'webmail';
|
||||
private $contactsManager;
|
||||
private $key;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->GetSavedAddressBookKey();
|
||||
}
|
||||
|
||||
private function GetSavedAddressBookKey(): string
|
||||
{
|
||||
$this->contactsManager = \OC::$server->getContactsManager();
|
||||
|
||||
foreach($this->contactsManager->getUserAddressBooks() as $addressBook) {
|
||||
if($addressBook->isSystemAddressBook()) {
|
||||
$this->contactsManager->unregisterAddressBook($addressBook);
|
||||
foreach ($this->contactsManager->getUserAddressBooks() as $addressBook) {
|
||||
if ($addressBook->isSystemAddressBook()) {
|
||||
$this->contactsManager->unregisterAddressBook($addressBook);
|
||||
}
|
||||
}
|
||||
$uid = \OC::$server->getUserSession()->getUser()->getUID();
|
||||
$cardDavBackend = \OC::$server->get(\OCA\DAV\CardDAV\CardDavBackend::class);
|
||||
$principalUri = 'principals/users/'. $uid;
|
||||
$addressBookId = $cardDavBackend->getAddressBooksByUri($principalUri, self::URI);
|
||||
$principalUri = 'principals/users/' . $uid;
|
||||
$uri = $this->GetSavedUri();
|
||||
$addressBookId = $cardDavBackend->getAddressBooksByUri($principalUri, $uri);
|
||||
if ($addressBookId === null) {
|
||||
$addressBookId = $cardDavBackend->createAddressBook($principalUri, self::URI, array_filter([
|
||||
'{DAV:}displayname' => 'Webmail',
|
||||
'{urn:ietf:params:xml:ns:carddav}addressbook-description' => 'Recipients from snappymail',
|
||||
]));
|
||||
$addressBookId = $cardDavBackend->createAddressBook($principalUri, $uri, array_filter([
|
||||
'{DAV:}displayname' => 'WebMail',
|
||||
'{urn:ietf:params:xml:ns:carddav}addressbook-description' => 'Recipients from snappymail',
|
||||
]));
|
||||
} else {
|
||||
$addressBookId = $addressBookId['id'];
|
||||
}
|
||||
$this->key = $addressBookId;
|
||||
return $addressBookId;
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
@ -43,53 +49,63 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
|
|||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
try {
|
||||
$oResult = \MailSo\Mime\Email::Parse(\trim($mItem));
|
||||
}
|
||||
catch (\Throwable $oException) {
|
||||
} catch (\Throwable $oException) {
|
||||
unset($oException);
|
||||
}
|
||||
return $oResult;
|
||||
|
|
@ -105,20 +121,21 @@ 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);
|
||||
|
||||
if (!count($aEmailsObjects)) {
|
||||
return false;
|
||||
}
|
||||
foreach ($aEmailsObjects as $oEmail) {
|
||||
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;
|
||||
}
|
||||
|
|
@ -127,20 +144,39 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
|
|||
'EMAIL' => $sEmail,
|
||||
'FN' => $sEmail
|
||||
];
|
||||
|
||||
|
||||
if ('' !== \trim($oEmail->GetDisplayName())) {
|
||||
$properties['FN'] = $oEmail->GetDisplayName();
|
||||
}
|
||||
$this->contactsManager->createOrUpdate($properties, $this->key);
|
||||
$this->contactsManager->createOrUpdate($properties, $this->GetSavedAddressBookKey());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function Test() : string {
|
||||
public function Test(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
private function Account(): \RainLoop\Model\Account
|
||||
{
|
||||
return \RainLoop\Api::Actions()->getAccountFromToken();
|
||||
}
|
||||
|
||||
private function SettingsProvider(): \RainLoop\Providers\Settings
|
||||
{
|
||||
return \RainLoop\Api::Actions()->SettingsProvider(true);
|
||||
}
|
||||
|
||||
private function Settings(): \RainLoop\Settings
|
||||
{
|
||||
return $this->SettingsProvider()->Load($this->Account());
|
||||
}
|
||||
|
||||
private function GetSavedUri(): string
|
||||
{
|
||||
return $this->Settings()->GetConf('NextCloudAddressBookUri', 'webmail');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue