Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: carddav client #3851

Merged
merged 121 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 101 commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
9c44609
first steps
asbiin Mar 22, 2020
2722649
update
asbiin Mar 23, 2020
be78bc5
update
asbiin Mar 24, 2020
4fca040
update
asbiin Mar 24, 2020
8cb2aea
update
asbiin Mar 24, 2020
96b56aa
update
asbiin Mar 26, 2020
1836146
Add notion of addressbooks
asbiin Mar 26, 2020
cfbd9ae
fixes
asbiin Mar 26, 2020
974c51d
Apply fixes from StyleCI
asbiin Mar 26, 2020
d3f6cb3
fix this
asbiin Mar 26, 2020
6aa31d8
Merge branch 'master' into 2020-03-26-addressbooks
asbiin Mar 27, 2020
1554413
update
asbiin Mar 29, 2020
e18e49d
Merge branch 'master' into 2020-03-26-addressbooks
asbiin Mar 29, 2020
20a2fa5
Apply fixes from StyleCI
asbiin Mar 29, 2020
47a113c
update
asbiin Mar 30, 2020
475fa3a
update
asbiin Mar 30, 2020
729264d
Merge remote-tracking branch 'origin/master' into 2020-03-22-carddav-…
asbiin Mar 30, 2020
ab6aeb3
add changelog
asbiin Mar 30, 2020
c331cd3
Merge remote-tracking branch 'origin/master' into 2020-03-26-addressb…
asbiin Apr 1, 2020
59c1db8
Merge remote-tracking branch 'origin/master' into 2020-03-26-addressb…
asbiin Apr 7, 2020
bf19272
Merge remote-tracking branch 'origin/master' into 2020-03-26-addressb…
asbiin Apr 13, 2020
10d57b1
add addressBookContacts function
asbiin Apr 14, 2020
e6bf337
Apply fixes from StyleCI
asbiin Apr 14, 2020
9125c17
fixes
asbiin Apr 14, 2020
0998cc4
fix searches
asbiin Apr 14, 2020
65637fb
Apply fixes from StyleCI
asbiin Apr 14, 2020
f865f99
fixes
asbiin Apr 14, 2020
3442e72
Merge branch '2020-03-26-addressbooks' of github.com:monicahq/monica …
asbiin Apr 14, 2020
2929fda
change id column name
asbiin Apr 14, 2020
1d122a1
change address book name column
asbiin Apr 14, 2020
4c40ff7
fix search
asbiin Apr 15, 2020
9972f23
Update CardDAVBackend.php
asbiin Apr 16, 2020
64d38e0
change address book name parameter
asbiin Apr 16, 2020
3c124a9
Merge branch '2020-03-26-addressbooks' into 2020-03-22-carddav-client
asbiin Apr 16, 2020
31a89be
Apply fixes from StyleCI
asbiin Apr 16, 2020
6c1f71b
Merge remote-tracking branch 'origin/master' into 2020-03-26-addressb…
asbiin Apr 16, 2020
ebf69fe
fixes
asbiin Apr 16, 2020
ef985af
fix
asbiin Apr 17, 2020
ab5cb71
fix search
asbiin Apr 17, 2020
73443b5
Apply fixes from StyleCI
asbiin Apr 17, 2020
c92976a
Merge remote-tracking branch 'origin/master' into 2020-03-26-addressb…
asbiin Apr 17, 2020
0b55387
Merge branch '2020-03-26-addressbooks' into 2020-03-22-carddav-client
asbiin Apr 17, 2020
268f693
update
asbiin Apr 18, 2020
6325d6f
implement method
asbiin Apr 18, 2020
3a34dbe
add etag
asbiin Apr 18, 2020
2f0d778
add AddressBookSynchronizer
asbiin Apr 18, 2020
2cb20bd
lot of fixes
asbiin Apr 18, 2020
fb83986
Apply fixes from StyleCI
asbiin Apr 18, 2020
d64d7af
Merge remote-tracking branch 'origin/master' into 2020-03-26-addressb…
asbiin Apr 19, 2020
30bb327
fix
asbiin Apr 19, 2020
ee9d1cc
Merge branch '2020-03-26-addressbooks' into 2020-03-22-carddav-client
asbiin Apr 19, 2020
512ae19
Merge remote-tracking branch 'origin/master' into 2020-03-22-carddav-…
asbiin Apr 19, 2020
bcce09b
Try to implement Google API, but Google carddav implementation sucks
asbiin Apr 19, 2020
e955415
Apply fixes from StyleCI
asbiin Apr 19, 2020
55b75d6
Merge remote-tracking branch 'origin/master' into 2020-03-22-carddav-…
asbiin Dec 6, 2020
c815faa
Apply fixes from StyleCI
asbiin Dec 6, 2020
36f1dae
fix phpstan
asbiin Dec 6, 2020
3339fea
Apply fixes from StyleCI
asbiin Dec 6, 2020
1c2333f
fix migration
asbiin Dec 6, 2020
e238c6c
fixes
asbiin Dec 7, 2020
a640d74
Apply fixes from StyleCI
asbiin Dec 7, 2020
94b7fd7
Merge branch 'master' into 2020-03-22-carddav-client
asbiin Dec 24, 2020
1639f6f
Apply fixes from StyleCI
asbiin Dec 24, 2020
e81089e
revert old changes
asbiin Dec 24, 2020
25f3477
idem
asbiin Dec 24, 2020
359646e
fix
asbiin Dec 24, 2020
a5be26c
fix refreshObject declaration
asbiin Dec 24, 2020
4c57e4b
Merge remote-tracking branch 'origin/master' into 2020-03-22-carddav-…
asbiin Sep 1, 2021
36e4736
fix
asbiin Sep 6, 2021
692fb11
add tests
asbiin Sep 10, 2021
3e1122a
Apply fixes from StyleCI
asbiin Sep 10, 2021
5ab3541
more tests
asbiin Sep 10, 2021
0fb211e
Apply fixes from StyleCI
asbiin Sep 10, 2021
4168814
tests
asbiin Sep 10, 2021
836085d
Apply fixes from StyleCI
asbiin Sep 10, 2021
4443feb
fix test
asbiin Sep 11, 2021
b214a4f
update
asbiin Sep 11, 2021
8f560ed
Apply fixes from StyleCI
asbiin Sep 11, 2021
c3a5a46
simplification
asbiin Sep 11, 2021
358843d
Apply fixes from StyleCI
asbiin Sep 11, 2021
375f208
update all
asbiin Sep 11, 2021
05cadf5
Apply fixes from StyleCI
asbiin Sep 11, 2021
ab1c96a
more tests
asbiin Sep 12, 2021
ed52649
Apply fixes from StyleCI
asbiin Sep 12, 2021
adc3f8a
last tests
asbiin Sep 12, 2021
9b17e94
Apply fixes from StyleCI
asbiin Sep 12, 2021
51c318b
Merge branch 'master' into 2020-03-22-carddav-client
asbiin Sep 12, 2021
693104d
Apply suggestions from code review
asbiin Sep 12, 2021
47011ad
improve support
asbiin Sep 15, 2021
eadc1c7
Apply fixes from StyleCI
asbiin Sep 15, 2021
67795c2
fix phpstan
asbiin Sep 15, 2021
b995fb0
Apply fixes from StyleCI
asbiin Sep 15, 2021
94435ab
fix phpstan
asbiin Sep 15, 2021
dd0dc0c
add test
asbiin Sep 16, 2021
1ae429e
fix test
asbiin Sep 16, 2021
b122341
Apply fixes from StyleCI
asbiin Sep 16, 2021
b6d01f2
Merge remote-tracking branch 'origin/master' into 2020-03-22-carddav-…
asbiin Sep 17, 2021
057275e
add tests
asbiin Sep 17, 2021
cbc1e4c
Apply fixes from StyleCI
asbiin Sep 17, 2021
31a90bf
Merge branch 'master' into 2020-03-22-carddav-client
asbiin Sep 18, 2021
d0c2738
use factory
asbiin Sep 18, 2021
d78fe88
fix reviews
asbiin Sep 18, 2021
c7f40d4
chain promise
asbiin Sep 19, 2021
55f827d
Apply fixes from StyleCI
asbiin Sep 19, 2021
dc42189
improve return
asbiin Sep 19, 2021
d5b2752
Merge branch '2020-03-22-carddav-client' of github.com:monicahq/monic…
asbiin Sep 19, 2021
e0aa28a
Apply fixes from StyleCI
asbiin Sep 19, 2021
7311718
simplification
asbiin Sep 19, 2021
a54d022
fix proppatch
asbiin Sep 19, 2021
2a3a248
improve getSupportedReportSetAsync
asbiin Sep 19, 2021
e0c6137
Apply fixes from StyleCI
asbiin Sep 19, 2021
d024c45
use empty
asbiin Sep 19, 2021
815c692
Merge branch '2020-03-22-carddav-client' of github.com:monicahq/monic…
asbiin Sep 19, 2021
374c740
add more typing
asbiin Sep 19, 2021
e3ccb35
Apply fixes from StyleCI
asbiin Sep 19, 2021
7f29cac
rename files
asbiin Sep 19, 2021
f059dd3
consistency
asbiin Sep 19, 2021
6e115c3
fix scheme
asbiin Sep 19, 2021
673f2db
create AddressBookContactsPushMissed
asbiin Sep 20, 2021
adfff5b
fix doc
asbiin Sep 20, 2021
05eb1f3
Apply fixes from StyleCI
asbiin Sep 20, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions app/Console/Commands/AddAddressBookSubscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Console\Commands;

use App\Models\User\User;
use Illuminate\Console\Command;
use App\Services\DavClient\AddAddressBook;

class AddAddressBookSubscription extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'monica:newaddressbooksubscription
{--email= : Monica account to add subscription to}
{--url= : CardDAV url of the address book}
{--login= : Login}
{--password= : Password of the account}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Add a new all dav subscriptions';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$user = User::where('email', $this->option('email'))->firstOrFail();

$url = $this->option('url') ?? $this->ask('url', 'CardDAV url of the address book');
$login = $this->option('login') ?? $this->ask('login', 'Login name');
$password = $this->option('password') ?? $this->ask('password', 'User password');

try {
$addressBook = app(AddAddressBook::class)->execute([
'account_id' => $user->account_id,
'user_id' => $user->id,
'base_uri' => $url,
'username' => $login,
'password' => $password,
]);
} catch (\Exception $e) {
$this->error($e->getMessage());
}

if (! isset($addressBook)) {
$this->error('Could not add subscription');
} else {
$this->info('Subscription added');
}
}
}
42 changes: 42 additions & 0 deletions app/Console/Commands/DavClientsUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Jobs\SynchronizeAddressBooks;
use App\Models\Account\AddressBookSubscription;

class DavClientsUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'monica:davclients';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update all dav subscriptions';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$subscriptions = AddressBookSubscription::active()->get();

$now = now();
$subscriptions->filter(function ($subscription) use ($now) {
return is_null($subscription->last_synchronized_at)
|| $subscription->last_synchronized_at->addMinutes($subscription->frequency)->lessThan($now);
asbiin marked this conversation as resolved.
Show resolved Hide resolved
})->each(function ($subscription) {
SynchronizeAddressBooks::dispatch($subscription);
});
}
}
1 change: 1 addition & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected function schedule(Schedule $schedule)
{
$this->scheduleCommand($schedule, 'send:reminders', 'hourly');
$this->scheduleCommand($schedule, 'send:stay_in_touch', 'hourly');
$this->scheduleCommand($schedule, 'monica:davclients', 'hourly');
$this->scheduleCommand($schedule, 'monica:calculatestatistics', 'daily');
$this->scheduleCommand($schedule, 'monica:ping', 'daily');
$this->scheduleCommand($schedule, 'monica:clean', 'daily');
Expand Down
2 changes: 1 addition & 1 deletion app/Helpers/SearchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function searchContacts(string $needle, string $orderByColumn, str
});

return $b->addressBook($accountId, $addressBookName)
asbiin marked this conversation as resolved.
Show resolved Hide resolved
->orderBy($orderByColumn, $orderByDirection);
->orderBy($orderByColumn, $orderByDirection);
}

return Contact::search($needle, $accountId, $orderByColumn, $orderByDirection)
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/DAV/Auth/AuthBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function check(RequestInterface $request, ResponseInterface $response)
return [false, 'User is not authenticated'];
}

return [true, PrincipalBackend::getPrincipalUser()];
return [true, PrincipalBackend::getPrincipalUser(Auth::user())];
}

/**
Expand Down
31 changes: 27 additions & 4 deletions app/Http/Controllers/DAV/Backend/CalDAV/AbstractCalDAVBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\DAV\Backend\CalDAV;

use App\Models\User\User;
use Sabre\DAV\Server as SabreServer;
use Sabre\CalDAV\Plugin as CalDAVPlugin;
use Sabre\DAV\Sync\Plugin as DAVSyncPlugin;
Expand All @@ -13,18 +14,40 @@ abstract class AbstractCalDAVBackend implements ICalDAVBackend, IDAVBackend
{
use SyncDAVBackend;

/**
* Create a new instance of AbstractCalDAVBackend.
*
* @param User $user
*/
public function __construct($user)
{
$this->user = $user;
}

/**
* Get description array.
*
* @return array
*/
public function getDescription()
{
$token = DAVSyncPlugin::SYNCTOKEN_PREFIX.$this->refreshSyncToken(null)->id;
$des = [

return [
'id' => $this->backendUri(),
'uri' => $this->backendUri(),
'principaluri' => PrincipalBackend::getPrincipalUser(),
'principaluri' => PrincipalBackend::getPrincipalUser($this->user),
'{DAV:}sync-token' => $token,
'{'.SabreServer::NS_SABREDAV.'}sync-token' => $token,
'{'.CalDAVPlugin::NS_CALENDARSERVER.'}getctag' => $token,
];

return $des;
}

/**
* Get the new exported version of the object.
*
* @param mixed $obj
* @return string
*/
abstract protected function refreshObject($obj): string;
}
20 changes: 18 additions & 2 deletions app/Http/Controllers/DAV/Backend/CalDAV/CalDAVBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@
namespace App\Http\Controllers\DAV\Backend\CalDAV;

use Sabre\DAV;
use App\Models\User\User;
use Sabre\CalDAV\Backend\SyncSupport;
use Sabre\CalDAV\Backend\AbstractBackend;

class CalDAVBackend extends AbstractBackend implements SyncSupport
{
/**
* @var User
*/
protected $user;

/**
* Create a new instance of CalDAVBackend.
*
* @param User $user
*/
public function __construct(User $user)
{
$this->user = $user;
}

/**
* Set the Calendar backends.
*
Expand All @@ -16,8 +32,8 @@ class CalDAVBackend extends AbstractBackend implements SyncSupport
private function getBackends(): array
{
return [
new CalDAVBirthdays(),
new CalDAVTasks(),
new CalDAVBirthdays($this->user),
new CalDAVTasks($this->user),
];
}

Expand Down
34 changes: 22 additions & 12 deletions app/Http/Controllers/DAV/Backend/CalDAV/CalDAVBirthdays.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Facades\Log;
use App\Models\Instance\SpecialDate;
use Illuminate\Support\Facades\Auth;
use Sabre\DAV\Server as SabreServer;
use Sabre\CalDAV\Plugin as CalDAVPlugin;
use App\Services\VCalendar\ExportVCalendar;
Expand All @@ -29,8 +28,8 @@ public function getDescription()
+ [
'{DAV:}displayname' => trans('app.dav_birthdays'),
'{'.SabreServer::NS_SABREDAV.'}read-only' => true,
'{'.CalDAVPlugin::NS_CALDAV.'}calendar-description' => trans('app.dav_birthdays_description', ['name' => Auth::user()->name]),
'{'.CalDAVPlugin::NS_CALDAV.'}calendar-timezone' => Auth::user()->timezone,
'{'.CalDAVPlugin::NS_CALDAV.'}calendar-description' => trans('app.dav_birthdays_description', ['name' => $this->user->name]),
'{'.CalDAVPlugin::NS_CALDAV.'}calendar-timezone' => $this->user->timezone,
'{'.CalDAVPlugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VEVENT']),
'{'.CalDAVPlugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp(ScheduleCalendarTransp::TRANSPARENT),
];
Expand All @@ -56,13 +55,7 @@ public function prepareData($obj)
{
if ($obj instanceof SpecialDate) {
try {
$vcal = app(ExportVCalendar::class)
->execute([
'account_id' => Auth::user()->account_id,
'special_date_id' => $obj->id,
]);

$calendardata = $vcal->serialize();
$calendardata = $this->refreshObject($obj);

return [
'id' => $obj->id,
Expand All @@ -79,6 +72,23 @@ public function prepareData($obj)
return [];
}

/**
* Get the new exported version of the object.
*
* @param mixed $obj date
* @return string
*/
protected function refreshObject($obj): string
{
$vcal = app(ExportVCalendar::class)
->execute([
'account_id' => $this->user->account_id,
'special_date_id' => $obj->id,
]);

return $vcal->serialize();
}

private function hasBirthday($contact)
{
if (! $contact || ! $contact->birthdate) {
Expand All @@ -102,7 +112,7 @@ private function hasBirthday($contact)
public function getObjectUuid($collectionId, $uuid)
{
return SpecialDate::where([
'account_id' => Auth::user()->account_id,
'account_id' => $this->user->account_id,
'uuid' => $uuid,
])->first();
}
Expand All @@ -115,7 +125,7 @@ public function getObjectUuid($collectionId, $uuid)
public function getObjects($collectionId)
{
// We only return the birthday of default addressBook
$contacts = Auth::user()->account->contacts()
$contacts = $this->user->account->contacts()
->real()
->active()
->get();
Expand Down
Loading