Webatrice admin commands (#5051)

* Add AdminCommand.updateServerMessage

* Add AdminCommand.shutdownServer

* Add AdminCommand.reloadConfig

* Cleanup

* Add AdminCommand.adjustMod

* Lint

* Lint
This commit is contained in:
Zach H
2024-06-12 22:52:40 -04:00
committed by GitHub
parent e45c4042fe
commit 34d70980e8
11 changed files with 167 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
import { AdminCommands } from 'websocket';
export default class AdminService {
static adjustMod(userName: string, shouldBeMod?: boolean, shouldBeJudge?: boolean): void {
AdminCommands.adjustMod(userName, shouldBeMod, shouldBeJudge);
}
static reloadConfig(): void {
AdminCommands.reloadConfig();
}
static shutdownServer(reason: string, minutes: number): void {
AdminCommands.shutdownServer(reason, minutes);
}
static updateServerMessage(): void {
AdminCommands.updateServerMessage();
}
}

View File

@@ -1,3 +1,4 @@
export { default as AdminService } from './AdminService';
export { default as AuthenticationService } from './AuthenticationService';
export { default as ModeratorService } from './ModeratorService';
export { default as RoomsService } from './RoomsService';

View File

@@ -0,0 +1,29 @@
import webClient from '../../WebClient';
import { AdminPersistence } from '../../persistence';
export function adjustMod(userName: string, shouldBeMod?: boolean, shouldBeJudge?: boolean): void {
const command = webClient.protobuf.controller.Command_AdjustMod.create({ userName, shouldBeMod, shouldBeJudge });
const sc = webClient.protobuf.controller.AdminCommand.create({
'.Command_AdjustMod.ext': command
});
webClient.protobuf.sendAdminCommand(sc, (raw) => {
const { responseCode } = raw;
let error: string;
switch (responseCode) {
case webClient.protobuf.controller.Response.ResponseCode.RespOk:
AdminPersistence.adjustMod(userName, shouldBeMod, shouldBeJudge);
return;
default:
error = 'Failed to reload config.';
break;
}
if (error) {
console.error(responseCode, error);
}
});
}

View File

@@ -0,0 +1,4 @@
export * from './adjustMod';
export * from './reloadConfig';
export * from './shutdownServer';
export * from './updateServerMessage';

View File

@@ -0,0 +1,29 @@
import webClient from '../../WebClient';
import { AdminPersistence } from '../../persistence';
export function reloadConfig(): void {
const command = webClient.protobuf.controller.Command_ReloadConfig.create();
const sc = webClient.protobuf.controller.AdminCommand.create({
'.Command_ReloadConfig.ext': command
});
webClient.protobuf.sendAdminCommand(sc, (raw) => {
const { responseCode } = raw;
let error: string;
switch (responseCode) {
case webClient.protobuf.controller.Response.ResponseCode.RespOk:
AdminPersistence.reloadConfig();
return;
default:
error = 'Failed to reload config.';
break;
}
if (error) {
console.error(responseCode, error);
}
});
}

View File

@@ -0,0 +1,29 @@
import webClient from '../../WebClient';
import { AdminPersistence } from '../../persistence';
export function shutdownServer(reason: string, minutes: number): void {
const command = webClient.protobuf.controller.Command_ShutdownServer.create({ reason, minutes });
const sc = webClient.protobuf.controller.AdminCommand.create({
'.Command_ShutdownServer.ext': command
});
webClient.protobuf.sendAdminCommand(sc, (raw) => {
const { responseCode } = raw;
let error: string;
switch (responseCode) {
case webClient.protobuf.controller.Response.ResponseCode.RespOk:
AdminPersistence.shutdownServer();
return;
default:
error = 'Failed to update server message.';
break;
}
if (error) {
console.error(responseCode, error);
}
});
}

View File

@@ -0,0 +1,29 @@
import webClient from '../../WebClient';
import { AdminPersistence } from '../../persistence';
export function updateServerMessage(): void {
const command = webClient.protobuf.controller.Command_UpdateServerMessage.create();
const sc = webClient.protobuf.controller.AdminCommand.create({
'.Command_UpdateServerMessage.ext': command
});
webClient.protobuf.sendAdminCommand(sc, (raw) => {
const { responseCode } = raw;
let error: string;
switch (responseCode) {
case webClient.protobuf.controller.Response.ResponseCode.RespOk:
AdminPersistence.updateServerMessage();
return;
default:
error = 'Failed to update server message.';
break;
}
if (error) {
console.error(responseCode, error);
}
});
}

View File

@@ -1,3 +1,4 @@
export * as AdminCommands from './admin';
export * as ModeratorCommands from './moderator';
export * as RoomCommands from './room';
export * as SessionCommands from './session';

View File

@@ -0,0 +1,17 @@
export class AdminPersistence {
static adjustMod(userName: string, shouldBeMod: boolean, shouldBeJudge: boolean) {
console.log('adjustMod');
}
static reloadConfig() {
console.log('reloadConfig');
}
static shutdownServer() {
console.log('shutdownServer');
}
static updateServerMessage() {
console.log('updateServerMessage');
}
}

View File

@@ -1,3 +1,4 @@
export { AdminPersistence } from './AdminPresistence';
export { RoomPersistence } from './RoomPersistence';
export { SessionPersistence } from './SessionPersistence';
export { ModeratorPersistence } from './ModeratorPresistence';

View File

@@ -55,6 +55,14 @@ export class ProtobufService {
this.sendCommand(cmd, (raw) => callback && callback(raw));
}
public sendAdminCommand(adminCmd: number, callback?: Function) {
const cmd = this.controller.CommandContainer.create({
'adminCommand': [adminCmd]
});
this.sendCommand(cmd, (raw) => callback && callback(raw));
}
public sendCommand(cmd: number, callback: Function) {
this.cmdId++;