Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Handle incoming webhook notification and sync events
- *
- * @param array $headers
- * @return bool
- */
- public function handleWebhookNotification(array $headers)
- {
- try {
- $channelId = $headers['X-Goog-Channel-ID'] ?? null;
- $resourceId = $headers['X-Goog-Resource-ID'] ?? null;
- $resourceState = $headers['X-Goog-Resource-State'] ?? null;
- if (!$channelId || !$resourceId) {
- Log::error('Missing required webhook headers');
- return false;
- }
- // Find the relevant webhook in our database
- $webhook = CalendarWebhook::where('channel_id', $channelId)
- ->where('resource_id', $resourceId)
- ->first();
- if (!$webhook) {
- Log::error('Webhook not found for channel: ' . $channelId);
- return false;
- }
- $user = User::find($webhook->user_id);
- if (!$user) {
- Log::error('User not found for webhook: ' . $webhook->id);
- return false;
- }
- // Set user's token for API access
- if ($user->google_token) {
- $this->client->setAccessToken($user->google_token);
- // Refresh token if expired
- if ($this->client->isAccessTokenExpired()) {
- $this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
- $user->google_token = $this->client->getAccessToken();
- $user->save();
- }
- }
- if ($resourceState === 'sync') {
- // This is just a sync message, no action needed
- return true;
- } elseif ($resourceState === 'exists' || $resourceState === 'not_exists') {
- // Get the sync token to do a partial sync
- $syncToken = $webhook->sync_token ?? null;
- // Process changes from Google Calendar
- $this->syncEvents($user, $webhook->calendar_id, $syncToken);
- return true;
- }
- return false;
- } catch (\Exception $e) {
- Log::error('Error handling Google Calendar webhook: ' . $e->getMessage());
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement