<commit_before>var init = require('../main/init');
var mongo = require('../main/mongo');
var es = require('../main/es');
var upload = require('../main/upload');
var rcs = require('../main/rcs');

init.add(function () {

	console.log('post:');

	exports.form = function (req) {
		var body = req.body;
		var form = {};
		form.now = new Date();
		form.categoryId = parseInt(body.categoryId) || 0;
		form.writer  = String(body.writer || '').trim();
		form.title = String(body.title || '').trim();
		form.text = String(body.text || '');
		form.visible = !!body.visible;
		form.delFiles = body.delFiles;
		form.tmpFiles = body.tmpFiles;
		return form;
	};

	exports.createThread = function (role, form, next) {
		categoryForUpdate(role, form.categoryId, function (err, category) {
			if (err) return next(err);
			checkForm(form, true, function (err) {
				if (err) return next(err);
				var threadId = mongo.getNewThreadId();
				insertThread(threadId, form, function (err, thread) {
					if (err) return next(err);
					var postId = mongo.getNewPostId();
					savePostFiles(postId, form, function (err) {
						if (err) return next(err);
						insertPost(postId, form, thread, category, function (err) {
							if (err) return next(err);
							next(null, threadId, postId);
						});
					});
				});
			});
		});
	}

	exports.createReply = function (role, form, next) {
		var threadId = form.threadId;
		findThread(threadId, function (err, thread) {
			if (err) return next(err);
			categoryForUpdate(role, thread.categoryId, function (err, category) {
				if (err) return next(err);
				checkForm(form, false, function (err) {
					if (err) return next(err);
					var postId = mongo.getNewPostId();
					savePostFiles(postId, form, function (err) {
						if (err) return next(err);
						insertPost(postId, form, thread, category, function (err) {
							if (err) return next(err);
							mongo.updateThreadLength(threadId, form.now, function (err) {
								if (err) return next(err);
								next(null, postId);
							});
						});
					});
				});
			});
		});
	};

	exports.update = function (role, form, editablePosts, next) {
		findThread(form.threadId, function (err, thread) {
			if (err) return next(err);
			mongo.findPost(form.postId, function (err, post) {
				if (err) return next(err);
				categoryForUpdate(role, thread.categoryId, function (err, category) {
					if (err) return next(err);
					if (!editable(category, post._id, editablePosts)) {
						next({ rc: rcs.NOT_AUTHORIZED });<commit_msg>minor.<commit_after>var init = require('../main/init');
var mongo = require('../main/mongo');
var es = require('../main/es');
var upload = require('../main/upload');
var rcs = require('../main/rcs');

init.add(function () {

	console.log('post:');

	exports.form = function (req) {
		var body = req.body;
		var form = {};
		form.now = new Date();
		form.categoryId = parseInt(body.categoryId) || 0;
		form.writer  = String(body.writer || '').trim();
		form.title = String(body.title || '').trim();
		form.text = String(body.text || '');
		form.visible = !!body.visible;
		form.delFiles = body.delFiles;
		form.tmpFiles = body.tmpFiles;
		return form;
	};

	exports.createThread = function (role, form, next) {
		categoryForUpdate(role, form.categoryId, function (err, category) {
			if (err) return next(err);
			checkForm(form, true, function (err) {
				if (err) return next(err);
				var threadId = mongo.getNewThreadId();
				insertThread(threadId, form, function (err, thread) {
					if (err) return next(err);
					var postId = mongo.getNewPostId();
					savePostFiles(postId, form, function (err) {
						if (err) return next(err);
						insertPost(postId, form, thread, category, function (err) {
							if (err) return next(err);
							next(null, threadId, postId);
						});
					});
				});
			});
		});
	}

	exports.createReply = function (role, form, next) {
		var threadId = form.threadId;
		findThread(threadId, function (err, thread) {
			if (err) return next(err);
			categoryForUpdate(role, thread.categoryId, function (err, category) {
				if (err) return next(err);
				checkForm(form, false, function (err) {
					if (err) return next(err);
					var postId = mongo.getNewPostId();
					savePostFiles(postId, form, function (err) {
						if (err) return next(err);
						insertPost(postId, form, thread, category, function (err) {
							if (err) return next(err);
							mongo.updateThreadLength(threadId, form.now, function (err) {
								if (err) return next(err);
								next(null, postId);
							});
						});
					});
				});
			});
		});
	};

	exports.update = function (role, form, editablePosts, next) {
		findThread(form.threadId, function (err, thread) {
			if (err) return next(err);
			mongo.findPost(form.postId, function (err, post) {
				if (err) return next(err);
				categoryForUpdate(role, thread.categoryId, function (err, category) {
					if (err) return next(err);
					if (!editable(category, post._id, editablePosts)) {
						return next({ rc: rcs.NOT_AUTHORIZED });
{'fraction_non_alphanumeric': 0.16487669053301512, 'fraction_numerical': 0.0003977724741447892, 'mean_word_length': 3.316738197424893, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5131679', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1655, 'n_words': 415}