all files / src/posts/ edit.js

8.47% Statements 5/59
0% Branches 0/20
5% Functions 1/20
8.47% Lines 5/59
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                                                                                                                                                                                                                                                                                                                     
'use strict';
 
var async = require('async'),
	validator = require('validator'),
	_ = require('underscore'),
	db = require('../database'),
	topics = require('../topics'),
	user = require('../user'),
	privileges = require('../privileges'),
	plugins = require('../plugins'),
	cache = require('./cache'),
	pubsub = require('../pubsub'),
	utils = require('../../public/src/utils');
 
module.exports = function(Posts) {
 
	pubsub.on('post:edit', function(pid) {
		cache.del(pid);
	});
 
	Posts.edit = function(data, callback) {
		var now = Date.now();
		var postData;
		var results;
 
		async.waterfall([
			function (next) {
				privileges.posts.canEdit(data.pid, data.uid, next);
			},
			function (canEdit, next) {
				if (!canEdit) {
					return next(new Error('[[error:no-privileges]]'));
				}
				Posts.getPostData(data.pid, next);
			},
			function (_postData, next) {
				if (!_postData) {
					return next(new Error('[[error:no-post]]'));
				}
				postData = _postData;
				postData.content = data.content;
				postData.edited = now;
				postData.editor = data.uid;
				plugins.fireHook('filter:post.edit', {req: data.req, post: postData, uid: data.uid}, next);
			},
			function (result, next) {
				postData = result.post;
				var updateData = {
					edited: postData.edited,
					editor: postData.editor,
					content: postData.content
				};
				if (data.handle) {
					updateData.handle = data.handle;
				}
				Posts.setPostFields(data.pid, updateData, next);
			},
			function (next) {
				async.parallel({
					editor: function(next) {
						user.getUserFields(data.uid, ['username', 'userslug'], next);
					},
					topic: function(next) {
						editMainPost(data, postData, next);
					}
				}, next);
			},
			function (_results, next) {
				results = _results;
 
				postData.cid = results.topic.cid;
 
				plugins.fireHook('action:post.edit', _.clone(postData));
 
				cache.del(postData.pid);
				pubsub.publish('post:edit', postData.pid);
 
				Posts.parsePost(postData, next);
			},
			function (postData, next) {
				results.post = postData;
				next(null, results);
			}
		], callback);
	};
 
	function editMainPost(data, postData, callback) {
		var tid = postData.tid;
		var title = data.title ? data.title.trim() : '';
 
		async.parallel({
			topic: function(next) {
				topics.getTopicFields(tid, ['cid', 'title'], next);
			},
			isMain: function(next) {
				Posts.isMain(data.pid, next);
			}
		}, function(err, results) {
			if (err) {
				return callback(err);
			}
 
			if (!results.isMain) {
				return callback(null, {
					tid: tid,
					cid: results.topic.cid,
					isMainPost: false,
					renamed: false
				});
			}
 
			var topicData = {
				tid: tid,
				cid: results.topic.cid,
				uid: postData.uid,
				mainPid: data.pid
			};
 
			if (title) {
				topicData.title = title;
				topicData.slug = tid + '/' + (utils.slugify(title) || 'topic');
			}
 
			topicData.thumb = data.topic_thumb || '';
 
			data.tags = data.tags || [];
 
			async.waterfall([
				async.apply(plugins.fireHook, 'filter:topic.edit', {req: data.req, topic: topicData}),
				function(results, next) {
					db.setObject('topic:' + tid, results.topic, next);
				},
				function(next) {
					topics.updateTags(tid, data.tags, next);
				},
				function(next) {
					topics.getTopicTagsObjects(tid, next);
				},
				function(tags, next) {
					topicData.tags = data.tags;
					plugins.fireHook('action:topic.edit', topicData);
					next(null, {
						tid: tid,
						cid: results.topic.cid,
						uid: postData.uid,
						title: validator.escape(title),
						oldTitle: results.topic.title,
						slug: topicData.slug,
						isMainPost: true,
						renamed: title !== results.topic.title,
						tags: tags
					});
				}
			], callback);
		});
	}
 
 
};