Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 80 additions & 67 deletions src/posts/data.js
Original file line number Diff line number Diff line change
@@ -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;
115 changes: 115 additions & 0 deletions src/posts/data.ts
Original file line number Diff line number Diff line change
@@ -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<object[]>
getPostData: (pid:string) => Promise<object>
getPostsData: (pids:string[]) => Promise<object[]>
getPostFields: (pid:string, fields:string[]) => Promise<object | null>
getPostField: (pid:string, field:string) => Promise<object | null>
setPostField: (pid:string, field:string, value:string) => Promise<void>
setPostFields: (pid:string, data:object) => Promise<void>
}


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<object[]> {
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<object | null> {
const posts:object[] = await Posts.getPostsFields([pid], []);
return posts && posts.length ? posts[0] : null;
};

Posts.getPostsData = async function (pids:string[]):Promise<object[]> {
return await Posts.getPostsFields(pids, []);
};

Posts.getPostFields = async function (pid:string, fields:string[]): Promise<object | null> {
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<object | null> {
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<void> {
await Posts.setPostFields(pid, { [field]: value });
};

Posts.setPostFields = async function (pid:string, data:object): Promise<void> {
// 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


2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"allowJs": false,
"target": "es6",
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
Expand Down