From 5d1345518512e5ca9aa9d5ec64f234b2c400347b Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Mon, 7 Oct 2024 23:18:01 -0400 Subject: [PATCH 01/14] V1.0(No funcional). Seguir corrigiendo errores de compilacion ts a js. Lint retorna 59 errores de formato que no son corregibles con npm run lint -- --fix. --- src/messaging/edit.ts | 129 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/messaging/edit.ts diff --git a/src/messaging/edit.ts b/src/messaging/edit.ts new file mode 100644 index 0000000000..269cedcd30 --- /dev/null +++ b/src/messaging/edit.ts @@ -0,0 +1,129 @@ +'use strict'; + +@typescript-eslint/no-unsafe-member-access, +@typescript-eslint/no-unsafe-call +import * as meta from '../meta'; +import * as user from '../user'; +import * as plugins from '../plugins'; +import * as privileges from '../privileges'; +import * as sockets from '../socket.io'; + +interface Payload { + content: string; + edited: number; +} + +interface MessageData { + fromuid: number; + timestamp: number; + system: boolean; +} + +interface MessagingInterface { + editMessage: (uid: number, mid: number, roomId: number, content: string) => Promise; + getMessageField: (mid: number, field: string) => Promise; + setMessageFields: (mid: number, payload: Payload) => Promise; + checkContent: (content: string) => Promise; + getMessagesData: (mids: number[], uid: number, roomId: number, includeDeleted: boolean) => Promise; + messageExists: (messageId: number) => Promise; + getMessageFields: (messageId: number, fields: string[]) => Promise; + isUserInRoom: (uid: number, roomId: number) => Promise; + isRoomOwner: (uid: number, roomId: number) => Promise; + canEdit: (messageId: number, uid: number) => Promise; + canDelete: (messageId: number, uid: number) => Promise; + canPin: (roomId: number, uid: number) => Promise; +} + +module.exports = function (Messaging: MessagingInterface) { + Messaging.editMessage = async (uid: number, mid: number, roomId: number, content: string): Promise => { + await Messaging.checkContent(content); + const raw = await Messaging.getMessageField(mid, 'content'); + if (raw === content) { + return; + } + + const payload: Payload = await plugins.hooks.fire('filter:messaging.edit', {content: content, edited: Date.now(),}); + + if (!String(payload.content).trim()) { + throw new Error('[[error:invalid-chat-message]]'); + } + await Messaging.setMessageFields(mid, payload); + + // Propagar este cambio a los usuarios en la sala + const messages = await Messaging.getMessagesData([mid], uid, roomId, true); + if (messages[0]) { + const roomName = messages[0].deleted ? `uid_${uid}` : `chat_room_${roomId}`; + sockets.in(roomName).emit('event:chats.edit', {messages: messages,}); + } + + plugins.hooks.fire('action:messaging.edit', { + message: { ...messages[0], content: payload.content }, + }); + }; + + const canEditDelete = async (messageId: number, uid: number, type: 'edit' | 'delete'): Promise => { + let durationConfig = ''; + if (type === 'edit') { + durationConfig = 'chatEditDuration'; + } else if (type === 'delete') { + durationConfig = 'chatDeleteDuration'; + } + + const exists = await Messaging.messageExists(messageId); + if (!exists) { + throw new Error('[[error:invalid-mid]]'); + } + + const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(uid); + + if (meta.config.disableChat) { + throw new Error('[[error:chat-disabled]]'); + } else if (!isAdminOrGlobalMod && meta.config.disableChatMessageEditing) { + throw new Error('[[error:chat-message-editing-disabled]]'); + } + + const userData = await user.getUserFields(uid, ['banned']); + if (userData.banned) { + throw new Error('[[error:user-banned]]'); + } + + const canChat = await privileges.global.can( + ['chat', 'chat:privileged'], + uid + ); + if (!canChat.includes(true)) { + throw new Error('[[error:no-privileges]]'); + } + + const messageData = await Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system',]); + if (isAdminOrGlobalMod && !messageData.system) { + return; + } + + const chatConfigDuration = meta.config[durationConfig]; + if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { + throw new Error(`[[error:chat-${type}-duration-expired, ${meta.config[durationConfig]}]]`); + } + + if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) { + return; + } + + throw new Error(`[[error:cant-${type}-chat-message]]`); + }; + + Messaging.canEdit = async (messageId: number, uid: number): Promise => await canEditDelete(messageId, uid, 'edit'); + Messaging.canDelete = async (messageId: number, uid: number): Promise => await canEditDelete(messageId, uid, 'delete'); + + Messaging.canPin = async (roomId: number, uid: number): Promise => { + const [isAdmin, isGlobalMod, inRoom, isRoomOwner] = await Promise.all([ + user.isAdministrator(uid), + user.isGlobalModerator(uid), + Messaging.isUserInRoom(uid, roomId), + Messaging.isRoomOwner(uid, roomId), + ]); + if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) { + throw new Error('[[error:no-privileges]]'); + } + }; +}; From 8b9352718f29eb99f00026dac6d542ed87bc6b5b Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Mon, 7 Oct 2024 23:35:04 -0400 Subject: [PATCH 02/14] V1.1(No funcional). Remover etiquetas que posiblemente generen lint and test exitoso en git. --- src/messaging/edit.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/messaging/edit.ts b/src/messaging/edit.ts index 269cedcd30..dab0b1e328 100644 --- a/src/messaging/edit.ts +++ b/src/messaging/edit.ts @@ -1,7 +1,5 @@ 'use strict'; -@typescript-eslint/no-unsafe-member-access, -@typescript-eslint/no-unsafe-call import * as meta from '../meta'; import * as user from '../user'; import * as plugins from '../plugins'; From 920c8e36918191b48e78bda537ca5e9936232d8f Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Tue, 8 Oct 2024 21:54:07 -0400 Subject: [PATCH 03/14] Solucionado varios errores de la prueba Lint, aun quedan 34 errores por solucionar. Este archivo es funcional en parte dado que la aplicacion logra montarse en servidor y la edicion de mensaje en todos los ambitos es funcional. --- src/messaging/edit.js | 276 ++++++++++++++++++++++++++++-------------- 1 file changed, 187 insertions(+), 89 deletions(-) diff --git a/src/messaging/edit.js b/src/messaging/edit.js index d42f4ce0ce..8a332196e3 100644 --- a/src/messaging/edit.js +++ b/src/messaging/edit.js @@ -1,105 +1,203 @@ 'use strict'; +let __assign = (this && this.__assign) || function () { + __assign = Object.assign || function (t) { + for (let s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (const p in s) { if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } + } + return t; + }; + return __assign.apply(this, arguments); +}; +const __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P((resolve) => { resolve(value); }); } + return new (P || (P = Promise))((resolve, reject) => { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +const __generator = (this && this.__generator) || function (thisArg, body) { + let t; let _ = { label: 0, sent: function () { if (t[0] && 1) throw t[1]; return t[1]; }, trys: [], ops: [] }; let f; let y; + let g = Object.create((typeof Iterator === 'function' ? Iterator : Object).prototype); + return g.next = verb(0), g.throw = verb(1), g.return = verb(2), typeof Symbol === 'function' && (g[Symbol.iterator] = function () { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError('Generator is already executing.'); + while (g && (g = 0, op[0] && (_ = 0)), _) { + try { + if (f = 1, y && (t = op[0] && 2 ? y.return : op[0] ? y.throw || ((t = y.return) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] && 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label + 1; return { value: op[1], done: false }; + case 5: _.label + 1; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { t = 0, f = t; } + } + if (op[0] && 5) throw op[1]; return { value: op[0] ? op[1] : undefined, done: true }; + } +}; +Object.defineProperty(exports, '__esModule', { value: true }); const meta = require('../meta'); const user = require('../user'); const plugins = require('../plugins'); const privileges = require('../privileges'); - const sockets = require('../socket.io'); - module.exports = function (Messaging) { - Messaging.editMessage = async (uid, mid, roomId, content) => { - await Messaging.checkContent(content); - const raw = await Messaging.getMessageField(mid, 'content'); - if (raw === content) { - return; - } - - const payload = await plugins.hooks.fire('filter:messaging.edit', { - content: content, - edited: Date.now(), + const _this = this; + Messaging.editMessage = function (uid, mid, roomId, content) { + return __awaiter(_this, undefined, undefined, function () { + let raw; let payload; let messages; let + roomName; + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, Messaging.checkContent(content)]; + case 1: + _a.sent(); + return [4 /* yield */, Messaging.getMessageField(mid, 'content')]; + case 2: + raw = _a.sent(); + if (raw === content) { + return [2]; + } + return [4 /* yield */, plugins.hooks.fire('filter:messaging.edit', { content: content, edited: Date.now() })]; + case 3: + payload = _a.sent(); + if (!String(payload.content).trim()) { + throw new Error('[[error:invalid-chat-message]]'); + } + return [4 /* yield */, Messaging.setMessageFields(mid, payload)]; + case 4: + _a.sent(); + return [4 /* yield */, Messaging.getMessagesData([mid], uid, roomId, true)]; + case 5: + messages = _a.sent(); + if (messages[0]) { + roomName = messages[0].deleted ? 'uid_'.concat(uid) : 'chat_room_'.concat(roomId); + sockets.in(roomName).emit('event:chats.edit', { messages: messages }); + } + plugins.hooks.fire('action:messaging.edit', { + message: { ...messages[0], content: payload.content }, + }); + return [2]; + } + }); }); - - if (!String(payload.content).trim()) { - throw new Error('[[error:invalid-chat-message]]'); - } - await Messaging.setMessageFields(mid, payload); - - // Propagate this change to users in the room - const messages = await Messaging.getMessagesData([mid], uid, roomId, true); - if (messages[0]) { - const roomName = messages[0].deleted ? `uid_${uid}` : `chat_room_${roomId}`; - sockets.in(roomName).emit('event:chats.edit', { - messages: messages, + }; + const canEditDelete = function (messageId, uid, type) { + return __awaiter(_this, undefined, undefined, function () { + let durationConfig; let exists; let isAdminOrGlobalMod; let userData; let canChat; let messageData; let + chatConfigDuration; + return __generator(this, (_a) => { + switch (_a.label) { + case 0: + durationConfig = ''; + if (type === 'edit') { + durationConfig = 'chatEditDuration'; + } else if (type === 'delete') { + durationConfig = 'chatDeleteDuration'; + } + return [4 /* yield */, Messaging.messageExists(messageId)]; + case 1: + exists = _a.sent(); + if (!exists) { + throw new Error('[[error:invalid-mid]]'); + } + return [4 /* yield */, user.isAdminOrGlobalMod(uid)]; + case 2: + isAdminOrGlobalMod = _a.sent(); + if (meta.config.disableChat) { + throw new Error('[[error:chat-disabled]]'); + } else if (!isAdminOrGlobalMod && meta.config.disableChatMessageEditing) { + throw new Error('[[error:chat-message-editing-disabled]]'); + } + return [4 /* yield */, user.getUserFields(uid, ['banned'])]; + case 3: + userData = _a.sent(); + if (userData.banned) { + throw new Error('[[error:user-banned]]'); + } + return [4 /* yield */, privileges.global.can(['chat', 'chat:privileged'], uid)]; + case 4: + canChat = _a.sent(); + if (!canChat.includes(true)) { + throw new Error('[[error:no-privileges]]'); + } + return [4 /* yield */, Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system'])]; + case 5: + messageData = _a.sent(); + if (isAdminOrGlobalMod && !messageData.system) { + return [2]; + } + chatConfigDuration = meta.config[durationConfig]; + if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { + throw new Error('[[error:chat-'.concat(type, '-duration-expired, ').concat(meta.config[durationConfig], ']]')); + } + if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) { + return [2]; + } + throw new Error('[[error:cant-'.concat(type, '-chat-message]]')); + } }); - } - - plugins.hooks.fire('action:messaging.edit', { - message: { ...messages[0], content: payload.content }, }); }; - - const canEditDelete = async (messageId, uid, type) => { - let durationConfig = ''; - if (type === 'edit') { - durationConfig = 'chatEditDuration'; - } else if (type === 'delete') { - durationConfig = 'chatDeleteDuration'; - } - - const exists = await Messaging.messageExists(messageId); - if (!exists) { - throw new Error('[[error:invalid-mid]]'); - } - - const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(uid); - - if (meta.config.disableChat) { - throw new Error('[[error:chat-disabled]]'); - } else if (!isAdminOrGlobalMod && meta.config.disableChatMessageEditing) { - throw new Error('[[error:chat-message-editing-disabled]]'); - } - - const userData = await user.getUserFields(uid, ['banned']); - if (userData.banned) { - throw new Error('[[error:user-banned]]'); - } - - const canChat = await privileges.global.can(['chat', 'chat:privileged'], uid); - if (!canChat.includes(true)) { - throw new Error('[[error:no-privileges]]'); - } - - const messageData = await Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system']); - if (isAdminOrGlobalMod && !messageData.system) { - return; - } - - const chatConfigDuration = meta.config[durationConfig]; - if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { - throw new Error(`[[error:chat-${type}-duration-expired, ${meta.config[durationConfig]}]]`); - } - - if (messageData.fromuid === parseInt(uid, 10) && !messageData.system) { - return; - } - - throw new Error(`[[error:cant-${type}-chat-message]]`); + Messaging.canEdit = function (messageId, uid) { + return __awaiter(_this, undefined, undefined, function () { + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, canEditDelete(messageId, uid, 'edit')]; + case 1: return [2 /* return */, _a.sent()]; + } + }); + }); }; - - Messaging.canEdit = async (messageId, uid) => await canEditDelete(messageId, uid, 'edit'); - Messaging.canDelete = async (messageId, uid) => await canEditDelete(messageId, uid, 'delete'); - - Messaging.canPin = async (roomId, uid) => { - const [isAdmin, isGlobalMod, inRoom, isRoomOwner] = await Promise.all([ - user.isAdministrator(uid), - user.isGlobalModerator(uid), - Messaging.isUserInRoom(uid, roomId), - Messaging.isRoomOwner(uid, roomId), - ]); - if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) { - throw new Error('[[error:no-privileges]]'); - } + Messaging.canDelete = function (messageId, uid) { + return __awaiter(_this, undefined, undefined, function () { + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, canEditDelete(messageId, uid, 'delete')]; + case 1: return [2 /* return */, _a.sent()]; + } + }); + }); + }; + Messaging.canPin = function (roomId, uid) { + return __awaiter(_this, undefined, undefined, function () { + let _a; let isAdmin; let isGlobalMod; let inRoom; let + isRoomOwner; + return __generator(this, (_b) => { + switch (_b.label) { + case 0: return [4 /* yield */, Promise.all([ + user.isAdministrator(uid), + user.isGlobalModerator(uid), + Messaging.isUserInRoom(uid, roomId), + Messaging.isRoomOwner(uid, roomId), + ])]; + case 1: + _a = _b.sent(); + isAdmin = _a[0]; + isGlobalMod = _a[1]; + inRoom = _a[2]; + isRoomOwner = _a[3]; + if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) { + throw new Error('[[error:no-privileges]]'); + } + return [2]; + } + }); + }); }; }; From 459a862c23fcb9d47acfc9afd61dc050640d7db6 Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Wed, 9 Oct 2024 19:10:03 -0400 Subject: [PATCH 04/14] Reduccion de errores en Linter. Total de errores encontrados 24. --- src/messaging/edit.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/messaging/edit.js b/src/messaging/edit.js index 8a332196e3..76b39f6c51 100644 --- a/src/messaging/edit.js +++ b/src/messaging/edit.js @@ -28,20 +28,25 @@ const __generator = (this && this.__generator) || function (thisArg, body) { if (f) throw new TypeError('Generator is already executing.'); while (g && (g = 0, op[0] && (_ = 0)), _) { try { - if (f = 1, y && (t = op[0] && 2 ? y.return : op[0] ? y.throw || ((t = y.return) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] && 2, t.value]; + f = 1; + if (y && (t = op[0] && 2 ? y.return : op[0] ? y.throw || ((t = y.return) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + y = 0; + if (t) op = [op[0] && 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label + 1; return { value: op[1], done: false }; - case 5: _.label + 1; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; + case 5: _.label + 1; y = op[1]; op = [0]; break; + case 7: op = _.ops.pop(); _.trys.pop(); break; default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + // if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; } + t = _.trys; + t = t.length; + if (!(t > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); - _.trys.pop(); continue; + _.trys.pop(); } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { t = 0, f = t; } From ce4a64ce8c513641c8e8332c072db4fae7efe364 Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Wed, 9 Oct 2024 22:41:36 -0400 Subject: [PATCH 05/14] Inclusion de comentarios tipo eslint-disable y eslint-disable-next-line para los casos de variables y funciones que provienen de modulos no traducidos a TS. De esta forma ignoramos dichos errores reduciendo a solo 2, uno relacionado al tipo return promise (linea 24) de la funcion getMessagesData y el otro en el tipo de promise retornado(linea 60) debido al mismo problema de la linea 24. El estado actual de este archivo genera un JS funcional para el proyecto NODEBB, sin embargo existe un problema con el uso de los metodos export/import ESModules y CommonJS lo que hace generar un problema de lo mismo en ESLint, sin embargo al corregirlo la app no funcion, por lo que se llevo a usar eslint-disable de import modules en linea 1 del archivo. --- src/messaging/edit.ts | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/messaging/edit.ts b/src/messaging/edit.ts index dab0b1e328..ccb1872343 100644 --- a/src/messaging/edit.ts +++ b/src/messaging/edit.ts @@ -1,5 +1,4 @@ -'use strict'; - +/* eslint-disable import/no-import-module-exports */ import * as meta from '../meta'; import * as user from '../user'; import * as plugins from '../plugins'; @@ -39,22 +38,27 @@ module.exports = function (Messaging: MessagingInterface) { if (raw === content) { return; } - - const payload: Payload = await plugins.hooks.fire('filter:messaging.edit', {content: content, edited: Date.now(),}); + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + const payload: Payload = await plugins.hooks.fire('filter:messaging.edit', { content: content, edited: Date.now() }); if (!String(payload.content).trim()) { throw new Error('[[error:invalid-chat-message]]'); } await Messaging.setMessageFields(mid, payload); - // Propagar este cambio a los usuarios en la sala const messages = await Messaging.getMessagesData([mid], uid, roomId, true); if (messages[0]) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call const roomName = messages[0].deleted ? `uid_${uid}` : `chat_room_${roomId}`; - sockets.in(roomName).emit('event:chats.edit', {messages: messages,}); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + sockets.in(roomName).emit('event:chats.edit', { messages: messages }); } + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment plugins.hooks.fire('action:messaging.edit', { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment message: { ...messages[0], content: payload.content }, }); }; @@ -72,34 +76,46 @@ module.exports = function (Messaging: MessagingInterface) { throw new Error('[[error:invalid-mid]]'); } + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(uid); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call if (meta.config.disableChat) { throw new Error('[[error:chat-disabled]]'); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call } else if (!isAdminOrGlobalMod && meta.config.disableChatMessageEditing) { throw new Error('[[error:chat-message-editing-disabled]]'); } + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment const userData = await user.getUserFields(uid, ['banned']); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call if (userData.banned) { throw new Error('[[error:user-banned]]'); } + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment const canChat = await privileges.global.can( ['chat', 'chat:privileged'], uid ); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call if (!canChat.includes(true)) { throw new Error('[[error:no-privileges]]'); } - const messageData = await Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system',]); + const messageData = await Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system']); if (isAdminOrGlobalMod && !messageData.system) { return; } + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access const chatConfigDuration = meta.config[durationConfig]; if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call throw new Error(`[[error:chat-${type}-duration-expired, ${meta.config[durationConfig]}]]`); } @@ -114,9 +130,11 @@ module.exports = function (Messaging: MessagingInterface) { Messaging.canDelete = async (messageId: number, uid: number): Promise => await canEditDelete(messageId, uid, 'delete'); Messaging.canPin = async (roomId: number, uid: number): Promise => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const [isAdmin, isGlobalMod, inRoom, isRoomOwner] = await Promise.all([ - user.isAdministrator(uid), - user.isGlobalModerator(uid), + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + user.isAdministrator(uid), user.isGlobalModerator(uid), Messaging.isUserInRoom(uid, roomId), Messaging.isRoomOwner(uid, roomId), ]); From 7eda24b10ee9b4c351659c994f3ef11e159d7cad Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Wed, 9 Oct 2024 22:44:00 -0400 Subject: [PATCH 06/14] Archivo JS generado por la traduccion TS con Error count 2. Esta version es funcional con la App. --- src/messaging/edit.js | 389 ++++++++++++++++++++---------------------- 1 file changed, 186 insertions(+), 203 deletions(-) diff --git a/src/messaging/edit.js b/src/messaging/edit.js index 76b39f6c51..42f403ccc1 100644 --- a/src/messaging/edit.js +++ b/src/messaging/edit.js @@ -1,208 +1,191 @@ -'use strict'; - -let __assign = (this && this.__assign) || function () { - __assign = Object.assign || function (t) { - for (let s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (const p in s) { if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } - } - return t; - }; - return __assign.apply(this, arguments); +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); }; -const __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P((resolve) => { resolve(value); }); } - return new (P || (P = Promise))((resolve, reject) => { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); }; -const __generator = (this && this.__generator) || function (thisArg, body) { - let t; let _ = { label: 0, sent: function () { if (t[0] && 1) throw t[1]; return t[1]; }, trys: [], ops: [] }; let f; let y; - let g = Object.create((typeof Iterator === 'function' ? Iterator : Object).prototype); - return g.next = verb(0), g.throw = verb(1), g.return = verb(2), typeof Symbol === 'function' && (g[Symbol.iterator] = function () { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError('Generator is already executing.'); - while (g && (g = 0, op[0] && (_ = 0)), _) { - try { - f = 1; - if (y && (t = op[0] && 2 ? y.return : op[0] ? y.throw || ((t = y.return) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - y = 0; - if (t) op = [op[0] && 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label + 1; return { value: op[1], done: false }; - case 5: _.label + 1; y = op[1]; op = [0]; break; - case 7: op = _.ops.pop(); _.trys.pop(); break; - default: - // if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; } - t = _.trys; - t = t.length; - if (!(t > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { t = 0, f = t; } - } - if (op[0] && 5) throw op[1]; return { value: op[0] ? op[1] : undefined, done: true }; - } +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); + return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (g && (g = 0, op[0] && (_ = 0)), _) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } }; -Object.defineProperty(exports, '__esModule', { value: true }); -const meta = require('../meta'); -const user = require('../user'); -const plugins = require('../plugins'); -const privileges = require('../privileges'); -const sockets = require('../socket.io'); - +Object.defineProperty(exports, "__esModule", { value: true }); +/* eslint-disable import/no-import-module-exports */ +var meta = require("../meta"); +var user = require("../user"); +var plugins = require("../plugins"); +var privileges = require("../privileges"); +var sockets = require("../socket.io"); module.exports = function (Messaging) { - const _this = this; - Messaging.editMessage = function (uid, mid, roomId, content) { - return __awaiter(_this, undefined, undefined, function () { - let raw; let payload; let messages; let - roomName; - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, Messaging.checkContent(content)]; - case 1: - _a.sent(); - return [4 /* yield */, Messaging.getMessageField(mid, 'content')]; - case 2: - raw = _a.sent(); - if (raw === content) { - return [2]; - } - return [4 /* yield */, plugins.hooks.fire('filter:messaging.edit', { content: content, edited: Date.now() })]; - case 3: - payload = _a.sent(); - if (!String(payload.content).trim()) { - throw new Error('[[error:invalid-chat-message]]'); - } - return [4 /* yield */, Messaging.setMessageFields(mid, payload)]; - case 4: - _a.sent(); - return [4 /* yield */, Messaging.getMessagesData([mid], uid, roomId, true)]; - case 5: - messages = _a.sent(); - if (messages[0]) { - roomName = messages[0].deleted ? 'uid_'.concat(uid) : 'chat_room_'.concat(roomId); - sockets.in(roomName).emit('event:chats.edit', { messages: messages }); - } - plugins.hooks.fire('action:messaging.edit', { - message: { ...messages[0], content: payload.content }, - }); - return [2]; - } - }); - }); - }; - const canEditDelete = function (messageId, uid, type) { - return __awaiter(_this, undefined, undefined, function () { - let durationConfig; let exists; let isAdminOrGlobalMod; let userData; let canChat; let messageData; let - chatConfigDuration; - return __generator(this, (_a) => { - switch (_a.label) { - case 0: - durationConfig = ''; - if (type === 'edit') { - durationConfig = 'chatEditDuration'; - } else if (type === 'delete') { - durationConfig = 'chatDeleteDuration'; - } - return [4 /* yield */, Messaging.messageExists(messageId)]; - case 1: - exists = _a.sent(); - if (!exists) { - throw new Error('[[error:invalid-mid]]'); - } - return [4 /* yield */, user.isAdminOrGlobalMod(uid)]; - case 2: - isAdminOrGlobalMod = _a.sent(); - if (meta.config.disableChat) { - throw new Error('[[error:chat-disabled]]'); - } else if (!isAdminOrGlobalMod && meta.config.disableChatMessageEditing) { - throw new Error('[[error:chat-message-editing-disabled]]'); - } - return [4 /* yield */, user.getUserFields(uid, ['banned'])]; - case 3: - userData = _a.sent(); - if (userData.banned) { - throw new Error('[[error:user-banned]]'); - } - return [4 /* yield */, privileges.global.can(['chat', 'chat:privileged'], uid)]; - case 4: - canChat = _a.sent(); - if (!canChat.includes(true)) { - throw new Error('[[error:no-privileges]]'); - } - return [4 /* yield */, Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system'])]; - case 5: - messageData = _a.sent(); - if (isAdminOrGlobalMod && !messageData.system) { - return [2]; - } - chatConfigDuration = meta.config[durationConfig]; - if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { - throw new Error('[[error:chat-'.concat(type, '-duration-expired, ').concat(meta.config[durationConfig], ']]')); - } - if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) { - return [2]; - } - throw new Error('[[error:cant-'.concat(type, '-chat-message]]')); - } - }); - }); - }; - Messaging.canEdit = function (messageId, uid) { - return __awaiter(_this, undefined, undefined, function () { - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, canEditDelete(messageId, uid, 'edit')]; - case 1: return [2 /* return */, _a.sent()]; - } - }); - }); - }; - Messaging.canDelete = function (messageId, uid) { - return __awaiter(_this, undefined, undefined, function () { - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, canEditDelete(messageId, uid, 'delete')]; - case 1: return [2 /* return */, _a.sent()]; - } - }); - }); - }; - Messaging.canPin = function (roomId, uid) { - return __awaiter(_this, undefined, undefined, function () { - let _a; let isAdmin; let isGlobalMod; let inRoom; let - isRoomOwner; - return __generator(this, (_b) => { - switch (_b.label) { - case 0: return [4 /* yield */, Promise.all([ - user.isAdministrator(uid), - user.isGlobalModerator(uid), - Messaging.isUserInRoom(uid, roomId), - Messaging.isRoomOwner(uid, roomId), - ])]; - case 1: - _a = _b.sent(); - isAdmin = _a[0]; - isGlobalMod = _a[1]; - inRoom = _a[2]; - isRoomOwner = _a[3]; - if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) { - throw new Error('[[error:no-privileges]]'); - } - return [2]; - } - }); - }); - }; + var _this = this; + Messaging.editMessage = function (uid, mid, roomId, content) { return __awaiter(_this, void 0, void 0, function () { + var raw, payload, messages, roomName; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, Messaging.checkContent(content)]; + case 1: + _a.sent(); + return [4 /*yield*/, Messaging.getMessageField(mid, 'content')]; + case 2: + raw = _a.sent(); + if (raw === content) { + return [2 /*return*/]; + } + return [4 /*yield*/, plugins.hooks.fire('filter:messaging.edit', { content: content, edited: Date.now() })]; + case 3: + payload = _a.sent(); + if (!String(payload.content).trim()) { + throw new Error('[[error:invalid-chat-message]]'); + } + return [4 /*yield*/, Messaging.setMessageFields(mid, payload)]; + case 4: + _a.sent(); + return [4 /*yield*/, Messaging.getMessagesData([mid], uid, roomId, true)]; + case 5: + messages = _a.sent(); + if (messages[0]) { + roomName = messages[0].deleted ? "uid_".concat(uid) : "chat_room_".concat(roomId); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + sockets.in(roomName).emit('event:chats.edit', { messages: messages }); + } + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + plugins.hooks.fire('action:messaging.edit', { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + message: __assign(__assign({}, messages[0]), { content: payload.content }), + }); + return [2 /*return*/]; + } + }); + }); }; + var canEditDelete = function (messageId, uid, type) { return __awaiter(_this, void 0, void 0, function () { + var durationConfig, exists, isAdminOrGlobalMod, userData, canChat, messageData, chatConfigDuration; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + durationConfig = ''; + if (type === 'edit') { + durationConfig = 'chatEditDuration'; + } + else if (type === 'delete') { + durationConfig = 'chatDeleteDuration'; + } + return [4 /*yield*/, Messaging.messageExists(messageId)]; + case 1: + exists = _a.sent(); + if (!exists) { + throw new Error('[[error:invalid-mid]]'); + } + return [4 /*yield*/, user.isAdminOrGlobalMod(uid)]; + case 2: + isAdminOrGlobalMod = _a.sent(); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + if (meta.config.disableChat) { + throw new Error('[[error:chat-disabled]]'); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + } + else if (!isAdminOrGlobalMod && meta.config.disableChatMessageEditing) { + throw new Error('[[error:chat-message-editing-disabled]]'); + } + return [4 /*yield*/, user.getUserFields(uid, ['banned'])]; + case 3: + userData = _a.sent(); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + if (userData.banned) { + throw new Error('[[error:user-banned]]'); + } + return [4 /*yield*/, privileges.global.can(['chat', 'chat:privileged'], uid)]; + case 4: + canChat = _a.sent(); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + if (!canChat.includes(true)) { + throw new Error('[[error:no-privileges]]'); + } + return [4 /*yield*/, Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system'])]; + case 5: + messageData = _a.sent(); + if (isAdminOrGlobalMod && !messageData.system) { + return [2 /*return*/]; + } + chatConfigDuration = meta.config[durationConfig]; + if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + throw new Error("[[error:chat-".concat(type, "-duration-expired, ").concat(meta.config[durationConfig], "]]")); + } + if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) { + return [2 /*return*/]; + } + throw new Error("[[error:cant-".concat(type, "-chat-message]]")); + } + }); + }); }; + Messaging.canEdit = function (messageId, uid) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, canEditDelete(messageId, uid, 'edit')]; + case 1: return [2 /*return*/, _a.sent()]; + } + }); }); }; + Messaging.canDelete = function (messageId, uid) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, canEditDelete(messageId, uid, 'delete')]; + case 1: return [2 /*return*/, _a.sent()]; + } + }); }); }; + Messaging.canPin = function (roomId, uid) { return __awaiter(_this, void 0, void 0, function () { + var _a, isAdmin, isGlobalMod, inRoom, isRoomOwner; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, Promise.all([ + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + user.isAdministrator(uid), user.isGlobalModerator(uid), + Messaging.isUserInRoom(uid, roomId), + Messaging.isRoomOwner(uid, roomId), + ])]; + case 1: + _a = _b.sent(), isAdmin = _a[0], isGlobalMod = _a[1], inRoom = _a[2], isRoomOwner = _a[3]; + if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) { + throw new Error('[[error:no-privileges]]'); + } + return [2 /*return*/]; + } + }); + }); }; }; From 5fb4d0556150757c7489f92df47d657a46c93e2a Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Wed, 9 Oct 2024 23:47:32 -0400 Subject: [PATCH 07/14] Arreglo del tipado any para la funcion getMessagesData a nivel de Promise y solucion al segundo problema colocando await para el plugins.hooks. Version del archivo: final(funcional). Verificacion de pruebas de Eslint completas en local. Arreglos finales de estilo, comentarios esenciales de informacion personal. --- src/messaging/edit.ts | 102 +++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/src/messaging/edit.ts b/src/messaging/edit.ts index ccb1872343..a6f739c37e 100644 --- a/src/messaging/edit.ts +++ b/src/messaging/edit.ts @@ -1,3 +1,20 @@ +/* + ******************************************************************************* + ************************ Universidad Simon Bolivar ************************** + ********* Departamento de Computacion y Tecnologia de la Informacion ******** + * * + * - Trimestre: Septiembre-Diciembre 2024 * + * - Materia: Ingenieria de Software 1 * + * - Profesor: Eduardo Feo Flushing * + * * + * - Author: Junior Lara (17-10303) * + * * + * Proyecto 1B: Traducción a TypeScript o Incremento de Cobertura de Código * + * * + ******************************************************************************* +*/ + +/* Seccion: IMPORTACIONES */ /* eslint-disable import/no-import-module-exports */ import * as meta from '../meta'; import * as user from '../user'; @@ -5,46 +22,47 @@ import * as plugins from '../plugins'; import * as privileges from '../privileges'; import * as sockets from '../socket.io'; +/* Seccion: INTERFACES */ interface Payload { - content: string; - edited: number; + content : string; + edited : number; } interface MessageData { - fromuid: number; - timestamp: number; - system: boolean; + fromuid : number; + timestamp : number; + system : boolean; + deleted : boolean; } interface MessagingInterface { - editMessage: (uid: number, mid: number, roomId: number, content: string) => Promise; - getMessageField: (mid: number, field: string) => Promise; - setMessageFields: (mid: number, payload: Payload) => Promise; - checkContent: (content: string) => Promise; - getMessagesData: (mids: number[], uid: number, roomId: number, includeDeleted: boolean) => Promise; - messageExists: (messageId: number) => Promise; - getMessageFields: (messageId: number, fields: string[]) => Promise; - isUserInRoom: (uid: number, roomId: number) => Promise; - isRoomOwner: (uid: number, roomId: number) => Promise; - canEdit: (messageId: number, uid: number) => Promise; - canDelete: (messageId: number, uid: number) => Promise; - canPin: (roomId: number, uid: number) => Promise; + editMessage : (uid: number, mid: number, roomId: number, content: string) => Promise; + getMessageField : (mid: number, field: string) => Promise; + setMessageFields : (mid: number, payload: Payload) => Promise; + checkContent : (content: string) => Promise; + getMessagesData : (mids: number[], uid: number, roomId: number, includeDeleted: boolean) => Promise; + messageExists : (messageId: number) => Promise; + getMessageFields : (messageId: number, fields: string[]) => Promise; + isUserInRoom : (uid: number, roomId: number) => Promise; + isRoomOwner : (uid: number, roomId: number) => Promise; + canEdit : (messageId: number, uid: number) => Promise; + canDelete : (messageId: number, uid: number) => Promise; + canPin : (roomId: number, uid: number) => Promise; } +/* Seccion: FUNCIONES */ module.exports = function (Messaging: MessagingInterface) { Messaging.editMessage = async (uid: number, mid: number, roomId: number, content: string): Promise => { await Messaging.checkContent(content); const raw = await Messaging.getMessageField(mid, 'content'); - if (raw === content) { - return; - } + if (raw === content) return; + // eslint-disable-next-line max-len // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment const payload: Payload = await plugins.hooks.fire('filter:messaging.edit', { content: content, edited: Date.now() }); - if (!String(payload.content).trim()) { - throw new Error('[[error:invalid-chat-message]]'); - } + if (!String(payload.content).trim()) throw new Error('[[error:invalid-chat-message]]'); + await Messaging.setMessageFields(mid, payload); const messages = await Messaging.getMessagesData([mid], uid, roomId, true); @@ -57,10 +75,7 @@ module.exports = function (Messaging: MessagingInterface) { // eslint-disable-next-line max-len // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment - plugins.hooks.fire('action:messaging.edit', { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - message: { ...messages[0], content: payload.content }, - }); + await plugins.hooks.fire('action:messaging.edit', { message: { ...messages[0], content: payload.content } }); }; const canEditDelete = async (messageId: number, uid: number, type: 'edit' | 'delete'): Promise => { @@ -72,9 +87,7 @@ module.exports = function (Messaging: MessagingInterface) { } const exists = await Messaging.messageExists(messageId); - if (!exists) { - throw new Error('[[error:invalid-mid]]'); - } + if (!exists) throw new Error('[[error:invalid-mid]]'); // eslint-disable-next-line max-len // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment @@ -91,26 +104,19 @@ module.exports = function (Messaging: MessagingInterface) { // eslint-disable-next-line max-len // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment const userData = await user.getUserFields(uid, ['banned']); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - if (userData.banned) { - throw new Error('[[error:user-banned]]'); - } + if (userData.banned) throw new Error('[[error:user-banned]]'); // eslint-disable-next-line max-len // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment - const canChat = await privileges.global.can( - ['chat', 'chat:privileged'], - uid - ); + const canChat = await privileges.global.can(['chat', 'chat:privileged'], uid); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - if (!canChat.includes(true)) { - throw new Error('[[error:no-privileges]]'); - } + if (!canChat.includes(true)) throw new Error('[[error:no-privileges]]'); const messageData = await Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system']); - if (isAdminOrGlobalMod && !messageData.system) { - return; - } + if (isAdminOrGlobalMod && !messageData.system) return; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access const chatConfigDuration = meta.config[durationConfig]; @@ -119,10 +125,7 @@ module.exports = function (Messaging: MessagingInterface) { throw new Error(`[[error:chat-${type}-duration-expired, ${meta.config[durationConfig]}]]`); } - if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) { - return; - } - + if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) return; throw new Error(`[[error:cant-${type}-chat-message]]`); }; @@ -138,8 +141,7 @@ module.exports = function (Messaging: MessagingInterface) { Messaging.isUserInRoom(uid, roomId), Messaging.isRoomOwner(uid, roomId), ]); - if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) { - throw new Error('[[error:no-privileges]]'); - } + + if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) throw new Error('[[error:no-privileges]]'); }; }; From b309b164e4c532bbe9a8919ed6329c033864b1a5 Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Wed, 9 Oct 2024 23:49:27 -0400 Subject: [PATCH 08/14] Archivo funcional, version final. Soluciones en el archivo TS completado. --- src/messaging/edit.js | 50 ++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/messaging/edit.js b/src/messaging/edit.js index 42f403ccc1..bad741fbdb 100644 --- a/src/messaging/edit.js +++ b/src/messaging/edit.js @@ -1,4 +1,19 @@ "use strict"; +/* + ******************************************************************************* + ************************ Universidad Simon Bolivar ************************** + ********* Departamento de Computacion y Tecnologia de la Informacion ******** + * * + * - Trimestre: Septiembre-Diciembre 2024 * + * - Materia: Ingenieria de Software 1 * + * - Profesor: Eduardo Feo Flushing * + * * + * - Author: Junior Lara (17-10303) * + * * + * Proyecto 1B: Traducción a TypeScript o Incremento de Cobertura de Código * + * * + ******************************************************************************* +*/ var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { @@ -47,12 +62,14 @@ var __generator = (this && this.__generator) || function (thisArg, body) { } }; Object.defineProperty(exports, "__esModule", { value: true }); +/* Seccion: IMPORTACIONES */ /* eslint-disable import/no-import-module-exports */ var meta = require("../meta"); var user = require("../user"); var plugins = require("../plugins"); var privileges = require("../privileges"); var sockets = require("../socket.io"); +/* Seccion: FUNCIONES */ module.exports = function (Messaging) { var _this = this; Messaging.editMessage = function (uid, mid, roomId, content) { return __awaiter(_this, void 0, void 0, function () { @@ -65,15 +82,13 @@ module.exports = function (Messaging) { return [4 /*yield*/, Messaging.getMessageField(mid, 'content')]; case 2: raw = _a.sent(); - if (raw === content) { + if (raw === content) return [2 /*return*/]; - } return [4 /*yield*/, plugins.hooks.fire('filter:messaging.edit', { content: content, edited: Date.now() })]; case 3: payload = _a.sent(); - if (!String(payload.content).trim()) { + if (!String(payload.content).trim()) throw new Error('[[error:invalid-chat-message]]'); - } return [4 /*yield*/, Messaging.setMessageFields(mid, payload)]; case 4: _a.sent(); @@ -87,10 +102,11 @@ module.exports = function (Messaging) { } // eslint-disable-next-line max-len // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment - plugins.hooks.fire('action:messaging.edit', { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - message: __assign(__assign({}, messages[0]), { content: payload.content }), - }); + return [4 /*yield*/, plugins.hooks.fire('action:messaging.edit', { message: __assign(__assign({}, messages[0]), { content: payload.content }) })]; + case 6: + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + _a.sent(); return [2 /*return*/]; } }); @@ -110,9 +126,8 @@ module.exports = function (Messaging) { return [4 /*yield*/, Messaging.messageExists(messageId)]; case 1: exists = _a.sent(); - if (!exists) { + if (!exists) throw new Error('[[error:invalid-mid]]'); - } return [4 /*yield*/, user.isAdminOrGlobalMod(uid)]; case 2: isAdminOrGlobalMod = _a.sent(); @@ -128,30 +143,26 @@ module.exports = function (Messaging) { case 3: userData = _a.sent(); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - if (userData.banned) { + if (userData.banned) throw new Error('[[error:user-banned]]'); - } return [4 /*yield*/, privileges.global.can(['chat', 'chat:privileged'], uid)]; case 4: canChat = _a.sent(); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - if (!canChat.includes(true)) { + if (!canChat.includes(true)) throw new Error('[[error:no-privileges]]'); - } return [4 /*yield*/, Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system'])]; case 5: messageData = _a.sent(); - if (isAdminOrGlobalMod && !messageData.system) { + if (isAdminOrGlobalMod && !messageData.system) return [2 /*return*/]; - } chatConfigDuration = meta.config[durationConfig]; if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call throw new Error("[[error:chat-".concat(type, "-duration-expired, ").concat(meta.config[durationConfig], "]]")); } - if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) { + if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) return [2 /*return*/]; - } throw new Error("[[error:cant-".concat(type, "-chat-message]]")); } }); @@ -181,9 +192,8 @@ module.exports = function (Messaging) { ])]; case 1: _a = _b.sent(), isAdmin = _a[0], isGlobalMod = _a[1], inRoom = _a[2], isRoomOwner = _a[3]; - if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) { + if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) throw new Error('[[error:no-privileges]]'); - } return [2 /*return*/]; } }); From f9cc7e6aed6fb4ffaa945e37d20503aef6477850 Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Thu, 10 Oct 2024 00:24:32 -0400 Subject: [PATCH 09/14] =?UTF-8?q?(Posible)=20Solucion=20al=20error=20inesp?= =?UTF-8?q?erado=20en=20el=20test=20de=20GitHub,=20dicho=20error=20en=20ve?= =?UTF-8?q?rsion=20local=20no=20se=20manifest=C3=B3.=20Version=20del=20arc?= =?UTF-8?q?hivo=20sigue=20siendo=20funcional=20y=20pasando=20todas=20las?= =?UTF-8?q?=20pruebas=20en=20ESLint=20local.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/messaging/edit.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/messaging/edit.ts b/src/messaging/edit.ts index a6f739c37e..96c547c719 100644 --- a/src/messaging/edit.ts +++ b/src/messaging/edit.ts @@ -79,7 +79,7 @@ module.exports = function (Messaging: MessagingInterface) { }; const canEditDelete = async (messageId: number, uid: number, type: 'edit' | 'delete'): Promise => { - let durationConfig = ''; + let durationConfig : string = ''; if (type === 'edit') { durationConfig = 'chatEditDuration'; } else if (type === 'delete') { @@ -119,10 +119,10 @@ module.exports = function (Messaging: MessagingInterface) { if (isAdminOrGlobalMod && !messageData.system) return; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access - const chatConfigDuration = meta.config[durationConfig]; + const chatConfigDuration : number = meta.config[durationConfig]; if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - throw new Error(`[[error:chat-${type}-duration-expired, ${meta.config[durationConfig]}]]`); + throw new Error(`[[error:chat-${type}-duration-expired, ${chatConfigDuration}]]`); } if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) return; From 38020c7b41c2d962c67abd2a1b246873503d968a Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Thu, 10 Oct 2024 00:30:13 -0400 Subject: [PATCH 10/14] =?UTF-8?q?(Posible)=20Solucion=20al=20SEGUNDO=20err?= =?UTF-8?q?or=20inesperado=20en=20el=20test=20de=20GitHub,=20dicho=20error?= =?UTF-8?q?=20en=20version=20local=20no=20se=20manifest=C3=B3.=20Version?= =?UTF-8?q?=20del=20archivo=20sigue=20siendo=20funcional=20y=20pasando=20t?= =?UTF-8?q?odas=20las=20pruebas=20en=20ESLint=20local.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/messaging/edit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/messaging/edit.ts b/src/messaging/edit.ts index 96c547c719..eabeb8bdc2 100644 --- a/src/messaging/edit.ts +++ b/src/messaging/edit.ts @@ -79,7 +79,7 @@ module.exports = function (Messaging: MessagingInterface) { }; const canEditDelete = async (messageId: number, uid: number, type: 'edit' | 'delete'): Promise => { - let durationConfig : string = ''; + let durationConfig = ''; if (type === 'edit') { durationConfig = 'chatEditDuration'; } else if (type === 'delete') { From 16df48013f4b5702f210bcd33b0134ad32bd3cfc Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Thu, 10 Oct 2024 18:47:16 -0400 Subject: [PATCH 11/14] Inclusion de nuevo archivo JS generado por el archivo traducido TS(edit.ts), esta version es funcional para la app NodeBB. --- src/messaging/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/messaging/edit.js b/src/messaging/edit.js index bad741fbdb..6c0bb25aa7 100644 --- a/src/messaging/edit.js +++ b/src/messaging/edit.js @@ -159,7 +159,7 @@ module.exports = function (Messaging) { chatConfigDuration = meta.config[durationConfig]; if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - throw new Error("[[error:chat-".concat(type, "-duration-expired, ").concat(meta.config[durationConfig], "]]")); + throw new Error("[[error:chat-".concat(type, "-duration-expired, ").concat(chatConfigDuration, "]]")); } if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) return [2 /*return*/]; From a60add9d9c3dec91a38a50e2537cff63bad18079 Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Thu, 10 Oct 2024 22:54:02 -0400 Subject: [PATCH 12/14] Actualizacion final de archivos. Dichas versiones funcionan y se integran a las pruebas y la App, es decir test y lint en local cumplen de forma exitosa. Este commit tiene como funciona, ademas de entregar final, actualizar los archivos tal que el test de github pase la seccion Node (que en local es exitoso). --- src/messaging/edit.js | 4 +--- src/messaging/edit.ts | 5 +---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/messaging/edit.js b/src/messaging/edit.js index 6c0bb25aa7..2284ed44c3 100644 --- a/src/messaging/edit.js +++ b/src/messaging/edit.js @@ -184,11 +184,9 @@ module.exports = function (Messaging) { return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, Promise.all([ - // eslint-disable-next-line max-len // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call user.isAdministrator(uid), user.isGlobalModerator(uid), - Messaging.isUserInRoom(uid, roomId), - Messaging.isRoomOwner(uid, roomId), + Messaging.isUserInRoom(uid, roomId), Messaging.isRoomOwner(uid, roomId), ])]; case 1: _a = _b.sent(), isAdmin = _a[0], isGlobalMod = _a[1], inRoom = _a[2], isRoomOwner = _a[3]; diff --git a/src/messaging/edit.ts b/src/messaging/edit.ts index eabeb8bdc2..fbcf513cc3 100644 --- a/src/messaging/edit.ts +++ b/src/messaging/edit.ts @@ -131,15 +131,12 @@ module.exports = function (Messaging: MessagingInterface) { Messaging.canEdit = async (messageId: number, uid: number): Promise => await canEditDelete(messageId, uid, 'edit'); Messaging.canDelete = async (messageId: number, uid: number): Promise => await canEditDelete(messageId, uid, 'delete'); - Messaging.canPin = async (roomId: number, uid: number): Promise => { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const [isAdmin, isGlobalMod, inRoom, isRoomOwner] = await Promise.all([ - // eslint-disable-next-line max-len // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call user.isAdministrator(uid), user.isGlobalModerator(uid), - Messaging.isUserInRoom(uid, roomId), - Messaging.isRoomOwner(uid, roomId), + Messaging.isUserInRoom(uid, roomId), Messaging.isRoomOwner(uid, roomId), ]); if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) throw new Error('[[error:no-privileges]]'); From be89f93c6f5dfaff211a1d7d18091d94b96d8d25 Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Sun, 13 Oct 2024 17:15:50 -0400 Subject: [PATCH 13/14] Solucion al error detectado en NodeBB "user.isAdminOrGlobalMod is not a function" al tratar de editar un mensaje. Se cambio el tipo de import a require dado que la version bajo ESModules genera dicho error. --- src/messaging/edit.js | 260 +++++++++++++++--------------------------- 1 file changed, 91 insertions(+), 169 deletions(-) diff --git a/src/messaging/edit.js b/src/messaging/edit.js index 2284ed44c3..af759d90bd 100644 --- a/src/messaging/edit.js +++ b/src/messaging/edit.js @@ -1,4 +1,3 @@ -"use strict"; /* ******************************************************************************* ************************ Universidad Simon Bolivar ************************** @@ -14,17 +13,6 @@ * * ******************************************************************************* */ -var __assign = (this && this.__assign) || function () { - __assign = Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); -}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -34,166 +22,100 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; -var __generator = (this && this.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); - return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (g && (g = 0, op[0] && (_ = 0)), _) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -Object.defineProperty(exports, "__esModule", { value: true }); /* Seccion: IMPORTACIONES */ -/* eslint-disable import/no-import-module-exports */ -var meta = require("../meta"); -var user = require("../user"); -var plugins = require("../plugins"); -var privileges = require("../privileges"); -var sockets = require("../socket.io"); +/* eslint-disable @typescript-eslint/no-var-requires */ +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +const meta = require('../meta'); +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +const user = require('../user'); +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +const plugins = require('../plugins'); +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +const privileges = require('../privileges'); +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +const sockets = require('../socket.io'); /* Seccion: FUNCIONES */ module.exports = function (Messaging) { - var _this = this; - Messaging.editMessage = function (uid, mid, roomId, content) { return __awaiter(_this, void 0, void 0, function () { - var raw, payload, messages, roomName; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: return [4 /*yield*/, Messaging.checkContent(content)]; - case 1: - _a.sent(); - return [4 /*yield*/, Messaging.getMessageField(mid, 'content')]; - case 2: - raw = _a.sent(); - if (raw === content) - return [2 /*return*/]; - return [4 /*yield*/, plugins.hooks.fire('filter:messaging.edit', { content: content, edited: Date.now() })]; - case 3: - payload = _a.sent(); - if (!String(payload.content).trim()) - throw new Error('[[error:invalid-chat-message]]'); - return [4 /*yield*/, Messaging.setMessageFields(mid, payload)]; - case 4: - _a.sent(); - return [4 /*yield*/, Messaging.getMessagesData([mid], uid, roomId, true)]; - case 5: - messages = _a.sent(); - if (messages[0]) { - roomName = messages[0].deleted ? "uid_".concat(uid) : "chat_room_".concat(roomId); - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - sockets.in(roomName).emit('event:chats.edit', { messages: messages }); - } - // eslint-disable-next-line max-len - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment - return [4 /*yield*/, plugins.hooks.fire('action:messaging.edit', { message: __assign(__assign({}, messages[0]), { content: payload.content }) })]; - case 6: - // eslint-disable-next-line max-len - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment - _a.sent(); - return [2 /*return*/]; - } - }); - }); }; - var canEditDelete = function (messageId, uid, type) { return __awaiter(_this, void 0, void 0, function () { - var durationConfig, exists, isAdminOrGlobalMod, userData, canChat, messageData, chatConfigDuration; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - durationConfig = ''; - if (type === 'edit') { - durationConfig = 'chatEditDuration'; - } - else if (type === 'delete') { - durationConfig = 'chatDeleteDuration'; - } - return [4 /*yield*/, Messaging.messageExists(messageId)]; - case 1: - exists = _a.sent(); - if (!exists) - throw new Error('[[error:invalid-mid]]'); - return [4 /*yield*/, user.isAdminOrGlobalMod(uid)]; - case 2: - isAdminOrGlobalMod = _a.sent(); - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - if (meta.config.disableChat) { - throw new Error('[[error:chat-disabled]]'); - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - } - else if (!isAdminOrGlobalMod && meta.config.disableChatMessageEditing) { - throw new Error('[[error:chat-message-editing-disabled]]'); - } - return [4 /*yield*/, user.getUserFields(uid, ['banned'])]; - case 3: - userData = _a.sent(); - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - if (userData.banned) - throw new Error('[[error:user-banned]]'); - return [4 /*yield*/, privileges.global.can(['chat', 'chat:privileged'], uid)]; - case 4: - canChat = _a.sent(); - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - if (!canChat.includes(true)) - throw new Error('[[error:no-privileges]]'); - return [4 /*yield*/, Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system'])]; - case 5: - messageData = _a.sent(); - if (isAdminOrGlobalMod && !messageData.system) - return [2 /*return*/]; - chatConfigDuration = meta.config[durationConfig]; - if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - throw new Error("[[error:chat-".concat(type, "-duration-expired, ").concat(chatConfigDuration, "]]")); - } - if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) - return [2 /*return*/]; - throw new Error("[[error:cant-".concat(type, "-chat-message]]")); - } - }); - }); }; - Messaging.canEdit = function (messageId, uid) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { - switch (_a.label) { - case 0: return [4 /*yield*/, canEditDelete(messageId, uid, 'edit')]; - case 1: return [2 /*return*/, _a.sent()]; + Messaging.editMessage = (uid, mid, roomId, content) => __awaiter(this, void 0, void 0, function* () { + yield Messaging.checkContent(content); + const raw = yield Messaging.getMessageField(mid, 'content'); + if (raw === content) + return; + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + const payload = yield plugins.hooks.fire('filter:messaging.edit', { content: content, edited: Date.now() }); + if (!String(payload.content).trim()) + throw new Error('[[error:invalid-chat-message]]'); + yield Messaging.setMessageFields(mid, payload); + // Propagate this change to users in the room + const messages = yield Messaging.getMessagesData([mid], uid, roomId, true); + if (messages[0]) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const roomName = messages[0].deleted ? `uid_${uid}` : `chat_room_${roomId}`; + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + sockets.in(roomName).emit('event:chats.edit', { messages: messages }); + } + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + yield plugins.hooks.fire('action:messaging.edit', { message: Object.assign(Object.assign({}, messages[0]), { content: payload.content }) }); + }); + const canEditDelete = (messageId, uid, type) => __awaiter(this, void 0, void 0, function* () { + let durationConfig = ''; + if (type === 'edit') { + durationConfig = 'chatEditDuration'; } - }); }); }; - Messaging.canDelete = function (messageId, uid) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { - switch (_a.label) { - case 0: return [4 /*yield*/, canEditDelete(messageId, uid, 'delete')]; - case 1: return [2 /*return*/, _a.sent()]; + else if (type === 'delete') { + durationConfig = 'chatDeleteDuration'; } - }); }); }; - Messaging.canPin = function (roomId, uid) { return __awaiter(_this, void 0, void 0, function () { - var _a, isAdmin, isGlobalMod, inRoom, isRoomOwner; - return __generator(this, function (_b) { - switch (_b.label) { - case 0: return [4 /*yield*/, Promise.all([ - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - user.isAdministrator(uid), user.isGlobalModerator(uid), - Messaging.isUserInRoom(uid, roomId), Messaging.isRoomOwner(uid, roomId), - ])]; - case 1: - _a = _b.sent(), isAdmin = _a[0], isGlobalMod = _a[1], inRoom = _a[2], isRoomOwner = _a[3]; - if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) - throw new Error('[[error:no-privileges]]'); - return [2 /*return*/]; - } - }); - }); }; + const exists = yield Messaging.messageExists(messageId); + if (!exists) + throw new Error('[[error:invalid-mid]]'); + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + const isAdminOrGlobalMod = yield user.isAdminOrGlobalMod(uid); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + if (meta.config.disableChat) { + throw new Error('[[error:chat-disabled]]'); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + } + else if (!isAdminOrGlobalMod && meta.config.disableChatMessageEditing) { + throw new Error('[[error:chat-message-editing-disabled]]'); + } + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + const userData = yield user.getUserFields(uid, ['banned']); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + if (userData.banned) + throw new Error('[[error:user-banned]]'); + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + const canChat = yield privileges.global.can(['chat', 'chat:privileged'], uid); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + if (!canChat.includes(true)) + throw new Error('[[error:no-privileges]]'); + const messageData = yield Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system']); + if (isAdminOrGlobalMod && !messageData.system) + return; + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access + const chatConfigDuration = meta.config[durationConfig]; + if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + throw new Error(`[[error:chat-${type}-duration-expired, ${chatConfigDuration}]]`); + } + if (messageData.fromuid === parseInt(uid.toString(), 10) && !messageData.system) + return; + throw new Error(`[[error:cant-${type}-chat-message]]`); + }); + Messaging.canEdit = (messageId, uid) => __awaiter(this, void 0, void 0, function* () { return yield canEditDelete(messageId, uid, 'edit'); }); + Messaging.canDelete = (messageId, uid) => __awaiter(this, void 0, void 0, function* () { return yield canEditDelete(messageId, uid, 'delete'); }); + Messaging.canPin = (roomId, uid) => __awaiter(this, void 0, void 0, function* () { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const [isAdmin, isGlobalMod, inRoom, isRoomOwner] = yield Promise.all([ + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + user.isAdministrator(uid), user.isGlobalModerator(uid), + Messaging.isUserInRoom(uid, roomId), Messaging.isRoomOwner(uid, roomId), + ]); + if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) + throw new Error('[[error:no-privileges]]'); + }); }; From 9360a059436b938dd8b02179708c6aadf864cf65 Mon Sep 17 00:00:00 2001 From: JMLTUnderCode Date: Sun, 13 Oct 2024 17:15:50 -0400 Subject: [PATCH 14/14] Modificacion de importacion de modulos, la version combinada ESModules with CommonJS genera el error en NodeBB "user.isAdminOrGlobalMod is not a function" al tratar de editar mensajes. La version actual de este archivo pasa ESlint. --- src/messaging/edit.ts | 71 ++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/src/messaging/edit.ts b/src/messaging/edit.ts index fbcf513cc3..2044efd2ae 100644 --- a/src/messaging/edit.ts +++ b/src/messaging/edit.ts @@ -15,12 +15,17 @@ */ /* Seccion: IMPORTACIONES */ -/* eslint-disable import/no-import-module-exports */ -import * as meta from '../meta'; -import * as user from '../user'; -import * as plugins from '../plugins'; -import * as privileges from '../privileges'; -import * as sockets from '../socket.io'; +/* eslint-disable @typescript-eslint/no-var-requires */ +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +const meta = require('../meta'); +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +const user = require('../user'); +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +const plugins = require('../plugins'); +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +const privileges = require('../privileges'); +// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment +const sockets = require('../socket.io'); /* Seccion: INTERFACES */ interface Payload { @@ -29,30 +34,40 @@ interface Payload { } interface MessageData { - fromuid : number; + fromuid : number | string; timestamp : number; system : boolean; deleted : boolean; } interface MessagingInterface { - editMessage : (uid: number, mid: number, roomId: number, content: string) => Promise; - getMessageField : (mid: number, field: string) => Promise; - setMessageFields : (mid: number, payload: Payload) => Promise; - checkContent : (content: string) => Promise; - getMessagesData : (mids: number[], uid: number, roomId: number, includeDeleted: boolean) => Promise; - messageExists : (messageId: number) => Promise; - getMessageFields : (messageId: number, fields: string[]) => Promise; - isUserInRoom : (uid: number, roomId: number) => Promise; - isRoomOwner : (uid: number, roomId: number) => Promise; - canEdit : (messageId: number, uid: number) => Promise; - canDelete : (messageId: number, uid: number) => Promise; - canPin : (roomId: number, uid: number) => Promise; + editMessage : (uid: number | string, mid: number | string, roomId: number | string, content: string) => Promise; + getMessageField : (mid: number | string, field: string) => Promise; + setMessageFields : (mid: number | string, payload: Payload) => Promise; + checkContent : (content: string | number) => Promise; + getMessagesData : ( + mids: (number | string)[], + uid: number | string, + roomId: number | string, + includeDeleted: boolean + ) => Promise; + messageExists : (messageId: number | string) => Promise; + getMessageFields : (messageId: number | string, fields: string[]) => Promise; + isUserInRoom : (uid: number | string, roomId: number | string) => Promise; + isRoomOwner : (uid: number | string, roomId: number | string) => Promise; + canEdit : (messageId: number | string, uid: number | string) => Promise; + canDelete : (messageId: number | string, uid: number | string) => Promise; + canPin : (roomId: number | string, uid: number | string) => Promise; } /* Seccion: FUNCIONES */ module.exports = function (Messaging: MessagingInterface) { - Messaging.editMessage = async (uid: number, mid: number, roomId: number, content: string): Promise => { + Messaging.editMessage = async ( + uid: number | string, + mid: number | string, + roomId: number | string, + content: string + ): Promise => { await Messaging.checkContent(content); const raw = await Messaging.getMessageField(mid, 'content'); if (raw === content) return; @@ -62,9 +77,9 @@ module.exports = function (Messaging: MessagingInterface) { const payload: Payload = await plugins.hooks.fire('filter:messaging.edit', { content: content, edited: Date.now() }); if (!String(payload.content).trim()) throw new Error('[[error:invalid-chat-message]]'); - await Messaging.setMessageFields(mid, payload); + // Propagate this change to users in the room const messages = await Messaging.getMessagesData([mid], uid, roomId, true); if (messages[0]) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call @@ -78,7 +93,7 @@ module.exports = function (Messaging: MessagingInterface) { await plugins.hooks.fire('action:messaging.edit', { message: { ...messages[0], content: payload.content } }); }; - const canEditDelete = async (messageId: number, uid: number, type: 'edit' | 'delete'): Promise => { + const canEditDelete = async (messageId: number | string, uid: number | string, type: 'edit' | 'delete'): Promise => { let durationConfig = ''; if (type === 'edit') { durationConfig = 'chatEditDuration'; @@ -86,12 +101,12 @@ module.exports = function (Messaging: MessagingInterface) { durationConfig = 'chatDeleteDuration'; } - const exists = await Messaging.messageExists(messageId); + const exists: boolean = await Messaging.messageExists(messageId); if (!exists) throw new Error('[[error:invalid-mid]]'); // eslint-disable-next-line max-len // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment - const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(uid); + const isAdminOrGlobalMod: boolean = await user.isAdminOrGlobalMod(uid); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call if (meta.config.disableChat) { @@ -110,7 +125,7 @@ module.exports = function (Messaging: MessagingInterface) { // eslint-disable-next-line max-len // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment - const canChat = await privileges.global.can(['chat', 'chat:privileged'], uid); + const canChat: boolean[] = await privileges.global.can(['chat', 'chat:privileged'], uid); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call if (!canChat.includes(true)) throw new Error('[[error:no-privileges]]'); @@ -119,7 +134,7 @@ module.exports = function (Messaging: MessagingInterface) { if (isAdminOrGlobalMod && !messageData.system) return; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access - const chatConfigDuration : number = meta.config[durationConfig]; + const chatConfigDuration: number = meta.config[durationConfig]; if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call throw new Error(`[[error:chat-${type}-duration-expired, ${chatConfigDuration}]]`); @@ -129,8 +144,8 @@ module.exports = function (Messaging: MessagingInterface) { throw new Error(`[[error:cant-${type}-chat-message]]`); }; - Messaging.canEdit = async (messageId: number, uid: number): Promise => await canEditDelete(messageId, uid, 'edit'); - Messaging.canDelete = async (messageId: number, uid: number): Promise => await canEditDelete(messageId, uid, 'delete'); + Messaging.canEdit = async (messageId: number | string, uid: number | string): Promise => await canEditDelete(messageId, uid, 'edit'); + Messaging.canDelete = async (messageId: number | string, uid: number | string): Promise => await canEditDelete(messageId, uid, 'delete'); Messaging.canPin = async (roomId: number, uid: number): Promise => { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const [isAdmin, isGlobalMod, inRoom, isRoomOwner] = await Promise.all([