|
| 1 | +"""Notification engine and management.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from datetime import datetime |
| 5 | +from typing import Dict, List, Optional |
| 6 | +from enum import Enum |
| 7 | +from dataclasses import dataclass, field |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +class NotificationType(Enum): |
| 13 | + """Types of notifications.""" |
| 14 | + ISSUE_CREATED = "issue_created" |
| 15 | + ISSUE_ASSIGNED = "issue_assigned" |
| 16 | + ISSUE_UPDATED = "issue_updated" |
| 17 | + COMMENT_ADDED = "comment_added" |
| 18 | + MENTION = "mention" |
| 19 | + DEADLINE_REMINDER = "deadline_reminder" |
| 20 | + SYSTEM_ALERT = "system_alert" |
| 21 | + TEAM_UPDATE = "team_update" |
| 22 | + PROJECT_UPDATE = "project_update" |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class NotificationPreferences: |
| 27 | + """User notification preferences.""" |
| 28 | + user_id: str |
| 29 | + enabled: bool = True |
| 30 | + email_enabled: bool = True |
| 31 | + push_enabled: bool = True |
| 32 | + in_app_enabled: bool = True |
| 33 | + notification_types: Dict[str, bool] = field(default_factory=dict) |
| 34 | + quiet_hours_enabled: bool = False |
| 35 | + quiet_hours_start: str = "22:00" |
| 36 | + quiet_hours_end: str = "08:00" |
| 37 | + mute_until: Optional[str] = None |
| 38 | + |
| 39 | + def __post_init__(self): |
| 40 | + if not self.notification_types: |
| 41 | + # Set defaults |
| 42 | + self.notification_types = { |
| 43 | + NotificationType.ISSUE_CREATED.value: True, |
| 44 | + NotificationType.ISSUE_ASSIGNED.value: True, |
| 45 | + NotificationType.ISSUE_UPDATED.value: True, |
| 46 | + NotificationType.COMMENT_ADDED.value: True, |
| 47 | + NotificationType.MENTION.value: True, |
| 48 | + NotificationType.DEADLINE_REMINDER.value: True, |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +@dataclass |
| 53 | +class Notification: |
| 54 | + """Notification object.""" |
| 55 | + id: str |
| 56 | + user_id: str |
| 57 | + notification_type: NotificationType |
| 58 | + title: str |
| 59 | + body: str |
| 60 | + icon: str |
| 61 | + badge: str = None |
| 62 | + tag: str = None |
| 63 | + data: Dict = field(default_factory=dict) |
| 64 | + timestamp: str = field(default_factory=lambda: datetime.now().isoformat()) |
| 65 | + is_read: bool = False |
| 66 | + is_clicked: bool = False |
| 67 | + action_url: Optional[str] = None |
| 68 | + |
| 69 | + |
| 70 | +class NotificationEngine: |
| 71 | + """Central notification engine.""" |
| 72 | + |
| 73 | + def __init__(self): |
| 74 | + """Initialize notification engine.""" |
| 75 | + self.notifications: Dict[str, Notification] = {} |
| 76 | + self.user_preferences: Dict[str, NotificationPreferences] = {} |
| 77 | + self.notification_queue: List[Notification] = [] |
| 78 | + self.sent_count = 0 |
| 79 | + self.failed_count = 0 |
| 80 | + |
| 81 | + def set_user_preferences(self, user_id: str, |
| 82 | + preferences: NotificationPreferences) -> None: |
| 83 | + """ |
| 84 | + Set user notification preferences. |
| 85 | + |
| 86 | + Args: |
| 87 | + user_id: User ID |
| 88 | + preferences: NotificationPreferences instance |
| 89 | + """ |
| 90 | + self.user_preferences[user_id] = preferences |
| 91 | + logger.info(f"Preferences updated for user: {user_id}") |
| 92 | + |
| 93 | + def get_user_preferences(self, user_id: str) -> NotificationPreferences: |
| 94 | + """ |
| 95 | + Get user notification preferences. |
| 96 | + |
| 97 | + Args: |
| 98 | + user_id: User ID |
| 99 | + |
| 100 | + Returns: |
| 101 | + NotificationPreferences |
| 102 | + """ |
| 103 | + if user_id not in self.user_preferences: |
| 104 | + self.user_preferences[user_id] = NotificationPreferences(user_id=user_id) |
| 105 | + |
| 106 | + return self.user_preferences[user_id] |
| 107 | + |
| 108 | + def create_notification(self, user_id: str, notification_type: NotificationType, |
| 109 | + title: str, body: str, icon: str, |
| 110 | + data: Dict = None, action_url: str = None) -> Notification: |
| 111 | + """ |
| 112 | + Create a new notification. |
| 113 | + |
| 114 | + Args: |
| 115 | + user_id: User ID |
| 116 | + notification_type: Type of notification |
| 117 | + title: Notification title |
| 118 | + body: Notification body |
| 119 | + icon: Icon URL |
| 120 | + data: Additional data |
| 121 | + action_url: Action URL when clicked |
| 122 | + |
| 123 | + Returns: |
| 124 | + Notification object |
| 125 | + """ |
| 126 | + notification_id = f"notif_{int(datetime.now().timestamp() * 1000)}" |
| 127 | + |
| 128 | + notification = Notification( |
| 129 | + id=notification_id, |
| 130 | + user_id=user_id, |
| 131 | + notification_type=notification_type, |
| 132 | + title=title, |
| 133 | + body=body, |
| 134 | + icon=icon, |
| 135 | + data=data or {}, |
| 136 | + action_url=action_url, |
| 137 | + tag=notification_type.value |
| 138 | + ) |
| 139 | + |
| 140 | + self.notifications[notification_id] = notification |
| 141 | + self.notification_queue.append(notification) |
| 142 | + |
| 143 | + logger.info(f"Notification created: {notification_id}") |
| 144 | + return notification |
| 145 | + |
| 146 | + def send_notification(self, notification: Notification) -> Dict: |
| 147 | + """ |
| 148 | + Send a notification. |
| 149 | + |
| 150 | + Args: |
| 151 | + notification: Notification to send |
| 152 | + |
| 153 | + Returns: |
| 154 | + Send result |
| 155 | + """ |
| 156 | + preferences = self.get_user_preferences(notification.user_id) |
| 157 | + |
| 158 | + # Check if notifications are enabled |
| 159 | + if not preferences.enabled: |
| 160 | + logger.warning(f"Notifications disabled for user {notification.user_id}") |
| 161 | + return {'status': 'disabled'} |
| 162 | + |
| 163 | + # Check notification type preference |
| 164 | + notif_type_key = notification.notification_type.value |
| 165 | + if not preferences.notification_types.get(notif_type_key, True): |
| 166 | + logger.warning(f"Notification type disabled for user: {notif_type_key}") |
| 167 | + return {'status': 'type_disabled'} |
| 168 | + |
| 169 | + # Check quiet hours |
| 170 | + if preferences.quiet_hours_enabled: |
| 171 | + # Implement quiet hours logic |
| 172 | + pass |
| 173 | + |
| 174 | + # Check muted |
| 175 | + if preferences.mute_until: |
| 176 | + mute_until = datetime.fromisoformat(preferences.mute_until) |
| 177 | + if datetime.now() < mute_until: |
| 178 | + return {'status': 'muted'} |
| 179 | + |
| 180 | + try: |
| 181 | + # Send through available channels |
| 182 | + results = {} |
| 183 | + |
| 184 | + if preferences.push_enabled: |
| 185 | + results['push'] = self._send_push(notification) |
| 186 | + |
| 187 | + if preferences.email_enabled: |
| 188 | + results['email'] = self._send_email(notification) |
| 189 | + |
| 190 | + if preferences.in_app_enabled: |
| 191 | + results['in_app'] = self._send_in_app(notification) |
| 192 | + |
| 193 | + self.sent_count += 1 |
| 194 | + logger.info(f"Notification sent: {notification.id}") |
| 195 | + |
| 196 | + return { |
| 197 | + 'status': 'success', |
| 198 | + 'notification_id': notification.id, |
| 199 | + 'channels': results |
| 200 | + } |
| 201 | + |
| 202 | + except Exception as e: |
| 203 | + self.failed_count += 1 |
| 204 | + logger.error(f"Failed to send notification: {e}") |
| 205 | + return {'status': 'error', 'error': str(e)} |
| 206 | + |
| 207 | + def _send_push(self, notification: Notification) -> Dict: |
| 208 | + """Send push notification.""" |
| 209 | + return {'status': 'queued', 'method': 'push'} |
| 210 | + |
| 211 | + def _send_email(self, notification: Notification) -> Dict: |
| 212 | + """Send email notification.""" |
| 213 | + return {'status': 'queued', 'method': 'email'} |
| 214 | + |
| 215 | + def _send_in_app(self, notification: Notification) -> Dict: |
| 216 | + """Store in-app notification.""" |
| 217 | + return {'status': 'stored', 'method': 'in_app'} |
| 218 | + |
| 219 | + def mark_as_read(self, notification_id: str) -> bool: |
| 220 | + """ |
| 221 | + Mark notification as read. |
| 222 | + |
| 223 | + Args: |
| 224 | + notification_id: Notification ID |
| 225 | + |
| 226 | + Returns: |
| 227 | + True if successful |
| 228 | + """ |
| 229 | + if notification_id in self.notifications: |
| 230 | + self.notifications[notification_id].is_read = True |
| 231 | + return True |
| 232 | + return False |
| 233 | + |
| 234 | + def mark_as_clicked(self, notification_id: str) -> bool: |
| 235 | + """ |
| 236 | + Mark notification as clicked. |
| 237 | + |
| 238 | + Args: |
| 239 | + notification_id: Notification ID |
| 240 | + |
| 241 | + Returns: |
| 242 | + True if successful |
| 243 | + """ |
| 244 | + if notification_id in self.notifications: |
| 245 | + self.notifications[notification_id].is_clicked = True |
| 246 | + return True |
| 247 | + return False |
| 248 | + |
| 249 | + def get_user_notifications(self, user_id: str, |
| 250 | + unread_only: bool = False) -> List[Notification]: |
| 251 | + """ |
| 252 | + Get notifications for a user. |
| 253 | + |
| 254 | + Args: |
| 255 | + user_id: User ID |
| 256 | + unread_only: Only return unread |
| 257 | + |
| 258 | + Returns: |
| 259 | + List of notifications |
| 260 | + """ |
| 261 | + notifications = [ |
| 262 | + n for n in self.notifications.values() |
| 263 | + if n.user_id == user_id |
| 264 | + ] |
| 265 | + |
| 266 | + if unread_only: |
| 267 | + notifications = [n for n in notifications if not n.is_read] |
| 268 | + |
| 269 | + # Sort by timestamp, newest first |
| 270 | + return sorted(notifications, key=lambda n: n.timestamp, reverse=True) |
| 271 | + |
| 272 | + def get_notification_history(self, user_id: str, limit: int = 50) -> List[Dict]: |
| 273 | + """ |
| 274 | + Get notification history for user. |
| 275 | + |
| 276 | + Args: |
| 277 | + user_id: User ID |
| 278 | + limit: Maximum to return |
| 279 | + |
| 280 | + Returns: |
| 281 | + List of notification dicts |
| 282 | + """ |
| 283 | + notifications = self.get_user_notifications(user_id) |
| 284 | + return [ |
| 285 | + { |
| 286 | + 'id': n.id, |
| 287 | + 'type': n.notification_type.value, |
| 288 | + 'title': n.title, |
| 289 | + 'body': n.body, |
| 290 | + 'timestamp': n.timestamp, |
| 291 | + 'is_read': n.is_read, |
| 292 | + 'action_url': n.action_url |
| 293 | + } |
| 294 | + for n in notifications[:limit] |
| 295 | + ] |
| 296 | + |
| 297 | + def mute_notifications(self, user_id: str, minutes: int = 60) -> None: |
| 298 | + """ |
| 299 | + Mute notifications for a user. |
| 300 | + |
| 301 | + Args: |
| 302 | + user_id: User ID |
| 303 | + minutes: Duration in minutes |
| 304 | + """ |
| 305 | + from datetime import timedelta |
| 306 | + mute_until = datetime.now() + timedelta(minutes=minutes) |
| 307 | + |
| 308 | + prefs = self.get_user_preferences(user_id) |
| 309 | + prefs.mute_until = mute_until.isoformat() |
| 310 | + |
| 311 | + logger.info(f"Notifications muted for {user_id} for {minutes} minutes") |
| 312 | + |
| 313 | + def unmute_notifications(self, user_id: str) -> None: |
| 314 | + """ |
| 315 | + Unmute notifications for a user. |
| 316 | + |
| 317 | + Args: |
| 318 | + user_id: User ID |
| 319 | + """ |
| 320 | + prefs = self.get_user_preferences(user_id) |
| 321 | + prefs.mute_until = None |
| 322 | + logger.info(f"Notifications unmuted for {user_id}") |
| 323 | + |
| 324 | + def get_stats(self) -> Dict: |
| 325 | + """ |
| 326 | + Get notification engine statistics. |
| 327 | + |
| 328 | + Returns: |
| 329 | + Dictionary with stats |
| 330 | + """ |
| 331 | + unread_count = sum(1 for n in self.notifications.values() if not n.is_read) |
| 332 | + |
| 333 | + return { |
| 334 | + 'total_notifications': len(self.notifications), |
| 335 | + 'unread_notifications': unread_count, |
| 336 | + 'sent_count': self.sent_count, |
| 337 | + 'failed_count': self.failed_count, |
| 338 | + 'users_with_preferences': len(self.user_preferences), |
| 339 | + 'queue_size': len(self.notification_queue) |
| 340 | + } |
0 commit comments