Created
March 2, 2016 18:52
-
-
Save psychobunny/48d656eb95d5c5ed5177 to your computer and use it in GitHub Desktop.
Script: Deleting topics/posts from a JSON file containing pids and tids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/*globals require, console, process */ | |
var nconf = require('nconf'); | |
var async = require('async'); | |
nconf.file({ | |
file: 'config.json' | |
}); | |
var db = require('./src/database'); | |
var topics = require('./src/topics'); | |
var posts = require('./src/posts'); | |
var json = require('./tornbanner.json'); | |
var tids = json.deletedTopics; | |
var pids = json.deletedPosts; | |
db.init(function(err) { | |
if (err) { | |
console.log("NodeBB could not connect to your Mongo database. Mongo returned the following error: " + err.message); | |
process.exit(); | |
} | |
async.series([ | |
function(next) { | |
exportPosts(next); | |
}, | |
function(next) { | |
exportTopics(next); | |
} | |
], function(err) { | |
if (err) { | |
console.log(err); | |
process.exit(); | |
return; | |
} | |
console.log('done '); | |
}); | |
}); | |
function exportTopics(callback) { | |
console.log('# of tids: ' + tids.length); | |
async.eachSeries(tids, function (_tid, next) { | |
db.getObject('_imported_topic:' + _tid, function(err, topic) { | |
if (err || !topic || !topic.tid) { | |
console.log('skipped topic: ' + topic.tid); | |
return next(); | |
} | |
topics.delete(topic.tid, function(err) { | |
if (err) { | |
console.log('topic delete fail: ' + topic.tid); | |
} | |
next(); | |
}); | |
}); | |
}, callback); | |
} | |
function exportPosts(callback) { | |
console.log('# of pids: ' + pids.length); | |
async.eachSeries(pids, function (_pid, next) { | |
db.getObject('_imported_post:' + _pid, function(err, post) { | |
if (err || !post || !post.tid) { | |
console.log('skipped post: ' + post.pid); | |
return next(); | |
} | |
posts.delete(post.pid, 1, function(err) { | |
if (err) { | |
console.log('post delete fail: ' + post.pid); | |
} | |
next(); | |
}); | |
}); | |
}, callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment