add basic socketio to server
This commit is contained in:
parent
45ec6b856a
commit
b29cdae088
8 changed files with 252 additions and 1 deletions
72
src/websocket/handleEvent.ts
Normal file
72
src/websocket/handleEvent.ts
Normal file
|
@ -0,0 +1,72 @@
|
|||
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";
|
||||
|
||||
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;
|
||||
}
|
||||
| "admin";
|
||||
|
||||
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`);
|
||||
}
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
socket.on(
|
||||
"event",
|
||||
{ permissions }
|
||||
handleResponse(
|
||||
async (data:any) => {
|
||||
throw new Error("failed")
|
||||
},
|
||||
socket,
|
||||
)
|
||||
);
|
||||
*/
|
Loading…
Add table
Add a link
Reference in a new issue