Created
July 6, 2015 16:01
-
-
Save fidelix/5b860d9be476d4fc18df to your computer and use it in GitHub Desktop.
IPB Post Process NodeBB
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"; | |
var async = require('async'); | |
var Data = require('nodebb-plugin-import/server/data.js'); | |
var Posts = require('./src/posts.js'); | |
var Users = require('./src/user.js'); | |
var cheerio = require('cheerio'); | |
function postProcess(post, next){ | |
post.modified = false; | |
var $ = cheerio.load(post._imported_content, {decodeEntities: false}); | |
var prependSlugFromUid = function (uid, bq, callback){ | |
Users.getUserField(uid, 'userslug', function (err, userslug) { | |
if (err) console.log(err); | |
if (userslug) { | |
bq.before('[[modules:composer.user_said, ' + userslug + ']]\n'); | |
post.modified = true; | |
} else { | |
console.log('Userslug not found'); | |
} | |
callback(); | |
}); | |
}; | |
var findUidFromCid = function(cid, bq, callback){ | |
// This is a reply to another post or topic. If the user was not found via the username, let's acquire him | |
// via the OP. Let's first check if the reply is a post. | |
Data.getImportedPost(cid, function (err, reply) { | |
if (err) console.log(err); | |
if (reply) { | |
prependSlugFromUid(reply.uid, bq, callback); | |
} else { | |
// Apparently the reply was not a post. Let's check if it is a topic. | |
Data.getImportedTopic(cid, function(err, reply_topic){ | |
if (err) console.log(err); | |
if (reply_topic) { | |
prependSlugFromUid(reply_topic.uid, bq, callback); | |
} else { | |
// Topic ID reference for IPB doesn't currently work on the importer. Let's try this another way by | |
// checking if the reply id is the same as the _pid on the topic. | |
Data.getImportedTopic(post._imported_tid, function(err, topic) { | |
if (err) console.log(err); | |
if (topic && topic._pid == cid) { | |
prependSlugFromUid(topic.uid, bq, callback); | |
} | |
else { | |
// It's possible that the cid (comment reply ID) came wrong from IPB, so the reference is invalid. | |
// This happens a lot when forums go through more than one migration. | |
console.log('Imported reply not found: ' + cid); | |
callback(); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
}; | |
var mention_ex = /\[member=(?:"|')(.{3,24}?)(?:"|')\]/gi; | |
/*if(mention_ex.test($.html())){ | |
var zuba = 'ahha'; | |
}*/ | |
var match; | |
var matches = []; | |
while (match = mention_ex.exec($.html())) { | |
matches.push(match); | |
} | |
async.each(matches, function(match, next){ | |
var _mention = match[0]; | |
var _username = match[1]; | |
Users.getUidByUsername(_username, function(err, uid){ | |
if(err) console.error(err); | |
if(uid) { | |
Users.getUserField(uid, 'userslug', function (err, userslug) { | |
if (err) console.log(err); | |
if (userslug) { | |
var first_pass = $.html().split('@' + _mention).join('@' + userslug); | |
var second_pass = first_pass.split(_mention).join('@' + userslug); | |
$ = cheerio.load(second_pass, {decodeEntities: false}); | |
//$.html(content.replace(replace, '@' + userslug)); | |
post.modified = true; | |
} else { | |
console.log('Userslug not found for mention'); | |
} | |
next(); | |
}); | |
} else { | |
console.log('Userslug not found for mention'); | |
next(); | |
} | |
}); | |
}); | |
var blockquotes = []; | |
$("blockquote").each(function(i, bq){ | |
blockquotes.push($(this)); | |
}); | |
if (!blockquotes.length) { | |
return next(); | |
} | |
async.each(blockquotes, function (blockquote, nextBq) { | |
var _op_username = blockquote.attr('data-author'); | |
var _op_cid = blockquote.attr('data-cid'); | |
// Try to find the UID from the data-author. | |
if (_op_username) { | |
Users.getUidByUsername(_op_username, function(err, uid){ | |
if(err) console.log(err); | |
else if(uid) { | |
prependSlugFromUid(uid, blockquote, nextBq); | |
} else if (_op_cid) { | |
// Can't find NodeBB user. Let's try to use the OP as the reference. | |
findUidFromCid(_op_cid, blockquote, nextBq); | |
} else { | |
console.log('No CID available and UID not found from Username.'); | |
nextBq(); | |
} | |
}); | |
} else if (_op_cid){ | |
findUidFromCid(_op_cid, blockquote, nextBq); | |
} else { | |
// This is an ordinary blockquote. Just skip. | |
nextBq(); | |
} | |
}, function (err) { | |
if (err) { | |
console.error(err.message); | |
return next(); | |
} | |
if (post.modified) { | |
post.new_content = $.html(); | |
// saveModifiedPost(post); | |
next(); | |
} else { | |
next(); | |
} | |
}); | |
var saveModifiedPost = function(modPost){ | |
Posts.setPostFields(modPost.pid, {content: modPost.content}, function (err) { | |
if (err) return next(err); | |
next(); // post saved! | |
}); | |
} | |
} | |
Data.eachPost(postProcess, | |
{ | |
async: true, // make sure each set of iterations waits for the next batch | |
eachLimit: 4 // limit 10 at the same time or whatever | |
}, | |
function (err) { | |
// when ALL of them are done or an error occurs | |
if (err) { | |
throw err; | |
} | |
console.log("DONE!!"); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment