Skip to content

Commit 3e75ff9

Browse files
committed
✨ Watch Google resources
1 parent 3e8b4aa commit 3e75ff9

5 files changed

Lines changed: 92 additions & 1 deletion

File tree

app/Jobs/PeriodicSynchronizations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ class PeriodicSynchronizations implements ShouldQueue
1515

1616
public function handle()
1717
{
18-
Synchronization::get()->each->ping();
18+
Synchronization::whereNull('resource_id')->get()->each->ping();
1919
}
2020
}

app/Jobs/WatchGoogleCalendars.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Jobs\WatchGoogleResource;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Queue\SerializesModels;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Foundation\Bus\Dispatchable;
11+
12+
class WatchGoogleCalendars extends WatchGoogleResource implements ShouldQueue
13+
{
14+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
15+
16+
public function getGoogleRequest($service, $channel)
17+
{
18+
return $service->calendarList->watch($channel);
19+
}
20+
}

app/Jobs/WatchGoogleEvents.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Jobs\WatchGoogleResource;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Queue\SerializesModels;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Foundation\Bus\Dispatchable;
11+
12+
class WatchGoogleEvents extends WatchGoogleResource implements ShouldQueue
13+
{
14+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
15+
16+
public function getGoogleRequest($service, $channel)
17+
{
18+
return $service->events->watch(
19+
$this->synchronizable->google_id, $channel
20+
);
21+
}
22+
}

app/Jobs/WatchGoogleResource.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use Illuminate\Support\Carbon;
6+
7+
abstract class WatchGoogleResource
8+
{
9+
protected $synchronizable;
10+
11+
public function __construct($synchronizable)
12+
{
13+
$this->synchronizable = $synchronizable;
14+
}
15+
16+
public function handle()
17+
{
18+
$synchronization = $this->synchronizable->synchronization;
19+
20+
try {
21+
$response = $this->getGoogleRequest(
22+
$this->synchronizable->getGoogleService('Calendar'),
23+
$synchronization->asGoogleChannel()
24+
);
25+
26+
$synchronization->update([
27+
'resource_id' => $response->getResourceId(),
28+
'expired_at' => Carbon::createFromTimestampMs($response->getExpiration())
29+
]);
30+
31+
} catch (\Google_Service_Exception $e) {
32+
// If we reach an error at this point, it is likely that
33+
// push notifications are not allowed for this resource.
34+
// Instead we will sync it manually at regular interval.
35+
}
36+
}
37+
38+
abstract public function getGoogleRequest($service, $channel);
39+
}

app/Synchronization.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ public function ping()
2323
return $this->synchronizable->synchronize();
2424
}
2525

26+
public function asGoogleChannel()
27+
{
28+
return tap(new \Google_Service_Calendar_Channel(), function ($channel) {
29+
$channel->setId($this->id);
30+
$channel->setResourceId($this->resource_id);
31+
$channel->setType('web_hook');
32+
$channel->setAddress(config('services.google.webhook_uri'));
33+
});
34+
}
35+
2636
public function synchronizable()
2737
{
2838
return $this->morphTo();

0 commit comments

Comments
 (0)