mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-06 16:07:03 +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,14 +1,18 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use RainLoop\Providers\AddressBook\Classes\Contact;
|
use RainLoop\Providers\AddressBook\Classes\Contact;
|
||||||
|
|
||||||
class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInterface
|
class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInterface
|
||||||
{
|
{
|
||||||
use RainLoop\Providers\AddressBook\CardDAV;
|
use RainLoop\Providers\AddressBook\CardDAV;
|
||||||
private const URI = 'webmail';
|
|
||||||
private $contactsManager;
|
private $contactsManager;
|
||||||
private $key;
|
|
||||||
|
|
||||||
function __construct()
|
function __construct()
|
||||||
|
{
|
||||||
|
$this->GetSavedAddressBookKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function GetSavedAddressBookKey(): string
|
||||||
{
|
{
|
||||||
$this->contactsManager = \OC::$server->getContactsManager();
|
$this->contactsManager = \OC::$server->getContactsManager();
|
||||||
|
|
||||||
|
|
@ -20,19 +24,21 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
|
||||||
$uid = \OC::$server->getUserSession()->getUser()->getUID();
|
$uid = \OC::$server->getUserSession()->getUser()->getUID();
|
||||||
$cardDavBackend = \OC::$server->get(\OCA\DAV\CardDAV\CardDavBackend::class);
|
$cardDavBackend = \OC::$server->get(\OCA\DAV\CardDAV\CardDavBackend::class);
|
||||||
$principalUri = 'principals/users/' . $uid;
|
$principalUri = 'principals/users/' . $uid;
|
||||||
$addressBookId = $cardDavBackend->getAddressBooksByUri($principalUri, self::URI);
|
$uri = $this->GetSavedUri();
|
||||||
|
$addressBookId = $cardDavBackend->getAddressBooksByUri($principalUri, $uri);
|
||||||
if ($addressBookId === null) {
|
if ($addressBookId === null) {
|
||||||
$addressBookId = $cardDavBackend->createAddressBook($principalUri, self::URI, array_filter([
|
$addressBookId = $cardDavBackend->createAddressBook($principalUri, $uri, array_filter([
|
||||||
'{DAV:}displayname' => 'Webmail',
|
'{DAV:}displayname' => 'WebMail',
|
||||||
'{urn:ietf:params:xml:ns:carddav}addressbook-description' => 'Recipients from snappymail',
|
'{urn:ietf:params:xml:ns:carddav}addressbook-description' => 'Recipients from snappymail',
|
||||||
]));
|
]));
|
||||||
} else {
|
} else {
|
||||||
$addressBookId = $addressBookId['id'];
|
$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
|
// Maybe just return true, contacts app is just a frontend
|
||||||
//return \OC::$server->getAppManager()->isEnabledForUser('contacts');
|
//return \OC::$server->getAppManager()->isEnabledForUser('contacts');
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -43,53 +49,63 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function SetEmail(string $sEmail) : bool {
|
public function SetEmail(string $sEmail): bool
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Sync() : bool {
|
public function Sync(): bool
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Export(string $sType = 'vcf') : bool {
|
public function Export(string $sType = 'vcf'): bool
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ContactSave(Contact $oContact) : bool {
|
public function ContactSave(Contact $oContact): bool
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function DeleteContacts(array $aContactIds) : bool {
|
public function DeleteContacts(array $aContactIds): bool
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function DeleteAllContacts(string $sEmail) : bool {
|
public function DeleteAllContacts(string $sEmail): bool
|
||||||
|
{
|
||||||
return false;
|
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 [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function GetContactByEmail(string $sEmail) : ?Contact {
|
public function GetContactByEmail(string $sEmail): ?Contact
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function GetContactByID($mID, bool $bIsStrID = false) : ?Contact {
|
public function GetContactByID($mID, bool $bIsStrID = false): ?Contact
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function GetSuggestions(string $sSearch, int $iLimit = 20) : array {
|
public function GetSuggestions(string $sSearch, int $iLimit = 20): array
|
||||||
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function GetEmailObjects(array $aEmails) : array {
|
private function GetEmailObjects(array $aEmails): array
|
||||||
|
{
|
||||||
$aEmailsObjects = \array_map(function ($mItem) {
|
$aEmailsObjects = \array_map(function ($mItem) {
|
||||||
$oResult = null;
|
$oResult = null;
|
||||||
try {
|
try {
|
||||||
$oResult = \MailSo\Mime\Email::Parse(\trim($mItem));
|
$oResult = \MailSo\Mime\Email::Parse(\trim($mItem));
|
||||||
}
|
} catch (\Throwable $oException) {
|
||||||
catch (\Throwable $oException) {
|
|
||||||
unset($oException);
|
unset($oException);
|
||||||
}
|
}
|
||||||
return $oResult;
|
return $oResult;
|
||||||
|
|
@ -105,7 +121,8 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
|
||||||
* Add/increment email address usage
|
* Add/increment email address usage
|
||||||
* Handy for "most used" sorting suggestions in PdoAddressBook
|
* 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) {
|
if ($bCreateAuto) {
|
||||||
$aEmailsObjects = $this->GetEmailObjects($aEmails);
|
$aEmailsObjects = $this->GetEmailObjects($aEmails);
|
||||||
|
|
||||||
|
|
@ -131,16 +148,35 @@ class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBoo
|
||||||
if ('' !== \trim($oEmail->GetDisplayName())) {
|
if ('' !== \trim($oEmail->GetDisplayName())) {
|
||||||
$properties['FN'] = $oEmail->GetDisplayName();
|
$properties['FN'] = $oEmail->GetDisplayName();
|
||||||
}
|
}
|
||||||
$this->contactsManager->createOrUpdate($properties, $this->key);
|
$this->contactsManager->createOrUpdate($properties, $this->GetSavedAddressBookKey());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Test() : string {
|
public function Test(): string
|
||||||
|
{
|
||||||
return '';
|
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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,11 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
|
||||||
$this->addHook('imap.before-login', 'beforeLogin');
|
$this->addHook('imap.before-login', 'beforeLogin');
|
||||||
$this->addHook('smtp.before-login', 'beforeLogin');
|
$this->addHook('smtp.before-login', 'beforeLogin');
|
||||||
$this->addHook('sieve.before-login', 'beforeLogin');
|
$this->addHook('sieve.before-login', 'beforeLogin');
|
||||||
|
|
||||||
|
$this->addJs('js/addressbook.js'); // add js file
|
||||||
|
$this->addTemplate('templates/AddressBookSettings.html');
|
||||||
|
$this->addJsonHook('JsonGetAddressbooks', 'GetAddressBooks');
|
||||||
|
$this->addJsonHook('NextcloudUpdateAddressBook', 'UpdateAddressBook');
|
||||||
} else {
|
} else {
|
||||||
\SnappyMail\Log::debug('Nextcloud', 'NOT integrated');
|
\SnappyMail\Log::debug('Nextcloud', 'NOT integrated');
|
||||||
// \OC::$server->getConfig()->getAppValue('snappymail', 'snappymail-no-embed');
|
// \OC::$server->getConfig()->getAppValue('snappymail', 'snappymail-no-embed');
|
||||||
|
|
@ -48,6 +53,48 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function GetAddressBooks()
|
||||||
|
{
|
||||||
|
$addressBooks = [];
|
||||||
|
|
||||||
|
$contactsManager = \OC::$server->getContactsManager();
|
||||||
|
|
||||||
|
$selectedUri = $this->Settings()->GetConf('NextCloudAddressBookUri', 'webmail');
|
||||||
|
|
||||||
|
foreach ($contactsManager->getUserAddressBooks() as $addressBook) {
|
||||||
|
if ($addressBook->isSystemAddressBook()) {
|
||||||
|
$contactsManager->unregisterAddressBook($addressBook);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$book = new AddressBook();
|
||||||
|
$book->uri = $addressBook->getUri();
|
||||||
|
$book->name = $addressBook->getDisplayName();
|
||||||
|
if (strcmp($selectedUri, $book->uri) === 0) {
|
||||||
|
$book->selected = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$addressBooks[] = $book;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return $this->jsonResponse(__FUNCTION__, array(
|
||||||
|
'addressbooks' => json_encode($addressBooks)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function UpdateAddressBook(): array
|
||||||
|
{
|
||||||
|
$uri = $this->jsonParam('uri');
|
||||||
|
$oSettings = $this->Settings();
|
||||||
|
if (\is_string($uri)) {
|
||||||
|
$oSettings->SetConf('NextCloudAddressBookUri', $uri);
|
||||||
|
$this->SettingsProvider()->Save($this->Account(), $oSettings);
|
||||||
|
}
|
||||||
|
return $this->jsonResponse(__FUNCTION__, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function ContentSecurityPolicy(\SnappyMail\HTTP\CSP $CSP)
|
public function ContentSecurityPolicy(\SnappyMail\HTTP\CSP $CSP)
|
||||||
{
|
{
|
||||||
if (\method_exists($CSP, 'add')) {
|
if (\method_exists($CSP, 'add')) {
|
||||||
|
|
@ -104,7 +151,8 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
|
||||||
// Only login with OIDC access token if
|
// Only login with OIDC access token if
|
||||||
// it is enabled in config, the user is currently logged in with OIDC,
|
// 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
|
// 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')
|
&& \OC::$server->getSession()->get('is_oidc')
|
||||||
&& $sNcEmail === $oSettings->username
|
&& $sNcEmail === $oSettings->username
|
||||||
&& !$bAccountDefinedExplicitly
|
&& !$bAccountDefinedExplicitly
|
||||||
|
|
@ -424,8 +472,7 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
|
||||||
$sFilePathNew = $aFileInfo['dirname'] . '/' .
|
$sFilePathNew = $aFileInfo['dirname'] . '/' .
|
||||||
\preg_replace('/\(\d{1,2}\)$/', '', $aFileInfo['filename']) .
|
\preg_replace('/\(\d{1,2}\)$/', '', $aFileInfo['filename']) .
|
||||||
' (' . $iIndex . ')' .
|
' (' . $iIndex . ')' .
|
||||||
(empty($aFileInfo['extension']) ? '' : '.'.$aFileInfo['extension'])
|
(empty($aFileInfo['extension']) ? '' : '.' . $aFileInfo['extension']);
|
||||||
;
|
|
||||||
if (!$oFiles->file_exists($sFilePathNew)) {
|
if (!$oFiles->file_exists($sFilePathNew)) {
|
||||||
return $sFilePathNew;
|
return $sFilePathNew;
|
||||||
}
|
}
|
||||||
|
|
@ -435,4 +482,26 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
|
||||||
}
|
}
|
||||||
return $sFilePath;
|
return $sFilePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AddressBook
|
||||||
|
{
|
||||||
|
public string $uri;
|
||||||
|
public string $name;
|
||||||
|
public bool $selected = false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
36
plugins/nextcloud/js/addressbook.js
Normal file
36
plugins/nextcloud/js/addressbook.js
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
(rl => {
|
||||||
|
if (rl) {
|
||||||
|
class AddressBookSettings /* extends AbstractViewSettings */ {
|
||||||
|
constructor() {
|
||||||
|
this.addressBookList = ko.observableArray();
|
||||||
|
this.selectedAddressBook = ko.observable();
|
||||||
|
|
||||||
|
rl.pluginRemoteRequest((iError, oData) => {
|
||||||
|
if (!iError) {
|
||||||
|
const books = JSON.parse(oData.Result.addressbooks);
|
||||||
|
books.forEach(book => {
|
||||||
|
this.addressBookList.push(book);
|
||||||
|
|
||||||
|
if (book.selected) {
|
||||||
|
this.selectedAddressBook(book.uri);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.selectedAddressBook.subscribe(value => {
|
||||||
|
rl.pluginRemoteRequest(() => { }, 'NextcloudUpdateAddressBook', {
|
||||||
|
uri: value
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, "JsonGetAddressbooks");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.addSettingsViewModel(
|
||||||
|
AddressBookSettings,
|
||||||
|
'AddressBookSettings',
|
||||||
|
'NEXTCLOUD/ADDRESS_BOOK',
|
||||||
|
'addressbook'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
})(window.rl);
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
"SELECT_CALENDAR": "Kalender auswählen",
|
"SELECT_CALENDAR": "Kalender auswählen",
|
||||||
"FILE_ATTACH": "anfügen",
|
"FILE_ATTACH": "anfügen",
|
||||||
"FILE_INTERNAL": "intern",
|
"FILE_INTERNAL": "intern",
|
||||||
"FILE_PUBLIC": "öffentlich"
|
"FILE_PUBLIC": "öffentlich",
|
||||||
|
"ADDRESS_BOOK": "Address Book"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
"SELECT_CALENDAR": "Select calendar",
|
"SELECT_CALENDAR": "Select calendar",
|
||||||
"FILE_ATTACH": "attach",
|
"FILE_ATTACH": "attach",
|
||||||
"FILE_INTERNAL": "internal",
|
"FILE_INTERNAL": "internal",
|
||||||
"FILE_PUBLIC": "public"
|
"FILE_PUBLIC": "public",
|
||||||
|
"ADDRESS_BOOK": "Address Book"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
"SELECT_CALENDAR": "Wybierz kalendarz",
|
"SELECT_CALENDAR": "Wybierz kalendarz",
|
||||||
"FILE_ATTACH": "dołącz",
|
"FILE_ATTACH": "dołącz",
|
||||||
"FILE_INTERNAL": "link wewnętrzny",
|
"FILE_INTERNAL": "link wewnętrzny",
|
||||||
"FILE_PUBLIC": "link publiczny"
|
"FILE_PUBLIC": "link publiczny",
|
||||||
|
"ADDRESS_BOOK": "Address Book"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
"SELECT_CALENDAR": "Выбрать календарь",
|
"SELECT_CALENDAR": "Выбрать календарь",
|
||||||
"FILE_ATTACH": "Прикрепить с ПК",
|
"FILE_ATTACH": "Прикрепить с ПК",
|
||||||
"FILE_INTERNAL": "Внутреняя",
|
"FILE_INTERNAL": "Внутреняя",
|
||||||
"FILE_PUBLIC": "Публичная"
|
"FILE_PUBLIC": "Публичная",
|
||||||
|
"ADDRESS_BOOK": "Address Book"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
"SELECT_CALENDAR": "選擇日曆",
|
"SELECT_CALENDAR": "選擇日曆",
|
||||||
"FILE_ATTACH": "附加",
|
"FILE_ATTACH": "附加",
|
||||||
"FILE_INTERNAL": "內部",
|
"FILE_INTERNAL": "內部",
|
||||||
"FILE_PUBLIC": "公開"
|
"FILE_PUBLIC": "公開",
|
||||||
|
"ADDRESS_BOOK": "Address Book"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
"SELECT_CALENDAR": "选择日历",
|
"SELECT_CALENDAR": "选择日历",
|
||||||
"FILE_ATTACH": "附加",
|
"FILE_ATTACH": "附加",
|
||||||
"FILE_INTERNAL": "内部",
|
"FILE_INTERNAL": "内部",
|
||||||
"FILE_PUBLIC": "公开"
|
"FILE_PUBLIC": "公开",
|
||||||
|
"ADDRESS_BOOK": "Address Book"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
plugins/nextcloud/templates/AddressBookSettings.html
Normal file
12
plugins/nextcloud/templates/AddressBookSettings.html
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
<div class="b-settings-nextcloud-addressbook form-horizontal">
|
||||||
|
<div class="legend">AddressBook</div>
|
||||||
|
<div class="form-horizontal">
|
||||||
|
|
||||||
|
<div class="control-group">
|
||||||
|
<label data-i18n="NEXTCLOUD/ADDRESS_BOOK"></label>
|
||||||
|
<select data-bind="options: addressBookList, value: selectedAddressBook,
|
||||||
|
optionsText: 'name', optionsValue: 'uri'"></select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue