| 
									
										
										
										
											2020-10-06 13:20:50 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const validator = require('validator'); | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const meta = require('../../meta'); | 
					
						
							| 
									
										
										
										
											2020-10-06 14:14:57 -04:00
										 |  |  | const privileges = require('../../privileges'); | 
					
						
							| 
									
										
										
										
											2020-10-06 13:20:50 -04:00
										 |  |  | const groups = require('../../groups'); | 
					
						
							|  |  |  | const posts = require('../../posts'); | 
					
						
							| 
									
										
										
										
											2020-10-06 14:12:02 -04:00
										 |  |  | const topics = require('../../topics'); | 
					
						
							| 
									
										
										
										
											2020-10-06 13:20:50 -04:00
										 |  |  | const events = require('../../events'); | 
					
						
							|  |  |  | const utils = require('../../utils'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const helpers = require('../helpers'); | 
					
						
							|  |  |  | const sockets = require('../../socket.io'); | 
					
						
							| 
									
										
										
										
											2020-10-06 14:47:03 -04:00
										 |  |  | const socketPostHelpers = require('../../socket.io/posts/helpers');	// eehhh...
 | 
					
						
							| 
									
										
										
										
											2020-10-06 14:12:02 -04:00
										 |  |  | const socketTopics = require('../../socket.io/topics');	// eehhh...
 | 
					
						
							| 
									
										
										
										
											2020-10-06 13:20:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | const Posts = module.exports; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Posts.edit = async (req, res) => { | 
					
						
							|  |  |  | 	// Trim and remove HTML (latter for composers that send in HTML, like redactor)
 | 
					
						
							|  |  |  | 	var contentLen = utils.stripHTMLTags(req.body.content).trim().length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (req.body.title && req.body.title.length < meta.config.minimumTitleLength) { | 
					
						
							|  |  |  | 		throw new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]'); | 
					
						
							|  |  |  | 	} else if (req.body.title && req.body.title.length > meta.config.maximumTitleLength) { | 
					
						
							|  |  |  | 		throw new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]'); | 
					
						
							|  |  |  | 	} else if (meta.config.minimumPostLength !== 0 && contentLen < meta.config.minimumPostLength) { | 
					
						
							|  |  |  | 		throw new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]'); | 
					
						
							|  |  |  | 	} else if (contentLen > meta.config.maximumPostLength) { | 
					
						
							|  |  |  | 		throw new Error('[[error:content-too-long, ' + meta.config.maximumPostLength + ']]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Payload construction
 | 
					
						
							|  |  |  | 	var payload = { | 
					
						
							|  |  |  | 		req, | 
					
						
							|  |  |  | 		uid: req.user.uid, | 
					
						
							|  |  |  | 		pid: req.params.pid, | 
					
						
							|  |  |  | 		content: req.body.content, | 
					
						
							|  |  |  | 		options: {}, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	['handle', 'title'].forEach((prop) => { | 
					
						
							|  |  |  | 		if (req.body.hasOwnProperty(prop)) { | 
					
						
							|  |  |  | 			payload[prop] = req.body[prop]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	['topic_thumb', 'tags'].forEach((prop) => { | 
					
						
							|  |  |  | 		if (req.body.hasOwnProperty(prop)) { | 
					
						
							|  |  |  | 			payload.options[prop] = req.body[prop]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const editResult = await posts.edit(payload); | 
					
						
							|  |  |  | 	helpers.formatApiResponse(200, res, await posts.getPostSummaryByPids([editResult.pid], req.user.uid, {})); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (editResult.topic.renamed) { | 
					
						
							|  |  |  | 		await events.log({ | 
					
						
							|  |  |  | 			type: 'topic-rename', | 
					
						
							|  |  |  | 			uid: req.user.uid, | 
					
						
							|  |  |  | 			ip: req.ip, | 
					
						
							|  |  |  | 			tid: editResult.topic.tid, | 
					
						
							|  |  |  | 			oldTitle: validator.escape(String(editResult.topic.oldTitle)), | 
					
						
							|  |  |  | 			newTitle: validator.escape(String(editResult.topic.title)), | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!editResult.post.deleted) { | 
					
						
							|  |  |  | 		sockets.in('topic_' + editResult.topic.tid).emit('event:post_edited', editResult); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const memberData = await groups.getMembersOfGroups([ | 
					
						
							|  |  |  | 		'administrators', | 
					
						
							|  |  |  | 		'Global Moderators', | 
					
						
							|  |  |  | 		'cid:' + editResult.topic.cid + ':privileges:moderate', | 
					
						
							|  |  |  | 		'cid:' + editResult.topic.cid + ':privileges:groups:moderate', | 
					
						
							|  |  |  | 	]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const uids = _.uniq(_.flatten(memberData).concat(req.user.uid.toString())); | 
					
						
							|  |  |  | 	uids.forEach(uid =>	sockets.in('uid_' + uid).emit('event:post_edited', editResult)); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-10-06 14:12:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | Posts.purge = async (req, res) => { | 
					
						
							|  |  |  | 	const results = await isMainAndLastPost(req.params.pid); | 
					
						
							|  |  |  | 	if (results.isMain && !results.isLast) { | 
					
						
							|  |  |  | 		throw new Error('[[error:cant-purge-main-post]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const isMainAndLast = results.isMain && results.isLast; | 
					
						
							|  |  |  | 	const postData = await posts.getPostFields(req.params.pid, ['pid', 'toPid', 'tid']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 14:14:57 -04:00
										 |  |  | 	const canPurge = await privileges.posts.canPurge(req.params.pid, req.user.uid); | 
					
						
							|  |  |  | 	if (!canPurge) { | 
					
						
							|  |  |  | 		throw new Error('[[error:no-privileges]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	require('../../posts/cache').del(req.params.pid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	await posts.purge(req.params.pid, req.user.uid); | 
					
						
							| 
									
										
										
										
											2020-10-06 14:12:02 -04:00
										 |  |  | 	helpers.formatApiResponse(200, res); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sockets.in('topic_' + postData.tid).emit('event:post_purged', postData); | 
					
						
							|  |  |  | 	const topicData = await topics.getTopicFields(postData.tid, ['title', 'cid']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	await events.log({ | 
					
						
							|  |  |  | 		type: 'post-purge', | 
					
						
							|  |  |  | 		uid: req.user.uid, | 
					
						
							|  |  |  | 		pid: req.params.pid, | 
					
						
							|  |  |  | 		ip: req.ip, | 
					
						
							|  |  |  | 		tid: postData.tid, | 
					
						
							|  |  |  | 		title: String(topicData.title), | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (isMainAndLast) { | 
					
						
							|  |  |  | 		await socketTopics.doTopicAction('purge', 'event:topic_purged', { | 
					
						
							|  |  |  | 			ip: req.ip, | 
					
						
							|  |  |  | 			uid: req.user.uid, | 
					
						
							|  |  |  | 		}, { tids: [postData.tid], cid: topicData.cid }); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Posts.restore = async (req, res) => { | 
					
						
							|  |  |  | 	await deleteOrRestore(req, { | 
					
						
							|  |  |  | 		pid: req.params.pid, | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		command: 'restore', | 
					
						
							|  |  |  | 		event: 'event:post_restored', | 
					
						
							|  |  |  | 		type: 'post-restore', | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	helpers.formatApiResponse(200, res); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Posts.delete = async (req, res) => { | 
					
						
							|  |  |  | 	await deleteOrRestore(req, { | 
					
						
							|  |  |  | 		pid: req.params.pid, | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		command: 'delete', | 
					
						
							|  |  |  | 		event: 'event:post_deleted', | 
					
						
							|  |  |  | 		type: 'post-delete', | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	helpers.formatApiResponse(200, res); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-08 15:58:42 -04:00
										 |  |  | async function mock(req) { | 
					
						
							| 
									
										
										
										
											2020-10-06 14:47:03 -04:00
										 |  |  | 	const tid = await posts.getPostField(req.params.pid, 'tid'); | 
					
						
							|  |  |  | 	const data = { pid: req.params.pid, room_id: `topic_${tid}` }; | 
					
						
							|  |  |  | 	const socketMock = { uid: req.user.uid }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-08 15:58:42 -04:00
										 |  |  | 	return { data, socketMock }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Posts.vote = async (req, res) => { | 
					
						
							|  |  |  | 	const { data, socketMock } = await mock(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 14:47:03 -04:00
										 |  |  | 	if (req.body.delta > 0) { | 
					
						
							|  |  |  | 		await socketPostHelpers.postCommand(socketMock, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data); | 
					
						
							|  |  |  | 	} else if (req.body.delta < 0) { | 
					
						
							|  |  |  | 		await socketPostHelpers.postCommand(socketMock, 'downvote', 'voted', '', data); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		await socketPostHelpers.postCommand(socketMock, 'unvote', 'voted', '', data); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	helpers.formatApiResponse(200, res); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Posts.unvote = async (req, res) => { | 
					
						
							| 
									
										
										
										
											2020-10-08 15:58:42 -04:00
										 |  |  | 	const { data, socketMock } = await mock(); | 
					
						
							| 
									
										
										
										
											2020-10-06 14:47:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	await socketPostHelpers.postCommand(socketMock, 'unvote', 'voted', '', data); | 
					
						
							|  |  |  | 	helpers.formatApiResponse(200, res); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 10:19:27 -04:00
										 |  |  | Posts.bookmark = async (req, res) => { | 
					
						
							| 
									
										
										
										
											2020-10-08 15:58:42 -04:00
										 |  |  | 	const { data, socketMock } = await mock(); | 
					
						
							| 
									
										
										
										
											2020-10-07 10:19:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	await socketPostHelpers.postCommand(socketMock, 'bookmark', 'bookmarked', '', data); | 
					
						
							|  |  |  | 	helpers.formatApiResponse(200, res); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Posts.unbookmark = async (req, res) => { | 
					
						
							| 
									
										
										
										
											2020-10-08 15:58:42 -04:00
										 |  |  | 	const { data, socketMock } = await mock(); | 
					
						
							| 
									
										
										
										
											2020-10-07 10:19:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	await socketPostHelpers.postCommand(socketMock, 'unbookmark', 'bookmarked', '', data); | 
					
						
							|  |  |  | 	helpers.formatApiResponse(200, res); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 14:12:02 -04:00
										 |  |  | async function isMainAndLastPost(pid) { | 
					
						
							|  |  |  | 	const [isMain, topicData] = await Promise.all([ | 
					
						
							|  |  |  | 		posts.isMain(pid), | 
					
						
							|  |  |  | 		posts.getTopicFields(pid, ['postcount']), | 
					
						
							|  |  |  | 	]); | 
					
						
							|  |  |  | 	return { | 
					
						
							|  |  |  | 		isMain: isMain, | 
					
						
							|  |  |  | 		isLast: topicData && topicData.postcount === 1, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function deleteOrRestoreTopicOf(command, pid, req) { | 
					
						
							|  |  |  | 	const topic = await posts.getTopicFields(pid, ['tid', 'cid', 'deleted']); | 
					
						
							|  |  |  | 	if (command === 'delete' && !topic.deleted) { | 
					
						
							|  |  |  | 		await socketTopics.doTopicAction('delete', 'event:topic_deleted', { | 
					
						
							|  |  |  | 			uid: req.user.uid, | 
					
						
							|  |  |  | 			ip: req.ip, | 
					
						
							|  |  |  | 		}, { tids: [topic.tid], cid: topic.cid }); | 
					
						
							|  |  |  | 	} else if (command === 'restore' && topic.deleted) { | 
					
						
							|  |  |  | 		await socketTopics.doTopicAction('restore', 'event:topic_restored', { | 
					
						
							|  |  |  | 			uid: req.user.uid, | 
					
						
							|  |  |  | 			ip: req.ip, | 
					
						
							|  |  |  | 		}, { tids: [topic.tid], cid: topic.cid }); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function deleteOrRestore(req, data, params) { | 
					
						
							|  |  |  | 	if (!data || !data.pid) { | 
					
						
							|  |  |  | 		throw new Error('[[error:invalid-data]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	const postData = await posts.tools[params.command](req.user.uid, data.pid); | 
					
						
							|  |  |  | 	const results = await isMainAndLastPost(data.pid); | 
					
						
							|  |  |  | 	if (results.isMain && results.isLast) { | 
					
						
							|  |  |  | 		await deleteOrRestoreTopicOf(params.command, data.pid, req); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sockets.in('topic_' + postData.tid).emit(params.event, postData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	await events.log({ | 
					
						
							|  |  |  | 		type: params.type, | 
					
						
							|  |  |  | 		uid: req.user.uid, | 
					
						
							|  |  |  | 		pid: data.pid, | 
					
						
							|  |  |  | 		tid: postData.tid, | 
					
						
							|  |  |  | 		ip: req.ip, | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } |