From 7983711a12280576ea017071eea0993a7be8a2b4 Mon Sep 17 00:00:00 2001 From: freddcastro Date: Wed, 9 Oct 2024 22:45:55 -0400 Subject: [PATCH 1/4] =?UTF-8?q?se=20a=C3=B1adi=C3=B3=20el=20archivo=20.ts?= =?UTF-8?q?=20de=20search.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/groups/search.ts | 86 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/groups/search.ts diff --git a/src/groups/search.ts b/src/groups/search.ts new file mode 100644 index 0000000000..3e0dfd2def --- /dev/null +++ b/src/groups/search.ts @@ -0,0 +1,86 @@ +'use strict'; + +const user = require('../user'); +const db = require('../database'); + +module.exports = function (Groups) { + Groups.search = async function (query, options) { + if (!query) { + return []; + } + query = String(query).toLowerCase(); + let groupNames = Object.values(await db.getObject('groupslug:groupname')); + if (!options.hideEphemeralGroups) { + groupNames = Groups.ephemeralGroups.concat(groupNames); + } + groupNames = groupNames.filter( + name => name.toLowerCase().includes(query) && name !== Groups.BANNED_USERS // hide banned-users in searches + ); + groupNames = groupNames.slice(0, 100); + + let groupsData; + if (options.showMembers) { + groupsData = await Groups.getGroupsAndMembers(groupNames); + } else { + groupsData = await Groups.getGroupsData(groupNames); + } + groupsData = groupsData.filter(Boolean); + if (options.filterHidden) { + groupsData = groupsData.filter(group => !group.hidden); + } + return Groups.sort(options.sort, groupsData); + }; + + Groups.sort = function (strategy, groups) { + switch (strategy) { + case 'count': + groups.sort((a, b) => a.slug > b.slug) + .sort((a, b) => b.memberCount - a.memberCount); + break; + + case 'date': + groups.sort((a, b) => b.createtime - a.createtime); + break; + + case 'alpha': // intentional fall-through + default: + groups.sort((a, b) => (a.slug > b.slug ? 1 : -1)); + } + + return groups; + }; + + Groups.searchMembers = async function (data) { + if (!data.query) { + const users = await Groups.getOwnersAndMembers(data.groupName, data.uid, 0, 19); + const matchCount = users.length; + const timing = '0.00'; + return { users, matchCount, timing }; + } + + const results = await user.search({ + ...data, + paginate: false, + hardCap: -1, + }); + + const uids = results.users.map(user => user && user.uid); + const isOwners = await Groups.ownership.isOwners(uids, data.groupName); + + results.users.forEach((user, index) => { + if (user) { + user.isOwner = isOwners[index]; + } + }); + + results.users.sort((a, b) => { + if (a.isOwner && !b.isOwner) { + return -1; + } else if (!a.isOwner && b.isOwner) { + return 1; + } + return 0; + }); + return results; + }; +}; From 81085e2c7b7d596c86b3b02129a3739753a3eec4 Mon Sep 17 00:00:00 2001 From: freddcastro Date: Wed, 9 Oct 2024 22:48:16 -0400 Subject: [PATCH 2/4] cambiamos los require por imports --- src/groups/search.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/groups/search.ts b/src/groups/search.ts index 3e0dfd2def..d9aac12789 100644 --- a/src/groups/search.ts +++ b/src/groups/search.ts @@ -1,7 +1,7 @@ 'use strict'; -const user = require('../user'); -const db = require('../database'); +import user from '../user'; +import db from '../database'; module.exports = function (Groups) { Groups.search = async function (query, options) { From 4597cb4d1fd26bdf6511ad73a0661b0fa128882f Mon Sep 17 00:00:00 2001 From: freddcastro Date: Sat, 12 Oct 2024 18:34:20 -0400 Subject: [PATCH 3/4] =?UTF-8?q?Se=20a=C3=B1adieron=20las=20interfaces=20y?= =?UTF-8?q?=20tipos=20principales=20para=20el=20retorno=20y=20uso=20de=20f?= =?UTF-8?q?unciones?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/groups/search.js | 175 ++++++++++++++++++++++--------------------- 1 file changed, 90 insertions(+), 85 deletions(-) diff --git a/src/groups/search.js b/src/groups/search.js index 3e0dfd2def..ed38fea527 100644 --- a/src/groups/search.js +++ b/src/groups/search.js @@ -1,86 +1,91 @@ -'use strict'; - -const user = require('../user'); -const db = require('../database'); - -module.exports = function (Groups) { - Groups.search = async function (query, options) { - if (!query) { - return []; - } - query = String(query).toLowerCase(); - let groupNames = Object.values(await db.getObject('groupslug:groupname')); - if (!options.hideEphemeralGroups) { - groupNames = Groups.ephemeralGroups.concat(groupNames); - } - groupNames = groupNames.filter( - name => name.toLowerCase().includes(query) && name !== Groups.BANNED_USERS // hide banned-users in searches - ); - groupNames = groupNames.slice(0, 100); - - let groupsData; - if (options.showMembers) { - groupsData = await Groups.getGroupsAndMembers(groupNames); - } else { - groupsData = await Groups.getGroupsData(groupNames); - } - groupsData = groupsData.filter(Boolean); - if (options.filterHidden) { - groupsData = groupsData.filter(group => !group.hidden); - } - return Groups.sort(options.sort, groupsData); - }; - - Groups.sort = function (strategy, groups) { - switch (strategy) { - case 'count': - groups.sort((a, b) => a.slug > b.slug) - .sort((a, b) => b.memberCount - a.memberCount); - break; - - case 'date': - groups.sort((a, b) => b.createtime - a.createtime); - break; - - case 'alpha': // intentional fall-through - default: - groups.sort((a, b) => (a.slug > b.slug ? 1 : -1)); - } - - return groups; - }; - - Groups.searchMembers = async function (data) { - if (!data.query) { - const users = await Groups.getOwnersAndMembers(data.groupName, data.uid, 0, 19); - const matchCount = users.length; - const timing = '0.00'; - return { users, matchCount, timing }; - } - - const results = await user.search({ - ...data, - paginate: false, - hardCap: -1, - }); - - const uids = results.users.map(user => user && user.uid); - const isOwners = await Groups.ownership.isOwners(uids, data.groupName); - - results.users.forEach((user, index) => { - if (user) { - user.isOwner = isOwners[index]; - } - }); - - results.users.sort((a, b) => { - if (a.isOwner && !b.isOwner) { - return -1; - } else if (!a.isOwner && b.isOwner) { - return 1; - } - return 0; - }); - return results; - }; +"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()); + }); }; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call +const user_1 = __importDefault(require("../user")); +const database_1 = __importDefault(require("../database")); +const groupsController = module.exports; +function attachSearchFunctions(Groups) { + Groups.search = function (query, options = {}) { + return __awaiter(this, void 0, void 0, function* () { + if (!query) { + return []; + } + query = query.toLowerCase(); + let groupNames = Object.values(yield database_1.default.getObject('groupslug:groupname')); + if (!options.hideEphemeralGroups) { + groupNames = Groups.ephemeralGroups.concat(groupNames); + } + groupNames = groupNames.filter((name) => name.toLowerCase().includes(query) && name !== Groups.BANNED_USERS // hide banned-users in searches + ); + groupNames = groupNames.slice(0, 100); + let groupsData; + if (options.showMembers) { + groupsData = yield Groups.getGroupsAndMembers(groupNames); + } + else { + groupsData = yield Groups.getGroupsData(groupNames); + } + groupsData = groupsData.filter(Boolean); + if (options.filterHidden) { + groupsData = groupsData.filter(group => !group.hidden); + } + return Groups.sort(options.sort, groupsData) || []; // Ensure non-empty array return + }); + }; + Groups.sort = function (strategy, groups) { + switch (strategy) { + case 'count': + groups.sort((a, b) => a.slug.localeCompare(b.slug)) + .sort((a, b) => b.memberCount - a.memberCount); + break; + case 'date': + groups.sort((a, b) => b.createtime - a.createtime); + break; + case 'alpha': // intentional fall-through + default: + groups.sort((a, b) => (a.slug > b.slug ? 1 : -1)); + } + return groups; + }; + Groups.searchMembers = function (data) { + return __awaiter(this, void 0, void 0, function* () { + if (!data.query) { + const users = yield Groups.getOwnersAndMembers(data.groupName, data.uid, 0, 19); + const matchCount = users.length; + const timing = '0.00'; + return { users, matchCount, timing }; + } + const results = yield user_1.default.search(Object.assign(Object.assign({}, data), { paginate: false, hardCap: -1 })); + const uids = results.users.map(user => user === null || user === void 0 ? void 0 : user.uid); + const isOwners = yield Groups.ownership.isOwners(uids, data.groupName); + results.users.forEach((user, index) => { + if (user) { + user.isOwner = isOwners[index]; + } + }); + results.users.sort((a, b) => { + if ((a === null || a === void 0 ? void 0 : a.isOwner) && !(b === null || b === void 0 ? void 0 : b.isOwner)) { + return -1; + } + else if (!(a === null || a === void 0 ? void 0 : a.isOwner) && (b === null || b === void 0 ? void 0 : b.isOwner)) { + return 1; + } + return 0; + }); + return results; + }); + }; +} +exports.default = attachSearchFunctions; From 59ac735b4ac97034545da59a54a3f1961204a81a Mon Sep 17 00:00:00 2001 From: freddcastro Date: Sat, 12 Oct 2024 19:34:25 -0400 Subject: [PATCH 4/4] =?UTF-8?q?Se=20modific=C3=B3=20el=20tsconfig=20y=20de?= =?UTF-8?q?talles=20varios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/groups/search.js | 114 +++++++++++++++++++------------------------ src/groups/search.ts | 102 +++++++++++++++++++++++++++++--------- tsconfig.json | 2 +- 3 files changed, 130 insertions(+), 88 deletions(-) diff --git a/src/groups/search.js b/src/groups/search.js index ed38fea527..c77525e7c1 100644 --- a/src/groups/search.js +++ b/src/groups/search.js @@ -1,48 +1,36 @@ "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()); - }); -}; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; -Object.defineProperty(exports, "__esModule", { value: true }); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call const user_1 = __importDefault(require("../user")); +// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call const database_1 = __importDefault(require("../database")); -const groupsController = module.exports; function attachSearchFunctions(Groups) { - Groups.search = function (query, options = {}) { - return __awaiter(this, void 0, void 0, function* () { - if (!query) { - return []; - } - query = query.toLowerCase(); - let groupNames = Object.values(yield database_1.default.getObject('groupslug:groupname')); - if (!options.hideEphemeralGroups) { - groupNames = Groups.ephemeralGroups.concat(groupNames); - } - groupNames = groupNames.filter((name) => name.toLowerCase().includes(query) && name !== Groups.BANNED_USERS // hide banned-users in searches - ); - groupNames = groupNames.slice(0, 100); - let groupsData; - if (options.showMembers) { - groupsData = yield Groups.getGroupsAndMembers(groupNames); - } - else { - groupsData = yield Groups.getGroupsData(groupNames); - } - groupsData = groupsData.filter(Boolean); - if (options.filterHidden) { - groupsData = groupsData.filter(group => !group.hidden); - } - return Groups.sort(options.sort, groupsData) || []; // Ensure non-empty array return - }); + Groups.search = async function (query, options = {}) { + if (!query) { + return []; + } + query = query.toLowerCase(); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + let groupNames = Object.values(await database_1.default.getObject('groupslug:groupname')); + if (!options.hideEphemeralGroups) { + groupNames = Groups.ephemeralGroups.concat(groupNames); + } + groupNames = groupNames.filter((name) => name.toLowerCase().includes(query) && name !== Groups.BANNED_USERS); + groupNames = groupNames.slice(0, 100); + let groupsData; + if (options.showMembers) { + groupsData = await Groups.getGroupsAndMembers(groupNames); + } + else { + groupsData = await Groups.getGroupsData(groupNames); + } + groupsData = groupsData.filter(Boolean); + if (options.filterHidden) { + groupsData = groupsData.filter(group => !group.hidden); + } + return Groups.sort(options.sort, groupsData) || []; // Ensure non-empty array return }; Groups.sort = function (strategy, groups) { switch (strategy) { @@ -59,33 +47,33 @@ function attachSearchFunctions(Groups) { } return groups; }; - Groups.searchMembers = function (data) { - return __awaiter(this, void 0, void 0, function* () { - if (!data.query) { - const users = yield Groups.getOwnersAndMembers(data.groupName, data.uid, 0, 19); - const matchCount = users.length; - const timing = '0.00'; - return { users, matchCount, timing }; + Groups.searchMembers = async function (data) { + if (!data.query) { + const users = await Groups.getOwnersAndMembers(data.groupName, data.uid, 0, 19); + const matchCount = users.length; + const timing = '0.00'; + return { users, matchCount, timing }; + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const results = await user_1.default.search(Object.assign(Object.assign({}, data), { paginate: false, hardCap: -1 })); + const uids = results.users.map(user => user === null || user === void 0 ? void 0 : user.uid); + const isOwners = await Groups.ownership.isOwners(uids, data.groupName); + results.users.forEach((user, index) => { + if (user) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + user.isOwner = isOwners[index]; + } + }); + results.users.sort((a, b) => { + if ((a === null || a === void 0 ? void 0 : a.isOwner) && !(b === null || b === void 0 ? void 0 : b.isOwner)) { + return -1; + } + else if (!(a === null || a === void 0 ? void 0 : a.isOwner) && (b === null || b === void 0 ? void 0 : b.isOwner)) { + return 1; } - const results = yield user_1.default.search(Object.assign(Object.assign({}, data), { paginate: false, hardCap: -1 })); - const uids = results.users.map(user => user === null || user === void 0 ? void 0 : user.uid); - const isOwners = yield Groups.ownership.isOwners(uids, data.groupName); - results.users.forEach((user, index) => { - if (user) { - user.isOwner = isOwners[index]; - } - }); - results.users.sort((a, b) => { - if ((a === null || a === void 0 ? void 0 : a.isOwner) && !(b === null || b === void 0 ? void 0 : b.isOwner)) { - return -1; - } - else if (!(a === null || a === void 0 ? void 0 : a.isOwner) && (b === null || b === void 0 ? void 0 : b.isOwner)) { - return 1; - } - return 0; - }); - return results; + return 0; }); + return results; }; } -exports.default = attachSearchFunctions; +module.exports = attachSearchFunctions; diff --git a/src/groups/search.ts b/src/groups/search.ts index d9aac12789..25af4327b3 100644 --- a/src/groups/search.ts +++ b/src/groups/search.ts @@ -1,40 +1,80 @@ -'use strict'; +// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call import user from '../user'; +// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call import db from '../database'; -module.exports = function (Groups) { - Groups.search = async function (query, options) { + +interface Groups { + slug: string; + memberCount: number; + hidden: boolean; + createtime: number; + search: (query: string, options: SearchOptions) => Promise; + ephemeralGroups: string[]; + BANNED_USERS: string; + getGroupsAndMembers: (groupNames: string[]) => Promise; + getGroupsData: (groupNames: string[]) => Promise; + sort: (strategy: string, groups: Groups[]) => Groups[]; + searchMembers: (data: { query: string; groupName: string; uid?: number }) => Promise; + getOwnersAndMembers: (groupName: string, uid?: number, start?: number, stop?: number) => Promise; + ownership: { + isOwners: (uids: number[], groupName: string) => Promise; + }; +} + +interface SearchOptions { + hideEphemeralGroups?: boolean; + showMembers?: boolean; + filterHidden?: boolean; + sort?: string; +} + +interface SearchResults { + users: User[]; + matchCount: number; + timing: string; +} + +interface User { + uid: number; + isOwner?: boolean; +} + + +function attachSearchFunctions(Groups: Groups) { + Groups.search = async function (query: string, options: SearchOptions = {}): Promise { if (!query) { return []; } - query = String(query).toLowerCase(); - let groupNames = Object.values(await db.getObject('groupslug:groupname')); + query = query.toLowerCase(); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + let groupNames = Object.values(await db.getObject('groupslug:groupname') as Groups); if (!options.hideEphemeralGroups) { - groupNames = Groups.ephemeralGroups.concat(groupNames); + groupNames = Groups.ephemeralGroups.concat(groupNames as string[]); } groupNames = groupNames.filter( - name => name.toLowerCase().includes(query) && name !== Groups.BANNED_USERS // hide banned-users in searches + (name: string) => name.toLowerCase().includes(query) && name !== Groups.BANNED_USERS ); groupNames = groupNames.slice(0, 100); - let groupsData; + let groupsData: Groups[] | undefined; if (options.showMembers) { - groupsData = await Groups.getGroupsAndMembers(groupNames); + groupsData = await Groups.getGroupsAndMembers(groupNames as string[]); } else { - groupsData = await Groups.getGroupsData(groupNames); + groupsData = await Groups.getGroupsData(groupNames as string[]); } groupsData = groupsData.filter(Boolean); if (options.filterHidden) { groupsData = groupsData.filter(group => !group.hidden); } - return Groups.sort(options.sort, groupsData); + return Groups.sort(options.sort, groupsData) || []; // Ensure non-empty array return }; - Groups.sort = function (strategy, groups) { + Groups.sort = function (strategy: string, groups: Groups[]): Groups[] { switch (strategy) { case 'count': - groups.sort((a, b) => a.slug > b.slug) + groups.sort((a, b) => a.slug.localeCompare(b.slug)) .sort((a, b) => b.memberCount - a.memberCount); break; @@ -49,38 +89,52 @@ module.exports = function (Groups) { return groups; }; - - Groups.searchMembers = async function (data) { + Groups.searchMembers = async function (data: { + query: string; + groupName: string; + uid?: number; + }): Promise { if (!data.query) { - const users = await Groups.getOwnersAndMembers(data.groupName, data.uid, 0, 19); + const users = await Groups.getOwnersAndMembers( + data.groupName, + data.uid, + 0, + 19 + ); const matchCount = users.length; const timing = '0.00'; return { users, matchCount, timing }; } - - const results = await user.search({ + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const results: SearchResults = await user.search({ ...data, paginate: false, hardCap: -1, - }); + }) as SearchResults; - const uids = results.users.map(user => user && user.uid); - const isOwners = await Groups.ownership.isOwners(uids, data.groupName); + const uids = results.users.map(user => user?.uid); + const isOwners = await Groups.ownership.isOwners( + uids, + data.groupName + ); results.users.forEach((user, index) => { if (user) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call user.isOwner = isOwners[index]; } }); results.users.sort((a, b) => { - if (a.isOwner && !b.isOwner) { + if (a?.isOwner && !b?.isOwner) { return -1; - } else if (!a.isOwner && b.isOwner) { + } else if (!a?.isOwner && b?.isOwner) { return 1; } return 0; }); return results; }; -}; +} + +export = attachSearchFunctions diff --git a/tsconfig.json b/tsconfig.json index 10aeeef7e6..c15cb37711 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "allowJs": false, - "target": "es6", + "target": "es2017", "module": "commonjs", "moduleResolution": "node", "esModuleInterop": true,