| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'), | 
					
						
							|  |  |  | 	validator = require('validator'), | 
					
						
							|  |  |  | 	db = require('../database'), | 
					
						
							|  |  |  | 	topics = require('../topics'), | 
					
						
							|  |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	privileges = require('../privileges'), | 
					
						
							|  |  |  | 	plugins = require('../plugins'), | 
					
						
							|  |  |  | 	cache = require('./cache'), | 
					
						
							| 
									
										
										
										
											2015-07-16 14:07:28 -04:00
										 |  |  | 	pubsub = require('../pubsub'), | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 	utils = require('../../public/src/utils'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 now = Date.now(); | 
					
						
							|  |  |  | 		var postData; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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) { | 
					
						
							|  |  |  | 				postData = _postData; | 
					
						
							|  |  |  | 				postData.content = data.content; | 
					
						
							|  |  |  | 				postData.edited = now; | 
					
						
							|  |  |  | 				postData.editor = data.uid; | 
					
						
							|  |  |  | 				plugins.fireHook('filter:post.edit', {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(err, result) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				editor: function(next) { | 
					
						
							|  |  |  | 					user.getUserFields(data.uid, ['username', 'userslug'], next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				topic: function(next) { | 
					
						
							|  |  |  | 					editMainPost(data, postData, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				post: function(next) { | 
					
						
							| 
									
										
										
										
											2015-07-16 14:07:28 -04:00
										 |  |  | 					pubsub.publish('post:edit', postData.pid); | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 					Posts.parsePost(postData, next); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, function(err, results) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				postData.cid = results.topic.cid; | 
					
						
							|  |  |  | 				plugins.fireHook('action:post.edit', postData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				callback(null, results); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function editMainPost(data, postData, callback) { | 
					
						
							|  |  |  | 		var tid = postData.tid; | 
					
						
							|  |  |  | 		var title = data.title.trim(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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; | 
					
						
							|  |  |  | 				topicData.slug = tid + '/' + utils.slugify(title); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (data.topic_thumb) { | 
					
						
							|  |  |  | 				topicData.thumb = data.topic_thumb; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			data.tags = data.tags || []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2015-04-23 11:15:37 -04:00
										 |  |  | 				async.apply(plugins.fireHook,'filter:topic.edit', topicData), | 
					
						
							|  |  |  | 				function(topicData, next) { | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 					db.setObject('topic:' + tid, topicData, 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, | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; |