diff --git a/src/posts/data.js b/src/posts/data.js index 3a4d303ff5..ce71c6b4ce 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -1,71 +1,84 @@ -'use strict'; - -const db = require('../database'); -const plugins = require('../plugins'); -const utils = require('../utils'); - +"use strict"; +const database_1 = require("../database"); +const plugins_1 = require("../plugins"); +const utils_1 = require("../utils"); const intFields = [ - 'uid', 'pid', 'tid', 'deleted', 'timestamp', - 'upvotes', 'downvotes', 'deleterUid', 'edited', - 'replies', 'bookmarks', + 'uid', 'pid', 'tid', 'deleted', 'timestamp', + 'upvotes', 'downvotes', 'deleterUid', 'edited', + 'replies', 'bookmarks', ]; - -module.exports = function (Posts) { - Posts.getPostsFields = async function (pids, fields) { - if (!Array.isArray(pids) || !pids.length) { - return []; - } - const keys = pids.map(pid => `post:${pid}`); - const postData = await db.getObjects(keys, fields); - const result = await plugins.hooks.fire('filter:post.getFields', { - pids: pids, - posts: postData, - fields: fields, - }); - result.posts.forEach(post => modifyPost(post, fields)); - return result.posts; - }; - - Posts.getPostData = async function (pid) { - const posts = await Posts.getPostsFields([pid], []); - return posts && posts.length ? posts[0] : null; - }; - - Posts.getPostsData = async function (pids) { - return await Posts.getPostsFields(pids, []); - }; - - Posts.getPostField = async function (pid, field) { - const post = await Posts.getPostFields(pid, [field]); - return post ? post[field] : null; - }; - - Posts.getPostFields = async function (pid, fields) { - const posts = await Posts.getPostsFields([pid], fields); - return posts ? posts[0] : null; - }; - - Posts.setPostField = async function (pid, field, value) { - await Posts.setPostFields(pid, { [field]: value }); - }; - - Posts.setPostFields = async function (pid, data) { - await db.setObject(`post:${pid}`, data); - plugins.hooks.fire('action:post.setFields', { data: { ...data, pid } }); - }; -}; - function modifyPost(post, fields) { - if (post) { - db.parseIntFields(post, intFields, fields); - if (post.hasOwnProperty('upvotes') && post.hasOwnProperty('downvotes')) { - post.votes = post.upvotes - post.downvotes; - } - if (post.hasOwnProperty('timestamp')) { - post.timestampISO = utils.toISOString(post.timestamp); - } - if (post.hasOwnProperty('edited')) { - post.editedISO = post.edited !== 0 ? utils.toISOString(post.edited) : ''; - } - } + if (post) { + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + (0, database_1.parseIntFields)(post, intFields, fields); + if (post.hasOwnProperty('upvotes') && post.hasOwnProperty('downvotes')) { + post.votes = post.upvotes - post.downvotes; + } + if (post.hasOwnProperty('timestamp')) { + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + post.timestampISO = (0, utils_1.toISOString)(post.timestamp); + } + if (post.hasOwnProperty('edited')) { + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line max-len + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + post.editedISO = post.edited !== 0 ? (0, utils_1.toISOString)(post.edited) : ''; + } + } } +function toExport(Posts) { + Posts.getPostsFields = async function (pids, fields) { + if (!Array.isArray(pids) || !pids.length) { + return []; + } + const keys = pids.map(pid => `post:${pid}`); + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + const postData = await (0, database_1.getObjects)(keys, fields); + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // 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 result = await plugins_1.hooks.fire('filter:post.getFields', { + pids: pids, + posts: postData, + fields: fields, + }); + result.posts.forEach((post) => modifyPost(post, fields)); + return result.posts; + }; + Posts.getPostData = async function (pid) { + const posts = await Posts.getPostsFields([pid], []); + return posts && posts.length ? posts[0] : null; + }; + Posts.getPostsData = async function (pids) { + return await Posts.getPostsFields(pids, []); + }; + Posts.getPostFields = async function (pid, fields) { + const posts = await Posts.getPostsFields([pid], fields); + if (posts && posts.length) { + return posts[0]; + } + return null; + }; + Posts.getPostField = async function (pid, field) { + const post = await Posts.getPostFields(pid, [field]); + if (post) { + return post[field]; + } + return null; + }; + Posts.setPostField = async function (pid, field, value) { + await Posts.setPostFields(pid, { [field]: value }); + }; + Posts.setPostFields = async function (pid, data) { + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + await (0, database_1.setObject)(`post:${pid}`, data); + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await plugins_1.hooks.fire('action:post.setFields', { data: Object.assign(Object.assign({}, data), { pid }) }); + }; +} +module.exports = toExport; diff --git a/src/posts/data.ts b/src/posts/data.ts new file mode 100644 index 0000000000..90e37f2ba6 --- /dev/null +++ b/src/posts/data.ts @@ -0,0 +1,115 @@ +import { getObjects, setObject, parseIntFields } from '../database'; +import { hooks } from '../plugins'; +import { toISOString } from '../utils'; + +const intFields: string[] = [ + 'uid', 'pid', 'tid', 'deleted', 'timestamp', + 'upvotes', 'downvotes', 'deleterUid', 'edited', + 'replies', 'bookmarks', +]; + +interface Post { + votes: number; + upvotes: number; + downvotes: number; + timestamp: number; + timestampISO: string; + edited: number; + editedISO: string; +} + +interface Posts { + getPostsFields: (pids:string[], fields: string[]) => Promise + getPostData: (pid:string) => Promise + getPostsData: (pids:string[]) => Promise + getPostFields: (pid:string, fields:string[]) => Promise + getPostField: (pid:string, field:string) => Promise + setPostField: (pid:string, field:string, value:string) => Promise + setPostFields: (pid:string, data:object) => Promise +} + + +function modifyPost(post:Post, fields:string[]): void { + if (post) { + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + parseIntFields(post, intFields, fields); + if (post.hasOwnProperty('upvotes') && post.hasOwnProperty('downvotes')) { + post.votes = post.upvotes - post.downvotes; + } + if (post.hasOwnProperty('timestamp')) { + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + post.timestampISO = toISOString(post.timestamp); + } + if (post.hasOwnProperty('edited')) { + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + post.editedISO = post.edited !== 0 ? toISOString(post.edited) : ''; + } + } +} + +function toExport(Posts:Posts):void { + Posts.getPostsFields = async function (pids:string[], fields: string[]): Promise { + if (!Array.isArray(pids) || !pids.length) { + return []; + } + const keys = pids.map(pid => `post:${pid}`); + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + const postData: Post[] = await getObjects(keys, fields); + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // 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 result: { pids: string[], posts: Post[], fields: string[] } = await hooks.fire('filter:post.getFields', { + pids: pids, + posts: postData, + fields: fields, + }); + result.posts.forEach((post: Post) => modifyPost(post, fields)); + return result.posts; + }; + + Posts.getPostData = async function (pid:string): Promise { + const posts:object[] = await Posts.getPostsFields([pid], []); + return posts && posts.length ? posts[0] : null; + }; + + Posts.getPostsData = async function (pids:string[]):Promise { + return await Posts.getPostsFields(pids, []); + }; + + Posts.getPostFields = async function (pid:string, fields:string[]): Promise { + const posts: object[] = await Posts.getPostsFields([pid], fields); + if (posts && posts.length) { + return posts[0]; + } + return null; + }; + + Posts.getPostField = async function (pid:string, field:string): Promise { + const post: object = await Posts.getPostFields(pid, [field]); + if (post) { + return post[field] as object; + } + return null; + }; + + Posts.setPostField = async function (pid:string, field:string, value:string):Promise { + await Posts.setPostFields(pid, { [field]: value }); + }; + + Posts.setPostFields = async function (pid:string, data:object): Promise { + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + await setObject(`post:${pid}`, data); + // La siguiente línea llama a una función en un módulo que aún no ha sido actualizado a TS + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await hooks.fire('action:post.setFields', { data: { ...data, pid } }); + }; +} + +export = toExport + + 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,