Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Added test contacts CardDAV settings
Browse files Browse the repository at this point in the history
  • Loading branch information
the-djmaze committed Oct 29, 2024
1 parent 2809e75 commit 3d55bda
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 25 deletions.
1 change: 1 addition & 0 deletions dev/External/ko.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const
* based solely on the values of other observables in the application
*/
koComputable = fn => ko.computed(fn, {'pure':true}),
// koObservable = value => ko.observable(value),

addObservablesTo = (target, observables) =>
forEachObjectEntry(observables, (key, value) =>
Expand Down
14 changes: 13 additions & 1 deletion dev/Settings/User/Contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ko from 'ko';
import { koComputable } from 'External/ko';

import { SettingsGet } from 'Common/Globals';
import { i18n, translateTrigger } from 'Common/Translator';
import { i18n, translateTrigger, getErrorMessage } from 'Common/Translator';
import { ContactUserStore } from 'Stores/User/Contact';
import Remote from 'Remote/User/Fetch';

Expand All @@ -15,6 +15,7 @@ export class UserSettingsContacts /*extends AbstractViewSettings*/ {
this.syncUrl = ContactUserStore.syncUrl;
this.syncUser = ContactUserStore.syncUser;
this.syncPass = ContactUserStore.syncPass;
this.syncError = ko.observable('');

this.syncModeOptions = koComputable(() => {
translateTrigger();
Expand Down Expand Up @@ -48,4 +49,15 @@ export class UserSettingsContacts /*extends AbstractViewSettings*/ {
})
);
}

test() {
this.syncError('');
Remote.request('TestContactsSyncData', (iError, data) => {
iError && this.syncError(data.messageAdditional || data.message || getErrorMessage(iError, data));
}, {
Url: ContactUserStore.syncUrl(),
User: ContactUserStore.syncUser(),
Password: ContactUserStore.syncPass()
})
}
}
58 changes: 52 additions & 6 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
namespace RainLoop\Actions;

use RainLoop\Enumerations\Capa;
use RainLoop\Exceptions\ClientException;

trait Contacts
{
/**
* @var \RainLoop\Providers\AddressBook
*/
protected $oAddressBookProvider = null;
protected ?\RainLoop\Providers\AddressBook $oAddressBookProvider = null;

public function AddressBookProvider(?\RainLoop\Model\Account $oAccount = null): \RainLoop\Providers\AddressBook
{
Expand Down Expand Up @@ -61,18 +59,66 @@ public function DoSaveContactsSyncData() : array
return $this->DefaultResponse($bResult);
}

public function DoTestContactsSyncData() : array
{
if (!$this->GetCapa(Capa::CONTACTS)) {
throw new ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'Disallowed');
}

$oAccount = $this->getAccountFromToken();

$sPassword = $this->GetActionParam('Password', '');
if (static::APP_DUMMY === $sPassword) {
$mData = $this->getContactsSyncData($oAccount);
$sPassword = isset($mData['Password']) ? $mData['Password'] : '';
}
$sPasswordHMAC = null;
if ($sPassword) {
$oMainAccount = $this->getMainAccountFromToken();
$sPassword = \SnappyMail\Crypt::EncryptToJSON($sPassword, $oMainAccount->CryptKey());
if ($sPassword) {
$sPasswordHMAC = \hash_hmac('sha1', $sPassword, $oMainAccount->CryptKey());
}
}

$oDriver = $this->fabrica('address-book', $oAccount);
if (!$oDriver) {
throw new ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'No driver');
}
$oDriver->SetEmail($this->GetMainEmail($oAccount));
$oDriver->setDAVClientConfig([
'Mode' => 2, // readonly
'User' => $this->GetActionParam('User', ''),
'Password' => $sPassword,
'Url' => $this->GetActionParam('Url', ''),
'PasswordHMAC' => $sPasswordHMAC
]);

$oClient = $oDriver->getDavClient();
if (!$oClient) {
throw new ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'No client');
}
$oClient->propFind($oClient->urlPath, [
'{DAV:}getlastmodified',
'{DAV:}resourcetype',
'{DAV:}getetag'
], 1);

return $this->TrueResponse();
}

public function DoContactsSync() : array
{
$oAccount = $this->getAccountFromToken();
$oAddressBookProvider = $this->AddressBookProvider($oAccount);
if (!$oAddressBookProvider) {
throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'No AddressBookProvider');
throw new ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'No AddressBookProvider');
}
\ignore_user_abort(true);
\SnappyMail\HTTP\Stream::start(/*$binary = false*/);
\SnappyMail\HTTP\Stream::JSON(['messsage'=>'start']);
if (!$oAddressBookProvider->Sync()) {
throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'AddressBookProvider->Sync() failed');
throw new ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'AddressBookProvider->Sync() failed');
}
return $this->TrueResponse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

class AddressBook extends AbstractProvider
{
/**
* @var \RainLoop\Providers\AddressBook\AddressBookInterface
*/
private $oDriver;
private ?AddressBook\AddressBookInterface $oDriver;

public function __construct(?AddressBook\AddressBookInterface $oDriver)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,9 @@ private function getContactsPaths(DAVClient $oClient, string $sPath, string $sUs
*/
private function checkContactsPath(DAVClient $oClient, string $sPath) : bool
{
$aResponse = null;
try
{
$aResponse = $oClient->propFind($sPath, array(
'{DAV:}resourcetype'
), 1);
}
catch (\Throwable $oException)
{
$this->logException($oException);
}
$aResponse = $oClient->propFind($sPath, array(
'{DAV:}resourcetype'
), 1);

$bGood = false;
if (\is_array($aResponse)) {
Expand Down Expand Up @@ -339,7 +331,7 @@ private function getDavClientFromUrl(string $sUrl, string $sUser,
return $oClient;
}

protected function getDavClient() : ?DAVClient
public function getDavClient() : ?DAVClient
{
if (!$this->aDAVConfig['Mode']) {
return null;
Expand Down Expand Up @@ -405,7 +397,11 @@ protected function getDavClient() : ?DAVClient

$bGood = $sNewPath && $this->checkContactsPath($oClient, $sNewPath);
if (!$bGood) {
$this->logWrite('Contacts path not found at: '.$sPath, \LOG_INFO, 'DAV');
throw new \RainLoop\Exceptions\ClientException(
\RainLoop\Notifications::ContactsSyncError,
null,
'Contacts path not found at: '.$sPath
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ public function Sync() : bool
return false;
}

$oClient = $this->getDavClient();
try {
$oClient = $this->getDavClient();
} catch (\Throwable $e) {
\SnappyMail\Log::error('DAV', $e->getMessage());
// $this->logException($oException);
}
if (!$oClient) {
\SnappyMail\Log::warning('PdoAddressBook', 'Sync() invalid DavClient');
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@
<input type="password" autocomplete="current-password" autocorrect="off" autocapitalize="off"
spellcheck="false" data-bind="value: syncPass">
</div>
<div class="control-group" data-bind="hidden: !syncUrl()">
<label></label>
<button class="btn" data-i18n="GLOBAL/TEST" data-bind="click: test"></button>
</div>
<div class="alert alert-error" data-bind="visible: syncError, text: syncError"></div>
</form>

0 comments on commit 3d55bda

Please sign in to comment.