Notification System
Overview
The notification system alerts users about important events such as comments on their posts. It provides real-time feedback through the UI with unread counts and notification lists.
Components
Models
Notification Model (app/models.py)
python
class Notification(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
content = db.Column(db.Text, nullable=False)
link = db.Column(db.String(256))
is_read = db.Column(db.Boolean, default=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
user = db.relationship('User', backref='notifications')
Routes
Notifications Route (/notifications)
- Requires authentication
- Lists all notifications for current user
- Shows unread count
- Displays notification content
- Links to related content
- Read/unread styling
Mark Read Route (/notifications//read)
- Requires authentication
- Marks notification as read
- Redirects to notification link (if exists)
- Redirects to notifications page (if no link)
Mark All Read Route (/notifications/read_all)
- Requires authentication
- Marks all notifications as read
- Bulk update for efficiency
- Flash confirmation
- Redirects to notifications page
Notification Types
Comment Notification
- Triggered when someone comments on your post
- Content: "{username} commented on your post '{post_title}'"
- Link: Links to the post
- Not sent if commenter is post author
Future Notification Types (Planned)
- Like on your post
- Reply to your comment
- New badge awarded
- User mentioned you
- New message received
- Repository synced
- Admin action on your content
Notification Creation
Automatic Notifications
Comment Addition (app/forum/routes.py)
python
if post.author_id != current_user.id:
notification = Notification(
user_id=post.author_id,
content=f'{current_user.username} commented on your post "{post.title}"',
link=url_for('forum.post', post_id=post.id)
)
db.session.add(notification)
Manual Notifications (Future)
Admin can create notifications
System notifications
Mass notificationsNotification Display
Navbar Badge
Bell icon in navbar
Shows unread count
Magenta color badge
Glowing effect
Hidden when zero unreadNotification List
Chronological order (newest first)
Read/unread styling
Content display
Action links
TimestampRead/Unread Styling
Unread: Cyan border, glow effect
Read: Normal border
Hover effects
Transition animationsNotification Metadata
Content
Human-readable message
Contextual information
Action-orientedLink
Optional link to related content
URL to relevant page
Used for "View" actionRead Status
Boolean flag
Default: false (unread)
Set to true when marked read
Used for unread countTimestamp
Creation time
Displayed in notifications
Used for sortingUnread Count
Calculation
python
unread_count = current_user.notifications.filter_by(is_read=False).count()
Display
Shown in navbar
Shown on notifications page
Updated in real-time
Badge disappears when zeroTemplates
Notifications Template (notification/notifications.html)
Hero section with count
Mark all read button
Notification list
Individual notification items
Read/unread styling
Action linksCSS Styling
Notification Badge
Magenta background
White text
Small font size
Rounded corners
Glow effectNotification Item
Dark card background
Border glow
Unread: Cyan border with glow
Hover effects
Smooth transitionsNotification Content
Primary text color
Readable font size
Clear hierarchyNotification Actions
Cyan color for links
Hover glow effects
Delete action: red hoverIntegration Points
Forum System
Comments on posts trigger notifications
Future: Likes, replies, mentionsMessaging System (Future)
New message notifications
Unread message countAdmin System (Future)
Admin action notifications
System notificationsPerformance Considerations
Database Queries
Filter by user_id
Filter by is_read
Order by created_at DESC
Lazy loading for efficiencyBulk Operations
Mark all read uses bulk update
Efficient for many notifications
Single database queryFuture Optimizations
Pagination for large lists
Notification archiving
Indexed fields (user_id, is_read)
Redis caching for countsBest Practices
Notification Content
Be clear and concise
Include relevant context
Use action-oriented language
Include usernames for personalizationNotification Links
Always provide relevant link
Link to specific content
Make links actionableNotification Volume
Don't overwhelm users
Group similar notifications
Allow notification preferences (future)
Provide digest option (future)Future Enhancements
Notification Preferences
Email notifications
Push notifications (mobile)
Notification frequency settings
Per-type preferences
Quiet hoursNotification Types
Like notifications
Reply notifications
Mention notifications
Badge awarded notifications
Message notifications
System notifications
Admin notificationsNotification Features
Notification grouping
Notification digest
Notification history
Notification search
Notification filtering
Notification archiving
Notification analytics
Real-time push (WebSockets)
Mobile push notifications