import { API, DefaultServerResponse } from '../API'; import io from 'socket.io-client'; import { Events } from './SocketEvents'; import { Config } from '../../lib/utils/Config'; export class RoomRequest { static create(buffer: Buffer) { return new API().upload('/room/create', buffer); } static createPassword(roomId: string, password: string) { return new API().post( '/room/create/' + roomId, JSON.stringify({ password }), 'application/json' ); } // How to use this function // const hash = await getPassword( '' ); // await API.RoomRequest.setPassword(roomId, socket.id, hash); static setPassword(roomId: string, userID: string, password: string) { return new API().post( '/room/set/' + roomId, JSON.stringify({ userID, password }), 'application/json' ); } static getRoom(roomId: string) { return new API().get('/room/' + roomId); } static listRooms() { return new API().get('/room/all'); } static listUsersInRoom(roomId: string) { return new API().get('/room/users/' + roomId); } static downloadRoom(roomId: string) { return new API().download('/room/download/' + roomId); } static createSocket(): Promise { // Create the server instance with the server const socket = io(process.env.SERVER_ENDPOINT, { transports: ['websocket'] }); return new Promise(resolve => { socket.once('connect', () => { // If the server asks for our public config read the config and send it back to the server. socket.on(Events.public_config, () => { Config.get() .then(conf => socket.emit(Events.public_config_res, { config: conf })) .catch(err => socket.emit(Events.public_config_err, 'Failed to get config')); }); resolve(socket); }); }); } }