This repository has been archived on 2026-05-04. You can view files and clone it, but cannot push or open issues or pull requests.
Galaxy-Strike-Online/game-server/src/sockets/handlers/notificationHandler.js

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);
}
});
};