| 
									
										
										
										
											2020-10-16 21:05:00 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const url = require('url'); | 
					
						
							| 
									
										
										
										
											2020-10-16 21:36:59 -04:00
										 |  |  | const user = require('../user'); | 
					
						
							|  |  |  | const topics = require('../topics'); | 
					
						
							| 
									
										
										
										
											2020-10-17 22:59:12 -04:00
										 |  |  | const posts = require('../posts'); | 
					
						
							| 
									
										
										
										
											2020-10-16 21:36:59 -04:00
										 |  |  | const privileges = require('../privileges'); | 
					
						
							| 
									
										
										
										
											2020-10-17 22:59:12 -04:00
										 |  |  | const plugins = require('../plugins'); | 
					
						
							| 
									
										
										
										
											2020-10-16 21:36:59 -04:00
										 |  |  | const socketHelpers = require('../socket.io/helpers'); | 
					
						
							| 
									
										
										
										
											2020-10-17 22:59:12 -04:00
										 |  |  | const websockets = require('../socket.io'); | 
					
						
							| 
									
										
										
										
											2020-10-16 21:36:59 -04:00
										 |  |  | const events = require('../events'); | 
					
						
							| 
									
										
										
										
											2020-10-16 21:05:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 15:34:05 -04:00
										 |  |  | exports.setDefaultPostData = function (reqOrSocket, data) { | 
					
						
							|  |  |  | 	data.uid = reqOrSocket.uid; | 
					
						
							|  |  |  | 	data.req = exports.buildReqObject(reqOrSocket, { ...data }); | 
					
						
							| 
									
										
										
										
											2021-03-24 21:28:02 +03:00
										 |  |  | 	data.timestamp = parseInt(data.timestamp, 10) || Date.now(); | 
					
						
							| 
									
										
										
										
											2021-03-15 15:34:05 -04:00
										 |  |  | 	data.fromQueue = false; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-16 21:05:00 -04:00
										 |  |  | // creates a slimmed down version of the request object
 | 
					
						
							|  |  |  | exports.buildReqObject = (req, payload) => { | 
					
						
							|  |  |  | 	req = req || {}; | 
					
						
							| 
									
										
										
										
											2021-04-23 12:41:58 -04:00
										 |  |  | 	const headers = req.headers || (req.request && req.request.headers) || {}; | 
					
						
							| 
									
										
										
										
											2020-10-16 21:05:00 -04:00
										 |  |  | 	const encrypted = req.connection ? !!req.connection.encrypted : false; | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 	let { host } = headers; | 
					
						
							| 
									
										
										
										
											2020-10-16 21:05:00 -04:00
										 |  |  | 	const referer = headers.referer || ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!host) { | 
					
						
							|  |  |  | 		host = url.parse(referer).host || ''; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return { | 
					
						
							|  |  |  | 		uid: req.uid, | 
					
						
							|  |  |  | 		params: req.params, | 
					
						
							|  |  |  | 		method: req.method, | 
					
						
							|  |  |  | 		body: payload || req.body, | 
					
						
							| 
									
										
										
										
											2021-02-05 14:31:23 -05:00
										 |  |  | 		session: req.session, | 
					
						
							| 
									
										
										
										
											2020-10-16 21:05:00 -04:00
										 |  |  | 		ip: req.ip, | 
					
						
							|  |  |  | 		host: host, | 
					
						
							|  |  |  | 		protocol: encrypted ? 'https' : 'http', | 
					
						
							|  |  |  | 		secure: encrypted, | 
					
						
							|  |  |  | 		url: referer, | 
					
						
							|  |  |  | 		path: referer.substr(referer.indexOf(host) + host.length), | 
					
						
							|  |  |  | 		headers: headers, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-10-16 21:36:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | exports.doTopicAction = async function (action, event, caller, { tids }) { | 
					
						
							|  |  |  | 	if (!Array.isArray(tids)) { | 
					
						
							|  |  |  | 		throw new Error('[[error:invalid-tid]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 12:19:24 -05:00
										 |  |  | 	const exists = await topics.exists(tids); | 
					
						
							|  |  |  | 	if (!exists.every(Boolean)) { | 
					
						
							| 
									
										
										
										
											2020-10-16 21:36:59 -04:00
										 |  |  | 		throw new Error('[[error:no-topic]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (typeof topics.tools[action] !== 'function') { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const uids = await user.getUidsFromSet('users:online', 0, -1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	await Promise.all(tids.map(async (tid) => { | 
					
						
							| 
									
										
										
										
											2020-10-16 21:36:59 -04:00
										 |  |  | 		const title = await topics.getTopicField(tid, 'title'); | 
					
						
							|  |  |  | 		const data = await topics.tools[action](tid, caller.uid); | 
					
						
							|  |  |  | 		const notifyUids = await privileges.categories.filterUids('topics:read', data.cid, uids); | 
					
						
							|  |  |  | 		socketHelpers.emitToUids(event, data, notifyUids); | 
					
						
							|  |  |  | 		await logTopicAction(action, caller, tid, title); | 
					
						
							|  |  |  | 	})); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function logTopicAction(action, req, tid, title) { | 
					
						
							| 
									
										
										
										
											2021-01-03 12:44:20 -05:00
										 |  |  | 	// Only log certain actions to system event log
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 	const actionsToLog = ['delete', 'restore', 'purge']; | 
					
						
							| 
									
										
										
										
											2020-10-16 21:36:59 -04:00
										 |  |  | 	if (!actionsToLog.includes(action)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	await events.log({ | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		type: `topic-${action}`, | 
					
						
							| 
									
										
										
										
											2020-10-16 21:36:59 -04:00
										 |  |  | 		uid: req.uid, | 
					
						
							|  |  |  | 		ip: req.ip, | 
					
						
							|  |  |  | 		tid: tid, | 
					
						
							|  |  |  | 		title: String(title), | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-10-17 22:59:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | exports.postCommand = async function (caller, command, eventName, notification, data) { | 
					
						
							|  |  |  | 	if (!caller.uid) { | 
					
						
							|  |  |  | 		throw new Error('[[error:not-logged-in]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!data || !data.pid) { | 
					
						
							|  |  |  | 		throw new Error('[[error:invalid-data]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!data.room_id) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		throw new Error(`[[error:invalid-room-id, ${data.room_id} ]]`); | 
					
						
							| 
									
										
										
										
											2020-10-17 22:59:12 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	const [exists, deleted] = await Promise.all([ | 
					
						
							|  |  |  | 		posts.exists(data.pid), | 
					
						
							|  |  |  | 		posts.getPostField(data.pid, 'deleted'), | 
					
						
							|  |  |  | 	]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!exists) { | 
					
						
							|  |  |  | 		throw new Error('[[error:invalid-pid]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (deleted) { | 
					
						
							|  |  |  | 		throw new Error('[[error:post-deleted]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* | 
					
						
							|  |  |  | 	hooks: | 
					
						
							|  |  |  | 		filter:post.upvote | 
					
						
							|  |  |  | 		filter:post.downvote | 
					
						
							|  |  |  | 		filter:post.unvote | 
					
						
							|  |  |  | 		filter:post.bookmark | 
					
						
							|  |  |  | 		filter:post.unbookmark | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	const filteredData = await plugins.hooks.fire(`filter:post.${command}`, { | 
					
						
							| 
									
										
										
										
											2020-10-17 22:59:12 -04:00
										 |  |  | 		data: data, | 
					
						
							|  |  |  | 		uid: caller.uid, | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	return await executeCommand(caller, command, eventName, notification, filteredData.data); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function executeCommand(caller, command, eventName, notification, data) { | 
					
						
							|  |  |  | 	const result = await posts[command](data.pid, caller.uid); | 
					
						
							|  |  |  | 	if (result && eventName) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		websockets.in(`uid_${caller.uid}`).emit(`posts.${command}`, result); | 
					
						
							|  |  |  | 		websockets.in(data.room_id).emit(`event:${eventName}`, result); | 
					
						
							| 
									
										
										
										
											2020-10-17 22:59:12 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (result && command === 'upvote') { | 
					
						
							|  |  |  | 		socketHelpers.upvote(result, notification); | 
					
						
							|  |  |  | 	} else if (result && notification) { | 
					
						
							|  |  |  | 		socketHelpers.sendNotificationToPostOwner(data.pid, caller.uid, command, notification); | 
					
						
							|  |  |  | 	} else if (result && command === 'unvote') { | 
					
						
							|  |  |  | 		socketHelpers.rescindUpvoteNotification(data.pid, caller.uid); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } |