Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
✨ Watch Google resources
  • Loading branch information
lorisleiva committed Oct 2, 2018
1 parent 3e8b4aa commit 3e75ff9
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/Jobs/PeriodicSynchronizations.php
Expand Up @@ -15,6 +15,6 @@ class PeriodicSynchronizations implements ShouldQueue

public function handle()
{
Synchronization::get()->each->ping();
Synchronization::whereNull('resource_id')->get()->each->ping();
}
}
20 changes: 20 additions & 0 deletions app/Jobs/WatchGoogleCalendars.php
@@ -0,0 +1,20 @@
<?php

namespace App\Jobs;

use App\Jobs\WatchGoogleResource;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class WatchGoogleCalendars extends WatchGoogleResource implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function getGoogleRequest($service, $channel)
{
return $service->calendarList->watch($channel);
}
}
22 changes: 22 additions & 0 deletions app/Jobs/WatchGoogleEvents.php
@@ -0,0 +1,22 @@
<?php

namespace App\Jobs;

use App\Jobs\WatchGoogleResource;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class WatchGoogleEvents extends WatchGoogleResource implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function getGoogleRequest($service, $channel)
{
return $service->events->watch(
$this->synchronizable->google_id, $channel
);
}
}
39 changes: 39 additions & 0 deletions app/Jobs/WatchGoogleResource.php
@@ -0,0 +1,39 @@
<?php

namespace App\Jobs;

use Illuminate\Support\Carbon;

abstract class WatchGoogleResource
{
protected $synchronizable;

public function __construct($synchronizable)
{
$this->synchronizable = $synchronizable;
}

public function handle()
{
$synchronization = $this->synchronizable->synchronization;

try {
$response = $this->getGoogleRequest(
$this->synchronizable->getGoogleService('Calendar'),
$synchronization->asGoogleChannel()
);

$synchronization->update([
'resource_id' => $response->getResourceId(),
'expired_at' => Carbon::createFromTimestampMs($response->getExpiration())
]);

} catch (\Google_Service_Exception $e) {
// If we reach an error at this point, it is likely that
// push notifications are not allowed for this resource.
// Instead we will sync it manually at regular interval.
}
}

abstract public function getGoogleRequest($service, $channel);
}
10 changes: 10 additions & 0 deletions app/Synchronization.php
Expand Up @@ -23,6 +23,16 @@ public function ping()
return $this->synchronizable->synchronize();
}

public function asGoogleChannel()
{
return tap(new \Google_Service_Calendar_Channel(), function ($channel) {
$channel->setId($this->id);
$channel->setResourceId($this->resource_id);
$channel->setType('web_hook');
$channel->setAddress(config('services.google.webhook_uri'));
});
}

public function synchronizable()
{
return $this->morphTo();
Expand Down

0 comments on commit 3e75ff9

Please sign in to comment.