Skip to content
Merged
Show file tree
Hide file tree
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 Feb 23, 2026
bb51a1e
Add insert_event method to GoogleClient and format body accordingly
davinotdavid Feb 23, 2026
d447462
Split logic between Google accounts vs non-Google accounts
davinotdavid Feb 23, 2026
1ae8502
Update google connector tests
davinotdavid Feb 23, 2026
d9231f1
Add google_calendar_channels table and model repo methods
davinotdavid Feb 24, 2026
6566e4f
Implement Google Calendar's Push Notifications API methods
davinotdavid Feb 24, 2026
01f3c8a
Add POST to /google-calendar as webhook to receive calendar notificat…
davinotdavid Feb 24, 2026
7a1fda4
Setup watch channels when connecting a google account
davinotdavid Feb 24, 2026
7b57d71
Move google calendar watch methods into its own controller
davinotdavid Feb 24, 2026
2741949
Setup and teadown watch channels when calendar connection changes
davinotdavid Feb 24, 2026
807b87c
Add backfill (to be ran once) and renew (periodically) to maintain ex…
davinotdavid Feb 24, 2026
81d6c23
Add tests
davinotdavid Feb 24, 2026
90ec212
Fix lint
davinotdavid Feb 25, 2026
b63bef9
Create google events as tentative and add patch_event method
davinotdavid Mar 6, 2026
23682d9
Skip HOLD event for Google events, and confirm through event through …
davinotdavid Mar 6, 2026
c20819b
Only care about the declined in webhook
davinotdavid Mar 6, 2026
bbdb367
Update tests
davinotdavid Mar 6, 2026
0c9c3d7
Only create watch channels for default connected calendars
davinotdavid Mar 9, 2026
9b43c26
Add tests for backfill and renew channel commands
davinotdavid Mar 9, 2026
418afb3
Fix tests regarding setup and teardown of watch channels during calen…
davinotdavid Mar 9, 2026
b312dff
Fix schedule tests to not depend on a real clock anymore
davinotdavid Mar 9, 2026
3b59ffe
Remove unnecessary setup_watch_channels_for_connection method
davinotdavid Mar 9, 2026
62dacac
Revert changes to the ics and mailer
davinotdavid Mar 9, 2026
3b8a0b6
Add Subscriber to attendee list with NeedsAction if booking_confirmation
davinotdavid Mar 9, 2026
b991a44
Subscribers actions in Google Calendar should backtrack the booking s…
davinotdavid Mar 10, 2026
2ba602f
Add tests for Susbcribers actions in Google Calendar
davinotdavid Mar 10, 2026
d67fd4e
Adding the organizer in the google event regardless of booking confir…
davinotdavid Mar 10, 2026
de76c3e
Also make a remote event when there is no need to require booking con…
davinotdavid Mar 10, 2026
1cd972a
More test coverage
davinotdavid Mar 10, 2026
811f629
Add GOOGLE_INVITE_ENABLED env var to control feature
davinotdavid Mar 10, 2026
6500a54
Add tests for GOOGLE_INVITE_ENABLED fallback into branded emails
davinotdavid Mar 10, 2026
e36541c
Add or create Zoom meeting link to the invite for Google Invite flows
davinotdavid Mar 10, 2026
8f1d8df
Update test suite with new coverage for Zoom meeting links
davinotdavid Mar 10, 2026
6889dd7
Attempt to fix CodeQL false positive in test
davinotdavid Mar 10, 2026
c4dd33c
No need for a helper function to check for GOOGLE_INVITE_ENABLED
davinotdavid Mar 26, 2026
411ca36
Consolidate 200 successful response in a variable
davinotdavid Mar 26, 2026
1b3462b
Remove early success return in webhook in case the channel don't have…
davinotdavid Mar 26, 2026
9f4a167
Share code for creating zoom meeting link
davinotdavid Mar 26, 2026
5c3f623
Update tests
davinotdavid Mar 26, 2026
5b71618
Add GOOGLE_CHANNEL_TTL_IN_SECONDS to safeguard against unexpected API…
davinotdavid Apr 1, 2026
7f86e34
Better query for google calendars in a schedule
davinotdavid Apr 1, 2026
556c829
Only sync watch channels if GOOGLE_INVITE_ENABLED is True
davinotdavid Apr 1, 2026
e5c636e
Add state to the google_calendar_channels table, populate it and chec…
davinotdavid Apr 1, 2026
3871d78
Better returns and erroring logs
davinotdavid Apr 1, 2026
f317c5f
Make db query do the filtering instead of looping myself
davinotdavid Apr 1, 2026
4a5e641
Update tests
davinotdavid Apr 1, 2026
e3611cb
Add renewal of google channels to Celery
davinotdavid Apr 1, 2026
8f7e899
(Re)fixing the schedule_calendar_ids query
davinotdavid Apr 1, 2026
f1a2bfc
Enumify send_updates
davinotdavid Apr 6, 2026
816b5c6
Remove unnecessary ruff noqa
davinotdavid Apr 6, 2026
7c73230
Make process_google_event_changes as a Celery task instead of backgro…
davinotdavid Apr 6, 2026
260ebbe
Enumify response_status
davinotdavid Apr 6, 2026
bb8124c
Wrap google channel renewal celery task in a try except
davinotdavid Apr 6, 2026
0c3a33e
Set GOOGLE_INVITE_ENABLE as False in .env.test
davinotdavid Apr 6, 2026
116a2b7
Make stop_google_channel as a retriable celery task
davinotdavid Apr 6, 2026
43eb4cd
Fix / update tests
davinotdavid Apr 6, 2026
5d7a149
Fix lint
davinotdavid Apr 6, 2026
2d093c0
Run alembic merge head to solve migration branching
davinotdavid Apr 8, 2026
d944889
Replace print with logging in renew_google_channels
davinotdavid Apr 8, 2026
d013347
Better response return pattern and better comments
davinotdavid Apr 8, 2026
415be16
Defining one day and seven days in seconds
davinotdavid Apr 8, 2026
3743970
Add EventStatus enum and replace them throughout
davinotdavid Apr 8, 2026
d286297
Allow calendar.get_by_subscriber to be filterable by provider
davinotdavid Apr 8, 2026
742b988
Move the google webhook processing into a celery task and return 200 …
davinotdavid Apr 8, 2026
45ab0c2
No need to check if sentry is initialized
davinotdavid Apr 8, 2026
e638922
Add comment to punt zoom link creation to a celery task in the future
davinotdavid Apr 8, 2026
6e7a66c
Fix lint
davinotdavid Apr 8, 2026
4676257
Pass in autogenerate_code_verifier for PKCE
davinotdavid Apr 22, 2026
b1ca02e
Address review comments
davinotdavid Apr 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ APP_ENV=dev
APP_ADMIN_ALLOW_LIST=
APP_SETUP
APP_ALLOW_FIRST_TIME_REGISTER=
# Enable Google Calendar native invites (True/False). Defaults based on APP_ENV if unset.
GOOGLE_INVITE_ENABLED=

# -- BACKEND --
BACKEND_URL=http://localhost:5000
Expand Down Expand Up @@ -95,6 +97,7 @@ GOOGLE_AUTH_CLIENT_ID=
GOOGLE_AUTH_SECRET=
GOOGLE_AUTH_PROJECT_ID=
GOOGLE_AUTH_CALLBACK=http://localhost:5000/google/callback
GOOGLE_CHANNEL_TTL_IN_SECONDS=604800 # 7 days

# -- Zoom API --
ZOOM_API_ENABLED=False
Expand Down
2 changes: 2 additions & 0 deletions backend/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ GOOGLE_AUTH_CLIENT_ID=
GOOGLE_AUTH_SECRET=
GOOGLE_AUTH_PROJECT_ID=
GOOGLE_AUTH_CALLBACK=http://localhost:5000/google/callback
GOOGLE_CHANNEL_TTL_IN_SECONDS=604800 # 7 days

# -- TB ACCOUNTS --
TB_ACCOUNTS_CALDAV_URL=https://stage-thundermail.com
Expand All @@ -70,6 +71,7 @@ SIGNED_SECRET=test-secret-pls-ignore
SENTRY_DSN=
# Possible values: prod, dev, test
APP_ENV=test
GOOGLE_INVITE_ENABLED=False
# Possible values: password, fxa
AUTH_SCHEME=password

Expand Down
8 changes: 8 additions & 0 deletions backend/src/appointment/celery_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sentry_sdk
from celery import Celery
from dotenv import load_dotenv
from appointment.defines import ONE_DAY_IN_SECONDS, SEVEN_DAYS_IN_SECONDS


def _base_redis_url() -> str:
Expand Down Expand Up @@ -40,6 +41,9 @@ def create_celery_app() -> Celery:
sentry_sdk.set_extra('CELERY_RESULT_EXPIRES', result_expires)
sentry_sdk.set_extra('CELERY_TASK_ALWAYS_EAGER', task_always_eager)

google_channel_ttl = float(os.getenv('GOOGLE_CHANNEL_TTL_IN_SECONDS', SEVEN_DAYS_IN_SECONDS))
google_channel_renew_interval = google_channel_ttl - ONE_DAY_IN_SECONDS

app = Celery('appointment')

app.config_from_object({
Expand All @@ -59,6 +63,10 @@ def create_celery_app() -> Celery:
'task': 'appointment.tasks.health.heartbeat',
'schedule': 60.0,
},
'renew-google-channels': {
'task': 'appointment.tasks.google.renew_google_channels',
'schedule': google_channel_renew_interval,
},
Comment thread
MelissaAutumn marked this conversation as resolved.
},
'beat_schedule_filename': 'celerybeat-appointment-schedule',
})
Expand Down
101 changes: 101 additions & 0 deletions backend/src/appointment/commands/backfill_google_channels.py
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.')
Comment thread
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.')
87 changes: 87 additions & 0 deletions backend/src/appointment/commands/renew_google_channels.py
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'
)
Loading
Loading