FEATURE: better implementation of the admin global message
This commit is contained in:
parent
df610aa01b
commit
ad7e16c33b
17 changed files with 177 additions and 302 deletions
73
pusher/src/Controller/AdminController.ts
Normal file
73
pusher/src/Controller/AdminController.ts
Normal file
|
@ -0,0 +1,73 @@
|
|||
import {BaseController} from "./BaseController";
|
||||
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
|
||||
import {ADMIN_API_TOKEN} from "../Enum/EnvironmentVariable";
|
||||
import {apiClientRepository} from "../Services/ApiClientRepository";
|
||||
import {AdminRoomMessage} from "../Messages/generated/messages_pb";
|
||||
|
||||
|
||||
export class AdminController extends BaseController{
|
||||
|
||||
constructor(private App : TemplatedApp) {
|
||||
super();
|
||||
this.App = App;
|
||||
this.receiveGlobalMessagePrompt();
|
||||
}
|
||||
|
||||
receiveGlobalMessagePrompt() {
|
||||
this.App.options("/message", (res: HttpResponse, req: HttpRequest) => {
|
||||
this.addCorsHeaders(res);
|
||||
res.end();
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
this.App.post("/message", async (res: HttpResponse, req: HttpRequest) => {
|
||||
|
||||
res.onAborted(() => {
|
||||
console.warn('/message request was aborted');
|
||||
})
|
||||
|
||||
|
||||
const token = req.getHeader('admin-token');
|
||||
const body = await res.json();
|
||||
|
||||
if (token !== ADMIN_API_TOKEN) {
|
||||
console.error('Admin access refused for token: '+token)
|
||||
res.writeStatus("401 Unauthorized").end('Incorrect token');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof body.text !== 'string') {
|
||||
throw 'Incorrect text parameter'
|
||||
}
|
||||
if (!body.targets || typeof body.targets !== 'object') {
|
||||
throw 'Incorrect targets parameter'
|
||||
}
|
||||
const text: string = body.text;
|
||||
const targets: string[] = body.targets;
|
||||
|
||||
await Promise.all(targets.map((roomId) => {
|
||||
return apiClientRepository.getClient(roomId).then((roomClient) =>{
|
||||
return new Promise((res, rej) => {
|
||||
const roomMessage = new AdminRoomMessage();
|
||||
roomMessage.setMessage(text);
|
||||
roomMessage.setRoomid(roomId);
|
||||
|
||||
roomClient.sendAdminMessageToRoom(roomMessage, (err) => {
|
||||
err ? rej(err) : res();
|
||||
});
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
} catch (err) {
|
||||
this.errorToResponse(err, res);
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeStatus("200");
|
||||
this.addCorsHeaders(res);
|
||||
res.end('ok');
|
||||
});
|
||||
}
|
||||
}
|
|
@ -56,6 +56,7 @@ export class AuthenticateController extends BaseController {
|
|||
worldSlug,
|
||||
roomSlug,
|
||||
mapUrlStart,
|
||||
organizationMemberToken,
|
||||
textures
|
||||
}));
|
||||
|
||||
|
|
|
@ -21,7 +21,6 @@ import {jwtTokenManager} from "../Services/JWTTokenManager";
|
|||
import {adminApi, CharacterTexture, FetchMemberDataByUuidResponse} from "../Services/AdminApi";
|
||||
import {SocketManager, socketManager} from "../Services/SocketManager";
|
||||
import {emitInBatch} from "../Services/IoSocketHelpers";
|
||||
import {clientEventsEmitter} from "../Services/ClientEventsEmitter";
|
||||
import {ADMIN_API_TOKEN, ADMIN_API_URL, SOCKET_IDLE_TIMER} from "../Enum/EnvironmentVariable";
|
||||
import {Zone} from "_Model/Zone";
|
||||
import {ExAdminSocketInterface} from "_Model/Websocket/ExAdminSocketInterface";
|
||||
|
@ -65,22 +64,6 @@ export class IoSocketController {
|
|||
ws.disconnecting = false;
|
||||
|
||||
socketManager.handleAdminRoom(ws as ExAdminSocketInterface, ws.roomId as string);
|
||||
|
||||
/*ws.send('Data:'+JSON.stringify(socketManager.getAdminSocketDataFor(ws.roomId as string)));
|
||||
ws.clientJoinCallback = (clientUUid: string, roomId: string) => {
|
||||
const wsroomId = ws.roomId as string;
|
||||
if(wsroomId === roomId) {
|
||||
ws.send('MemberJoin:'+clientUUid+';'+roomId);
|
||||
}
|
||||
};
|
||||
ws.clientLeaveCallback = (clientUUid: string, roomId: string) => {
|
||||
const wsroomId = ws.roomId as string;
|
||||
if(wsroomId === roomId) {
|
||||
ws.send('MemberLeave:'+clientUUid+';'+roomId);
|
||||
}
|
||||
};
|
||||
clientEventsEmitter.registerToClientJoin(ws.clientJoinCallback);
|
||||
clientEventsEmitter.registerToClientLeave(ws.clientLeaveCallback);*/
|
||||
},
|
||||
message: (ws, arrayBuffer, isBinary): void => {
|
||||
try {
|
||||
|
@ -107,7 +90,6 @@ export class IoSocketController {
|
|||
const Client = (ws as ExAdminSocketInterface);
|
||||
try {
|
||||
Client.disconnecting = true;
|
||||
//leave room
|
||||
socketManager.leaveAdminRoom(Client);
|
||||
} catch (e) {
|
||||
console.error('An error occurred on admin "disconnect"');
|
||||
|
@ -207,8 +189,6 @@ export class IoSocketController {
|
|||
if (!room.anonymous && room.policyType === GameRoomPolicyTypes.MEMBERS_ONLY_POLICY && userData.anonymous === true) {
|
||||
throw new Error('No correct member')
|
||||
}
|
||||
|
||||
//console.log('access granted for user '+userUuid+' and room '+roomId);
|
||||
} catch (e) {
|
||||
console.log('access not granted for user '+userUuid+' and room '+roomId);
|
||||
console.error(e);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue