52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
const Notification = require("../../models/Notification");
|
|
|
|
module.exports = (io, socket) => {
|
|
socket.on("notifications:get_all", async () => {
|
|
try {
|
|
const list = await Notification.findAll({
|
|
where: { playerId: socket.user.id },
|
|
order: [["createdAt", "DESC"]],
|
|
limit: 50,
|
|
});
|
|
|
|
socket.emit("notifications:list", list);
|
|
|
|
const unreadCount = list.filter((n) => !n.isRead).length;
|
|
socket.emit("notifications:unread_count", unreadCount);
|
|
} catch (e) {
|
|
console.error("Fetch notifications error:", e);
|
|
}
|
|
});
|
|
|
|
socket.on("notification:read", async ({ id }) => {
|
|
try {
|
|
await Notification.update(
|
|
{ isRead: true },
|
|
{ where: { id, playerId: socket.user.id } },
|
|
);
|
|
|
|
const unreadCount = await Notification.count({
|
|
where: { playerId: socket.user.id, isRead: false },
|
|
});
|
|
socket.emit("notifications:unread_count", unreadCount);
|
|
} catch (e) {
|
|
console.error("Read notification error:", e);
|
|
}
|
|
});
|
|
|
|
socket.on("notification:dismiss", async ({ id }) => {
|
|
try {
|
|
await Notification.destroy({
|
|
where: { id, playerId: socket.user.id },
|
|
});
|
|
|
|
const unreadCount = await Notification.count({
|
|
where: { playerId: socket.user.id, isRead: false },
|
|
});
|
|
socket.emit("notifications:unread_count", unreadCount);
|
|
} catch (e) {
|
|
console.error("Dismiss notification error:", e);
|
|
}
|
|
});
|
|
};
|