uyem
Version:
WebRTC client-server SFU application
602 lines • 20.9 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/******************************************************************************************
* Repository: https://github.com/kolserdav/werift-sfu-react.git
* File name: settings.ts
* Author: Sergey Kolmiller
* Email: <uyem.ru@gmail.com>
* License: MIT
* License text: See in LICENSE file
* Copyright: kolserdav, All rights reserved (c)
* Create Date: Wed Nov 23 2022 15:23:26 GMT+0700 (Krasnoyarsk Standard Time)
******************************************************************************************/
// eslint-disable-next-line max-classes-per-file
const fs_1 = __importStar(require("fs"));
const interfaces_1 = require("../types/interfaces");
const lib_1 = require("../utils/lib");
const db_1 = __importDefault(require("../core/db"));
class Settings extends db_1.default {
users = {};
cloudPath;
constructor({ cloudPath, prisma }) {
super({ prisma });
this.cloudPath = cloudPath;
}
setUnit = ({ roomId, userId, ws, locale, connId }) => {
const lang = (0, lib_1.getLocale)(locale).server;
if (!this.users[roomId]) {
this.users[roomId] = {};
}
if (this.users[roomId][userId]) {
(0, lib_1.log)('warn', 'Duplicate settings user', { roomId, userId });
}
this.users[roomId][userId] = {
locale,
ws,
connId,
};
if ((0, lib_1.checkDefaultAuth)({ unitId: userId.toString() })) {
this.sendMessage({
roomId,
msg: {
id: userId,
connId,
type: interfaces_1.MessageType.SET_ERROR,
data: {
type: 'warn',
message: lang.notAuthorised,
code: interfaces_1.ErrorCode.notAuthorised,
},
},
}, () => {
delete this.users[roomId][userId];
});
return;
}
this.sendMessage({
roomId,
msg: {
id: userId,
connId,
type: interfaces_1.MessageType.SET_SETTINGS_UNIT,
data: undefined,
},
});
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sendMessage = ({ msg, roomId }, cb) => {
const { id } = msg;
return new Promise((resolve) => {
setTimeout(() => {
let res = '';
try {
res = JSON.stringify(msg);
}
catch (e) {
(0, lib_1.log)('error', 'Error send settings message', e);
resolve(1);
}
if (!this.users[roomId][id]) {
(0, lib_1.log)('warn', 'Settings user not found', { roomId, id });
resolve(1);
return;
}
this.users[roomId][id].ws.send(res, cb);
resolve(0);
}, 0);
});
};
cleanUnit = ({ roomId, userId }) => {
if (!this.users[roomId][userId]) {
(0, lib_1.log)('warn', 'Settings user can not remove', { roomId, userId });
return;
}
delete this.users[roomId][userId];
};
getLocale({ userId, roomId }) {
return (0, lib_1.getLocale)(this.users[roomId][userId].locale).server;
}
async videoFindManyHandler({ id, data: { args, userId, token }, connId, }) {
const locale = this.getLocale({ roomId: id, userId });
const { errorCode, unitId } = await this.checkTokenCb({ token });
const isDefault = (0, lib_1.checkDefaultAuth)({ unitId });
if (errorCode !== 0 && !isDefault) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.forbidden,
code: interfaces_1.ErrorCode.forbidden,
},
},
});
return;
}
if (!isDefault && userId.toString() !== unitId) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.notAuthorised,
code: interfaces_1.ErrorCode.notAuthorised,
},
},
});
return;
}
const videos = await this.videoFindMany({ ...args });
if (videos === undefined) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'error',
message: locale.serverError,
code: interfaces_1.ErrorCode.serverError,
},
},
});
return;
}
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_VIDEO_FIND_MANY,
id: userId,
connId,
data: {
videos,
},
},
});
}
async videoFindFirstHandler({ id, data: { args, userId, token }, connId, }) {
const locale = this.getLocale({ roomId: id, userId });
const { errorCode, unitId } = await this.checkTokenCb({ token });
const isDefault = (0, lib_1.checkDefaultAuth)({ unitId });
if (errorCode !== 0 && !isDefault) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.forbidden,
code: interfaces_1.ErrorCode.forbidden,
},
},
});
return;
}
if (!isDefault && userId.toString() !== unitId) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.notAuthorised,
code: interfaces_1.ErrorCode.notAuthorised,
},
},
});
return;
}
const video = await this.videoFindFirst({ ...args });
if (video === undefined) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'error',
message: locale.serverError,
code: interfaces_1.ErrorCode.serverError,
},
},
});
return;
}
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_VIDEO_FIND_FIRST,
id: userId,
connId,
data: {
video,
},
},
});
}
async videoDeleteHandler({ id, data: { args, userId, token }, connId, }) {
const locale = this.getLocale({ roomId: id, userId });
const { errorCode, unitId } = await this.checkTokenCb({ token });
const isDefault = (0, lib_1.checkDefaultAuth)({ unitId });
// TODO refactor checks to one auth method
if (errorCode !== 0 && !isDefault) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.forbidden,
code: interfaces_1.ErrorCode.forbidden,
},
},
});
return;
}
if (!isDefault && userId.toString() !== unitId) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.notAuthorised,
code: interfaces_1.ErrorCode.notAuthorised,
},
},
});
return;
}
if (!args.where.id) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.badRequest,
code: interfaces_1.ErrorCode.badRequest,
},
},
});
return;
}
const _video = await this.videoFindFirst({
where: {
id: args.where.id,
},
include: {
Room: {
select: {
id: true,
authorId: true,
},
},
},
});
if (_video === undefined) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'error',
message: locale.serverError,
code: interfaces_1.ErrorCode.serverError,
},
},
});
return;
}
if (_video === null) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.notFound,
code: interfaces_1.ErrorCode.notFound,
},
},
});
return;
}
if (userId.toString() !== _video.Room.authorId || id.toString() !== _video.Room.id) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.notAuthorised,
code: interfaces_1.ErrorCode.notAuthorised,
},
},
});
return;
}
const video = await this.videoDelete({ ...args });
if (video === undefined) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'error',
message: locale.serverError,
code: interfaces_1.ErrorCode.serverError,
},
},
});
return;
}
if (video === null) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.notFound,
code: interfaces_1.ErrorCode.notFound,
},
},
});
return;
}
const videoPath = (0, lib_1.getVideoPath)({ cloudPath: this.cloudPath, roomId: id, name: video.name });
fs_1.default.rmSync(videoPath);
const videosDirPath = (0, lib_1.getVideosDirPath)({ cloudPath: this.cloudPath });
const roomDirPath = (0, lib_1.getRoomDirPath)({ videosDirPath, roomId: id });
const dir = (0, fs_1.readdirSync)(roomDirPath);
if (dir.length === 0) {
fs_1.default.rmSync(roomDirPath, { recursive: true, force: true });
}
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_VIDEO_DELETE,
id: userId,
connId,
data: {
video,
},
},
});
}
async videoUpdateHandler({ id, data: { args, userId, token }, connId, }) {
const locale = this.getLocale({ roomId: id, userId });
const { errorCode, unitId } = await this.checkTokenCb({ token });
const isDefault = (0, lib_1.checkDefaultAuth)({ unitId });
// TODO refactor checks to one auth method
if (errorCode !== 0 && !isDefault) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.forbidden,
code: interfaces_1.ErrorCode.forbidden,
},
},
});
return;
}
if (!isDefault && userId.toString() !== unitId) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.notAuthorised,
code: interfaces_1.ErrorCode.notAuthorised,
},
},
});
return;
}
if (!args.where.id) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.badRequest,
code: interfaces_1.ErrorCode.badRequest,
},
},
});
return;
}
const _video = await this.videoFindFirst({
where: {
id: args.where.id,
},
include: {
Room: {
select: {
id: true,
authorId: true,
},
},
},
});
if (_video === undefined) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'error',
message: locale.serverError,
code: interfaces_1.ErrorCode.serverError,
},
},
});
return;
}
if (_video === null) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.notFound,
code: interfaces_1.ErrorCode.notFound,
},
},
});
return;
}
if (userId.toString() !== _video.Room.authorId || id.toString() !== _video.Room.id) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.notAuthorised,
code: interfaces_1.ErrorCode.notAuthorised,
},
},
});
return;
}
const oldVideoPath = (0, lib_1.getVideoPath)({ cloudPath: this.cloudPath, roomId: id, name: _video.name });
if (!fs_1.default.existsSync(oldVideoPath)) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.notFound,
code: interfaces_1.ErrorCode.notFound,
},
},
});
return;
}
const video = await this.videoUpdate({ ...args });
if (video === undefined) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'error',
message: locale.serverError,
code: interfaces_1.ErrorCode.serverError,
},
},
});
return;
}
if (video === null) {
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_ERROR,
connId,
id: userId,
data: {
type: 'warn',
message: locale.notFound,
code: interfaces_1.ErrorCode.notFound,
},
},
});
return;
}
const videoPath = (0, lib_1.getVideoPath)({ cloudPath: this.cloudPath, roomId: id, name: video.name });
fs_1.default.renameSync(oldVideoPath, videoPath);
this.sendMessage({
roomId: id,
msg: {
type: interfaces_1.MessageType.SET_VIDEO_UPDATE,
id: userId,
connId,
data: {
video,
},
},
});
}
}
exports.default = Settings;
//# sourceMappingURL=settings.js.map