MOOC Utils

Utils like auto assignment solver extension and course notice announcment notification for MOOC courses

Announcements & notifications API

Introduction#

Endpoints for course announcements and notification history:

  • List notices for a course
  • Notification inbox and history
  • Channel-based delivery records
  • Subscriptions that drive what you get notified about
  • Preferences and channel management
  • Parsing and caching of announcement content

Real-time push is modeled; what is actually wired today is called out per endpoint.

Project structure#

The API is organized into routers, models, schemas, services, and core configuration. Routers define endpoint groups, schemas define request/response shapes, models define database entities, and services encapsulate business logic.

Core components#

  • Announcement retrieval: Fetches and caches announcements per course, deduplicating by course, title, and date.
  • Notification history: Lists notifications for a user, supports marking as read.
  • Subscription management: Create/list/delete subscriptions to courses.
  • Channel management: Store user notification channels (type and address) with activation flag.
  • Real-time delivery: Notifications are persisted upon creation; delivery to channels is externalized by the service layer.
  • Preferences: Controlled via channel activation and subscription toggles.

Key endpoints:

  • GET /courses/{course_code}/announcements
  • GET /notifications
  • GET /notifications/users/{user_id}
  • PATCH /notifications/{notification_id}/read
  • POST /subscriptions
  • GET /subscriptions
  • DELETE /subscriptions/{subscription_id}

Architecture overview#

The API follows a layered architecture:

  • Routers handle HTTP requests, inject dependencies, and enforce authentication.
  • Services encapsulate business logic and orchestrate model operations.
  • Models define persistence and relationships.
  • Schemas define serialization contracts.

Detailed component analysis#

Announcements endpoint#

  • Path: GET /courses/{course_code}/announcements
  • Authentication: Required
  • Behavior:
  • Validates course existence by code.
  • Fetches announcements from upstream via service and caches locally.
  • Deduplicates by course, title, and date; updates content if changed.
  • Returns paginated-like list ordered by fetch time.

Notifications endpoint#

  • Paths:
  • GET /notifications (lists current user’s notifications)
  • GET /notifications/users/{user_id} (admin/self-only access)
  • PATCH /notifications/{notification_id}/read (mark as read)
  • Authentication: Required
  • Behavior:
  • Listing filters by user and orders by send time descending.
  • Mark-as-read validates ownership and toggles read flag.

Subscriptions endpoint#

  • Paths:
  • POST /subscriptions (create subscription)
  • GET /subscriptions (list user subscriptions)
  • DELETE /subscriptions/{subscription_id} (delete subscription)
  • Authentication: Required
  • Behavior:
  • Create validates course existence by code and creates a subscription.
  • Delete enforces ownership and removes the subscription.

Notification channels#

  • Purpose: Define where notifications are delivered (e.g., email, push).
  • Entities:
  • NotificationChannel: stores channel type, address, activation flag, and timestamps.
  • Notification: links a user, subscription, announcement, and optional channel.
  • Relationships:
  • One-to-many from User to NotificationChannel.
  • Many-to-one from Notification to NotificationChannel.

Data models overview#

Dependency analysis#

  • Routers depend on services via FastAPI Depends and global auth decorator.
  • Services depend on models and external integrations (e.g., SwayamService).
  • Models define foreign keys and constraints; schemas define serialization.
  • No circular dependencies observed among routers and services.

Performance considerations#

  • Announcement caching: Deduplication by course, title, and date reduces redundant writes and improves retrieval performance.
  • Sorting and pagination: Results are ordered by time; consider adding explicit pagination for large datasets.
  • Database constraints: Unique constraints on user-course and channel-address combinations prevent duplicates and support fast lookups.
  • Asynchronous operations: Services use async/await; ensure database connection pooling and indexing align with query patterns.

Troubleshooting guide#

Common errors and resolutions:

  • Course not found when fetching announcements:
  • Cause: Invalid course code.
  • Resolution: Verify course code; ensure course exists.
  • Access denied for notifications:
  • Cause: Attempting to access another user’s notifications.
  • Resolution: Only access your own notifications or have appropriate admin privileges.
  • Notification not found:
  • Cause: Invalid notification ID.
  • Resolution: Confirm the notification exists and belongs to the current user.
  • Subscription not found or access denied:
  • Cause: Non-existent subscription or unauthorized deletion.
  • Resolution: Check subscription ownership and existence.

Operational checks:

  • Ensure authentication is active and tokens are valid.
  • Confirm database connectivity and migrations are applied.
  • Validate upstream service availability for announcement retrieval.

Conclusion#

List announcements for a course, manage the notification inbox, and keep channel records in sync with subscriptions. Delivery beyond the API store is still thin, so treat channels as data until workers exist.