Remove exclude of linting tools folder, lint restore posts tool

master
Thomas Lynch 8 months ago
parent a91c645e02
commit 5518a9849a
  1. 2
      .eslintrc.json
  2. 128
      tools/restore_posts.js

@ -1,6 +1,6 @@
{
"ignorePatterns": [
"docker/", "tmp/", "static/", "gulp/res/css/", "gulp/res/icons/", "gulp/res/img/", "tools/", "views/", "node_modules/",
"docker/", "tmp/", "static/", "gulp/res/css/", "gulp/res/icons/", "gulp/res/img/", "views/", "node_modules/",
"gulp/res/js/pugfilters.js",
"gulp/res/js/locals.js",
"gulp/res/js/post.js",

@ -4,70 +4,67 @@
* Restore a post or a full thread from a mongodump backup.
*/
(async () => {
const Mongo = require(__dirname+'/../db/db.js')
, { execSync } = require('child_process')
, { EJSON, ObjectId } = require('bson')
, config = require(__dirname+'/../lib/misc/config.js');
const Mongo = require(__dirname+'/../db/db.js')
, { execSync } = require('child_process')
, { EJSON } = require('bson')
, config = require(__dirname+'/../lib/misc/config.js');
console.log('CONNECTING TO MONGODB');
await Mongo.connect();
await Mongo.checkVersion();
await config.load();
const db = Mongo.db.collection('posts');
console.log('CONNECTING TO MONGODB');
await Mongo.connect();
await Mongo.checkVersion();
await config.load();
const db = Mongo.db.collection('posts');
if (process.argv.length !== 4) {
console.error('Usage: node restore_posts.js PATH_TO_posts.bson OBJECT_ID_OF_POST');
process.exit(2);
}
const path = process.argv[2];
const post_object_id = process.argv[3];
// for some reason, the node BSON package can't handle some mongodump BSON files like posts.bson
// so we'll run bsondump to convert to JSON, then use EJSON from the BSON package to parse the special type information
const postsdata = execSync(`bsondump --quiet --type json ${path}`, (error, stdout, stderr) => {
if (error) {
console.error(`error: ${error.message}`);
process.exit(1);
}
if (stderr) {
console.error(`stderr: ${stderr}`);
process.exit(1);
}
return stdout;
});
// and, because it's a debug tool, bsondump outputs each document as a different json on a new line, instead of printing a valid json file
const posts_json = EJSON.parse('['+postsdata.toString().replace(/}(\r\n|\n|\r){/gm, "},{")+']');
// iterate over all the posts and find the one with the correct ID
let i = 0;
while (i < posts_json.length && posts_json[i]["_id"] != post_object_id) {i += 1}
if (i == posts_json.length) {console.log('Object ID not found in posts'); process.exit(2);}
const data = posts_json[i];
let res = await insertPost(db, data);
console.log(res);
// if the post doesn't have a parent thread, it is a thread. we need to insert the child posts too
// since
if (data.thread == null) {
let replies = [];
for (const e of posts_json) {
if (e['thread'] == data['postId']){
// since posts are already indexed by postID, which contains a timestamp to the nearest second, there's no risk for a post trying to add a backlink before the target exists
res = await insertPost(db, e);
console.log(res);
if (process.argv.length !== 4) {
console.error('Usage: node restore_posts.js PATH_TO_posts.bson OBJECT_ID_OF_POST');
process.exit(2);
}
const path = process.argv[2];
const post_object_id = process.argv[3];
// for some reason, the node BSON package can't handle some mongodump BSON files like posts.bson
// so we'll run bsondump to convert to JSON, then use EJSON from the BSON package to parse the special type information
const postsdata = execSync(`bsondump --quiet --type json ${path}`, (error, stdout, stderr) => {
if (error) {
console.error(`error: ${error.message}`);
process.exit(1);
}
if (stderr) {
console.error(`stderr: ${stderr}`);
process.exit(1);
}
return stdout;
});
// and, because it's a debug tool, bsondump outputs each document as a different json on a new line, instead of printing a valid json file
const posts_json = EJSON.parse('['+postsdata.toString().replace(/}(\r\n|\n|\r){/gm, '},{')+']');
// iterate over all the posts and find the one with the correct ID
let i = 0;
while (i < posts_json.length && posts_json[i]['_id'] != post_object_id) { i += 1; }
if (i == posts_json.length) { console.log('Object ID not found in posts'); process.exit(2); }
const data = posts_json[i];
let res = await insertPost(db, data);
console.log(res);
// if the post doesn't have a parent thread, it is a thread. we need to insert the child posts too
if (data.thread == null) {
for (const e of posts_json) {
if (e['thread'] == data['postId']) {
// since posts are already indexed by postID, which contains a timestamp to the nearest second, there's no risk for a post trying to add a backlink before the target exists
res = await insertPost(db, e);
console.log(res);
}
}
}
}
console.log("Make sure to run `gulp html` and restore the attached files");
process.exit();
console.log('Make sure to run `gulp html` and restore the attached files');
process.exit();
})();
@ -75,7 +72,6 @@ async function insertPost(db, data) {
const postMongoId = await db.insertOne(data).then(result => result.insertedId); //_id of post
const postId = data['postId'];
const board = data['board'];
//add backlinks to the posts this post quotes
if (data.thread && data.quotes.length > 0) {
@ -110,15 +106,15 @@ async function insertPost(db, data) {
}
},
[{
'$set': {
message: {
$replaceOne: { input: "$message", find: `<span class="invalid-quote">&gt;&gt;${postId}</span>`, replacement: `<a class="quote" href="/${board}/thread/${threadId}.html#${postId}">&gt;&gt;${postId}</a>` }
}
[{
'$set': {
message: {
$replaceOne: { input: '$message', find: `<span class="invalid-quote">&gt;&gt;${postId}</span>`, replacement: `<a class="quote" href="/${board}/thread/${threadId}.html#${postId}">&gt;&gt;${postId}</a>` }
}
}]
}
}]
);
}
return ({ postId, postMongoId });
}
}

Loading…
Cancel
Save