Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation of src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.js to TypeScript #96

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions src/types/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export type PostObject = {
category: CategoryObject;
isMainPost: boolean;
replies: number;
flags: number;
};
144 changes: 93 additions & 51 deletions src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.js
Original file line number Diff line number Diff line change
@@ -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));
});
},
};
91 changes: 91 additions & 0 deletions src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.ts
Original file line number Diff line number Diff line change
@@ -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));
},
};