| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2017-05-26 01:39:40 -06:00
										 |  |  | var _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2016-08-27 14:06:14 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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'); | 
					
						
							| 
									
										
										
										
											2017-04-08 20:22:21 -06:00
										 |  |  | var utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | module.exports = function (Posts) { | 
					
						
							|  |  |  | 	pubsub.on('post:edit', function (pid) { | 
					
						
							| 
									
										
										
										
											2015-07-16 14:07:28 -04:00
										 |  |  | 		cache.del(pid); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Posts.edit = function (data, callback) { | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 		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; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				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; | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 				async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					editor: function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 						user.getUserFields(data.uid, ['username', 'userslug'], next); | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					topic: function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 						editMainPost(data, postData, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (_results, next) { | 
					
						
							|  |  |  | 				results = _results; | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 				Posts.setPostFields(data.pid, postData, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 				postData.cid = results.topic.cid; | 
					
						
							| 
									
										
										
										
											2016-11-11 17:28:32 +03:00
										 |  |  | 				postData.topic = results.topic; | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 				plugins.fireHook('action:post.edit', { post: _.clone(postData), uid: data.uid }); | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 21:39:02 +03:00
										 |  |  | 				cache.del(String(postData.pid)); | 
					
						
							|  |  |  | 				pubsub.publish('post:edit', String(postData.pid)); | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				Posts.parsePost(postData, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (postData, next) { | 
					
						
							|  |  |  | 				results.post = postData; | 
					
						
							|  |  |  | 				next(null, results); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-09-23 20:20:30 -04:00
										 |  |  | 		], 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 16:40:03 -04:00
										 |  |  | 		var topicData; | 
					
						
							|  |  |  | 		var results; | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				async.parallel({ | 
					
						
							|  |  |  | 					topic: function (next) { | 
					
						
							|  |  |  | 						topics.getTopicFields(tid, ['cid', 'title', 'timestamp'], next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					isMain: function (next) { | 
					
						
							|  |  |  | 						Posts.isMain(data.pid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-05-25 16:40:03 -04:00
										 |  |  | 			function (_results, next) { | 
					
						
							|  |  |  | 				results = _results; | 
					
						
							|  |  |  | 				if (!results.isMain) { | 
					
						
							|  |  |  | 					return callback(null, { | 
					
						
							|  |  |  | 						tid: tid, | 
					
						
							|  |  |  | 						cid: results.topic.cid, | 
					
						
							|  |  |  | 						isMainPost: false, | 
					
						
							|  |  |  | 						renamed: false, | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 16:40:03 -04:00
										 |  |  | 				topicData = { | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 					tid: tid, | 
					
						
							| 
									
										
										
										
											2015-06-25 15:15:33 -04:00
										 |  |  | 					cid: results.topic.cid, | 
					
						
							| 
									
										
										
										
											2017-05-25 16:40:03 -04:00
										 |  |  | 					uid: postData.uid, | 
					
						
							|  |  |  | 					mainPid: data.pid, | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (title) { | 
					
						
							|  |  |  | 					topicData.title = title; | 
					
						
							|  |  |  | 					topicData.slug = tid + '/' + (utils.slugify(title) || 'topic'); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				topicData.thumb = data.thumb || ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				data.tags = data.tags || []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 				if (!data.tags.length) { | 
					
						
							|  |  |  | 					return next(null, true); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				privileges.categories.can('topics:tag', topicData.cid, data.uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (canTag, next) { | 
					
						
							|  |  |  | 				if (!canTag) { | 
					
						
							|  |  |  | 					return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 16:40:03 -04:00
										 |  |  | 				plugins.fireHook('filter:topic.edit', { req: data.req, topic: topicData, data: data }, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			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; | 
					
						
							|  |  |  | 				topicData.oldTitle = results.topic.title; | 
					
						
							|  |  |  | 				topicData.timestamp = results.topic.timestamp; | 
					
						
							|  |  |  | 				plugins.fireHook('action:topic.edit', { topic: topicData, uid: data.uid }); | 
					
						
							|  |  |  | 				next(null, { | 
					
						
							|  |  |  | 					tid: tid, | 
					
						
							|  |  |  | 					cid: topicData.cid, | 
					
						
							|  |  |  | 					uid: postData.uid, | 
					
						
							|  |  |  | 					title: validator.escape(String(title)), | 
					
						
							|  |  |  | 					oldTitle: results.topic.title, | 
					
						
							|  |  |  | 					slug: topicData.slug, | 
					
						
							|  |  |  | 					isMainPost: true, | 
					
						
							|  |  |  | 					renamed: title !== results.topic.title, | 
					
						
							|  |  |  | 					tags: tags, | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2017-05-25 16:40:03 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; |