UNPKG

uyem

Version:
324 lines 10.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const interfaces_1 = require("../types/interfaces"); const lib_1 = require("../utils/lib"); const db_1 = __importDefault(require("../core/db")); const constants_1 = require("../utils/constants"); class Chat extends db_1.default { users = {}; blocked = {}; constructor({ prisma }) { super({ prisma }); } setUnit = async ({ roomId, userId, ws, locale, connId, }) => { if (userId === constants_1.AUTH_UNIT_ID_DEFAULT) { return; } if (!this.users[roomId]) { this.users[roomId] = {}; } if (!this.blocked[roomId]) { this.blocked[roomId] = []; } const lang = (0, lib_1.getLocale)(locale).server; if (this.users[roomId][userId] && !constants_1.IS_DEV) { (0, lib_1.log)('warn', 'Duplicate chat user', { roomId, userId }); ws.send(JSON.stringify({ type: interfaces_1.MessageType.SET_ERROR, id: userId, connId, data: { type: 'warn', code: interfaces_1.ErrorCode.duplicateTab, message: lang.duplicateTab, }, })); return; } this.users[roomId][userId] = { locale, ws, connId, }; this.sendMessage({ roomId, msg: { id: userId, connId, type: interfaces_1.MessageType.SET_CHAT_UNIT, data: undefined, }, }); this.sendMessage({ roomId, msg: { id: userId, connId, type: interfaces_1.MessageType.SET_BLOCK_CHAT, data: { target: 0, blocked: this.blocked[roomId], }, }, }); }; cleanUnit = ({ roomId, userId }) => { if (!this.users[roomId][userId]) { (0, lib_1.log)('warn', 'Chat user can not remove', { roomId, userId }); return; } delete this.users[roomId][userId]; }; // eslint-disable-next-line @typescript-eslint/no-explicit-any sendMessage = ({ msg, roomId }) => { const { id } = msg; return new Promise((resolve) => { setTimeout(() => { let res = ''; try { res = JSON.stringify(msg); } catch (e) { (0, lib_1.log)('error', 'Error send chat message', e); resolve(1); } if (!this.users[roomId][id]) { (0, lib_1.log)('error', 'Chat user not found', { roomId, id }); resolve(1); } this.users[roomId][id].ws.send(res); resolve(0); }, 0); }); }; getLocale({ userId, roomId }) { return (0, lib_1.getLocale)(this.users[roomId][userId].locale).server; } async handleRoomMessage({ id, connId, data: { userId, message }, }) { if (!this.users[id][userId]) { (0, lib_1.log)('warn', 'Send chat message without user', { userId, id, message }); return; } const locale = this.getLocale({ roomId: id, userId }); const res = await this.messageCreate({ data: { unitId: userId.toString(), roomId: id.toString(), text: message, }, include: { Unit: { select: { id: true, name: true, }, }, Quote: { select: { MessageQuote: { include: { Unit: { select: { id: true, name: true, }, }, }, }, }, }, }, }); if (!res) { this.sendMessage({ roomId: id, msg: { type: interfaces_1.MessageType.SET_ERROR, connId, id: userId, data: { message: locale.errorSendMessage, type: 'error', code: interfaces_1.ErrorCode.errorSendMessage, }, }, }); return; } const uKeys = Object.keys(this.users[id]); uKeys.forEach((item) => { this.sendMessage({ roomId: id, msg: { type: interfaces_1.MessageType.SET_ROOM_MESSAGE, id: item, connId, data: res, }, }); }); } async handleEditMessage({ id, data: { args }, }) { const res = await this.messageUpdate(args); if (!res) { (0, lib_1.log)('error', 'Error update message'); // TODO send to user return; } const uKeys = Object.keys(this.users[id]); uKeys.forEach((item) => { this.sendMessage({ roomId: id, msg: { type: interfaces_1.MessageType.SET_EDIT_MESSAGE, connId: '', id: item, data: res, }, }); }); } async handleCreateMessage({ id, data: { args }, }) { const res = await this.messageCreate(args); if (!res) { (0, lib_1.log)('error', 'Error create message'); // TODO send to user return; } const uKeys = Object.keys(this.users[id]); uKeys.forEach((item) => { this.sendMessage({ roomId: id, msg: { type: interfaces_1.MessageType.SET_CREATE_MESSAGE, connId: '', id: item, data: res, }, }); }); } getBlockChatHandler = ({ id, data: { target, command }, }) => { let index = -1; switch (command) { case 'add': if (this.blocked[id].indexOf(target) === -1) { this.blocked[id].push(target); } else { (0, lib_1.log)('warn', 'Duplicate block chat', { id, target }); } break; case 'delete': index = this.blocked[id].indexOf(target); if (index !== -1) { this.blocked[id].splice(index, 1); } else { (0, lib_1.log)('warn', 'Removed block chat is missing', { id, target }); } break; default: } Object.keys(this.users[id]).forEach((item) => { this.sendMessage({ msg: { type: interfaces_1.MessageType.SET_BLOCK_CHAT, id: item, connId: '', data: { target, blocked: this.blocked[id], }, }, roomId: id, }); }); }; async handleCreateQuote({ id, data: { args }, }) { const res = await this.quoteCreate(args); const uKeys = Object.keys(this.users[id]); if (!res) { // TODO send to user (0, lib_1.log)('error', 'Error create quote'); return; } uKeys.forEach((item) => { this.sendMessage({ roomId: id, msg: { type: interfaces_1.MessageType.SET_CREATE_QUOTE, connId: '', id: item, data: res, }, }); }); } async handleDeleteMessage({ id, connId, data: { args, userId }, }) { const res = await this.messageDelete(args); const locale = this.getLocale({ roomId: id, userId }); if (res === null) { this.sendMessage({ roomId: id, msg: { id: userId, connId, type: interfaces_1.MessageType.SET_ERROR, data: { type: 'error', message: locale.error, code: interfaces_1.ErrorCode.errorDeleteMessage, }, }, }); return; } const uKeys = Object.keys(this.users[id]); uKeys.forEach((item) => { this.sendMessage({ roomId: id, msg: { type: interfaces_1.MessageType.SET_DELETE_MESSAGE, connId: '', id: item, data: res, }, }); }); } async getChatMessages({ id, connId, data: { args, userId }, }) { const locale = this.getLocale({ roomId: id, userId }); const data = await this.messageFindMany(args); if (data === 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_CHAT_MESSAGES, connId, id: userId, data, }, }); } } exports.default = Chat; //# sourceMappingURL=chat.js.map