From 44f5ab83c3db408dbe65683bc4c1b8591ea6f6b8 Mon Sep 17 00:00:00 2001 From: Skultrix Date: Wed, 30 Aug 2023 20:54:29 +0300 Subject: [PATCH 1/2] Translated dismiss flags from deleted topics file to typescript --- src/types/post.ts | 1 + .../dismiss_flags_from_deleted_topics.js | 144 +++++++++++------- 2 files changed, 94 insertions(+), 51 deletions(-) diff --git a/src/types/post.ts b/src/types/post.ts index 982b7364e..8976eabcd 100644 --- a/src/types/post.ts +++ b/src/types/post.ts @@ -18,4 +18,5 @@ export type PostObject = { category: CategoryObject; isMainPost: boolean; replies: number; + flags: number; }; diff --git a/src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.js b/src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.js index d86095965..1325e756a 100644 --- a/src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.js +++ b/src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.js @@ -1,56 +1,98 @@ -'use strict'; - - -const winston = require('winston'); -const db = require('../../database'); - -module.exports = { - name: 'Dismiss flags from deleted topics', - timestamp: Date.UTC(2016, 3, 29), - method: async function () { - const posts = require('../../posts'); - const topics = require('../../topics'); - - const pids = await db.getSortedSetRange('posts:flagged', 0, -1); - const postData = await posts.getPostsFields(pids, ['tid']); - const tids = postData.map(t => t.tid); - const topicData = await topics.getTopicsFields(tids, ['deleted']); - const toDismiss = topicData.map((t, idx) => (parseInt(t.deleted, 10) === 1 ? pids[idx] : null)).filter(Boolean); - - winston.verbose(`[2016/04/29] ${toDismiss.length} dismissable flags found`); - await Promise.all(toDismiss.map(dismissFlag)); - }, +"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 }; }; - +const database_1 = __importDefault(require("../../database")); +const posts_1 = __importDefault(require("../../posts")); +const topics_1 = __importDefault(require("../../topics")); // copied from core since this function was removed // https://github.com/NodeBB/NodeBB/blob/v1.x.x/src/posts/flags.js -async function dismissFlag(pid) { - const postData = await db.getObjectFields(`post:${pid}`, ['pid', 'uid', 'flags']); - if (!postData.pid) { - return; - } - if (parseInt(postData.uid, 10) && parseInt(postData.flags, 10) > 0) { - await Promise.all([ - db.sortedSetIncrBy('users:flags', -postData.flags, postData.uid), - db.incrObjectFieldBy(`user:${postData.uid}`, 'flags', -postData.flags), +function dismissFlag(pid) { + return __awaiter(this, void 0, void 0, function* () { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const postData = yield database_1.default.getObjectFields(`post:${pid}`, ['pid', 'uid', 'flags']); + if (!postData.pid) { + return; + } + if (postData.uid && postData.flags > 0) { + yield Promise.all([ + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + database_1.default.sortedSetIncrBy('users:flags', -postData.flags, postData.uid), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + database_1.default.incrObjectFieldBy(`user:${postData.uid}`, 'flags', -postData.flags), + ]); + } + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const uids = yield database_1.default.getSortedSetRange(`pid:${pid}:flag:uids`, 0, -1); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const nids = uids.map(uid => `post_flag:${pid}:uid:${uid}`); + yield Promise.all([ + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + database_1.default.deleteAll(nids.map(nid => `notifications:${nid}`)), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + database_1.default.sortedSetRemove('notifications', nids), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + database_1.default.delete(`pid:${pid}:flag:uids`), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + database_1.default.sortedSetsRemove([ + 'posts:flagged', + 'posts:flags:count', + `uid:${postData.uid}:flag:pids`, + ], pid), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + database_1.default.deleteObjectField(`post:${pid}`, 'flags'), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + database_1.default.delete(`pid:${pid}:flag:uid:reason`), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + database_1.default.deleteObjectFields(`post:${pid}`, ['flag:state', 'flag:assignee', 'flag:notes', 'flag:history']), ]); - } - const uids = await db.getSortedSetRange(`pid:${pid}:flag:uids`, 0, -1); - const nids = uids.map(uid => `post_flag:${pid}:uid:${uid}`); - - await Promise.all([ - db.deleteAll(nids.map(nid => `notifications:${nid}`)), - db.sortedSetRemove('notifications', nids), - db.delete(`pid:${pid}:flag:uids`), - db.sortedSetsRemove([ - 'posts:flagged', - 'posts:flags:count', - `uid:${postData.uid}:flag:pids`, - ], pid), - db.deleteObjectField(`post:${pid}`, 'flags'), - db.delete(`pid:${pid}:flag:uid:reason`), - db.deleteObjectFields(`post:${pid}`, ['flag:state', 'flag:assignee', 'flag:notes', 'flag:history']), - ]); - - await db.sortedSetsRemoveRangeByScore(['users:flags'], '-inf', 0); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + yield database_1.default.sortedSetsRemoveRangeByScore(['users:flags'], '-inf', 0); + }); } +module.exports = { + name: 'Dismiss flags from deleted topics', + timestamp: Date.UTC(2016, 3, 29), + method: function () { + return __awaiter(this, void 0, void 0, function* () { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const pids = yield database_1.default.getSortedSetRange('posts:flagged', 0, -1); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const postData = yield posts_1.default.getPostsFields(pids, ['tid']); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const tids = postData.map(t => t.tid); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const topicData = yield topics_1.default.getTopicsFields(tids, ['deleted']); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const toDismiss = topicData.map((t, idx) => (parseInt(t.deleted, 10) === 1 ? pids[idx] : null)).filter(Boolean); + yield Promise.all(toDismiss.map(dismissFlag)); + }); + }, +}; From a960d7ee4530d13a5bca668213180643a6a21304 Mon Sep 17 00:00:00 2001 From: Skultrix Date: Wed, 30 Aug 2023 20:58:43 +0300 Subject: [PATCH 2/2] Translated from JS to TS --- .../dismiss_flags_from_deleted_topics.ts | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.ts diff --git a/src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.ts b/src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.ts new file mode 100644 index 000000000..41aa29e71 --- /dev/null +++ b/src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.ts @@ -0,0 +1,91 @@ +import winston from 'winston'; +import db from '../../database'; + +import posts from '../../posts'; +import topics from '../../topics'; + +import { PostObject, TopicObject } from '../../types'; +// copied from core since this function was removed +// https://github.com/NodeBB/NodeBB/blob/v1.x.x/src/posts/flags.js +async function dismissFlag(pid: number) { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const postData: PostObject = await db.getObjectFields(`post:${pid}`, ['pid', 'uid', 'flags']) as PostObject; + if (!postData.pid) { + return; + } + if (postData.uid && postData.flags > 0) { + await Promise.all([ + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + db.sortedSetIncrBy('users:flags', -postData.flags, postData.uid), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + db.incrObjectFieldBy(`user:${postData.uid}`, 'flags', -postData.flags), + ]); + } + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const uids: number[] = await db.getSortedSetRange(`pid:${pid}:flag:uids`, 0, -1) as number[]; + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const nids = uids.map(uid => `post_flag:${pid}:uid:${uid}`); + + await Promise.all([ + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + db.deleteAll(nids.map(nid => `notifications:${nid}`)), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + db.sortedSetRemove('notifications', nids), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + db.delete(`pid:${pid}:flag:uids`), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + db.sortedSetsRemove([ + 'posts:flagged', + 'posts:flags:count', + `uid:${postData.uid}:flag:pids`, + ], pid), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + db.deleteObjectField(`post:${pid}`, 'flags'), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + db.delete(`pid:${pid}:flag:uid:reason`), + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + db.deleteObjectFields(`post:${pid}`, ['flag:state', 'flag:assignee', 'flag:notes', 'flag:history']), + ]); + + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + await db.sortedSetsRemoveRangeByScore(['users:flags'], '-inf', 0); +} + + +export = { + name: 'Dismiss flags from deleted topics', + timestamp: Date.UTC(2016, 3, 29), + method: async function () { + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const pids: number [] = await db.getSortedSetRange('posts:flagged', 0, -1) as number []; + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const postData: PostObject [] = await posts.getPostsFields(pids, ['tid']) as PostObject[]; + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const tids = postData.map(t => t.tid); + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const topicData: TopicObject[] = await topics.getTopicsFields(tids, ['deleted']) as TopicObject[]; + // The next line calls a function in a module that has not been updated to TS yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + const toDismiss = topicData.map((t, idx) => (parseInt(t.deleted, 10) === 1 ? pids[idx] : null)).filter(Boolean); + + winston.verbose(`[2016/04/29] ${toDismiss.length} dismissable flags found`); + await Promise.all(toDismiss.map(dismissFlag)); + }, +};