forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotificationController.js
More file actions
445 lines (387 loc) · 12.7 KB
/
Copy pathnotificationController.js
File metadata and controls
445 lines (387 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import Notification from "../models/Notification.js";
import logger from "../config/logger.js";
import User from "../models/User.js";
import { getRedisClient, isRedisReady } from "../config/redis.js";
/**
* SSE Connections Map
* Maps userId (string) -> Set of active Express Response streams.
* Allows multiple concurrent browser tabs/sessions per user and proper auth-scoped cleanup on disconnect.
*/
const sseConnections = new Map();
/**
* Multi-Instance SSE Delivery via Redis Pub/Sub:
* When horizontally scaled (e.g. on Render), an instance producing a notification
* publishes the event to the Redis channel 'notifications:sse'. All backend instances
* listen on this channel and deliver the payload to any locally connected client SSE stream.
* If Redis is unavailable, the system falls back seamlessly to in-memory local delivery.
*/
let isSubscriberInit = false;
export const initRedisSubscriber = async () => {
if (isSubscriberInit) return;
if (!isRedisReady()) return;
try {
const mainClient = getRedisClient();
if (!mainClient) return;
const subClient = mainClient.duplicate();
await subClient.connect();
await subClient.subscribe("notifications:sse", (message) => {
try {
const { recipientId, notification } = JSON.parse(message);
deliverLocalSSENotification(recipientId, notification);
} catch (err) {
logger.error("Error handling Redis SSE pub/sub message:", err);
}
});
isSubscriberInit = true;
logger.info("✅ Redis SSE Pub/Sub subscriber initialized");
} catch (err) {
logger.error("Failed to initialize Redis SSE subscriber:", err);
}
};
/**
* Deliver SSE payload to active local connections for a given user
*/
export const deliverLocalSSENotification = (recipientId, notification) => {
const userConns = sseConnections.get(recipientId.toString());
if (userConns && userConns.size > 0) {
const payload = `data: ${JSON.stringify({
type: "new_notification",
notification,
})}\n\n`;
userConns.forEach((res) => {
try {
res.write(payload);
} catch (err) {
logger.error(`Error writing SSE payload to user ${recipientId}:`, err);
}
});
}
};
/**
* Dispatch SSE notification across multi-instance deployment via Redis or local Map
*/
export const dispatchSSENotification = async (recipientId, notification) => {
if (isRedisReady()) {
try {
await initRedisSubscriber();
const client = getRedisClient();
if (client) {
await client.publish(
"notifications:sse",
JSON.stringify({ recipientId: recipientId.toString(), notification })
);
return;
}
} catch (err) {
logger.error("Error publishing SSE notification to Redis:", err);
}
}
// Fallback to local delivery if Redis is not active or fails
deliverLocalSSENotification(recipientId, notification);
};
// SSE endpoint for real-time notifications
export const sseNotifications = async (req, res) => {
const userId = req.user._id.toString();
// Set SSE headers
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Cache-Control",
});
// Send initial connection message
res.write(
`data: ${JSON.stringify({
type: "connection",
message: "Connected to notifications",
})}\n\n`
);
// Store connection in user's connection set
if (!sseConnections.has(userId)) {
sseConnections.set(userId, new Set());
}
const userConns = sseConnections.get(userId);
userConns.add(res);
// Initialize Redis subscriber if available
initRedisSubscriber().catch((err) =>
logger.error("Redis subscriber init error:", err)
);
// Handle client disconnect
req.on("close", () => {
const conns = sseConnections.get(userId);
if (conns) {
conns.delete(res);
if (conns.size === 0) {
sseConnections.delete(userId);
}
}
logger.info(`SSE connection closed for user: ${userId}`);
});
// Keep connection alive
const keepAlive = setInterval(() => {
const conns = sseConnections.get(userId);
if (conns && conns.has(res)) {
res.write(
`data: ${JSON.stringify({ type: "ping", timestamp: Date.now() })}\n\n`
);
} else {
clearInterval(keepAlive);
}
}, 30000);
};
// Send notification to specific user (for real-time updates)
export const sendNotificationToUser = async (userId, notificationData) => {
try {
const notification = await Notification.create({
recipient: userId,
...notificationData,
});
// Populate sender info
await notification.populate("sender", "name avatar");
// Dispatch via SSE
await dispatchSSENotification(userId, notification);
return notification;
} catch (error) {
logger.error("Error sending notification:", error);
throw error;
}
};
// Create follow notification
export const createFollowNotification = async (followerId, followedId) => {
const follower = await User.findById(followerId).select("name avatar");
if (!follower) return;
return sendNotificationToUser(followedId, {
sender: followerId,
type: "follow",
title: "New Follower",
message: `${follower.name} started following you`,
priority: "medium",
});
};
// Create unfollow notification
export const createUnfollowNotification = async (unfollowerId, unfollowedId) => {
const unfollower = await User.findById(unfollowerId).select("name avatar");
if (!unfollower) return;
return sendNotificationToUser(unfollowedId, {
sender: unfollowerId,
type: "unfollow",
title: "User Unfollowed",
message: `${unfollower.name} unfollowed you`,
priority: "low",
});
};
// Create new course notification for followers (batched fan-out)
export const createNewCourseNotification = async (courseId, creatorId, courseTitle) => {
try {
const creator = await User.findById(creatorId).select("name avatar followers");
if (!creator || !creator.followers || creator.followers.length === 0) return [];
const docs = creator.followers.map((followerId) => ({
recipient: followerId,
sender: creatorId,
type: "new_course",
title: "New Course Available",
message: `${creator.name} created a new course: ${courseTitle}`,
data: { courseId },
priority: "medium",
}));
// Batched DB insert in one write operation
const createdNotifications = await Notification.insertMany(docs);
// Broadcast SSE notifications with pre-populated sender info
const senderPayload = { _id: creatorId, name: creator.name, avatar: creator.avatar };
for (const notification of createdNotifications) {
const plainNotif = notification.toObject ? notification.toObject() : { ...notification };
plainNotif.sender = senderPayload;
dispatchSSENotification(plainNotif.recipient, plainNotif);
}
return createdNotifications;
} catch (error) {
logger.error("Error creating new course notifications:", error);
throw error;
}
};
// Create new book notification for followers (batched fan-out)
export const createNewBookNotification = async (bookId, authorId, bookTitle) => {
try {
const author = await User.findById(authorId).select("name avatar followers");
if (!author || !author.followers || author.followers.length === 0) return [];
const docs = author.followers.map((followerId) => ({
recipient: followerId,
sender: authorId,
type: "new_book",
title: "New Book Available",
message: `${author.name} published a new book: ${bookTitle}`,
data: { bookId },
priority: "medium",
}));
// Batched DB insert in one write operation
const createdNotifications = await Notification.insertMany(docs);
// Broadcast SSE notifications with pre-populated author info
const senderPayload = { _id: authorId, name: author.name, avatar: author.avatar };
for (const notification of createdNotifications) {
const plainNotif = notification.toObject ? notification.toObject() : { ...notification };
plainNotif.sender = senderPayload;
dispatchSSENotification(plainNotif.recipient, plainNotif);
}
return createdNotifications;
} catch (error) {
logger.error("Error creating new book notifications:", error);
throw error;
}
};
// Get user notifications (bounded pagination)
export const getUserNotifications = async (req, res) => {
try {
const userId = req.user._id;
let page = parseInt(req.query.page, 10);
if (isNaN(page) || page < 1) page = 1;
let limit = parseInt(req.query.limit, 10);
if (isNaN(limit) || limit < 1) limit = 20;
if (limit > 100) limit = 100; // Enforce pagination cap of 100
const unreadOnly = req.query.unreadOnly === "true";
const query = {
recipient: userId,
isDeleted: false,
};
if (unreadOnly) {
query.isRead = false;
}
const skip = (page - 1) * limit;
const notifications = await Notification.find(query)
.populate("sender", "name avatar")
.populate("data.courseId", "title thumbnail")
.populate("data.bookId", "title image")
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit)
.lean();
const total = await Notification.countDocuments(query);
const unreadCount = await Notification.countDocuments({
recipient: userId,
isRead: false,
isDeleted: false,
});
res.status(200).json({
success: true,
notifications,
pagination: {
currentPage: page,
totalPages: Math.ceil(total / limit) || 1,
totalNotifications: total,
hasNextPage: page * limit < total,
hasPrevPage: page > 1,
},
unreadCount,
});
} catch (error) {
logger.error("Get notifications error:", error);
res.status(500).json({
success: false,
message: "Failed to fetch notifications",
error: error.message,
});
}
};
// Mark notification as read
export const markNotificationAsRead = async (req, res) => {
try {
const { notificationId } = req.params;
const userId = req.user._id;
const notification = await Notification.findOneAndUpdate(
{ _id: notificationId, recipient: userId },
{ isRead: true },
{ new: true }
);
if (!notification) {
return res.status(404).json({
success: false,
message: "Notification not found",
});
}
res.status(200).json({
success: true,
notification,
});
} catch (error) {
logger.error("Mark notification read error:", error);
res.status(500).json({
success: false,
message: "Failed to mark notification as read",
error: error.message,
});
}
};
// Mark all notifications as read
export const markAllNotificationsAsRead = async (req, res) => {
try {
const userId = req.user._id;
await Notification.updateMany(
{ recipient: userId, isRead: false, isDeleted: false },
{ $set: { isRead: true } }
);
res.status(200).json({
success: true,
message: "All notifications marked as read",
});
} catch (error) {
logger.error("Mark all notifications read error:", error);
res.status(500).json({
success: false,
message: "Failed to mark notifications as read",
error: error.message,
});
}
};
// Delete notification
export const deleteNotification = async (req, res) => {
try {
const { notificationId } = req.params;
const userId = req.user._id;
const notification = await Notification.findOneAndUpdate(
{ _id: notificationId, recipient: userId },
{ isDeleted: true },
{ new: true }
);
if (!notification) {
return res.status(404).json({
success: false,
message: "Notification not found",
});
}
res.status(200).json({
success: true,
message: "Notification deleted",
});
} catch (error) {
logger.error("Delete notification error:", error);
res.status(500).json({
success: false,
message: "Failed to delete notification",
error: error.message,
});
}
};
// Get notification settings (for future use)
export const getNotificationSettings = async (req, res) => {
try {
const userId = req.user._id;
const user = await User.findById(userId).select("notificationSettings");
res.status(200).json({
success: true,
settings: user?.notificationSettings || {
follow: true,
newContent: true,
likes: true,
comments: true,
system: true,
},
});
} catch (error) {
logger.error("Get notification settings error:", error);
res.status(500).json({
success: false,
message: "Failed to fetch notification settings",
error: error.message,
});
}
};