Add push notification functionality with tests and digest scheduler
All checks were successful
Chore App Build, Test, and Push Docker Images / build-and-push (push) Successful in 2m14s
All checks were successful
Chore App Build, Test, and Push Docker Images / build-and-push (push) Successful in 2m14s
- Implemented push subscription API with tests for subscribing and unsubscribing users. - Created web push notification tests triggered by child actions. - Added digest scheduler to send email digests to users at 9 PM local time. - Developed utility functions for creating and validating digest action tokens. - Integrated web push sender to handle sending notifications to users. - Added service worker for handling push notifications in the frontend. - Created a push opt-in component for user notification preferences. - Implemented tests for the push opt-in component to ensure correct behavior. - Updated frontend services to manage push subscriptions and permissions.
This commit is contained in:
@@ -79,6 +79,8 @@ child_overrides_path = os.path.join(base_dir, 'child_overrides.json')
|
||||
chore_schedules_path = os.path.join(base_dir, 'chore_schedules.json')
|
||||
task_extensions_path = os.path.join(base_dir, 'task_extensions.json')
|
||||
refresh_tokens_path = os.path.join(base_dir, 'refresh_tokens.json')
|
||||
push_subscriptions_path = os.path.join(base_dir, 'push_subscriptions.json')
|
||||
digest_action_tokens_path = os.path.join(base_dir, 'digest_action_tokens.json')
|
||||
|
||||
# Use separate TinyDB instances/files for each collection
|
||||
_child_db = TinyDB(child_path, indent=2)
|
||||
@@ -93,6 +95,8 @@ _child_overrides_db = TinyDB(child_overrides_path, indent=2)
|
||||
_chore_schedules_db = TinyDB(chore_schedules_path, indent=2)
|
||||
_task_extensions_db = TinyDB(task_extensions_path, indent=2)
|
||||
_refresh_tokens_db = TinyDB(refresh_tokens_path, indent=2)
|
||||
_push_subscriptions_db = TinyDB(push_subscriptions_path, indent=2)
|
||||
_digest_action_tokens_db = TinyDB(digest_action_tokens_path, indent=2)
|
||||
|
||||
# Expose table objects wrapped with locking
|
||||
child_db = LockedTable(_child_db)
|
||||
@@ -107,6 +111,8 @@ child_overrides_db = LockedTable(_child_overrides_db)
|
||||
chore_schedules_db = LockedTable(_chore_schedules_db)
|
||||
task_extensions_db = LockedTable(_task_extensions_db)
|
||||
refresh_tokens_db = LockedTable(_refresh_tokens_db)
|
||||
push_subscriptions_db = LockedTable(_push_subscriptions_db)
|
||||
digest_action_tokens_db = LockedTable(_digest_action_tokens_db)
|
||||
|
||||
if os.environ.get('DB_ENV', 'prod') == 'test':
|
||||
child_db.truncate()
|
||||
@@ -121,4 +127,6 @@ if os.environ.get('DB_ENV', 'prod') == 'test':
|
||||
chore_schedules_db.truncate()
|
||||
task_extensions_db.truncate()
|
||||
refresh_tokens_db.truncate()
|
||||
push_subscriptions_db.truncate()
|
||||
digest_action_tokens_db.truncate()
|
||||
|
||||
|
||||
18
backend/db/digest_action_tokens.py
Normal file
18
backend/db/digest_action_tokens.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from tinydb import Query
|
||||
from db.db import digest_action_tokens_db
|
||||
from models.digest_action_token import DigestActionToken
|
||||
|
||||
|
||||
def insert_token(token: DigestActionToken) -> None:
|
||||
digest_action_tokens_db.insert(token.to_dict())
|
||||
|
||||
|
||||
def get_token_by_id(token_id: str) -> DigestActionToken | None:
|
||||
Q = Query()
|
||||
result = digest_action_tokens_db.get(Q.id == token_id)
|
||||
return DigestActionToken.from_dict(result) if result else None
|
||||
|
||||
|
||||
def mark_token_used(token_id: str) -> None:
|
||||
Q = Query()
|
||||
digest_action_tokens_db.update({'used': True}, Q.id == token_id)
|
||||
38
backend/db/push_subscriptions.py
Normal file
38
backend/db/push_subscriptions.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from tinydb import Query
|
||||
from db.db import push_subscriptions_db
|
||||
from models.push_subscription import PushSubscription
|
||||
|
||||
|
||||
def get_subscriptions_by_user(user_id: str) -> list[PushSubscription]:
|
||||
"""Return all push subscriptions for a user."""
|
||||
Q = Query()
|
||||
results = push_subscriptions_db.search(Q.user_id == user_id)
|
||||
return [PushSubscription.from_dict(r) for r in results]
|
||||
|
||||
|
||||
def upsert_subscription(user_id: str, endpoint: str, keys: dict) -> PushSubscription:
|
||||
"""Insert or update a subscription for the given user+endpoint pair."""
|
||||
Q = Query()
|
||||
existing = push_subscriptions_db.get((Q.user_id == user_id) & (Q.endpoint == endpoint))
|
||||
if existing:
|
||||
sub = PushSubscription.from_dict(existing)
|
||||
sub.keys = keys
|
||||
sub.touch()
|
||||
push_subscriptions_db.update(sub.to_dict(), (Q.user_id == user_id) & (Q.endpoint == endpoint))
|
||||
return sub
|
||||
sub = PushSubscription(user_id=user_id, endpoint=endpoint, keys=keys)
|
||||
push_subscriptions_db.insert(sub.to_dict())
|
||||
return sub
|
||||
|
||||
|
||||
def delete_by_endpoint(user_id: str, endpoint: str) -> int:
|
||||
"""Remove the subscription with the given endpoint for this user. Returns count removed."""
|
||||
Q = Query()
|
||||
removed = push_subscriptions_db.remove((Q.user_id == user_id) & (Q.endpoint == endpoint))
|
||||
return len(removed)
|
||||
|
||||
|
||||
def delete_subscription_by_id(subscription_id: str) -> None:
|
||||
"""Remove a subscription by its ID (used when push delivery fails)."""
|
||||
Q = Query()
|
||||
push_subscriptions_db.remove(Q.id == subscription_id)
|
||||
Reference in New Issue
Block a user