Skip to content

Commit 0f853f7

Browse files
committed
✨ Start, stop and refresh webhooks
1 parent 3e75ff9 commit 0f853f7

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

Diff for: app/Calendar.php

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Event;
77
use App\GoogleAccount;
88
use App\Jobs\SynchronizeGoogleEvents;
9+
use App\Jobs\WatchGoogleEvents;
910
use Illuminate\Database\Eloquent\Model;
1011

1112
class Calendar extends Model
@@ -30,4 +31,9 @@ public function synchronize()
3031
{
3132
SynchronizeGoogleEvents::dispatch($this);
3233
}
34+
35+
public function watch()
36+
{
37+
WatchGoogleEvents::dispatch($this);
38+
}
3339
}

Diff for: app/Concerns/Synchronizable.php

+1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ public function getGoogleService($service)
3333
}
3434

3535
abstract public function synchronize();
36+
abstract public function watch();
3637
}

Diff for: app/Console/Kernel.php

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Console;
44

55
use App\Jobs\PeriodicSynchronizations;
6+
use App\Jobs\RefreshWebhookSynchronizations;
67
use Illuminate\Console\Scheduling\Schedule;
78
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
89

@@ -26,6 +27,7 @@ class Kernel extends ConsoleKernel
2627
protected function schedule(Schedule $schedule)
2728
{
2829
$schedule->job(new PeriodicSynchronizations())->everyFifteenMinutes();
30+
$schedule->job(new RefreshWebhookSynchronizations())->daily();
2931
}
3032

3133
/**

Diff for: app/GoogleAccount.php

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Calendar;
66
use App\Concerns\Synchronizable;
77
use App\Jobs\SynchronizeGoogleCalendars;
8+
use App\Jobs\WatchGoogleCalendars;
89
use App\Services\Google;
910
use App\User;
1011
use Illuminate\Database\Eloquent\Model;
@@ -35,4 +36,9 @@ public function synchronize()
3536
{
3637
SynchronizeGoogleCalendars::dispatch($this);
3738
}
39+
40+
public function watch()
41+
{
42+
WatchGoogleCalendars::dispatch($this);
43+
}
3844
}

Diff for: app/Jobs/RefreshWebhookSynchronizations.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Queue\InteractsWithQueue;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
11+
class RefreshWebhookSynchronizations implements ShouldQueue
12+
{
13+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
14+
15+
public function handle()
16+
{
17+
Synchronization::query()
18+
->whereNotNull('resource_id')
19+
->whereNull('expired_at')
20+
->orWhere('expired_at', '<', now()->addDays(2))
21+
->get()
22+
->each->refreshWebhook();
23+
}
24+
}

Diff for: app/Synchronization.php

+35
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,36 @@ public function ping()
2323
return $this->synchronizable->synchronize();
2424
}
2525

26+
public function startListeningForChanges()
27+
{
28+
return $this->synchronizable->watch();
29+
}
30+
31+
public function stopListeningForChanges()
32+
{
33+
if (! $this->resource_id) {
34+
return;
35+
}
36+
37+
$this->synchronizable
38+
->getGoogleService('Calendar')
39+
->channels->stop($this->asGoogleChannel());
40+
}
41+
42+
public function refreshWebhook()
43+
{
44+
$this->stopListeningForChanges();
45+
46+
// Update the UUID since the previous one has
47+
// already been associated to a Google Channel.
48+
$this->id = Uuid::uuid4();
49+
$this->save();
50+
51+
$this->startListeningForChanges();
52+
53+
return $this;
54+
}
55+
2656
public function asGoogleChannel()
2757
{
2858
return tap(new \Google_Service_Calendar_Channel(), function ($channel) {
@@ -48,7 +78,12 @@ public static function boot()
4878
});
4979

5080
static::created(function ($synchronization) {
81+
$synchronization->startListeningForChanges();
5182
$synchronization->ping();
5283
});
84+
85+
static::deleting(function ($synchronization) {
86+
$synchronization->stopListeningForChanges();
87+
});
5388
}
5489
}

0 commit comments

Comments
 (0)