| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-27 14:06:14 +03:00
										 |  |  | var async = require('async'); | 
					
						
							|  |  |  | var validator = require('validator'); | 
					
						
							|  |  |  | var _ = require('underscore'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var db = require('../database'); | 
					
						
							|  |  |  | var topics = require('../topics'); | 
					
						
							|  |  |  | var user = require('../user'); | 
					
						
							|  |  |  | var privileges = require('../privileges'); | 
					
						
							|  |  |  | var plugins = require('../plugins'); | 
					
						
							|  |  |  | var cache = require('./cache'); | 
					
						
							|  |  |  | var pubsub = require('../pubsub'); | 
					
						
							|  |  |  | var utils = require('../../public/src/utils'); | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = function(Posts) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-16 14:07:28 -04:00
										 |  |  | 	pubsub.on('post:edit', function(pid) { | 
					
						
							|  |  |  | 		cache.del(pid); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 	Posts.edit = function(data, callback) { | 
					
						
							|  |  |  | 		var postData; | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 		var results; | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				privileges.posts.canEdit(data.pid, data.uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 			function (canEdit, next) { | 
					
						
							| 
									
										
										
										
											2016-08-10 23:55:49 +03:00
										 |  |  | 				if (!canEdit.flag) { | 
					
						
							|  |  |  | 					return next(new Error(canEdit.message)); | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				Posts.getPostData(data.pid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 			function (_postData, next) { | 
					
						
							| 
									
										
										
										
											2015-10-02 16:39:07 -04:00
										 |  |  | 				if (!_postData) { | 
					
						
							|  |  |  | 					return next(new Error('[[error:no-post]]')); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-08-27 14:06:14 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 				postData = _postData; | 
					
						
							|  |  |  | 				postData.content = data.content; | 
					
						
							| 
									
										
										
										
											2016-08-27 14:06:14 +03:00
										 |  |  | 				postData.edited = Date.now(); | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 				postData.editor = data.uid; | 
					
						
							| 
									
										
										
										
											2016-08-27 14:06:14 +03:00
										 |  |  | 				if (data.handle) { | 
					
						
							|  |  |  | 					postData.handle = data.handle; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				plugins.fireHook('filter:post.edit', {req: data.req, post: postData, data: data, uid: data.uid}, next); | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 			function (result, next) { | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 				postData = result.post; | 
					
						
							| 
									
										
										
										
											2016-08-27 14:06:14 +03:00
										 |  |  | 				Posts.setPostFields(data.pid, postData, next); | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			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; | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				postData.cid = results.topic.cid; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 				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); | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function editMainPost(data, postData, callback) { | 
					
						
							|  |  |  | 		var tid = postData.tid; | 
					
						
							| 
									
										
										
										
											2015-10-29 11:45:28 -04:00
										 |  |  | 		var title = data.title ? data.title.trim() : ''; | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							| 
									
										
										
										
											2015-06-25 15:15:33 -04:00
										 |  |  | 			topic: function(next) { | 
					
						
							|  |  |  | 				topics.getTopicFields(tid, ['cid', 'title'], next); | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			isMain: function(next) { | 
					
						
							|  |  |  | 				Posts.isMain(data.pid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, function(err, results) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!results.isMain) { | 
					
						
							|  |  |  | 				return callback(null, { | 
					
						
							|  |  |  | 					tid: tid, | 
					
						
							| 
									
										
										
										
											2015-06-25 15:15:33 -04:00
										 |  |  | 					cid: results.topic.cid, | 
					
						
							|  |  |  | 					isMainPost: false, | 
					
						
							|  |  |  | 					renamed: false | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var topicData = { | 
					
						
							|  |  |  | 				tid: tid, | 
					
						
							| 
									
										
										
										
											2015-06-25 15:15:33 -04:00
										 |  |  | 				cid: results.topic.cid, | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 				uid: postData.uid, | 
					
						
							|  |  |  | 				mainPid: data.pid | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (title) { | 
					
						
							|  |  |  | 				topicData.title = title; | 
					
						
							| 
									
										
										
										
											2016-03-22 19:51:31 -05:00
										 |  |  | 				topicData.slug = tid + '/' + (utils.slugify(title) || 'topic'); | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-27 14:06:14 +03:00
										 |  |  | 			topicData.thumb = data.thumb || ''; | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			data.tags = data.tags || []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-08-27 14:06:14 +03:00
										 |  |  | 				function(next) { | 
					
						
							|  |  |  | 					plugins.fireHook('filter:topic.edit', {req: data.req, topic: topicData, data: data}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-03-09 16:42:26 +02:00
										 |  |  | 				function(results, next) { | 
					
						
							| 
									
										
										
										
											2016-03-09 18:06:05 +02:00
										 |  |  | 					db.setObject('topic:' + tid, results.topic, next); | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				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, | 
					
						
							| 
									
										
										
										
											2015-06-25 15:15:33 -04:00
										 |  |  | 						cid: results.topic.cid, | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 						uid: postData.uid, | 
					
						
							|  |  |  | 						title: validator.escape(title), | 
					
						
							| 
									
										
										
										
											2015-06-25 15:15:33 -04:00
										 |  |  | 						oldTitle: results.topic.title, | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 						slug: topicData.slug, | 
					
						
							| 
									
										
										
										
											2015-06-25 15:15:33 -04:00
										 |  |  | 						isMainPost: true, | 
					
						
							|  |  |  | 						renamed: title !== results.topic.title, | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 						tags: tags | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			], callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; |