-
Notifications
You must be signed in to change notification settings - Fork 20
Use Google Calendar API calendar invites / Implement Push Notifications #1529
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
Merged
Merged
Changes from all commits
Commits
Show all changes
69 commits
Select commit
Hold shift + click to select a range
917d26f
Update mailer attachments and ics file handling
davinotdavid bb51a1e
Add insert_event method to GoogleClient and format body accordingly
davinotdavid d447462
Split logic between Google accounts vs non-Google accounts
davinotdavid 1ae8502
Update google connector tests
davinotdavid d9231f1
Add google_calendar_channels table and model repo methods
davinotdavid 6566e4f
Implement Google Calendar's Push Notifications API methods
davinotdavid 01f3c8a
Add POST to /google-calendar as webhook to receive calendar notificat…
davinotdavid 7a1fda4
Setup watch channels when connecting a google account
davinotdavid 7b57d71
Move google calendar watch methods into its own controller
davinotdavid 2741949
Setup and teadown watch channels when calendar connection changes
davinotdavid 807b87c
Add backfill (to be ran once) and renew (periodically) to maintain ex…
davinotdavid 81d6c23
Add tests
davinotdavid 90ec212
Fix lint
davinotdavid b63bef9
Create google events as tentative and add patch_event method
davinotdavid 23682d9
Skip HOLD event for Google events, and confirm through event through …
davinotdavid c20819b
Only care about the declined in webhook
davinotdavid bbdb367
Update tests
davinotdavid 0c9c3d7
Only create watch channels for default connected calendars
davinotdavid 9b43c26
Add tests for backfill and renew channel commands
davinotdavid 418afb3
Fix tests regarding setup and teardown of watch channels during calen…
davinotdavid b312dff
Fix schedule tests to not depend on a real clock anymore
davinotdavid 3b59ffe
Remove unnecessary setup_watch_channels_for_connection method
davinotdavid 62dacac
Revert changes to the ics and mailer
davinotdavid 3b8a0b6
Add Subscriber to attendee list with NeedsAction if booking_confirmation
davinotdavid b991a44
Subscribers actions in Google Calendar should backtrack the booking s…
davinotdavid 2ba602f
Add tests for Susbcribers actions in Google Calendar
davinotdavid d67fd4e
Adding the organizer in the google event regardless of booking confir…
davinotdavid de76c3e
Also make a remote event when there is no need to require booking con…
davinotdavid 1cd972a
More test coverage
davinotdavid 811f629
Add GOOGLE_INVITE_ENABLED env var to control feature
davinotdavid 6500a54
Add tests for GOOGLE_INVITE_ENABLED fallback into branded emails
davinotdavid e36541c
Add or create Zoom meeting link to the invite for Google Invite flows
davinotdavid 8f1d8df
Update test suite with new coverage for Zoom meeting links
davinotdavid 6889dd7
Attempt to fix CodeQL false positive in test
davinotdavid c4dd33c
No need for a helper function to check for GOOGLE_INVITE_ENABLED
davinotdavid 411ca36
Consolidate 200 successful response in a variable
davinotdavid 1b3462b
Remove early success return in webhook in case the channel don't have…
davinotdavid 9f4a167
Share code for creating zoom meeting link
davinotdavid 5c3f623
Update tests
davinotdavid 5b71618
Add GOOGLE_CHANNEL_TTL_IN_SECONDS to safeguard against unexpected API…
davinotdavid 7f86e34
Better query for google calendars in a schedule
davinotdavid 556c829
Only sync watch channels if GOOGLE_INVITE_ENABLED is True
davinotdavid e5c636e
Add state to the google_calendar_channels table, populate it and chec…
davinotdavid 3871d78
Better returns and erroring logs
davinotdavid f317c5f
Make db query do the filtering instead of looping myself
davinotdavid 4a5e641
Update tests
davinotdavid e3611cb
Add renewal of google channels to Celery
davinotdavid 8f7e899
(Re)fixing the schedule_calendar_ids query
davinotdavid f1a2bfc
Enumify send_updates
davinotdavid 816b5c6
Remove unnecessary ruff noqa
davinotdavid 7c73230
Make process_google_event_changes as a Celery task instead of backgro…
davinotdavid 260ebbe
Enumify response_status
davinotdavid bb8124c
Wrap google channel renewal celery task in a try except
davinotdavid 0c3a33e
Set GOOGLE_INVITE_ENABLE as False in .env.test
davinotdavid 116a2b7
Make stop_google_channel as a retriable celery task
davinotdavid 43eb4cd
Fix / update tests
davinotdavid 5d7a149
Fix lint
davinotdavid 2d093c0
Run alembic merge head to solve migration branching
davinotdavid d944889
Replace print with logging in renew_google_channels
davinotdavid d013347
Better response return pattern and better comments
davinotdavid 415be16
Defining one day and seven days in seconds
davinotdavid 3743970
Add EventStatus enum and replace them throughout
davinotdavid d286297
Allow calendar.get_by_subscriber to be filterable by provider
davinotdavid 742b988
Move the google webhook processing into a celery task and return 200 …
davinotdavid 45ab0c2
No need to check if sentry is initialized
davinotdavid e638922
Add comment to punt zoom link creation to a celery task in the future
davinotdavid 6e7a66c
Fix lint
davinotdavid 4676257
Pass in autogenerate_code_verifier for PKCE
davinotdavid b1ca02e
Address review comments
davinotdavid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
backend/src/appointment/commands/backfill_google_channels.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """One-off command to set up Google Calendar watch channels for existing connected calendars.""" | ||
|
|
||
| import json | ||
| import logging | ||
| import uuid | ||
| from datetime import datetime, timezone | ||
|
|
||
| from google.oauth2.credentials import Credentials | ||
|
|
||
| from ..controller.google_watch import get_webhook_url | ||
| from ..database import repo, models | ||
| from ..dependencies.database import get_engine_and_session | ||
| from ..dependencies.google import get_google_client | ||
| from ..main import _common_setup | ||
|
|
||
|
|
||
| def run(): | ||
| _common_setup() | ||
| google_client = get_google_client() | ||
|
|
||
| _, SessionLocal = get_engine_and_session() | ||
| db = SessionLocal() | ||
|
|
||
| webhook_url = get_webhook_url() | ||
| if not webhook_url: | ||
| print('BACKEND_URL not set, aborting.') | ||
| db.close() | ||
| return | ||
|
|
||
| # Find connected Google calendars that are the default in a schedule | ||
| # and don't yet have a watch channel | ||
| schedule_calendar_ids = [ | ||
| s.calendar_id for s in | ||
| db.query(models.Schedule.calendar_id).filter(models.Schedule.calendar_id != None).all() # noqa: E711 | ||
| ] | ||
|
|
||
| all_calendars = db.query(models.Calendar).filter( | ||
| models.Calendar.provider == models.CalendarProvider.google, | ||
| models.Calendar.connected == True, # noqa: E712 | ||
| models.Calendar.id.in_(schedule_calendar_ids), | ||
| ).all() | ||
|
|
||
| candidates = [] | ||
| for cal in all_calendars: | ||
| existing = repo.google_calendar_channel.get_by_calendar_id(db, cal.id) | ||
| if not existing: | ||
| candidates.append(cal) | ||
|
|
||
| print(f'Found {len(candidates)} connected Google calendar(s) without a watch channel.') | ||
|
MelissaAutumn marked this conversation as resolved.
|
||
|
|
||
| created = 0 | ||
| skipped = 0 | ||
| failed = 0 | ||
|
|
||
| for calendar in candidates: | ||
| ext_conn = calendar.external_connection | ||
| if not ext_conn or not ext_conn.token: | ||
| print(f' Calendar {calendar.id}: no external connection or token, skipping.') | ||
| skipped += 1 | ||
| continue | ||
|
|
||
| try: | ||
| token = Credentials.from_authorized_user_info( | ||
| json.loads(ext_conn.token), google_client.SCOPES | ||
| ) | ||
| except Exception as e: | ||
| print(f' Calendar {calendar.id}: failed to parse token ({e}), skipping.') | ||
| skipped += 1 | ||
| continue | ||
|
|
||
| try: | ||
| state = str(uuid.uuid4()) | ||
| response = google_client.watch_events(calendar.user, webhook_url, token, state=state) | ||
| if not response: | ||
| print(f' Calendar {calendar.id}: watch_events returned no response.') | ||
| failed += 1 | ||
| continue | ||
|
|
||
| expiration_ms = int(response.get('expiration', 0)) | ||
| expiration_dt = datetime.fromtimestamp(expiration_ms / 1000, tz=timezone.utc) | ||
|
|
||
| sync_token = google_client.get_initial_sync_token(calendar.user, token) | ||
|
|
||
| repo.google_calendar_channel.create( | ||
| db, | ||
| calendar_id=calendar.id, | ||
| channel_id=response['id'], | ||
| resource_id=response['resourceId'], | ||
| expiration=expiration_dt, | ||
| state=state, | ||
| sync_token=sync_token, | ||
| ) | ||
| created += 1 | ||
| print(f' Calendar {calendar.id}: channel created (expires {expiration_dt}).') | ||
| except Exception as e: | ||
| print(f' Calendar {calendar.id}: failed ({e}).') | ||
| logging.error(f'[backfill_google_channels] Error for calendar {calendar.id}: {e}') | ||
| failed += 1 | ||
|
|
||
| db.close() | ||
| print(f'\nBackfill complete: {created} created, {skipped} skipped, {failed} failed.') | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """Renew expiring Google Calendar push notification channels. | ||
|
|
||
| Run periodically (e.g., daily) to ensure channels don't expire. | ||
| Google channels typically last ~7 days, so daily renewal keeps a buffer. | ||
| """ | ||
|
|
||
| import json | ||
| import logging | ||
| import uuid | ||
| from datetime import datetime, timedelta, timezone | ||
|
|
||
| from google.oauth2.credentials import Credentials | ||
|
|
||
| from ..controller.google_watch import get_webhook_url | ||
| from ..database import repo | ||
| from ..dependencies.database import get_engine_and_session | ||
| from ..dependencies.google import get_google_client | ||
| from ..main import _common_setup | ||
|
|
||
|
|
||
| def run(): | ||
| from ..tasks.google import stop_google_channel | ||
|
|
||
| _common_setup() | ||
| google_client = get_google_client() | ||
|
|
||
| _, SessionLocal = get_engine_and_session() | ||
| db = SessionLocal() | ||
|
|
||
| webhook_url = get_webhook_url() | ||
| if not webhook_url: | ||
| logging.error('[renew_google_channels] BACKEND_URL not set, aborting') | ||
| db.close() | ||
| return | ||
|
|
||
| # Renew channels that expire within the next 24 hours | ||
| threshold = datetime.now(tz=timezone.utc) + timedelta(hours=24) | ||
| channels = repo.google_calendar_channel.get_expiring(db, before=threshold) | ||
|
|
||
| renewed = 0 | ||
| failed = 0 | ||
|
|
||
| for channel in channels: | ||
| calendar = channel.calendar | ||
| if not calendar or not calendar.connected: | ||
| repo.google_calendar_channel.delete(db, channel) | ||
| continue | ||
|
|
||
| external_connection = calendar.external_connection | ||
| if not external_connection or not external_connection.token: | ||
| repo.google_calendar_channel.delete(db, channel) | ||
| continue | ||
|
|
||
| token = Credentials.from_authorized_user_info(json.loads(external_connection.token), google_client.SCOPES) | ||
|
|
||
| # Stop the old channel (fire-and-forget via Celery with retries) | ||
| stop_google_channel.delay(channel.channel_id, channel.resource_id, external_connection.token) | ||
|
|
||
| # Create a new channel | ||
| try: | ||
| new_state = str(uuid.uuid4()) | ||
| response = google_client.watch_events(calendar.user, webhook_url, token, state=new_state) | ||
| if response: | ||
| expiration_ms = int(response.get('expiration', 0)) | ||
| expiration_dt = datetime.fromtimestamp(expiration_ms / 1000, tz=timezone.utc) | ||
|
|
||
| repo.google_calendar_channel.update_expiration( | ||
| db, | ||
| channel, | ||
| new_channel_id=response['id'], | ||
| new_resource_id=response['resourceId'], | ||
| new_expiration=expiration_dt, | ||
| new_state=new_state, | ||
| ) | ||
| renewed += 1 | ||
| else: | ||
| repo.google_calendar_channel.delete(db, channel) | ||
| failed += 1 | ||
| except Exception as e: | ||
| logging.error(f'[renew_google_channels] Failed to renew channel for calendar {calendar.id}: {e}') | ||
| failed += 1 | ||
|
|
||
| db.close() | ||
| logging.info( | ||
| f'[renew_google_channels] Channel renewal complete: ' | ||
| f'{renewed} renewed, {failed} failed, {len(channels)} total processed' | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.