Users Collection
This document provides comprehensive documentation for the Users collection schema that stores user subscription data and preferences for the SuperSet Telegram Notification Bot. The Users collection serves as the central repository for managing user subscriptions, notification preferences, and associated metadata. It enables the system to deliver notifications via Telegram and Web Push channels while maintaining user preferences and activity tracking.
The Users collection schema is designed to handle multiple notification channels, track user engagement, and support flexible subscription management. It integrates seamlessly with the broader notification ecosystem, providing a robust foundation for user-centric communication workflows.
The Users collection is part of the MongoDB database schema and interacts with several key components:
Diagram sources
Section sources
Users Collection Schema#
The Users collection implements a comprehensive schema designed for flexible user subscription management:
Primary Identifier: user_id (String)
Telegram user ID serving as the unique identifier
Separate from MongoDB’s ObjectId for external system integration
Unique index ensures data integrity
Personal Information Fields:
first_name: String - User’s first nameusername: String (Optional) - Telegram username
Subscription Management:
subscription_active: Boolean - Overall subscription statusnotification_preferences: Embedded document controlling notification channels
Notification Preferences: Embedded document with three boolean flags:
telegram: Boolean - Telegram notification preferencewebpush: Boolean - Web Push notification preferenceemail: Boolean - Email notification preference
Web Push Subscriptions: Array of embedded subscription objects:
subscription_id: String - Unique subscription identifierendpoint: String - Web Push endpoint URLkeys: Embedded object containing cryptographic keysp256dh: String - ECDH keyauth: String - Authentication key
created_at: Date - Timestamp of subscription creation
Activity Tracking:
registered_at: Date - User registration timestamplast_active: Date - Last user activity timestampnotifications_sent: Number - Counter for sent notifications
Metadata: Embedded document with optional fields:
branch: String - Student’s academic branchbatch_year: String - Graduation yeardevice_type: String - Device classification (‘mobile’, ‘desktop’, ‘web’)
Section sources
Database Integration#
The Users collection is accessed through the DatabaseService, which provides a clean abstraction layer for MongoDB operations:
Diagram sources
Section sources
Notification Delivery Workflow#
The Users collection integrates with the notification delivery pipeline:
Diagram sources
Web Push Subscription Management#
The system handles Web Push subscriptions through dedicated endpoints:
Diagram sources
Section sources
User Registration and Management#
The user registration process involves creating user records with comprehensive metadata:
Registration Flow:
User initiates subscription via Telegram bot
System creates user record with
user_idas primary identifierSets initial preferences and activity timestamps
Stores personal information (first_name, username)
Initializes counters and metadata fields
Deactivation Process:
Soft deletion mechanism using
is_activeflagPreserves user data for potential reactivation
Maintains historical activity records
Section sources
Notification Preference Management#
The notification preferences system provides granular control over communication channels:
Preference Structure:
telegram: Controls Telegram message deliverywebpush: Controls browser notification deliveryemail: Controls email notification delivery
Integration Points:
Real-time preference evaluation during notification dispatch
Batch processing considers individual user preferences
Admin interface allows bulk preference updates
Section sources
Web Push Subscription Handling#
The Web Push subscription system manages browser push endpoints:
Subscription Data Model:
Endpoint URL for push delivery
Cryptographic keys for secure communication
Creation timestamp for subscription lifecycle management
Unique subscription identifiers
Subscription Lifecycle:
Client browser registers for push notifications
Server validates subscription with VAPID authentication
Subscription stored in user document
Automatic cleanup of expired/invalid subscriptions
Support for user-initiated unsubscription
Section sources
Activity Tracking and Analytics#
The Users collection maintains comprehensive activity metrics:
Tracking Fields:
registered_at: User registration timestamplast_active: Last recorded user interactionnotifications_sent: Running counter for delivered notificationsMetadata fields for demographic and behavioral insights
Analytics Applications:
User engagement analysis
Subscription retention metrics
Platform usage patterns
Personalized notification targeting
Section sources
Component Relationships#
The Users collection participates in several critical dependency relationships:
Diagram sources
External Dependencies#
The Users collection relies on several external systems:
MongoDB Integration:
Connection management through DBClient
Index management for query optimization
Transaction support for atomic operations
Third-party Services:
Telegram Bot API for message delivery
VAPID Web Push infrastructure for browser notifications
External authentication providers for user verification
Section sources
Index Strategy#
The Users collection benefits from strategic indexing:
Critical Indexes:
{ user_id: 1 }(Unique) - Primary user lookup{ subscription_active: 1 }- Active user filtering{ last_active: 1 }- Activity-based queries
Query Optimization:
Efficient user preference filtering
Rapid subscription status checks
Optimized broadcast operations
Scalability Factors#
Horizontal scaling through sharding by user_id
Connection pooling for database operations
Asynchronous processing for notification delivery
Caching strategies for frequently accessed user data
Common Issues and Solutions#
User Registration Failures:
Verify MongoDB connectivity and authentication
Check for duplicate user_id entries
Validate required field constraints
Notification Delivery Problems:
Confirm user subscription status
Verify notification preferences alignment
Check external service availability (Telegram/VAPID)
Web Push Subscription Issues:
Validate VAPID key configuration
Check subscription endpoint validity
Monitor for expired/invalid subscriptions
Section sources
The Users collection schema provides a robust foundation for managing user subscriptions and preferences in the SuperSet Telegram Notification Bot. Its comprehensive design supports multiple notification channels, detailed activity tracking, and flexible subscription management. The integration with the broader notification ecosystem ensures seamless user experience while maintaining data integrity and system performance.
The schema’s flexibility accommodates various user scenarios, from simple Telegram-only subscribers to complex multi-channel users with Web Push subscriptions. The embedded document structure optimizes query performance while maintaining data normalization principles. With proper indexing and monitoring, the Users collection scales effectively to support the application’s growing user base and evolving feature requirements.