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

Commit 3d55bda

Browse files
author
the-djmaze
committed
Added test contacts CardDAV settings
1 parent 2809e75 commit 3d55bda

File tree

7 files changed

+87
-25
lines changed

7 files changed

+87
-25
lines changed

dev/External/ko.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const
1515
* based solely on the values of other observables in the application
1616
*/
1717
koComputable = fn => ko.computed(fn, {'pure':true}),
18+
// koObservable = value => ko.observable(value),
1819

1920
addObservablesTo = (target, observables) =>
2021
forEachObjectEntry(observables, (key, value) =>

dev/Settings/User/Contacts.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import ko from 'ko';
22
import { koComputable } from 'External/ko';
33

44
import { SettingsGet } from 'Common/Globals';
5-
import { i18n, translateTrigger } from 'Common/Translator';
5+
import { i18n, translateTrigger, getErrorMessage } from 'Common/Translator';
66
import { ContactUserStore } from 'Stores/User/Contact';
77
import Remote from 'Remote/User/Fetch';
88

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

1920
this.syncModeOptions = koComputable(() => {
2021
translateTrigger();
@@ -48,4 +49,15 @@ export class UserSettingsContacts /*extends AbstractViewSettings*/ {
4849
})
4950
);
5051
}
52+
53+
test() {
54+
this.syncError('');
55+
Remote.request('TestContactsSyncData', (iError, data) => {
56+
iError && this.syncError(data.messageAdditional || data.message || getErrorMessage(iError, data));
57+
}, {
58+
Url: ContactUserStore.syncUrl(),
59+
User: ContactUserStore.syncUser(),
60+
Password: ContactUserStore.syncPass()
61+
})
62+
}
5163
}

snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Contacts.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
namespace RainLoop\Actions;
44

55
use RainLoop\Enumerations\Capa;
6+
use RainLoop\Exceptions\ClientException;
67

78
trait Contacts
89
{
9-
/**
10-
* @var \RainLoop\Providers\AddressBook
11-
*/
12-
protected $oAddressBookProvider = null;
10+
protected ?\RainLoop\Providers\AddressBook $oAddressBookProvider = null;
1311

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

62+
public function DoTestContactsSyncData() : array
63+
{
64+
if (!$this->GetCapa(Capa::CONTACTS)) {
65+
throw new ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'Disallowed');
66+
}
67+
68+
$oAccount = $this->getAccountFromToken();
69+
70+
$sPassword = $this->GetActionParam('Password', '');
71+
if (static::APP_DUMMY === $sPassword) {
72+
$mData = $this->getContactsSyncData($oAccount);
73+
$sPassword = isset($mData['Password']) ? $mData['Password'] : '';
74+
}
75+
$sPasswordHMAC = null;
76+
if ($sPassword) {
77+
$oMainAccount = $this->getMainAccountFromToken();
78+
$sPassword = \SnappyMail\Crypt::EncryptToJSON($sPassword, $oMainAccount->CryptKey());
79+
if ($sPassword) {
80+
$sPasswordHMAC = \hash_hmac('sha1', $sPassword, $oMainAccount->CryptKey());
81+
}
82+
}
83+
84+
$oDriver = $this->fabrica('address-book', $oAccount);
85+
if (!$oDriver) {
86+
throw new ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'No driver');
87+
}
88+
$oDriver->SetEmail($this->GetMainEmail($oAccount));
89+
$oDriver->setDAVClientConfig([
90+
'Mode' => 2, // readonly
91+
'User' => $this->GetActionParam('User', ''),
92+
'Password' => $sPassword,
93+
'Url' => $this->GetActionParam('Url', ''),
94+
'PasswordHMAC' => $sPasswordHMAC
95+
]);
96+
97+
$oClient = $oDriver->getDavClient();
98+
if (!$oClient) {
99+
throw new ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'No client');
100+
}
101+
$oClient->propFind($oClient->urlPath, [
102+
'{DAV:}getlastmodified',
103+
'{DAV:}resourcetype',
104+
'{DAV:}getetag'
105+
], 1);
106+
107+
return $this->TrueResponse();
108+
}
109+
64110
public function DoContactsSync() : array
65111
{
66112
$oAccount = $this->getAccountFromToken();
67113
$oAddressBookProvider = $this->AddressBookProvider($oAccount);
68114
if (!$oAddressBookProvider) {
69-
throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'No AddressBookProvider');
115+
throw new ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'No AddressBookProvider');
70116
}
71117
\ignore_user_abort(true);
72118
\SnappyMail\HTTP\Stream::start(/*$binary = false*/);
73119
\SnappyMail\HTTP\Stream::JSON(['messsage'=>'start']);
74120
if (!$oAddressBookProvider->Sync()) {
75-
throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'AddressBookProvider->Sync() failed');
121+
throw new ClientException(\RainLoop\Notifications::ContactsSyncError, null, 'AddressBookProvider->Sync() failed');
76122
}
77123
return $this->TrueResponse();
78124
}

snappymail/v/0.0.0/app/libraries/RainLoop/Providers/AddressBook.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
class AddressBook extends AbstractProvider
66
{
7-
/**
8-
* @var \RainLoop\Providers\AddressBook\AddressBookInterface
9-
*/
10-
private $oDriver;
7+
private ?AddressBook\AddressBookInterface $oDriver;
118

129
public function __construct(?AddressBook\AddressBookInterface $oDriver)
1310
{

snappymail/v/0.0.0/app/libraries/RainLoop/Providers/AddressBook/CardDAV.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,9 @@ private function getContactsPaths(DAVClient $oClient, string $sPath, string $sUs
265265
*/
266266
private function checkContactsPath(DAVClient $oClient, string $sPath) : bool
267267
{
268-
$aResponse = null;
269-
try
270-
{
271-
$aResponse = $oClient->propFind($sPath, array(
272-
'{DAV:}resourcetype'
273-
), 1);
274-
}
275-
catch (\Throwable $oException)
276-
{
277-
$this->logException($oException);
278-
}
268+
$aResponse = $oClient->propFind($sPath, array(
269+
'{DAV:}resourcetype'
270+
), 1);
279271

280272
$bGood = false;
281273
if (\is_array($aResponse)) {
@@ -339,7 +331,7 @@ private function getDavClientFromUrl(string $sUrl, string $sUser,
339331
return $oClient;
340332
}
341333

342-
protected function getDavClient() : ?DAVClient
334+
public function getDavClient() : ?DAVClient
343335
{
344336
if (!$this->aDAVConfig['Mode']) {
345337
return null;
@@ -405,7 +397,11 @@ protected function getDavClient() : ?DAVClient
405397

406398
$bGood = $sNewPath && $this->checkContactsPath($oClient, $sNewPath);
407399
if (!$bGood) {
408-
$this->logWrite('Contacts path not found at: '.$sPath, \LOG_INFO, 'DAV');
400+
throw new \RainLoop\Exceptions\ClientException(
401+
\RainLoop\Notifications::ContactsSyncError,
402+
null,
403+
'Contacts path not found at: '.$sPath
404+
);
409405
}
410406
}
411407

snappymail/v/0.0.0/app/libraries/RainLoop/Providers/AddressBook/PdoAddressBook.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ public function Sync() : bool
129129
return false;
130130
}
131131

132-
$oClient = $this->getDavClient();
132+
try {
133+
$oClient = $this->getDavClient();
134+
} catch (\Throwable $e) {
135+
\SnappyMail\Log::error('DAV', $e->getMessage());
136+
// $this->logException($oException);
137+
}
133138
if (!$oClient) {
134139
\SnappyMail\Log::warning('PdoAddressBook', 'Sync() invalid DavClient');
135140
return false;

snappymail/v/0.0.0/app/templates/Views/User/SettingsContacts.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@
3939
<input type="password" autocomplete="current-password" autocorrect="off" autocapitalize="off"
4040
spellcheck="false" data-bind="value: syncPass">
4141
</div>
42+
<div class="control-group" data-bind="hidden: !syncUrl()">
43+
<label></label>
44+
<button class="btn" data-i18n="GLOBAL/TEST" data-bind="click: test"></button>
45+
</div>
46+
<div class="alert alert-error" data-bind="visible: syncError, text: syncError"></div>
4247
</form>

0 commit comments

Comments
 (0)