2025-07-15 11:52:58 +02:00
|
|
|
import { Server, Socket } from "socket.io";
|
|
|
|
import { PermissionObject, PermissionType, PermissionSection, PermissionModule } from "../type/permissionTypes";
|
|
|
|
import PermissionHelper from "../helpers/permissionHelper";
|
|
|
|
import ForbiddenRequestException from "../exceptions/forbiddenRequestException";
|
|
|
|
import { SocketMap } from "../storage/socketMap";
|
2025-07-15 15:16:11 +02:00
|
|
|
import { SocketConnectionTypes } from "../enums/socketEnum";
|
|
|
|
import SocketServer from ".";
|
2025-07-15 11:52:58 +02:00
|
|
|
|
|
|
|
export type EventResponseType = {
|
|
|
|
answer: any;
|
|
|
|
type:
|
|
|
|
| `package-${string}`
|
|
|
|
| `status-${string}:${string}`
|
|
|
|
| "display"
|
|
|
|
| "warning"
|
|
|
|
| "reload"
|
|
|
|
| "deleted"
|
|
|
|
| "action required";
|
|
|
|
room?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type PermissionPass =
|
|
|
|
| {
|
|
|
|
type: PermissionType;
|
|
|
|
section: PermissionSection;
|
|
|
|
module?: PermissionModule;
|
|
|
|
}
|
2025-07-15 15:16:11 +02:00
|
|
|
| "admin"
|
|
|
|
| "noPermissionsRequired";
|
2025-07-15 11:52:58 +02:00
|
|
|
|
|
|
|
export let handleEvent = (
|
|
|
|
permissions: PermissionPass,
|
|
|
|
handler: (...args: any[]) => Promise<EventResponseType>,
|
|
|
|
socket: Socket
|
|
|
|
) => {
|
|
|
|
return async (...args: any[]) => {
|
|
|
|
try {
|
|
|
|
const socketData = SocketMap.read(socket.id);
|
|
|
|
if (permissions == "admin") {
|
|
|
|
if (!socketData.isOwner && !socketData.permissions.admin) {
|
|
|
|
throw new ForbiddenRequestException(`missing admin permission`);
|
|
|
|
}
|
2025-07-15 15:16:11 +02:00
|
|
|
} else if (permissions != "noPermissionsRequired") {
|
2025-07-15 11:52:58 +02:00
|
|
|
if (
|
|
|
|
!socketData.isOwner &&
|
|
|
|
!PermissionHelper.can(socketData.permissions, permissions.type, permissions.section, permissions.module)
|
|
|
|
) {
|
|
|
|
throw new ForbiddenRequestException(`missing required permission`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const { answer, type, room } = await handler(...args);
|
|
|
|
if (room === undefined || room == "") {
|
|
|
|
socket.emit(type, answer);
|
|
|
|
} else {
|
|
|
|
socket.to(room).emit(type, answer);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
socket.emit("error", e.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2025-07-15 15:16:11 +02:00
|
|
|
export let emitEvent = (
|
|
|
|
event: EventResponseType & { namespace?: SocketConnectionTypes },
|
|
|
|
socket: Socket,
|
|
|
|
io: Server
|
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
const { answer, type, room, namespace } = event;
|
|
|
|
if (room === undefined || room == "") {
|
|
|
|
socket.emit(type, answer);
|
|
|
|
} else if (namespace === undefined) {
|
|
|
|
socket.to(room).emit(type, answer);
|
|
|
|
} else {
|
|
|
|
io.of(namespace).emit(type, answer);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
socket.emit("error", e.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-07-15 11:52:58 +02:00
|
|
|
/**
|
|
|
|
socket.on(
|
|
|
|
"event",
|
|
|
|
{ permissions }
|
|
|
|
handleResponse(
|
|
|
|
async (data:any) => {
|
|
|
|
throw new Error("failed")
|
|
|
|
},
|
|
|
|
socket,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
*/
|