From 898aaff2e6f0f4ea20d7c535a492eaad219d0db0 Mon Sep 17 00:00:00 2001 From: A6575 Date: Mon, 7 Oct 2024 20:00:08 -0400 Subject: [PATCH 1/6] =?UTF-8?q?Creaci=C3=B3n=20del=20archivo=20unread.ts?= =?UTF-8?q?=20sin=20traducir?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/messaging/unread.ts | 74 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/messaging/unread.ts diff --git a/src/messaging/unread.ts b/src/messaging/unread.ts new file mode 100644 index 0000000000..92d06c6441 --- /dev/null +++ b/src/messaging/unread.ts @@ -0,0 +1,74 @@ +'use strict'; + +const db = require('../database'); +const io = require('../socket.io'); + +module.exports = function (Messaging) { + Messaging.getUnreadCount = async (uid) => { + if (!(parseInt(uid, 10) > 0)) { + return 0; + } + + return await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`); + }; + + Messaging.pushUnreadCount = async (uids, data = null) => { + if (!Array.isArray(uids)) { + uids = [uids]; + } + uids = uids.filter(uid => parseInt(uid, 10) > 0); + if (!uids.length) { + return; + } + uids.forEach((uid) => { + io.in(`uid_${uid}`).emit('event:unread.updateChatCount', data); + }); + }; + + Messaging.markRead = async (uid, roomId) => { + await Promise.all([ + db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId), + db.setObjectField(`uid:${uid}:chat:rooms:read`, roomId, Date.now()), + ]); + }; + + Messaging.hasRead = async (uids, roomId) => { + if (!uids.length) { + return []; + } + const roomData = await Messaging.getRoomData(roomId); + if (!roomData) { + return uids.map(() => false); + } + if (roomData.public) { + const [userTimestamps, mids] = await Promise.all([ + db.getObjectsFields(uids.map(uid => `uid:${uid}:chat:rooms:read`), [roomId]), + db.getSortedSetRevRangeWithScores(`chat:room:${roomId}:mids`, 0, 0), + ]); + const lastMsgTimestamp = mids[0] ? mids[0].score : 0; + return uids.map( + (uid, index) => !userTimestamps[index] || + !userTimestamps[index][roomId] || + parseInt(userTimestamps[index][roomId], 10) > lastMsgTimestamp + ); + } + const isMembers = await db.isMemberOfSortedSets( + uids.map(uid => `uid:${uid}:chat:rooms:unread`), + roomId + ); + return uids.map((uid, index) => !isMembers[index]); + }; + + Messaging.markAllRead = async (uid) => { + await db.delete(`uid:${uid}:chat:rooms:unread`); + }; + + Messaging.markUnread = async (uids, roomId) => { + const exists = await Messaging.roomExists(roomId); + if (!exists) { + return; + } + const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`); + await db.sortedSetsAdd(keys, Date.now(), roomId); + }; +}; \ No newline at end of file From b9f8965422b4782a409bce8070bac9bd952982e4 Mon Sep 17 00:00:00 2001 From: A6575 Date: Mon, 7 Oct 2024 23:20:21 -0400 Subject: [PATCH 2/6] Traducir archivo scr/messaging/unread.js de JS a TS. Posee 51 errores de eslint que no se pueden reparar con el flag --fix --- src/messaging/unread.js | 213 ++++++++++++++++++++++++++++------------ src/messaging/unread.ts | 162 ++++++++++++++++-------------- 2 files changed, 242 insertions(+), 133 deletions(-) diff --git a/src/messaging/unread.js b/src/messaging/unread.js index 6144def618..6ae1487361 100644 --- a/src/messaging/unread.js +++ b/src/messaging/unread.js @@ -1,74 +1,163 @@ 'use strict'; -const db = require('../database'); -const io = require('../socket.io'); - -module.exports = function (Messaging) { - Messaging.getUnreadCount = async (uid) => { - if (!(parseInt(uid, 10) > 0)) { - return 0; +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 _ = { label: 0, sent: function () { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }; let f; let y; let t; let + g; + return g = { next: verb(0), throw: verb(1), 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 database_1 = require('../database'); +const socket_io_1 = require('../socket.io'); - return await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`); +module.exports = function (Messaging) { + const _this = this; + Messaging.getUnreadCount = function (uid) { + return __awaiter(_this, void 0, void 0, function () { + return __generator(this, (_a) => { + switch (_a.label) { + case 0: + if (!(parseInt(uid, 10) > 0)) { + return [2 /* return */, 0]; + } + return [4 /* yield */, database_1.default.sortedSetCard('uid:'.concat(uid, ':chat:rooms:unread'))]; + case 1: return [2 /* return */, _a.sent()]; + } + }); + }); }; - - Messaging.pushUnreadCount = async (uids, data = null) => { - if (!Array.isArray(uids)) { - uids = [uids]; - } - uids = uids.filter(uid => parseInt(uid, 10) > 0); - if (!uids.length) { - return; - } - uids.forEach((uid) => { - io.in(`uid_${uid}`).emit('event:unread.updateChatCount', data); + Messaging.pushUnreadCount = function (uids, data) { + if (data === void 0) { data = null; } + return __awaiter(_this, void 0, void 0, function () { + return __generator(this, (_a) => { + if (!Array.isArray(uids)) { + uids = [uids]; + } + uids = uids.filter(uid => parseInt(uid, 10) > 0); + if (!uids.length) { + return [2]; + } + uids.forEach((uid) => { + socket_io_1.default.in('uid_'.concat(uid)).emit('event:unread.updateChatCount', data); + }); + return [2]; + }); }); }; - - Messaging.markRead = async (uid, roomId) => { - await Promise.all([ - db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId), - db.setObjectField(`uid:${uid}:chat:rooms:read`, roomId, Date.now()), - ]); + Messaging.markRead = function (uid, roomId) { + return __awaiter(_this, void 0, void 0, function () { + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, Promise.all([ + database_1.default.sortedSetRemove('uid:'.concat(uid, ':chat:rooms:unread'), roomId), + database_1.default.setObjectField('uid:'.concat(uid, ':chat:rooms:read'), roomId, Date.now()), + ])]; + case 1: + _a.sent(); + return [2]; + } + }); + }); }; - - Messaging.hasRead = async (uids, roomId) => { - if (!uids.length) { - return []; - } - const roomData = await Messaging.getRoomData(roomId); - if (!roomData) { - return uids.map(() => false); - } - if (roomData.public) { - const [userTimestamps, mids] = await Promise.all([ - db.getObjectsFields(uids.map(uid => `uid:${uid}:chat:rooms:read`), [roomId]), - db.getSortedSetRevRangeWithScores(`chat:room:${roomId}:mids`, 0, 0), - ]); - const lastMsgTimestamp = mids[0] ? mids[0].score : 0; - return uids.map( - (uid, index) => !userTimestamps[index] || - !userTimestamps[index][roomId] || - parseInt(userTimestamps[index][roomId], 10) > lastMsgTimestamp - ); - } - const isMembers = await db.isMemberOfSortedSets( - uids.map(uid => `uid:${uid}:chat:rooms:unread`), - roomId - ); - return uids.map((uid, index) => !isMembers[index]); + Messaging.hasRead = function (uids, roomId) { + return __awaiter(_this, void 0, void 0, function () { + let roomData; let _a; let userTimestamps_1; let mids; let lastMsgTimestamp_1; let + isMembers; + return __generator(this, (_b) => { + switch (_b.label) { + case 0: + if (!uids.length) { + return [2 /* return */, []]; + } + return [4 /* yield */, Messaging.getRoomData(roomId)]; + case 1: + roomData = _b.sent(); + if (!roomData) { + return [2 /* return */, uids.map(() => false)]; + } + if (!roomData.public) return [3 /* break */, 3]; + return [4 /* yield */, Promise.all([ + database_1.default.getObjectsFields(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:read')), [roomId]), + database_1.default.getSortedSetRevRangeWithScores('chat:room:'.concat(roomId, ':mids'), 0, 0), + ])]; + case 2: + _a = _b.sent(), userTimestamps_1 = _a[0], mids = _a[1]; + lastMsgTimestamp_1 = mids[0] ? mids[0].score : 0; + return [2 /* return */, uids.map((uid, index) => !userTimestamps_1[index] || + !userTimestamps_1[index][roomId] || + parseInt(userTimestamps_1[index][roomId], 10) > lastMsgTimestamp_1)]; + case 3: return [4 /* yield */, database_1.default.isMemberOfSortedSets(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')), roomId)]; + case 4: + isMembers = _b.sent(); + return [2 /* return */, uids.map((uid, index) => !isMembers[index])]; + } + }); + }); }; - - Messaging.markAllRead = async (uid) => { - await db.delete(`uid:${uid}:chat:rooms:unread`); + Messaging.markAllRead = function (uid) { + return __awaiter(_this, void 0, void 0, function () { + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, database_1.default.delete('uid:'.concat(uid, ':chat:rooms:unread'))]; + case 1: + _a.sent(); + return [2]; + } + }); + }); }; - - Messaging.markUnread = async (uids, roomId) => { - const exists = await Messaging.roomExists(roomId); - if (!exists) { - return; - } - const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`); - await db.sortedSetsAdd(keys, Date.now(), roomId); + Messaging.markUnread = function (uids, roomId) { + return __awaiter(_this, void 0, void 0, function () { + let exists; let + keys; + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, Messaging.roomExists(roomId)]; + case 1: + exists = _a.sent(); + if (!exists) { + return [2]; + } + keys = uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')); + return [4 /* yield */, database_1.default.sortedSetsAdd(keys, Date.now(), roomId)]; + case 2: + _a.sent(); + return [2]; + } + }); + }); }; }; diff --git a/src/messaging/unread.ts b/src/messaging/unread.ts index 92d06c6441..49287fd308 100644 --- a/src/messaging/unread.ts +++ b/src/messaging/unread.ts @@ -1,74 +1,94 @@ 'use strict'; -const db = require('../database'); -const io = require('../socket.io'); +import db from '../database'; +import io from '../socket.io'; +interface Messaging { + getUnreadCount(uid: string | number): Promise; + pushUnreadCount(uids: string[] | string, data?: any): Promise; + markRead(uid: string | number, roomId: string): Promise; + hasRead(uids: string[], roomId: string): Promise; + markAllRead(uid: string | number): Promise; + markUnread(uids: string[], roomId: string): Promise; + roomExists(roomId: string): Promise; + getRoomData(roomId: string): Promise; +} -module.exports = function (Messaging) { - Messaging.getUnreadCount = async (uid) => { - if (!(parseInt(uid, 10) > 0)) { - return 0; - } - - return await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`); - }; - - Messaging.pushUnreadCount = async (uids, data = null) => { - if (!Array.isArray(uids)) { - uids = [uids]; - } - uids = uids.filter(uid => parseInt(uid, 10) > 0); - if (!uids.length) { - return; - } - uids.forEach((uid) => { - io.in(`uid_${uid}`).emit('event:unread.updateChatCount', data); - }); - }; - - Messaging.markRead = async (uid, roomId) => { - await Promise.all([ - db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId), - db.setObjectField(`uid:${uid}:chat:rooms:read`, roomId, Date.now()), - ]); - }; - - Messaging.hasRead = async (uids, roomId) => { - if (!uids.length) { - return []; - } - const roomData = await Messaging.getRoomData(roomId); - if (!roomData) { - return uids.map(() => false); - } - if (roomData.public) { - const [userTimestamps, mids] = await Promise.all([ - db.getObjectsFields(uids.map(uid => `uid:${uid}:chat:rooms:read`), [roomId]), - db.getSortedSetRevRangeWithScores(`chat:room:${roomId}:mids`, 0, 0), - ]); - const lastMsgTimestamp = mids[0] ? mids[0].score : 0; - return uids.map( - (uid, index) => !userTimestamps[index] || - !userTimestamps[index][roomId] || - parseInt(userTimestamps[index][roomId], 10) > lastMsgTimestamp - ); - } - const isMembers = await db.isMemberOfSortedSets( - uids.map(uid => `uid:${uid}:chat:rooms:unread`), - roomId - ); - return uids.map((uid, index) => !isMembers[index]); - }; - - Messaging.markAllRead = async (uid) => { - await db.delete(`uid:${uid}:chat:rooms:unread`); - }; - - Messaging.markUnread = async (uids, roomId) => { - const exists = await Messaging.roomExists(roomId); - if (!exists) { - return; - } - const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`); - await db.sortedSetsAdd(keys, Date.now(), roomId); - }; -}; \ No newline at end of file +module.exports = function (Messaging: Messaging) { + Messaging.getUnreadCount = async (uid: string | number): Promise => { + if (!(parseInt(uid as string, 10) > 0)) { + return 0; + } + + return await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`); + }; + + Messaging.pushUnreadCount = async (uids: string[] | string, data: any = null): Promise => { + if (!Array.isArray(uids)) { + uids = [uids]; + } + + uids = uids.filter(uid => parseInt(uid as string, 10) > 0); + + if (!uids.length) { + return; + } + + uids.forEach((uid) => { + io.in(`uid_${uid}`).emit('event:unread.updateChatCount', data); + }); + }; + + Messaging.markRead = async (uid: string | number, roomId: string): Promise => { + await Promise.all([ + db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId), + db.setObjectField(`uid:${uid}:chat:rooms:read`, roomId, Date.now()), + ]); + }; + + Messaging.hasRead = async (uids: string[], roomId: string): Promise => { + if (!uids.length) { + return []; + } + + const roomData = await Messaging.getRoomData(roomId); + if (!roomData) { + return uids.map(() => false); + } + + if (roomData.public) { + const [userTimestamps, mids] = await Promise.all([ + db.getObjectsFields(uids.map(uid => `uid:${uid}:chat:rooms:read`), [roomId]), + db.getSortedSetRevRangeWithScores(`chat:room:${roomId}:mids`, 0, 0), + ]); + + const lastMsgTimestamp = mids[0] ? mids[0].score : 0; + return uids.map( + (uid, index) => + !userTimestamps[index] || + !userTimestamps[index][roomId] || + parseInt(userTimestamps[index][roomId], 10) > lastMsgTimestamp + ); + } + + const isMembers = await db.isMemberOfSortedSets( + uids.map(uid => `uid:${uid}:chat:rooms:unread`), + roomId + ); + + return uids.map((uid, index) => !isMembers[index]); + }; + + Messaging.markAllRead = async (uid: string | number): Promise => { + await db.delete(`uid:${uid}:chat:rooms:unread`); + }; + + Messaging.markUnread = async (uids: string[], roomId: string): Promise => { + const exists = await Messaging.roomExists(roomId); + if (!exists) { + return; + } + + const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`); + await db.sortedSetsAdd(keys, Date.now(), roomId); + }; + }; \ No newline at end of file From 7239ac4cbbe290dfe4cc816b7a76c6b1715c8fcd Mon Sep 17 00:00:00 2001 From: A6575 Date: Tue, 8 Oct 2024 17:53:15 -0400 Subject: [PATCH 3/6] Eliminar uso de interfaces para tipado en TS. Luego del cambio aun posee errores de ESLint --- src/messaging/unread.js | 263 +++++++++++++++++++++++----------------- src/messaging/unread.ts | 152 +++++++++++------------ 2 files changed, 231 insertions(+), 184 deletions(-) diff --git a/src/messaging/unread.js b/src/messaging/unread.js index 6ae1487361..2daedbf3ed 100644 --- a/src/messaging/unread.js +++ b/src/messaging/unread.js @@ -11,8 +11,8 @@ const __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, }; const __generator = (this && this.__generator) || function (thisArg, body) { let _ = { label: 0, sent: function () { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }; let f; let y; let t; let - g; - return g = { next: verb(0), throw: verb(1), return: verb(2) }, typeof Symbol === 'function' && (g[Symbol.iterator] = function () { return this; }), g; + 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.'); @@ -39,125 +39,170 @@ const __generator = (this && this.__generator) || function (thisArg, body) { if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; +const __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { + if (pack || arguments.length === 2) { + for (var i = 0, l = from.length, ar; i < l; i++) { + if (ar || !(i in from)) { + if (!ar) ar = Array.prototype.slice.call(from, 0, i); + ar[i] = from[i]; + } + } + } + return to.concat(ar || Array.prototype.slice.call(from)); +}; Object.defineProperty(exports, '__esModule', { value: true }); +exports.Messaging = Messaging; const database_1 = require('../database'); const socket_io_1 = require('../socket.io'); -module.exports = function (Messaging) { +function Messaging() { const _this = this; - Messaging.getUnreadCount = function (uid) { - return __awaiter(_this, void 0, void 0, function () { - return __generator(this, (_a) => { - switch (_a.label) { - case 0: - if (!(parseInt(uid, 10) > 0)) { - return [2 /* return */, 0]; - } - return [4 /* yield */, database_1.default.sortedSetCard('uid:'.concat(uid, ':chat:rooms:unread'))]; - case 1: return [2 /* return */, _a.sent()]; - } - }); - }); - }; - Messaging.pushUnreadCount = function (uids, data) { - if (data === void 0) { data = null; } - return __awaiter(_this, void 0, void 0, function () { - return __generator(this, (_a) => { - if (!Array.isArray(uids)) { - uids = [uids]; - } - uids = uids.filter(uid => parseInt(uid, 10) > 0); - if (!uids.length) { - return [2]; - } - uids.forEach((uid) => { - socket_io_1.default.in('uid_'.concat(uid)).emit('event:unread.updateChatCount', data); + return { + getUnreadCount: function (uid) { + return __awaiter(_this, void 0, void 0, function () { + let result; + return __generator(this, (_a) => { + switch (_a.label) { + case 0: + if (!(parseInt(uid, 10) > 0)) { + return [2 /* return */, 0]; + } + return [4 /* yield */, database_1.default.sortedSetCard('uid:'.concat(uid, ':chat:rooms:unread'))]; + case 1: + result = _a.sent(); + return [2 /* return */, result]; + } }); - return [2]; }); - }); - }; - Messaging.markRead = function (uid, roomId) { - return __awaiter(_this, void 0, void 0, function () { - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, Promise.all([ - database_1.default.sortedSetRemove('uid:'.concat(uid, ':chat:rooms:unread'), roomId), - database_1.default.setObjectField('uid:'.concat(uid, ':chat:rooms:read'), roomId, Date.now()), - ])]; - case 1: - _a.sent(); + }, + pushUnreadCount: function (uids_1) { + const args_1 = []; + for (let _i = 1; _i < arguments.length; _i++) { + args_1[_i - 1] = arguments[_i]; + } + return __awaiter(_this, __spreadArray([uids_1], args_1, true), void 0, function (uids, data) { + if (data === void 0) { data = null; } + return __generator(this, (_a) => { + if (!Array.isArray(uids)) { + uids = [uids]; + } + uids = uids.filter(uid => parseInt(uid, 10) > 0); + if (!uids.length) { return [2]; - } + } + uids.forEach((uid) => { + socket_io_1.default.in('uid_'.concat(uid)).emit('event:unread.updateChatCount', data); + }); + return [2]; + }); }); - }); - }; - Messaging.hasRead = function (uids, roomId) { - return __awaiter(_this, void 0, void 0, function () { - let roomData; let _a; let userTimestamps_1; let mids; let lastMsgTimestamp_1; let - isMembers; - return __generator(this, (_b) => { - switch (_b.label) { - case 0: - if (!uids.length) { - return [2 /* return */, []]; - } - return [4 /* yield */, Messaging.getRoomData(roomId)]; - case 1: - roomData = _b.sent(); - if (!roomData) { - return [2 /* return */, uids.map(() => false)]; - } - if (!roomData.public) return [3 /* break */, 3]; - return [4 /* yield */, Promise.all([ - database_1.default.getObjectsFields(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:read')), [roomId]), - database_1.default.getSortedSetRevRangeWithScores('chat:room:'.concat(roomId, ':mids'), 0, 0), + }, + markRead: function (uid, roomId) { + return __awaiter(_this, void 0, void 0, function () { + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, Promise.all([ + database_1.default.sortedSetRemove('uid:'.concat(uid, ':chat:rooms:unread'), roomId), + database_1.default.setObjectField('uid:'.concat(uid, ':chat:rooms:read'), roomId, Date.now()), ])]; - case 2: - _a = _b.sent(), userTimestamps_1 = _a[0], mids = _a[1]; - lastMsgTimestamp_1 = mids[0] ? mids[0].score : 0; - return [2 /* return */, uids.map((uid, index) => !userTimestamps_1[index] || - !userTimestamps_1[index][roomId] || - parseInt(userTimestamps_1[index][roomId], 10) > lastMsgTimestamp_1)]; - case 3: return [4 /* yield */, database_1.default.isMemberOfSortedSets(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')), roomId)]; - case 4: - isMembers = _b.sent(); - return [2 /* return */, uids.map((uid, index) => !isMembers[index])]; - } + case 1: + _a.sent(); + return [2]; + } + }); }); - }); - }; - Messaging.markAllRead = function (uid) { - return __awaiter(_this, void 0, void 0, function () { - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, database_1.default.delete('uid:'.concat(uid, ':chat:rooms:unread'))]; - case 1: - _a.sent(); - return [2]; - } + }, + hasRead: function (uids, roomId) { + return __awaiter(_this, void 0, void 0, function () { + let roomData; let _a; let userTimestamps_1; let mids; let lastMsgTimestamp_1; let + isMembers; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + if (!uids.length) { + return [2 /* return */, []]; + } + return [4 /* yield */, this.getRoomData(roomId)]; + case 1: + roomData = _b.sent(); + if (!roomData) { + return [2 /* return */, uids.map(() => false)]; + } + if (!roomData.public) return [3 /* break */, 3]; + return [4 /* yield */, Promise.all([ + database_1.default.getObjectsFields(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:read')), [roomId]), + database_1.default.getSortedSetRevRangeWithScores('chat:room:'.concat(roomId, ':mids'), 0, 0), + ])]; + case 2: + _a = _b.sent(), userTimestamps_1 = _a[0], mids = _a[1]; + lastMsgTimestamp_1 = mids[0] ? mids[0].score : 0; + return [2 /* return */, uids.map((uid, index) => !userTimestamps_1[index] || + !userTimestamps_1[index][roomId] || + parseInt(userTimestamps_1[index][roomId], 10) > lastMsgTimestamp_1)]; + case 3: return [4 /* yield */, database_1.default.isMemberOfSortedSets(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')), roomId)]; + case 4: + isMembers = _b.sent(); + return [2 /* return */, uids.map((uid, index) => !isMembers[index])]; + } + }); }); - }); - }; - Messaging.markUnread = function (uids, roomId) { - return __awaiter(_this, void 0, void 0, function () { - let exists; let - keys; - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, Messaging.roomExists(roomId)]; - case 1: - exists = _a.sent(); - if (!exists) { + }, + markAllRead: function (uid) { + return __awaiter(_this, void 0, void 0, function () { + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, database_1.default.delete('uid:'.concat(uid, ':chat:rooms:unread'))]; + case 1: + _a.sent(); return [2]; - } - keys = uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')); - return [4 /* yield */, database_1.default.sortedSetsAdd(keys, Date.now(), roomId)]; - case 2: - _a.sent(); - return [2]; - } + } + }); }); - }); + }, + markUnread: function (uids, roomId) { + return __awaiter(_this, void 0, void 0, function () { + let exists; let + keys; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /* yield */, this.roomExists(roomId)]; + case 1: + exists = _a.sent(); + if (!exists) { + return [2]; + } + keys = uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')); + return [4 /* yield */, database_1.default.sortedSetsAdd(keys, Date.now(), roomId)]; + case 2: + _a.sent(); + return [2]; + } + }); + }); + }, + roomExists: function (roomId) { + return __awaiter(_this, void 0, void 0, function () { + let exists; + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, database_1.default.exists('chat:room:'.concat(roomId))]; + case 1: + exists = _a.sent(); + return [2 /* return */, exists > 0]; + } + }); + }); + }, + getRoomData: function (roomId) { + return __awaiter(_this, void 0, void 0, function () { + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, database_1.default.getObject('chat:room:'.concat(roomId))]; + case 1: return [2 /* return */, _a.sent()]; + } + }); + }); + }, }; -}; +} + diff --git a/src/messaging/unread.ts b/src/messaging/unread.ts index 49287fd308..9d950731ce 100644 --- a/src/messaging/unread.ts +++ b/src/messaging/unread.ts @@ -2,93 +2,95 @@ import db from '../database'; import io from '../socket.io'; -interface Messaging { - getUnreadCount(uid: string | number): Promise; - pushUnreadCount(uids: string[] | string, data?: any): Promise; - markRead(uid: string | number, roomId: string): Promise; - hasRead(uids: string[], roomId: string): Promise; - markAllRead(uid: string | number): Promise; - markUnread(uids: string[], roomId: string): Promise; - roomExists(roomId: string): Promise; - getRoomData(roomId: string): Promise; -} -module.exports = function (Messaging: Messaging) { - Messaging.getUnreadCount = async (uid: string | number): Promise => { - if (!(parseInt(uid as string, 10) > 0)) { - return 0; - } - - return await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`); - }; - - Messaging.pushUnreadCount = async (uids: string[] | string, data: any = null): Promise => { - if (!Array.isArray(uids)) { - uids = [uids]; - } - - uids = uids.filter(uid => parseInt(uid as string, 10) > 0); - - if (!uids.length) { - return; - } - - uids.forEach((uid) => { - io.in(`uid_${uid}`).emit('event:unread.updateChatCount', data); - }); - }; - - Messaging.markRead = async (uid: string | number, roomId: string): Promise => { - await Promise.all([ - db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId), - db.setObjectField(`uid:${uid}:chat:rooms:read`, roomId, Date.now()), - ]); - }; - - Messaging.hasRead = async (uids: string[], roomId: string): Promise => { - if (!uids.length) { - return []; - } - - const roomData = await Messaging.getRoomData(roomId); - if (!roomData) { - return uids.map(() => false); - } - - if (roomData.public) { - const [userTimestamps, mids] = await Promise.all([ - db.getObjectsFields(uids.map(uid => `uid:${uid}:chat:rooms:read`), [roomId]), - db.getSortedSetRevRangeWithScores(`chat:room:${roomId}:mids`, 0, 0), +export function Messaging() { + return { + getUnreadCount: async (uid: string | number): Promise => { + if (!(parseInt(uid as string, 10) > 0)) { + return 0; + } + + const result = await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`); + return result; + }, + + pushUnreadCount: async (uids: string[] | string, data: any = null): Promise => { + if (!Array.isArray(uids)) { + uids = [uids]; + } + + uids = uids.filter(uid => parseInt(uid as string, 10) > 0); + + if (!uids.length) { + return; + } + + uids.forEach((uid) => { + io.in(`uid_${uid}`).emit('event:unread.updateChatCount', data); + }); + }, + + markRead: async (uid: string | number, roomId: string): Promise => { + await Promise.all([ + db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId), + db.setObjectField(`uid:${uid}:chat:rooms:read`, roomId, Date.now()), ]); + }, + + hasRead: async (uids: string[], roomId: string): Promise => { + if (!uids.length) { + return []; + } - const lastMsgTimestamp = mids[0] ? mids[0].score : 0; - return uids.map( - (uid, index) => - !userTimestamps[index] || - !userTimestamps[index][roomId] || - parseInt(userTimestamps[index][roomId], 10) > lastMsgTimestamp - ); + const roomData = await this.getRoomData(roomId); + if (!roomData) { + return uids.map(() => false); } + if (roomData.public) { + const [userTimestamps, mids] = await Promise.all([ + db.getObjectsFields(uids.map(uid => `uid:${uid}:chat:rooms:read`), [roomId]), + db.getSortedSetRevRangeWithScores(`chat:room:${roomId}:mids`, 0, 0), + ]); + + const lastMsgTimestamp = mids[0] ? mids[0].score : 0; + return uids.map( + (uid, index) => + !userTimestamps[index] || + !userTimestamps[index][roomId] || + parseInt(userTimestamps[index][roomId], 10) > lastMsgTimestamp + ); + } + const isMembers = await db.isMemberOfSortedSets( uids.map(uid => `uid:${uid}:chat:rooms:unread`), roomId ); - + return uids.map((uid, index) => !isMembers[index]); - }; - - Messaging.markAllRead = async (uid: string | number): Promise => { + }, + + markAllRead: async (uid: string | number): Promise => { await db.delete(`uid:${uid}:chat:rooms:unread`); - }; - - Messaging.markUnread = async (uids: string[], roomId: string): Promise => { - const exists = await Messaging.roomExists(roomId); + }, + + markUnread: async (uids: string[], roomId: string): Promise => { + const exists = await this.roomExists(roomId); if (!exists) { - return; + return; } - + const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`); await db.sortedSetsAdd(keys, Date.now(), roomId); - }; - }; \ No newline at end of file + }, + + roomExists: async (roomId: string): Promise => { + const exists = await db.exists(`chat:room:${roomId}`); + return exists > 0; + }, + + getRoomData: async (roomId: string): Promise => { + return await db.getObject(`chat:room:${roomId}`); + } + }; +}; \ No newline at end of file From 9e1fcedd708f5ae2fbddd5028c04622a87b38945 Mon Sep 17 00:00:00 2001 From: A6575 Date: Wed, 9 Oct 2024 00:52:08 -0400 Subject: [PATCH 4/6] Cambio de import y uso de interface para tipado en TS --- src/messaging/unread.js | 256 ++++++++++++++++++---------------------- src/messaging/unread.ts | 165 ++++++++++++-------------- 2 files changed, 190 insertions(+), 231 deletions(-) diff --git a/src/messaging/unread.js b/src/messaging/unread.js index 2daedbf3ed..5500c587b0 100644 --- a/src/messaging/unread.js +++ b/src/messaging/unread.js @@ -51,158 +51,128 @@ const __spreadArray = (this && this.__spreadArray) || function (to, from, pack) return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, '__esModule', { value: true }); -exports.Messaging = Messaging; -const database_1 = require('../database'); -const socket_io_1 = require('../socket.io'); +const db = require('../database'); +const io = require('../socket.io'); -function Messaging() { +module.exports = function (Messaging) { const _this = this; - return { - getUnreadCount: function (uid) { - return __awaiter(_this, void 0, void 0, function () { - let result; - return __generator(this, (_a) => { - switch (_a.label) { - case 0: - if (!(parseInt(uid, 10) > 0)) { - return [2 /* return */, 0]; - } - return [4 /* yield */, database_1.default.sortedSetCard('uid:'.concat(uid, ':chat:rooms:unread'))]; - case 1: - result = _a.sent(); - return [2 /* return */, result]; - } - }); + Messaging.getUnreadCount = function (uid) { + return __awaiter(_this, void 0, void 0, function () { + return __generator(this, (_a) => { + switch (_a.label) { + case 0: + if (!(parseInt(uid, 10) > 0)) { + return [2 /* return */, 0]; + } + return [4 /* yield */, db.sortedSetCard('uid:'.concat(uid, ':chat:rooms:unread'))]; + case 1: return [2 /* return */, _a.sent()]; + } }); - }, - pushUnreadCount: function (uids_1) { - const args_1 = []; - for (let _i = 1; _i < arguments.length; _i++) { - args_1[_i - 1] = arguments[_i]; - } - return __awaiter(_this, __spreadArray([uids_1], args_1, true), void 0, function (uids, data) { - if (data === void 0) { data = null; } - return __generator(this, (_a) => { - if (!Array.isArray(uids)) { - uids = [uids]; - } - uids = uids.filter(uid => parseInt(uid, 10) > 0); - if (!uids.length) { - return [2]; - } - uids.forEach((uid) => { - socket_io_1.default.in('uid_'.concat(uid)).emit('event:unread.updateChatCount', data); - }); + }); + }; + Messaging.pushUnreadCount = function (uids_1) { + const args_1 = []; + for (let _i = 1; _i < arguments.length; _i++) { + args_1[_i - 1] = arguments[_i]; + } + return __awaiter(_this, __spreadArray([uids_1], args_1, true), void 0, function (uids, data) { + if (data === void 0) { data = null; } + return __generator(this, (_a) => { + if (!Array.isArray(uids)) { + uids = [uids]; + } + uids = uids.filter(uid => parseInt(uid, 10) > 0); + if (!uids.length) { return [2]; + } + uids.forEach((uid) => { + io.in('uid_'.concat(uid)).emit('event:unread.updateChatCount', data); }); + return [2]; }); - }, - markRead: function (uid, roomId) { - return __awaiter(_this, void 0, void 0, function () { - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, Promise.all([ - database_1.default.sortedSetRemove('uid:'.concat(uid, ':chat:rooms:unread'), roomId), - database_1.default.setObjectField('uid:'.concat(uid, ':chat:rooms:read'), roomId, Date.now()), - ])]; - case 1: - _a.sent(); - return [2]; - } - }); + }); + }; + Messaging.markRead = function (uid, roomId) { + return __awaiter(_this, void 0, void 0, function () { + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, Promise.all([ + db.sortedSetRemove('uid:'.concat(uid, ':chat:rooms:unread'), roomId), + db.setObjectField('uid:'.concat(uid, ':chat:rooms:read'), roomId, Date.now()), + ])]; + case 1: + _a.sent(); + return [2]; + } }); - }, - hasRead: function (uids, roomId) { - return __awaiter(_this, void 0, void 0, function () { - let roomData; let _a; let userTimestamps_1; let mids; let lastMsgTimestamp_1; let - isMembers; - return __generator(this, function (_b) { - switch (_b.label) { - case 0: - if (!uids.length) { - return [2 /* return */, []]; - } - return [4 /* yield */, this.getRoomData(roomId)]; - case 1: - roomData = _b.sent(); - if (!roomData) { - return [2 /* return */, uids.map(() => false)]; - } - if (!roomData.public) return [3 /* break */, 3]; - return [4 /* yield */, Promise.all([ - database_1.default.getObjectsFields(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:read')), [roomId]), - database_1.default.getSortedSetRevRangeWithScores('chat:room:'.concat(roomId, ':mids'), 0, 0), - ])]; - case 2: - _a = _b.sent(), userTimestamps_1 = _a[0], mids = _a[1]; - lastMsgTimestamp_1 = mids[0] ? mids[0].score : 0; - return [2 /* return */, uids.map((uid, index) => !userTimestamps_1[index] || - !userTimestamps_1[index][roomId] || - parseInt(userTimestamps_1[index][roomId], 10) > lastMsgTimestamp_1)]; - case 3: return [4 /* yield */, database_1.default.isMemberOfSortedSets(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')), roomId)]; - case 4: - isMembers = _b.sent(); - return [2 /* return */, uids.map((uid, index) => !isMembers[index])]; - } - }); + }); + }; + Messaging.hasRead = function (uids, roomId) { + return __awaiter(_this, void 0, void 0, function () { + let roomData; let _a; let userTimestamps_1; let mids; let lastMsgTimestamp_1; let + isMembers; + return __generator(this, (_b) => { + switch (_b.label) { + case 0: + if (!uids.length) { + return [2 /* return */, []]; + } + return [4 /* yield */, Messaging.getRoomData(roomId)]; + case 1: + roomData = _b.sent(); + if (!roomData) { + return [2 /* return */, uids.map(() => false)]; + } + if (!roomData.public) return [3 /* break */, 3]; + return [4 /* yield */, Promise.all([ + db.getObjectsFields(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:read')), [roomId]), + db.getSortedSetRevRangeWithScores('chat:room:'.concat(roomId, ':mids'), 0, 0), + ])]; + case 2: + _a = _b.sent(), userTimestamps_1 = _a[0], mids = _a[1]; + lastMsgTimestamp_1 = mids[0] ? mids[0].score : 0; + return [2 /* return */, uids.map((uid, index) => !userTimestamps_1[index] || + !userTimestamps_1[index][roomId] || + parseInt(userTimestamps_1[index][roomId], 10) > lastMsgTimestamp_1)]; + case 3: return [4 /* yield */, db.isMemberOfSortedSets(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')), roomId)]; + case 4: + isMembers = _b.sent(); + return [2 /* return */, uids.map((uid, index) => !isMembers[index])]; + } }); - }, - markAllRead: function (uid) { - return __awaiter(_this, void 0, void 0, function () { - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, database_1.default.delete('uid:'.concat(uid, ':chat:rooms:unread'))]; - case 1: - _a.sent(); - return [2]; - } - }); + }); + }; + Messaging.markAllRead = function (uid) { + return __awaiter(_this, void 0, void 0, function () { + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, db.delete('uid:'.concat(uid, ':chat:rooms:unread'))]; + case 1: + _a.sent(); + return [2]; + } }); - }, - markUnread: function (uids, roomId) { - return __awaiter(_this, void 0, void 0, function () { - let exists; let - keys; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: return [4 /* yield */, this.roomExists(roomId)]; - case 1: - exists = _a.sent(); - if (!exists) { - return [2]; - } - keys = uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')); - return [4 /* yield */, database_1.default.sortedSetsAdd(keys, Date.now(), roomId)]; - case 2: - _a.sent(); + }); + }; + Messaging.markUnread = function (uids, roomId) { + return __awaiter(_this, void 0, void 0, function () { + let exists; let + keys; + return __generator(this, (_a) => { + switch (_a.label) { + case 0: return [4 /* yield */, Messaging.roomExists(roomId)]; + case 1: + exists = _a.sent(); + if (!exists) { return [2]; - } - }); - }); - }, - roomExists: function (roomId) { - return __awaiter(_this, void 0, void 0, function () { - let exists; - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, database_1.default.exists('chat:room:'.concat(roomId))]; - case 1: - exists = _a.sent(); - return [2 /* return */, exists > 0]; - } - }); - }); - }, - getRoomData: function (roomId) { - return __awaiter(_this, void 0, void 0, function () { - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, database_1.default.getObject('chat:room:'.concat(roomId))]; - case 1: return [2 /* return */, _a.sent()]; - } - }); + } + keys = uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')); + return [4 /* yield */, db.sortedSetsAdd(keys, Date.now(), roomId)]; + case 2: + _a.sent(); + return [2]; + } }); - }, + }); }; -} - +}; diff --git a/src/messaging/unread.ts b/src/messaging/unread.ts index 9d950731ce..7a6f94e697 100644 --- a/src/messaging/unread.ts +++ b/src/messaging/unread.ts @@ -1,96 +1,85 @@ 'use strict'; -import db from '../database'; -import io from '../socket.io'; +import * as db from '../database'; +import * as io from '../socket.io'; -export function Messaging() { - return { - getUnreadCount: async (uid: string | number): Promise => { - if (!(parseInt(uid as string, 10) > 0)) { - return 0; - } - - const result = await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`); - return result; - }, +interface Messaging { + getUnreadCount: (uid: string | number) => Promise; + pushUnreadCount: (uids: Array, data?: any) => Promise; + markRead: (uid: string | number, roomId: number) => Promise; + hasRead: (uids: Array, roomId: number) => Promise; + markAllRead: (uid: string | number) => Promise; + markUnread: (uids: Array, roomId: number) => Promise; + getRoomData: (roomId: number) => Promise; + roomExists: (roomId: number) => Promise; +} - pushUnreadCount: async (uids: string[] | string, data: any = null): Promise => { - if (!Array.isArray(uids)) { - uids = [uids]; - } - - uids = uids.filter(uid => parseInt(uid as string, 10) > 0); - - if (!uids.length) { - return; - } - - uids.forEach((uid) => { - io.in(`uid_${uid}`).emit('event:unread.updateChatCount', data); - }); - }, +module.exports = function (Messaging: Messaging) { + Messaging.getUnreadCount = async (uid: string | number) => { + if (!(parseInt(uid as string, 10) > 0)) { + return 0; + } - markRead: async (uid: string | number, roomId: string): Promise => { - await Promise.all([ - db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId), - db.setObjectField(`uid:${uid}:chat:rooms:read`, roomId, Date.now()), - ]); - }, + return await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`); + }; - hasRead: async (uids: string[], roomId: string): Promise => { - if (!uids.length) { - return []; - } - - const roomData = await this.getRoomData(roomId); - if (!roomData) { - return uids.map(() => false); - } - - if (roomData.public) { - const [userTimestamps, mids] = await Promise.all([ - db.getObjectsFields(uids.map(uid => `uid:${uid}:chat:rooms:read`), [roomId]), - db.getSortedSetRevRangeWithScores(`chat:room:${roomId}:mids`, 0, 0), - ]); - - const lastMsgTimestamp = mids[0] ? mids[0].score : 0; - return uids.map( - (uid, index) => - !userTimestamps[index] || - !userTimestamps[index][roomId] || - parseInt(userTimestamps[index][roomId], 10) > lastMsgTimestamp - ); - } - - const isMembers = await db.isMemberOfSortedSets( - uids.map(uid => `uid:${uid}:chat:rooms:unread`), - roomId - ); - - return uids.map((uid, index) => !isMembers[index]); - }, + Messaging.pushUnreadCount = async (uids: Array, data: any = null) => { + if (!Array.isArray(uids)) { + uids = [uids]; + } + uids = uids.filter(uid => parseInt(uid as string, 10) > 0); + if (!uids.length) { + return; + } + uids.forEach((uid) => { + io.in(`uid_${uid}`).emit('event:unread.updateChatCount', data); + }); + }; - markAllRead: async (uid: string | number): Promise => { - await db.delete(`uid:${uid}:chat:rooms:unread`); - }, + Messaging.markRead = async (uid: string | number, roomId: number) => { + await Promise.all([ + db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId), + db.setObjectField(`uid:${uid}:chat:rooms:read`, roomId, Date.now()), + ]); + }; - markUnread: async (uids: string[], roomId: string): Promise => { - const exists = await this.roomExists(roomId); - if (!exists) { - return; - } - - const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`); - await db.sortedSetsAdd(keys, Date.now(), roomId); - }, - - roomExists: async (roomId: string): Promise => { - const exists = await db.exists(`chat:room:${roomId}`); - return exists > 0; - }, - - getRoomData: async (roomId: string): Promise => { - return await db.getObject(`chat:room:${roomId}`); - } - }; -}; \ No newline at end of file + Messaging.hasRead = async (uids: Array, roomId: number) => { + if (!uids.length) { + return []; + } + const roomData = await Messaging.getRoomData(roomId); + if (!roomData) { + return uids.map(() => false); + } + if (roomData.public) { + const [userTimestamps, mids] = await Promise.all([ + db.getObjectsFields(uids.map(uid => `uid:${uid}:chat:rooms:read`), [roomId]), + db.getSortedSetRevRangeWithScores(`chat:room:${roomId}:mids`, 0, 0), + ]); + const lastMsgTimestamp = mids[0] ? mids[0].score : 0; + return uids.map( + (uid, index) => !userTimestamps[index] || + !userTimestamps[index][roomId] || + parseInt(userTimestamps[index][roomId], 10) > lastMsgTimestamp + ); + } + const isMembers = await db.isMemberOfSortedSets( + uids.map(uid => `uid:${uid}:chat:rooms:unread`), + roomId + ); + return uids.map((uid, index) => !isMembers[index]); + }; + + Messaging.markAllRead = async (uid: string | number) => { + await db.delete(`uid:${uid}:chat:rooms:unread`); + }; + + Messaging.markUnread = async (uids: Array, roomId: number) => { + const exists = await Messaging.roomExists(roomId); + if (!exists) { + return; + } + const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`); + await db.sortedSetsAdd(keys, Date.now(), roomId); + }; +} From 21ef50639bfa62c8a4f2dd19465b2dd90af92f18 Mon Sep 17 00:00:00 2001 From: A6575 Date: Fri, 11 Oct 2024 17:55:12 -0400 Subject: [PATCH 5/6] Arreglado errores del linter del ts. Proyecto actualmente funcional y con los test aprobados --- src/messaging/unread.js | 315 ++++++++++++++++++---------------------- src/messaging/unread.ts | 138 +++++++++--------- 2 files changed, 213 insertions(+), 240 deletions(-) diff --git a/src/messaging/unread.js b/src/messaging/unread.js index 5500c587b0..be15ca42fe 100644 --- a/src/messaging/unread.js +++ b/src/messaging/unread.js @@ -1,178 +1,145 @@ -'use strict'; - -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()); - }); +"use strict"; +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 _ = { label: 0, sent: function () { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }; let f; let y; let t; 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++; 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 }; - } +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 }; + } }; -const __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { - if (pack || arguments.length === 2) { - for (var i = 0, l = from.length, ar; i < l; i++) { - if (ar || !(i in from)) { - if (!ar) ar = Array.prototype.slice.call(from, 0, i); - ar[i] = from[i]; - } - } - } - return to.concat(ar || Array.prototype.slice.call(from)); -}; -Object.defineProperty(exports, '__esModule', { value: true }); -const db = require('../database'); -const io = require('../socket.io'); - +Object.defineProperty(exports, "__esModule", { value: true }); +/* eslint-disable import/no-import-module-exports */ +var db = require("../database"); +var io = require("../socket.io"); module.exports = function (Messaging) { - const _this = this; - Messaging.getUnreadCount = function (uid) { - return __awaiter(_this, void 0, void 0, function () { - return __generator(this, (_a) => { - switch (_a.label) { - case 0: - if (!(parseInt(uid, 10) > 0)) { - return [2 /* return */, 0]; - } - return [4 /* yield */, db.sortedSetCard('uid:'.concat(uid, ':chat:rooms:unread'))]; - case 1: return [2 /* return */, _a.sent()]; - } - }); - }); - }; - Messaging.pushUnreadCount = function (uids_1) { - const args_1 = []; - for (let _i = 1; _i < arguments.length; _i++) { - args_1[_i - 1] = arguments[_i]; - } - return __awaiter(_this, __spreadArray([uids_1], args_1, true), void 0, function (uids, data) { - if (data === void 0) { data = null; } - return __generator(this, (_a) => { - if (!Array.isArray(uids)) { - uids = [uids]; - } - uids = uids.filter(uid => parseInt(uid, 10) > 0); - if (!uids.length) { - return [2]; - } - uids.forEach((uid) => { - io.in('uid_'.concat(uid)).emit('event:unread.updateChatCount', data); - }); - return [2]; - }); - }); - }; - Messaging.markRead = function (uid, roomId) { - return __awaiter(_this, void 0, void 0, function () { - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, Promise.all([ - db.sortedSetRemove('uid:'.concat(uid, ':chat:rooms:unread'), roomId), - db.setObjectField('uid:'.concat(uid, ':chat:rooms:read'), roomId, Date.now()), - ])]; - case 1: - _a.sent(); - return [2]; - } - }); - }); - }; - Messaging.hasRead = function (uids, roomId) { - return __awaiter(_this, void 0, void 0, function () { - let roomData; let _a; let userTimestamps_1; let mids; let lastMsgTimestamp_1; let - isMembers; - return __generator(this, (_b) => { - switch (_b.label) { - case 0: - if (!uids.length) { - return [2 /* return */, []]; - } - return [4 /* yield */, Messaging.getRoomData(roomId)]; - case 1: - roomData = _b.sent(); - if (!roomData) { - return [2 /* return */, uids.map(() => false)]; - } - if (!roomData.public) return [3 /* break */, 3]; - return [4 /* yield */, Promise.all([ - db.getObjectsFields(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:read')), [roomId]), - db.getSortedSetRevRangeWithScores('chat:room:'.concat(roomId, ':mids'), 0, 0), - ])]; - case 2: - _a = _b.sent(), userTimestamps_1 = _a[0], mids = _a[1]; - lastMsgTimestamp_1 = mids[0] ? mids[0].score : 0; - return [2 /* return */, uids.map((uid, index) => !userTimestamps_1[index] || - !userTimestamps_1[index][roomId] || - parseInt(userTimestamps_1[index][roomId], 10) > lastMsgTimestamp_1)]; - case 3: return [4 /* yield */, db.isMemberOfSortedSets(uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')), roomId)]; - case 4: - isMembers = _b.sent(); - return [2 /* return */, uids.map((uid, index) => !isMembers[index])]; - } - }); - }); - }; - Messaging.markAllRead = function (uid) { - return __awaiter(_this, void 0, void 0, function () { - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, db.delete('uid:'.concat(uid, ':chat:rooms:unread'))]; - case 1: - _a.sent(); - return [2]; - } - }); - }); - }; - Messaging.markUnread = function (uids, roomId) { - return __awaiter(_this, void 0, void 0, function () { - let exists; let - keys; - return __generator(this, (_a) => { - switch (_a.label) { - case 0: return [4 /* yield */, Messaging.roomExists(roomId)]; - case 1: - exists = _a.sent(); - if (!exists) { - return [2]; - } - keys = uids.map(uid => 'uid:'.concat(uid, ':chat:rooms:unread')); - return [4 /* yield */, db.sortedSetsAdd(keys, Date.now(), roomId)]; - case 2: - _a.sent(); - return [2]; - } - }); - }); - }; + var _this = this; + Messaging.getUnreadCount = function (uid) { return __awaiter(_this, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!(parseInt(uid, 10) > 0)) { + return [2 /*return*/, 0]; + } + return [4 /*yield*/, db.sortedSetCard("uid:".concat(uid, ":chat:rooms:unread"))]; + case 1: + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return + return [2 /*return*/, _a.sent()]; + } + }); + }); }; + Messaging.pushUnreadCount = function (uids, data) { + if (data === void 0) { data = null; } + if (!Array.isArray(uids)) { + uids = [uids]; + } + uids = uids.filter(function (uid) { return parseInt(uid, 10) > 0; }); + if (!uids.length) { + return; + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + uids.forEach(function (uid) { io.in("uid_".concat(uid)).emit('event:unread.updateChatCount', data); }); + }; + Messaging.markRead = function (uid, roomId) { return __awaiter(_this, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + return [4 /*yield*/, Promise.all([db.sortedSetRemove("uid:".concat(uid, ":chat:rooms:unread"), roomId), db.setObjectField("uid:".concat(uid, ":chat:rooms:read"), roomId, Date.now()), + ])]; + case 1: + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + _a.sent(); + return [2 /*return*/]; + } + }); + }); }; + Messaging.hasRead = function (uids, roomId) { return __awaiter(_this, void 0, void 0, function () { + var roomData, _a, userTimestamps_1, mids, lastMsgTimestamp_1, isMembers; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + if (!uids.length) { + return [2 /*return*/, []]; + } + return [4 /*yield*/, Messaging.getRoomData(roomId)]; + case 1: + roomData = _b.sent(); + if (!roomData) { + return [2 /*return*/, uids.map(function () { return false; })]; + } + if (!roomData.public) return [3 /*break*/, 3]; + return [4 /*yield*/, Promise.all([db.getObjectsFields(uids.map(function (uid) { return "uid:".concat(uid, ":chat:rooms:read"); }), [roomId]), db.getSortedSetRevRangeWithScores("chat:room:".concat(roomId, ":mids"), 0, 0)])]; + case 2: + _a = _b.sent(), userTimestamps_1 = _a[0], mids = _a[1]; + lastMsgTimestamp_1 = (mids.length > 0 && mids[0].score) ? mids[0].score : 0; + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + return [2 /*return*/, uids.map(function (uid, index) { return !userTimestamps_1[index] || !userTimestamps_1[index][roomId] || + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + parseInt(userTimestamps_1[index][roomId], 10) > lastMsgTimestamp_1; })]; + case 3: return [4 /*yield*/, db.isMemberOfSortedSets(uids.map(function (uid) { return "uid:".concat(uid, ":chat:rooms:unread"); }), roomId)]; + case 4: + isMembers = _b.sent(); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + return [2 /*return*/, uids.map(function (uid, index) { return !isMembers[index]; })]; + } + }); + }); }; + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + Messaging.markAllRead = function (uid) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, db.delete("uid:".concat(uid, ":chat:rooms:unread"))]; + case 1: + _a.sent(); + return [2 /*return*/]; + } + }); }); }; + Messaging.markUnread = function (uids, roomId) { return __awaiter(_this, void 0, void 0, function () { + var exists, keys; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, Messaging.roomExists(roomId)]; + case 1: + exists = _a.sent(); + if (!exists) { + return [2 /*return*/]; + } + keys = uids.map(function (uid) { return "uid:".concat(uid, ":chat:rooms:unread"); }); + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + return [4 /*yield*/, db.sortedSetsAdd(keys, Date.now(), roomId)]; + case 2: + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + _a.sent(); + return [2 /*return*/]; + } + }); + }); }; }; diff --git a/src/messaging/unread.ts b/src/messaging/unread.ts index 7a6f94e697..341184a7c1 100644 --- a/src/messaging/unread.ts +++ b/src/messaging/unread.ts @@ -1,85 +1,91 @@ -'use strict'; +/* eslint-disable import/no-import-module-exports */ import * as db from '../database'; import * as io from '../socket.io'; interface Messaging { getUnreadCount: (uid: string | number) => Promise; - pushUnreadCount: (uids: Array, data?: any) => Promise; + pushUnreadCount: (uids: Array, data?: unknown) => void; markRead: (uid: string | number, roomId: number) => Promise; hasRead: (uids: Array, roomId: number) => Promise; markAllRead: (uid: string | number) => Promise; markUnread: (uids: Array, roomId: number) => Promise; - getRoomData: (roomId: number) => Promise; + getRoomData: (roomId: number) => Promise<{ public: boolean } | null>; roomExists: (roomId: number) => Promise; } -module.exports = function (Messaging: Messaging) { - Messaging.getUnreadCount = async (uid: string | number) => { - if (!(parseInt(uid as string, 10) > 0)) { - return 0; - } +// Definir el tipo correcto para los datos que devuelven las llamadas a la base de datos +interface TimestampData { + [key: string]: string; +} + +interface MidData { + score: number; +} - return await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`); - }; +module.exports = function (Messaging: Messaging) { + Messaging.getUnreadCount = async (uid: string | number) => { + if (!(parseInt(uid as string, 10) > 0)) { + return 0; + } + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return + return await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`); + }; - Messaging.pushUnreadCount = async (uids: Array, data: any = null) => { - if (!Array.isArray(uids)) { - uids = [uids]; - } - uids = uids.filter(uid => parseInt(uid as string, 10) > 0); - if (!uids.length) { - return; - } - uids.forEach((uid) => { - io.in(`uid_${uid}`).emit('event:unread.updateChatCount', data); - }); - }; + Messaging.pushUnreadCount = (uids: Array, data: unknown = null) => { + if (!Array.isArray(uids)) { + uids = [uids]; + } + uids = uids.filter(uid => parseInt(uid as string, 10) > 0); + if (!uids.length) { + return; + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + uids.forEach((uid) => { io.in(`uid_${uid}`).emit('event:unread.updateChatCount', data); }); + }; - Messaging.markRead = async (uid: string | number, roomId: number) => { - await Promise.all([ - db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId), - db.setObjectField(`uid:${uid}:chat:rooms:read`, roomId, Date.now()), - ]); - }; + Messaging.markRead = async (uid: string | number, roomId: number) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + await Promise.all([db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId), db.setObjectField(`uid:${uid}:chat:rooms:read`, roomId, Date.now()), + ]); + }; - Messaging.hasRead = async (uids: Array, roomId: number) => { - if (!uids.length) { - return []; - } - const roomData = await Messaging.getRoomData(roomId); - if (!roomData) { - return uids.map(() => false); - } - if (roomData.public) { - const [userTimestamps, mids] = await Promise.all([ - db.getObjectsFields(uids.map(uid => `uid:${uid}:chat:rooms:read`), [roomId]), - db.getSortedSetRevRangeWithScores(`chat:room:${roomId}:mids`, 0, 0), - ]); - const lastMsgTimestamp = mids[0] ? mids[0].score : 0; - return uids.map( - (uid, index) => !userTimestamps[index] || - !userTimestamps[index][roomId] || - parseInt(userTimestamps[index][roomId], 10) > lastMsgTimestamp - ); - } - const isMembers = await db.isMemberOfSortedSets( - uids.map(uid => `uid:${uid}:chat:rooms:unread`), - roomId - ); - return uids.map((uid, index) => !isMembers[index]); - }; + Messaging.hasRead = async (uids: Array, roomId: number) => { + if (!uids.length) { + return []; + } - Messaging.markAllRead = async (uid: string | number) => { - await db.delete(`uid:${uid}:chat:rooms:unread`); - }; + const roomData = await Messaging.getRoomData(roomId); + if (!roomData) { + return uids.map(() => false); + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + if (roomData.public) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + const [userTimestamps, mids] = await Promise.all([db.getObjectsFields(uids.map(uid => `uid:${uid}:chat:rooms:read`), [roomId]) as Promise, db.getSortedSetRevRangeWithScores(`chat:room:${roomId}:mids`, 0, 0) as Promise]); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + const lastMsgTimestamp = (mids.length > 0 && mids[0].score) ? mids[0].score : 0; + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + return uids.map((uid, index) => !userTimestamps[index] || !userTimestamps[index][roomId] || + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + parseInt(userTimestamps[index][roomId], 10) > lastMsgTimestamp); + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + const isMembers = await db.isMemberOfSortedSets(uids.map(uid => `uid:${uid}:chat:rooms:unread`), roomId) as boolean[]; + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + return uids.map((uid, index) => !isMembers[index]); + }; + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + Messaging.markAllRead = async (uid: string | number) => { await db.delete(`uid:${uid}:chat:rooms:unread`); }; - Messaging.markUnread = async (uids: Array, roomId: number) => { - const exists = await Messaging.roomExists(roomId); - if (!exists) { - return; - } - const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`); - await db.sortedSetsAdd(keys, Date.now(), roomId); - }; -} + Messaging.markUnread = async (uids: Array, roomId: number) => { + const exists = await Messaging.roomExists(roomId); + if (!exists) { + return; + } + const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`); + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access + await db.sortedSetsAdd(keys, Date.now(), roomId); + }; +}; From f2d5fde888d2a0041f85b47df74ffac321c08744 Mon Sep 17 00:00:00 2001 From: A6575 Date: Fri, 11 Oct 2024 18:59:45 -0400 Subject: [PATCH 6/6] Agregado comentarios finales --- src/messaging/unread.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/messaging/unread.ts b/src/messaging/unread.ts index 341184a7c1..04166ff9ca 100644 --- a/src/messaging/unread.ts +++ b/src/messaging/unread.ts @@ -3,6 +3,7 @@ import * as db from '../database'; import * as io from '../socket.io'; +// Interfaces para el tipado de funciones y/o variables interface Messaging { getUnreadCount: (uid: string | number) => Promise; pushUnreadCount: (uids: Array, data?: unknown) => void; @@ -14,7 +15,6 @@ interface Messaging { roomExists: (roomId: number) => Promise; } -// Definir el tipo correcto para los datos que devuelven las llamadas a la base de datos interface TimestampData { [key: string]: string; } @@ -23,6 +23,7 @@ interface MidData { score: number; } +// Codigo principal module.exports = function (Messaging: Messaging) { Messaging.getUnreadCount = async (uid: string | number) => { if (!(parseInt(uid as string, 10) > 0)) {