| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | const validator = require('validator'); | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const db = require('../database'); | 
					
						
							|  |  |  | const meta = require('../meta'); | 
					
						
							|  |  |  | const topics = require('../topics'); | 
					
						
							|  |  |  | const user = require('../user'); | 
					
						
							|  |  |  | const privileges = require('../privileges'); | 
					
						
							|  |  |  | const plugins = require('../plugins'); | 
					
						
							|  |  |  | const pubsub = require('../pubsub'); | 
					
						
							|  |  |  | const utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											2020-10-11 21:49:37 -04:00
										 |  |  | const slugify = require('../slugify'); | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | const translator = require('../translator'); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2018-10-21 16:47:51 -04:00
										 |  |  | 		require('./cache').del(pid); | 
					
						
							| 
									
										
										
										
											2015-07-16 14:07:28 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 	Posts.edit = async function (data) { | 
					
						
							|  |  |  | 		const canEdit = await privileges.posts.canEdit(data.pid, data.uid); | 
					
						
							|  |  |  | 		if (!canEdit.flag) { | 
					
						
							|  |  |  | 			throw new Error(canEdit.message); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 		const postData = await Posts.getPostData(data.pid); | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 		if (!postData) { | 
					
						
							|  |  |  | 			throw new Error('[[error:no-post]]'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const oldContent = postData.content; // for diffing purposes
 | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 		const now = Date.now(); | 
					
						
							|  |  |  | 		const editPostData = { | 
					
						
							|  |  |  | 			content: data.content, | 
					
						
							|  |  |  | 			edited: now, | 
					
						
							|  |  |  | 			editor: data.uid, | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 		if (data.handle) { | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 			editPostData.handle = data.handle; | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const result = await plugins.fireHook('filter:post.edit', { | 
					
						
							|  |  |  | 			req: data.req, | 
					
						
							|  |  |  | 			post: editPostData, | 
					
						
							|  |  |  | 			data: data, | 
					
						
							|  |  |  | 			uid: data.uid, | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const [editor, topic] = await Promise.all([ | 
					
						
							|  |  |  | 			user.getUserFields(data.uid, ['username', 'userslug']), | 
					
						
							|  |  |  | 			editMainPost(data, postData), | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 		await Posts.setPostFields(data.pid, result.post); | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (meta.config.enablePostHistory === 1) { | 
					
						
							| 
									
										
										
										
											2020-06-15 11:02:56 -04:00
										 |  |  | 			await Posts.diffs.save({ | 
					
						
							|  |  |  | 				pid: data.pid, | 
					
						
							|  |  |  | 				uid: data.uid, | 
					
						
							|  |  |  | 				oldContent: oldContent, | 
					
						
							|  |  |  | 				newContent: data.content, | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		await Posts.uploads.sync(data.pid); | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 		const returnPostData = { ...postData, ...editPostData }; | 
					
						
							|  |  |  | 		returnPostData.cid = topic.cid; | 
					
						
							|  |  |  | 		returnPostData.topic = topic; | 
					
						
							|  |  |  | 		returnPostData.editedISO = utils.toISOString(now); | 
					
						
							| 
									
										
										
										
											2020-10-17 15:07:04 -04:00
										 |  |  | 		returnPostData.changed = oldContent !== data.content; | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 		await topics.notifyFollowers(returnPostData, data.uid, { | 
					
						
							| 
									
										
										
										
											2020-05-10 22:21:32 -04:00
										 |  |  | 			type: 'post-edit', | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 			bodyShort: translator.compile('notifications:user_edited_post', editor.username, topic.title), | 
					
						
							|  |  |  | 			nid: 'edit_post:' + data.pid + ':uid:' + data.uid, | 
					
						
							| 
									
										
										
										
											2020-05-10 22:21:32 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 		plugins.fireHook('action:post.edit', { post: _.clone(returnPostData), data: data, uid: data.uid }); | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		require('./cache').del(String(postData.pid)); | 
					
						
							|  |  |  | 		pubsub.publish('post:edit', String(postData.pid)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 		await Posts.parsePost(returnPostData); | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return { | 
					
						
							|  |  |  | 			topic: topic, | 
					
						
							|  |  |  | 			editor: editor, | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 			post: returnPostData, | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 	async function editMainPost(data, postData) { | 
					
						
							|  |  |  | 		const tid = postData.tid; | 
					
						
							|  |  |  | 		const title = data.title ? data.title.trim() : ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const [topicData, isMain] = await Promise.all([ | 
					
						
							|  |  |  | 			topics.getTopicFields(tid, ['cid', 'title', 'timestamp']), | 
					
						
							|  |  |  | 			Posts.isMain(data.pid), | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!isMain) { | 
					
						
							|  |  |  | 			return { | 
					
						
							|  |  |  | 				tid: tid, | 
					
						
							|  |  |  | 				cid: topicData.cid, | 
					
						
							| 
									
										
										
										
											2020-05-10 22:21:32 -04:00
										 |  |  | 				title: validator.escape(String(topicData.title)), | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 				isMainPost: false, | 
					
						
							|  |  |  | 				renamed: false, | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const newTopicData = { | 
					
						
							|  |  |  | 			tid: tid, | 
					
						
							|  |  |  | 			cid: topicData.cid, | 
					
						
							|  |  |  | 			uid: postData.uid, | 
					
						
							|  |  |  | 			mainPid: data.pid, | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		if (title) { | 
					
						
							|  |  |  | 			newTopicData.title = title; | 
					
						
							| 
									
										
										
										
											2020-10-11 21:49:37 -04:00
										 |  |  | 			newTopicData.slug = tid + '/' + (slugify(title) || 'topic'); | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		newTopicData.thumb = data.thumb || ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		data.tags = data.tags || []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (data.tags.length) { | 
					
						
							|  |  |  | 			const canTag = await privileges.categories.can('topics:tag', topicData.cid, data.uid); | 
					
						
							|  |  |  | 			if (!canTag) { | 
					
						
							|  |  |  | 				throw new Error('[[error:no-privileges]]'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-06-24 16:02:57 -04:00
										 |  |  | 		await topics.validateTags(data.tags, topicData.cid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 15:54:55 -04:00
										 |  |  | 		const results = await plugins.fireHook('filter:topic.edit', { | 
					
						
							|  |  |  | 			req: data.req, | 
					
						
							|  |  |  | 			topic: newTopicData, | 
					
						
							|  |  |  | 			data: data, | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 		await db.setObject('topic:' + tid, results.topic); | 
					
						
							|  |  |  | 		await topics.updateTopicTags(tid, data.tags); | 
					
						
							|  |  |  | 		const tags = await topics.getTopicTagsObjects(tid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 23:50:23 -04:00
										 |  |  | 		newTopicData.tags = data.tags; | 
					
						
							|  |  |  | 		newTopicData.oldTitle = topicData.title; | 
					
						
							|  |  |  | 		newTopicData.timestamp = topicData.timestamp; | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 		const renamed = translator.escape(validator.escape(String(title))) !== topicData.title; | 
					
						
							| 
									
										
										
										
											2019-07-24 23:50:23 -04:00
										 |  |  | 		plugins.fireHook('action:topic.edit', { topic: newTopicData, uid: data.uid }); | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 		return { | 
					
						
							|  |  |  | 			tid: tid, | 
					
						
							| 
									
										
										
										
											2019-07-24 23:50:23 -04:00
										 |  |  | 			cid: newTopicData.cid, | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 			uid: postData.uid, | 
					
						
							|  |  |  | 			title: validator.escape(String(title)), | 
					
						
							|  |  |  | 			oldTitle: topicData.title, | 
					
						
							| 
									
										
										
										
											2019-07-24 23:50:23 -04:00
										 |  |  | 			slug: newTopicData.slug, | 
					
						
							| 
									
										
										
										
											2019-07-17 13:38:40 -04:00
										 |  |  | 			isMainPost: true, | 
					
						
							|  |  |  | 			renamed: renamed, | 
					
						
							|  |  |  | 			tags: tags, | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2015-04-20 17:56:43 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; |