| 
									
										
										
										
											2016-11-23 19:41:35 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | const winston = require('winston'); | 
					
						
							|  |  |  | const validator = require('validator'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const db = require('./database'); | 
					
						
							|  |  |  | const user = require('./user'); | 
					
						
							|  |  |  | const groups = require('./groups'); | 
					
						
							|  |  |  | const meta = require('./meta'); | 
					
						
							|  |  |  | const notifications = require('./notifications'); | 
					
						
							|  |  |  | const analytics = require('./analytics'); | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | const categories = require('./categories'); | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | const topics = require('./topics'); | 
					
						
							|  |  |  | const posts = require('./posts'); | 
					
						
							|  |  |  | const privileges = require('./privileges'); | 
					
						
							|  |  |  | const plugins = require('./plugins'); | 
					
						
							|  |  |  | const utils = require('../public/src/utils'); | 
					
						
							| 
									
										
										
										
											2020-08-22 17:34:19 -04:00
										 |  |  | const batch = require('./batch'); | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | const Flags = module.exports; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-20 10:19:23 -05:00
										 |  |  | Flags._constants = { | 
					
						
							|  |  |  | 	states: ['open', 'wip', 'resolved', 'rejected'], | 
					
						
							|  |  |  | 	state_class: { | 
					
						
							|  |  |  | 		open: 'info', | 
					
						
							|  |  |  | 		wip: 'warning', | 
					
						
							|  |  |  | 		resolved: 'success', | 
					
						
							|  |  |  | 		rejected: 'danger', | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | Flags.init = async function () { | 
					
						
							| 
									
										
										
										
											2017-07-04 10:09:37 -04:00
										 |  |  | 	// Query plugins for custom filter strategies and merge into core filter strategies
 | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 	function prepareSets(sets, orSets, prefix, value) { | 
					
						
							| 
									
										
										
										
											2017-07-04 10:09:37 -04:00
										 |  |  | 		if (!Array.isArray(value)) { | 
					
						
							|  |  |  | 			sets.push(prefix + value); | 
					
						
							|  |  |  | 		} else if (value.length) { | 
					
						
							| 
									
										
										
										
											2021-01-08 12:17:42 -05:00
										 |  |  | 			if (value.length === 1) { | 
					
						
							| 
									
										
										
										
											2021-01-08 13:57:20 -05:00
										 |  |  | 				sets.push(prefix + value[0]); | 
					
						
							| 
									
										
										
										
											2021-01-08 12:17:42 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2021-01-25 11:29:00 -05:00
										 |  |  | 				orSets.push(value.map(x => prefix + x)); | 
					
						
							| 
									
										
										
										
											2021-01-08 12:17:42 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-07-04 10:09:37 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-07-04 10:09:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 	const hookData = { | 
					
						
							| 
									
										
										
										
											2017-07-04 10:09:37 -04:00
										 |  |  | 		filters: { | 
					
						
							|  |  |  | 			type: function (sets, orSets, key) { | 
					
						
							|  |  |  | 				prepareSets(sets, orSets, 'flags:byType:', key); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			state: function (sets, orSets, key) { | 
					
						
							|  |  |  | 				prepareSets(sets, orSets, 'flags:byState:', key); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			reporterId: function (sets, orSets, key) { | 
					
						
							|  |  |  | 				prepareSets(sets, orSets, 'flags:byReporter:', key); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			assignee: function (sets, orSets, key) { | 
					
						
							|  |  |  | 				prepareSets(sets, orSets, 'flags:byAssignee:', key); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			targetUid: function (sets, orSets, key) { | 
					
						
							|  |  |  | 				prepareSets(sets, orSets, 'flags:byTargetUid:', key); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			cid: function (sets, orSets, key) { | 
					
						
							|  |  |  | 				prepareSets(sets, orSets, 'flags:byCid:', key); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2018-03-09 12:57:52 -05:00
										 |  |  | 			page: function () {	/* noop */ }, | 
					
						
							|  |  |  | 			perPage: function () {	/* noop */ }, | 
					
						
							| 
									
										
										
										
											2017-07-04 10:09:37 -04:00
										 |  |  | 			quick: function (sets, orSets, key, uid) { | 
					
						
							|  |  |  | 				switch (key) { | 
					
						
							| 
									
										
										
										
											2020-06-03 11:25:25 -04:00
										 |  |  | 					case 'mine': | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 						sets.push(`flags:byAssignee:${uid}`); | 
					
						
							| 
									
										
										
										
											2020-06-03 11:25:25 -04:00
										 |  |  | 						break; | 
					
						
							| 
									
										
										
										
											2021-01-08 12:17:42 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					case 'unresolved': | 
					
						
							|  |  |  | 						prepareSets(sets, orSets, 'flags:byState:', ['open', 'wip']); | 
					
						
							|  |  |  | 						break; | 
					
						
							| 
									
										
										
										
											2017-07-04 10:09:37 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		helpers: { | 
					
						
							|  |  |  | 			prepareSets: prepareSets, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2017-07-04 10:09:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 	try { | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 		const data = await plugins.hooks.fire('filter:flags.getFilters', hookData); | 
					
						
							| 
									
										
										
										
											2017-07-04 10:09:37 -04:00
										 |  |  | 		Flags._filters = data.filters; | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 	} catch (err) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		winston.error(`[flags/init] Could not retrieve filters\n${err.stack}`); | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 		Flags._filters = {}; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-07-04 10:09:37 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | Flags.get = async function (flagId) { | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 	const [base, history, notes, reports] = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		db.getObject(`flag:${flagId}`), | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 		Flags.getHistory(flagId), | 
					
						
							|  |  |  | 		Flags.getNotes(flagId), | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 		Flags.getReports(flagId), | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 	]); | 
					
						
							|  |  |  | 	if (!base) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const flagObj = { | 
					
						
							|  |  |  | 		state: 'open', | 
					
						
							| 
									
										
										
										
											2020-04-23 21:50:08 -04:00
										 |  |  | 		assignee: null, | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 		...base, | 
					
						
							|  |  |  | 		datetimeISO: utils.toISOString(base.datetime), | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		target_readable: `${base.type.charAt(0).toUpperCase() + base.type.slice(1)} ${base.targetId}`, | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 		target: await Flags.getTarget(base.type, base.targetId, 0), | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 		history: history, | 
					
						
							|  |  |  | 		notes: notes, | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 		reports: reports, | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 	const data = await plugins.hooks.fire('filter:flags.get', { | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 		flag: flagObj, | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	return data.flag; | 
					
						
							| 
									
										
										
										
											2016-11-25 14:17:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 12:17:42 -05:00
										 |  |  | Flags.getCount = async function ({ uid, filters }) { | 
					
						
							|  |  |  | 	filters = filters || {}; | 
					
						
							|  |  |  | 	const flagIds = await Flags.getFlagIdsWithFilters({ filters, uid }); | 
					
						
							|  |  |  | 	return flagIds.length; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-11-25 12:43:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 12:17:42 -05:00
										 |  |  | Flags.getFlagIdsWithFilters = async function ({ filters, uid }) { | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	let sets = []; | 
					
						
							|  |  |  | 	const orSets = []; | 
					
						
							| 
									
										
										
										
											2017-01-03 13:38:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-09 12:57:52 -05:00
										 |  |  | 	// Default filter
 | 
					
						
							|  |  |  | 	filters.page = filters.hasOwnProperty('page') ? Math.abs(parseInt(filters.page, 10) || 1) : 1; | 
					
						
							|  |  |  | 	filters.perPage = filters.hasOwnProperty('perPage') ? Math.abs(parseInt(filters.perPage, 10) || 20) : 20; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 01:34:30 -07:00
										 |  |  | 	for (const type of Object.keys(filters)) { | 
					
						
							|  |  |  | 		if (Flags._filters.hasOwnProperty(type)) { | 
					
						
							|  |  |  | 			Flags._filters[type](sets, orSets, filters[type], uid); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			winston.warn(`[flags/list] No flag filter type found: ${type}`); | 
					
						
							| 
									
										
										
										
											2016-12-02 15:28:28 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-03 13:38:06 -05:00
										 |  |  | 	sets = (sets.length || orSets.length) ? sets : ['flags:datetime'];	// No filter default
 | 
					
						
							| 
									
										
										
										
											2016-12-02 15:28:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	let flagIds = []; | 
					
						
							|  |  |  | 	if (sets.length === 1) { | 
					
						
							|  |  |  | 		flagIds = await db.getSortedSetRevRange(sets[0], 0, -1); | 
					
						
							|  |  |  | 	} else if (sets.length > 1) { | 
					
						
							|  |  |  | 		flagIds = await db.getSortedSetRevIntersect({ sets: sets, start: 0, stop: -1, aggregate: 'MAX' }); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-06 16:11:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	if (orSets.length) { | 
					
						
							| 
									
										
										
										
											2021-01-25 11:29:00 -05:00
										 |  |  | 		let _flagIds = await Promise.all(orSets.map(async orSet => await db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' }))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Each individual orSet is ANDed together to construct the final list of flagIds
 | 
					
						
							|  |  |  | 		_flagIds = _.intersection(..._flagIds); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Merge with flagIds returned by sets
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 		if (sets.length) { | 
					
						
							|  |  |  | 			// If flag ids are already present, return a subset of flags that are in both sets
 | 
					
						
							|  |  |  | 			flagIds = _.intersection(flagIds, _flagIds); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			// Otherwise, return all flags returned via orSets
 | 
					
						
							|  |  |  | 			flagIds = _.union(flagIds, _flagIds); | 
					
						
							| 
									
										
										
										
											2016-12-06 16:11:56 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-06 16:11:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 10:54:43 -04:00
										 |  |  | 	const result = await plugins.hooks.fire('filter:flags.getFlagIdsWithFilters', { | 
					
						
							|  |  |  | 		filters, | 
					
						
							|  |  |  | 		uid, | 
					
						
							|  |  |  | 		flagIds, | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	return result.flagIds; | 
					
						
							| 
									
										
										
										
											2021-01-08 12:17:42 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Flags.list = async function (data) { | 
					
						
							|  |  |  | 	const filters = data.filters || {}; | 
					
						
							| 
									
										
										
										
											2021-01-08 13:57:20 -05:00
										 |  |  | 	let flagIds = await Flags.getFlagIdsWithFilters({ | 
					
						
							| 
									
										
										
										
											2021-01-08 12:17:42 -05:00
										 |  |  | 		filters, | 
					
						
							|  |  |  | 		uid: data.uid, | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2020-08-18 21:03:59 -04:00
										 |  |  | 	flagIds = await Flags.sort(flagIds, data.sort); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	// Create subset for parsing based on page number (n=20)
 | 
					
						
							|  |  |  | 	const flagsPerPage = Math.abs(parseInt(filters.perPage, 10) || 1); | 
					
						
							|  |  |  | 	const pageCount = Math.ceil(flagIds.length / flagsPerPage); | 
					
						
							|  |  |  | 	flagIds = flagIds.slice((filters.page - 1) * flagsPerPage, filters.page * flagsPerPage); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 	const reportCounts = await db.sortedSetsCard(flagIds.map(flagId => `flag:${flagId}:reports`)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const flags = await Promise.all(flagIds.map(async (flagId, idx) => { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		let flagObj = await db.getObject(`flag:${flagId}`); | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 		flagObj = { | 
					
						
							|  |  |  | 			state: 'open', | 
					
						
							| 
									
										
										
										
											2020-04-23 21:50:08 -04:00
										 |  |  | 			assignee: null, | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 			heat: reportCounts[idx], | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 			...flagObj, | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2020-01-20 10:19:23 -05:00
										 |  |  | 		flagObj.labelClass = Flags._constants.state_class[flagObj.state]; | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return Object.assign(flagObj, { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			target_readable: `${flagObj.type.charAt(0).toUpperCase() + flagObj.type.slice(1)} ${flagObj.targetId}`, | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 			datetimeISO: utils.toISOString(flagObj.datetime), | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	})); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 	const payload = await plugins.hooks.fire('filter:flags.list', { | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 		flags: flags, | 
					
						
							|  |  |  | 		page: filters.page, | 
					
						
							| 
									
										
										
										
											2020-08-18 21:03:59 -04:00
										 |  |  | 		uid: data.uid, | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-12-07 12:07:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	return { | 
					
						
							| 
									
										
										
										
											2020-08-18 21:03:59 -04:00
										 |  |  | 		flags: payload.flags, | 
					
						
							|  |  |  | 		page: payload.page, | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 		pageCount: pageCount, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-18 21:03:59 -04:00
										 |  |  | Flags.sort = async function (flagIds, sort) { | 
					
						
							| 
									
										
										
										
											2020-08-31 17:15:04 -04:00
										 |  |  | 	const filterPosts = async (flagIds) => { | 
					
						
							|  |  |  | 		const keys = flagIds.map(id => `flag:${id}`); | 
					
						
							|  |  |  | 		const types = await db.getObjectsFields(keys, ['type']); | 
					
						
							|  |  |  | 		return flagIds.filter((id, idx) => types[idx].type === 'post'); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-18 21:03:59 -04:00
										 |  |  | 	switch (sort) { | 
					
						
							|  |  |  | 		// 'newest' is not handled because that is default
 | 
					
						
							|  |  |  | 		case 'oldest': | 
					
						
							|  |  |  | 			flagIds = flagIds.reverse(); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case 'reports': { | 
					
						
							|  |  |  | 			const keys = flagIds.map(id => `flag:${id}:reports`); | 
					
						
							|  |  |  | 			const heat = await db.sortedSetsCard(keys); | 
					
						
							|  |  |  | 			const mapped = heat.map((el, i) => ({ | 
					
						
							|  |  |  | 				index: i, heat: el, | 
					
						
							|  |  |  | 			})); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			mapped.sort((a, b) => b.heat - a.heat); | 
					
						
							| 
									
										
										
										
											2020-08-18 21:03:59 -04:00
										 |  |  | 			flagIds = mapped.map(obj => flagIds[obj.index]); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-31 17:15:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		case 'upvotes':	// fall-through
 | 
					
						
							|  |  |  | 		case 'downvotes': | 
					
						
							|  |  |  | 		case 'replies': { | 
					
						
							|  |  |  | 			flagIds = await filterPosts(flagIds); | 
					
						
							|  |  |  | 			const keys = flagIds.map(id => `flag:${id}`); | 
					
						
							|  |  |  | 			const pids = (await db.getObjectsFields(keys, ['targetId'])).map(obj => obj.targetId); | 
					
						
							|  |  |  | 			const votes = (await posts.getPostsFields(pids, [sort])).map(obj => parseInt(obj[sort], 10) || 0); | 
					
						
							|  |  |  | 			const sortRef = flagIds.reduce((memo, cur, idx) => { | 
					
						
							|  |  |  | 				memo[cur] = votes[idx]; | 
					
						
							|  |  |  | 				return memo; | 
					
						
							|  |  |  | 			}, {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			flagIds = flagIds.sort((a, b) => sortRef[b] - sortRef[a]); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-18 21:03:59 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-08-31 17:15:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-18 21:03:59 -04:00
										 |  |  | 	return flagIds; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | Flags.validate = async function (payload) { | 
					
						
							|  |  |  | 	const [target, reporter] = await Promise.all([ | 
					
						
							|  |  |  | 		Flags.getTarget(payload.type, payload.id, payload.uid), | 
					
						
							|  |  |  | 		user.getUserData(payload.uid), | 
					
						
							|  |  |  | 	]); | 
					
						
							| 
									
										
										
										
											2016-12-07 12:07:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	if (!target) { | 
					
						
							|  |  |  | 		throw new Error('[[error:invalid-data]]'); | 
					
						
							|  |  |  | 	} else if (target.deleted) { | 
					
						
							|  |  |  | 		throw new Error('[[error:post-deleted]]'); | 
					
						
							|  |  |  | 	} else if (!reporter || !reporter.userslug) { | 
					
						
							|  |  |  | 		throw new Error('[[error:no-user]]'); | 
					
						
							|  |  |  | 	} else if (reporter.banned) { | 
					
						
							|  |  |  | 		throw new Error('[[error:user-banned]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 11:54:27 -05:00
										 |  |  | 	// Disallow flagging of profiles/content of privileged users
 | 
					
						
							|  |  |  | 	const [targetPrivileged, reporterPrivileged] = await Promise.all([ | 
					
						
							|  |  |  | 		user.isPrivileged(target.uid), | 
					
						
							|  |  |  | 		user.isPrivileged(reporter.uid), | 
					
						
							|  |  |  | 	]); | 
					
						
							|  |  |  | 	if (targetPrivileged && !reporterPrivileged) { | 
					
						
							|  |  |  | 		throw new Error('[[error:cant-flag-privileged]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	if (payload.type === 'post') { | 
					
						
							|  |  |  | 		const editable = await privileges.posts.canEdit(payload.id, payload.uid); | 
					
						
							|  |  |  | 		if (!editable.flag && !meta.config['reputation:disabled'] && reporter.reputation < meta.config['min:rep:flag']) { | 
					
						
							|  |  |  | 			throw new Error('[[error:not-enough-reputation-to-flag]]'); | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	} else if (payload.type === 'user') { | 
					
						
							| 
									
										
										
										
											2020-11-17 21:28:32 -05:00
										 |  |  | 		if (parseInt(payload.id, 10) === parseInt(payload.uid, 10)) { | 
					
						
							|  |  |  | 			throw new Error('[[error:cant-flag-self]]'); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 		const editable = await privileges.users.canEdit(payload.uid, payload.id); | 
					
						
							|  |  |  | 		if (!editable && !meta.config['reputation:disabled'] && reporter.reputation < meta.config['min:rep:flag']) { | 
					
						
							|  |  |  | 			throw new Error('[[error:not-enough-reputation-to-flag]]'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		throw new Error('[[error:invalid-data]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-06 16:11:56 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | Flags.getNotes = async function (flagId) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	let notes = await db.getSortedSetRevRangeWithScores(`flag:${flagId}:notes`, 0, -1); | 
					
						
							| 
									
										
										
										
											2020-07-13 20:29:05 -04:00
										 |  |  | 	notes = await modifyNotes(notes); | 
					
						
							|  |  |  | 	return notes; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Flags.getNote = async function (flagId, datetime) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	let notes = await db.getSortedSetRangeByScoreWithScores(`flag:${flagId}:notes`, 0, 1, datetime, datetime); | 
					
						
							| 
									
										
										
										
											2020-07-13 20:29:05 -04:00
										 |  |  | 	if (!notes.length) { | 
					
						
							|  |  |  | 		throw new Error('[[error:invalid-data]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	notes = await modifyNotes(notes); | 
					
						
							|  |  |  | 	return notes[0]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | Flags.getFlagIdByTarget = async function (type, id) { | 
					
						
							|  |  |  | 	let method; | 
					
						
							|  |  |  | 	switch (type) { | 
					
						
							|  |  |  | 		case 'post': | 
					
						
							|  |  |  | 			method = posts.getPostField; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case 'user': | 
					
						
							|  |  |  | 			method = user.getUserField; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			throw new Error('[[error:invalid-data]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return await method(id, 'flagId'); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-13 20:29:05 -04:00
										 |  |  | async function modifyNotes(notes) { | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	const uids = []; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	notes = notes.map((note) => { | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 		const noteObj = JSON.parse(note.value); | 
					
						
							|  |  |  | 		uids.push(noteObj[0]); | 
					
						
							|  |  |  | 		return { | 
					
						
							|  |  |  | 			uid: noteObj[0], | 
					
						
							|  |  |  | 			content: noteObj[1], | 
					
						
							|  |  |  | 			datetime: note.score, | 
					
						
							|  |  |  | 			datetimeISO: utils.toISOString(note.score), | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	return notes.map((note, idx) => { | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 		note.user = userData[idx]; | 
					
						
							|  |  |  | 		note.content = validator.escape(note.content); | 
					
						
							|  |  |  | 		return note; | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2020-07-13 20:29:05 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Flags.deleteNote = async function (flagId, datetime) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	const note = await db.getSortedSetRangeByScore(`flag:${flagId}:notes`, 0, 1, datetime, datetime); | 
					
						
							| 
									
										
										
										
											2020-07-13 20:29:05 -04:00
										 |  |  | 	if (!note.length) { | 
					
						
							|  |  |  | 		throw new Error('[[error:invalid-data]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	await db.sortedSetRemove(`flag:${flagId}:notes`, note[0]); | 
					
						
							| 
									
										
										
										
											2016-11-30 20:34:06 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | Flags.create = async function (type, id, uid, reason, timestamp) { | 
					
						
							|  |  |  | 	let doHistoryAppend = false; | 
					
						
							|  |  |  | 	if (!timestamp) { | 
					
						
							| 
									
										
										
										
											2016-12-09 14:33:59 -05:00
										 |  |  | 		timestamp = Date.now(); | 
					
						
							|  |  |  | 		doHistoryAppend = true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 	const [flagExists, targetExists,, targetFlagged, targetUid, targetCid] = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 		// Sanity checks
 | 
					
						
							|  |  |  | 		Flags.exists(type, id, uid), | 
					
						
							|  |  |  | 		Flags.targetExists(type, id), | 
					
						
							| 
									
										
										
										
											2020-01-22 12:14:50 -05:00
										 |  |  | 		Flags.canFlag(type, id, uid), | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 		Flags.targetFlagged(type, id), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 		// Extra data for zset insertion
 | 
					
						
							|  |  |  | 		Flags.getTargetUid(type, id), | 
					
						
							|  |  |  | 		Flags.getTargetCid(type, id), | 
					
						
							|  |  |  | 	]); | 
					
						
							| 
									
										
										
										
											2020-01-22 12:14:50 -05:00
										 |  |  | 	if (flagExists) { | 
					
						
							| 
									
										
										
										
											2020-07-15 17:06:49 -04:00
										 |  |  | 		throw new Error(`[[error:${type}-already-flagged]]`); | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	} else if (!targetExists) { | 
					
						
							|  |  |  | 		throw new Error('[[error:invalid-data]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-15 17:06:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 	// If the flag already exists, just add the report
 | 
					
						
							|  |  |  | 	if (targetFlagged) { | 
					
						
							|  |  |  | 		const flagId = await Flags.getFlagIdByTarget(type, id); | 
					
						
							|  |  |  | 		await Promise.all([ | 
					
						
							| 
									
										
										
										
											2020-07-31 10:55:51 -04:00
										 |  |  | 			Flags.addReport(flagId, type, id, uid, reason, timestamp), | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 			Flags.update(flagId, uid, { state: 'open' }), | 
					
						
							|  |  |  | 		]); | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 		return await Flags.get(flagId); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const flagId = await db.incrObjectField('global', 'nextFlagId'); | 
					
						
							|  |  |  | 	const batched = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	batched.push( | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		db.setObject(`flag:${flagId}`, { | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 			flagId: flagId, | 
					
						
							|  |  |  | 			type: type, | 
					
						
							|  |  |  | 			targetId: id, | 
					
						
							| 
									
										
										
										
											2020-08-22 17:13:26 -04:00
										 |  |  | 			targetUid: targetUid, | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 			datetime: timestamp, | 
					
						
							|  |  |  | 		}), | 
					
						
							| 
									
										
										
										
											2020-07-31 10:55:51 -04:00
										 |  |  | 		Flags.addReport(flagId, type, id, uid, reason, timestamp), | 
					
						
							| 
									
										
										
										
											2020-07-25 09:14:13 -04:00
										 |  |  | 		db.sortedSetAdd('flags:datetime', timestamp, flagId), // by time, the default
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		db.sortedSetAdd(`flags:byType:${type}`, timestamp, flagId),	// by flag type
 | 
					
						
							| 
									
										
										
										
											2020-07-25 09:14:13 -04:00
										 |  |  | 		db.sortedSetIncrBy('flags:byTarget', 1, [type, id].join(':')),	// by flag target (score is count)
 | 
					
						
							|  |  |  | 		analytics.increment('flags') // some fancy analytics
 | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 	); | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (targetUid) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		batched.push(db.sortedSetAdd(`flags:byTargetUid:${targetUid}`, timestamp, flagId)); // by target uid
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-06 16:11:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	if (targetCid) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		batched.push(db.sortedSetAdd(`flags:byCid:${targetCid}`, timestamp, flagId)); // by target cid
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-12 08:45:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	if (type === 'post') { | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 		batched.push( | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			db.sortedSetAdd(`flags:byPid:${id}`, timestamp, flagId),	// by target pid
 | 
					
						
							| 
									
										
										
										
											2020-07-25 09:14:13 -04:00
										 |  |  | 			posts.setPostField(id, 'flagId', flagId) | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 21:28:32 -05:00
										 |  |  | 		if (targetUid && parseInt(targetUid, 10) !== parseInt(uid, 10)) { | 
					
						
							| 
									
										
										
										
											2020-07-25 09:14:13 -04:00
										 |  |  | 			batched.push(user.incrementUserFlagsBy(targetUid, 1)); | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 	} else if (type === 'user') { | 
					
						
							| 
									
										
										
										
											2020-07-25 09:14:13 -04:00
										 |  |  | 		batched.push(user.setUserField(id, 'flagId', flagId)); | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 	// Run all the database calls in one single batched call...
 | 
					
						
							| 
									
										
										
										
											2020-07-25 09:14:13 -04:00
										 |  |  | 	await Promise.all(batched); | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	if (doHistoryAppend) { | 
					
						
							| 
									
										
										
										
											2021-03-15 17:55:14 -04:00
										 |  |  | 		await Flags.update(flagId, uid, { state: 'open' }); | 
					
						
							| 
									
										
										
										
											2019-09-26 22:51:11 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-09 14:33:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 10:08:07 -05:00
										 |  |  | 	const flagObj = await Flags.get(flagId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	plugins.hooks.fire('action:flags.create', { flag: flagObj }); | 
					
						
							|  |  |  | 	return flagObj; | 
					
						
							| 
									
										
										
										
											2016-11-23 19:41:35 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | Flags.getReports = async function (flagId) { | 
					
						
							| 
									
										
										
										
											2020-07-31 11:37:53 -04:00
										 |  |  | 	const payload = await db.getSortedSetRevRangeWithScores(`flag:${flagId}:reports`, 0, -1); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	const [reports, uids] = payload.reduce((memo, cur) => { | 
					
						
							| 
									
										
										
										
											2020-07-31 11:37:53 -04:00
										 |  |  | 		const value = cur.value.split(';'); | 
					
						
							|  |  |  | 		memo[1].push(value.shift()); | 
					
						
							| 
									
										
										
										
											2021-04-09 14:20:42 -04:00
										 |  |  | 		cur.value = validator.escape(String(value.join(';'))); | 
					
						
							| 
									
										
										
										
											2020-07-31 11:37:53 -04:00
										 |  |  | 		memo[0].push(cur); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return memo; | 
					
						
							|  |  |  | 	}, [[], []]); | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	await Promise.all(reports.map(async (report, idx) => { | 
					
						
							|  |  |  | 		report.timestamp = report.score; | 
					
						
							|  |  |  | 		report.timestampISO = new Date(report.score).toISOString(); | 
					
						
							|  |  |  | 		delete report.score; | 
					
						
							| 
									
										
										
										
											2020-07-31 11:37:53 -04:00
										 |  |  | 		report.reporter = await user.getUserFields(uids[idx], ['username', 'userslug', 'picture', 'reputation']); | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 	})); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return reports; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 10:55:51 -04:00
										 |  |  | Flags.addReport = async function (flagId, type, id, uid, reason, timestamp) { | 
					
						
							| 
									
										
										
										
											2020-07-25 18:26:09 -04:00
										 |  |  | 	await db.sortedSetAddBulk([ | 
					
						
							|  |  |  | 		[`flags:byReporter:${uid}`, timestamp, flagId], | 
					
						
							| 
									
										
										
										
											2020-07-31 11:37:53 -04:00
										 |  |  | 		[`flag:${flagId}:reports`, timestamp, [uid, reason].join(';')], | 
					
						
							| 
									
										
										
										
											2020-07-31 10:55:51 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		['flags:hash', flagId, [type, id, uid].join(':')], | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | 	]); | 
					
						
							| 
									
										
										
										
											2021-03-02 10:08:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-11 21:43:11 -05:00
										 |  |  | 	plugins.hooks.fire('action:flags.addReport', { flagId, type, id, uid, reason, timestamp }); | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | Flags.exists = async function (type, id, uid) { | 
					
						
							|  |  |  | 	return await db.isSortedSetMember('flags:hash', [type, id, uid].join(':')); | 
					
						
							| 
									
										
										
										
											2016-11-23 19:41:35 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-22 12:14:50 -05:00
										 |  |  | Flags.canFlag = async function (type, id, uid) { | 
					
						
							| 
									
										
										
										
											2020-07-15 17:06:49 -04:00
										 |  |  | 	const limit = meta.config['flags:limitPerTarget']; | 
					
						
							|  |  |  | 	if (limit > 0) { | 
					
						
							|  |  |  | 		const score = await db.sortedSetScore('flags:byTarget', `${type}:${id}`); | 
					
						
							|  |  |  | 		if (score >= limit) { | 
					
						
							|  |  |  | 			throw new Error(`[[error:${type}-flagged-too-many-times]]`); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-01-22 12:14:50 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-15 17:06:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	const canRead = await privileges.posts.can('topics:read', id, uid); | 
					
						
							|  |  |  | 	switch (type) { | 
					
						
							|  |  |  | 		case 'user': | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case 'post': | 
					
						
							|  |  |  | 			if (!canRead) { | 
					
						
							|  |  |  | 				throw new Error('[[error:no-privileges]]'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			throw new Error('[[error:invalid-data]]'); | 
					
						
							| 
									
										
										
										
											2020-01-22 12:14:50 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | Flags.getTarget = async function (type, id, uid) { | 
					
						
							|  |  |  | 	if (type === 'user') { | 
					
						
							|  |  |  | 		const userData = await user.getUserData(id); | 
					
						
							|  |  |  | 		return userData && userData.uid ? userData : {}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (type === 'post') { | 
					
						
							|  |  |  | 		let postData = await posts.getPostData(id); | 
					
						
							|  |  |  | 		if (!postData) { | 
					
						
							|  |  |  | 			return {}; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		postData = await posts.parsePost(postData); | 
					
						
							|  |  |  | 		postData = await topics.addPostData([postData], uid); | 
					
						
							|  |  |  | 		return postData[0]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	throw new Error('[[error:invalid-data]]'); | 
					
						
							| 
									
										
										
										
											2016-12-19 11:16:03 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | Flags.targetExists = async function (type, id) { | 
					
						
							|  |  |  | 	if (type === 'post') { | 
					
						
							|  |  |  | 		return await posts.exists(id); | 
					
						
							|  |  |  | 	} else if (type === 'user') { | 
					
						
							|  |  |  | 		return await user.exists(id); | 
					
						
							| 
									
										
										
										
											2016-11-23 19:41:35 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 	throw new Error('[[error:invalid-data]]'); | 
					
						
							| 
									
										
										
										
											2016-11-23 19:41:35 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 14:10:37 -04:00
										 |  |  | Flags.targetFlagged = async function (type, id) { | 
					
						
							|  |  |  | 	return await db.sortedSetScore('flags:byTarget', [type, id].join(':')) >= 1; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | Flags.getTargetUid = async function (type, id) { | 
					
						
							|  |  |  | 	if (type === 'post') { | 
					
						
							|  |  |  | 		return await posts.getPostField(id, 'uid'); | 
					
						
							| 
									
										
										
										
											2016-12-06 16:11:56 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | 	return id; | 
					
						
							| 
									
										
										
										
											2016-12-06 16:11:56 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 00:30:59 -04:00
										 |  |  | Flags.getTargetCid = async function (type, id) { | 
					
						
							|  |  |  | 	if (type === 'post') { | 
					
						
							|  |  |  | 		return await posts.getCidByPid(id); | 
					
						
							| 
									
										
										
										
											2016-12-14 15:53:57 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-09-24 12:22:40 -04:00
										 |  |  | 	return null; | 
					
						
							| 
									
										
										
										
											2016-12-14 15:53:57 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | Flags.update = async function (flagId, uid, changeset) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	const current = await db.getObjectFields(`flag:${flagId}`, ['uid', 'state', 'assignee', 'type', 'targetId']); | 
					
						
							| 
									
										
										
										
											2020-07-25 18:26:09 -04:00
										 |  |  | 	if (!current.type) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 	const now = changeset.datetime || Date.now(); | 
					
						
							|  |  |  | 	const notifyAssignee = async function (assigneeId) { | 
					
						
							| 
									
										
										
										
											2018-09-18 17:28:24 -04:00
										 |  |  | 		if (assigneeId === '' || parseInt(uid, 10) === parseInt(assigneeId, 10)) { | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2017-03-17 13:16:16 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 		const notifObj = await notifications.create({ | 
					
						
							| 
									
										
										
										
											2017-03-17 13:16:16 -04:00
										 |  |  | 			type: 'my-flags', | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			bodyShort: `[[notifications:flag_assigned_to_you, ${flagId}]]`, | 
					
						
							| 
									
										
										
										
											2017-03-17 13:16:16 -04:00
										 |  |  | 			bodyLong: '', | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			path: `/flags/${flagId}`, | 
					
						
							|  |  |  | 			nid: `flags:assign:${flagId}:uid:${assigneeId}`, | 
					
						
							| 
									
										
										
										
											2017-03-17 13:16:16 -04:00
										 |  |  | 			from: uid, | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 		await notifications.push(notifObj, [assigneeId]); | 
					
						
							| 
									
										
										
										
											2017-03-17 13:16:16 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2020-01-20 10:19:23 -05:00
										 |  |  | 	const isAssignable = async function (assigneeId) { | 
					
						
							|  |  |  | 		let allowed = false; | 
					
						
							|  |  |  | 		allowed = await user.isAdminOrGlobalMod(assigneeId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Mods are also allowed to be assigned, if flag target is post in uid's moderated cid
 | 
					
						
							|  |  |  | 		if (!allowed && current.type === 'post') { | 
					
						
							|  |  |  | 			const cid = await posts.getCidByPid(current.targetId); | 
					
						
							|  |  |  | 			allowed = await user.isModerator(assigneeId, cid); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return allowed; | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2016-11-23 19:41:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-20 10:19:23 -05:00
										 |  |  | 	// Retrieve existing flag data to compare for history-saving/reference purposes
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 	const tasks = []; | 
					
						
							| 
									
										
										
										
											2021-02-04 01:34:30 -07:00
										 |  |  | 	for (const prop of Object.keys(changeset)) { | 
					
						
							|  |  |  | 		if (current[prop] === changeset[prop]) { | 
					
						
							|  |  |  | 			delete changeset[prop]; | 
					
						
							|  |  |  | 		} else if (prop === 'state') { | 
					
						
							|  |  |  | 			if (!Flags._constants.states.includes(changeset[prop])) { | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 				delete changeset[prop]; | 
					
						
							| 
									
										
										
										
											2021-02-04 01:34:30 -07:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				tasks.push(db.sortedSetAdd(`flags:byState:${changeset[prop]}`, now, flagId)); | 
					
						
							|  |  |  | 				tasks.push(db.sortedSetRemove(`flags:byState:${current[prop]}`, flagId)); | 
					
						
							|  |  |  | 				if (changeset[prop] === 'resolved' || changeset[prop] === 'rejected') { | 
					
						
							|  |  |  | 					tasks.push(notifications.rescind(`flag:${current.type}:${current.targetId}`)); | 
					
						
							| 
									
										
										
										
											2020-01-20 10:19:23 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-11-23 19:41:35 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-02-04 01:34:30 -07:00
										 |  |  | 		} else if (prop === 'assignee') { | 
					
						
							|  |  |  | 			/* eslint-disable-next-line */ | 
					
						
							|  |  |  | 			if (!await isAssignable(parseInt(changeset[prop], 10))) { | 
					
						
							|  |  |  | 				delete changeset[prop]; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				tasks.push(db.sortedSetAdd(`flags:byAssignee:${changeset[prop]}`, now, flagId)); | 
					
						
							|  |  |  | 				tasks.push(notifyAssignee(changeset[prop])); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-23 19:41:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 	if (!Object.keys(changeset).length) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-12 10:03:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	tasks.push(db.setObject(`flag:${flagId}`, changeset)); | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 	tasks.push(Flags.appendHistory(flagId, uid, changeset)); | 
					
						
							|  |  |  | 	await Promise.all(tasks); | 
					
						
							| 
									
										
										
										
											2020-07-25 18:26:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 	plugins.hooks.fire('action:flags.update', { flagId: flagId, changeset: changeset, uid: uid }); | 
					
						
							| 
									
										
										
										
											2020-07-25 18:26:09 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Flags.resolveFlag = async function (type, id, uid) { | 
					
						
							|  |  |  | 	const flagId = await Flags.getFlagIdByTarget(type, id); | 
					
						
							|  |  |  | 	if (parseInt(flagId, 10)) { | 
					
						
							|  |  |  | 		await Flags.update(flagId, uid, { state: 'resolved' }); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-23 19:41:35 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-22 17:34:19 -04:00
										 |  |  | Flags.resolveUserPostFlags = async function (uid, callerUid) { | 
					
						
							| 
									
										
										
										
											2021-01-25 11:07:16 -05:00
										 |  |  | 	if (meta.config['flags:autoResolveOnBan']) { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		await batch.processSortedSet(`uid:${uid}:posts`, async (pids) => { | 
					
						
							| 
									
										
										
										
											2021-01-25 11:07:16 -05:00
										 |  |  | 			let postData = await posts.getPostsFields(pids, ['pid', 'flagId']); | 
					
						
							|  |  |  | 			postData = postData.filter(p => p && p.flagId); | 
					
						
							|  |  |  | 			for (const postObj of postData) { | 
					
						
							|  |  |  | 				if (parseInt(postObj.flagId, 10)) { | 
					
						
							|  |  |  | 					// eslint-disable-next-line no-await-in-loop
 | 
					
						
							|  |  |  | 					await Flags.update(postObj.flagId, callerUid, { state: 'resolved' }); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-08-22 17:34:19 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-01-25 11:07:16 -05:00
										 |  |  | 		}, { | 
					
						
							|  |  |  | 			batch: 500, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-08-22 17:34:19 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | Flags.getHistory = async function (flagId) { | 
					
						
							|  |  |  | 	const uids = []; | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	let history = await db.getSortedSetRevRangeWithScores(`flag:${flagId}:history`, 0, -1); | 
					
						
							|  |  |  | 	const targetUid = await db.getObjectField(`flag:${flagId}`, 'targetUid'); | 
					
						
							| 
									
										
										
										
											2016-12-01 16:22:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	history = history.map((entry) => { | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 		entry.value = JSON.parse(entry.value); | 
					
						
							| 
									
										
										
										
											2016-12-01 16:22:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 		uids.push(entry.value[0]); | 
					
						
							| 
									
										
										
										
											2016-12-02 12:34:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 		// Deserialise changeset
 | 
					
						
							|  |  |  | 		const changeset = entry.value[1]; | 
					
						
							|  |  |  | 		if (changeset.hasOwnProperty('state')) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			changeset.state = changeset.state === undefined ? '' : `[[flags:state-${changeset.state}]]`; | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-01 16:22:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 		return { | 
					
						
							|  |  |  | 			uid: entry.value[0], | 
					
						
							|  |  |  | 			fields: changeset, | 
					
						
							|  |  |  | 			datetime: entry.score, | 
					
						
							|  |  |  | 			datetimeISO: utils.toISOString(entry.score), | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2020-05-26 20:27:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Append ban history and username change data
 | 
					
						
							| 
									
										
										
										
											2020-05-26 20:40:55 -04:00
										 |  |  | 	history = await mergeBanHistory(history, targetUid, uids); | 
					
						
							|  |  |  | 	history = await mergeUsernameEmailChanges(history, targetUid, uids); | 
					
						
							| 
									
										
										
										
											2020-05-26 20:27:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 	const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']); | 
					
						
							|  |  |  | 	history.forEach((event, idx) => { event.user = userData[idx]; }); | 
					
						
							| 
									
										
										
										
											2020-05-26 20:27:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Resort by date
 | 
					
						
							|  |  |  | 	history = history.sort((a, b) => b.datetime - a.datetime); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 	return history; | 
					
						
							| 
									
										
										
										
											2016-12-01 16:22:10 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | Flags.appendHistory = async function (flagId, uid, changeset) { | 
					
						
							|  |  |  | 	const datetime = changeset.datetime || Date.now(); | 
					
						
							| 
									
										
										
										
											2016-12-09 14:33:59 -05:00
										 |  |  | 	delete changeset.datetime; | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 	const payload = JSON.stringify([uid, changeset, datetime]); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	await db.sortedSetAdd(`flag:${flagId}:history`, datetime, payload); | 
					
						
							| 
									
										
										
										
											2016-12-01 11:42:06 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-11-23 19:41:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | Flags.appendNote = async function (flagId, uid, note, datetime) { | 
					
						
							| 
									
										
										
										
											2020-07-14 13:38:23 -04:00
										 |  |  | 	if (datetime) { | 
					
						
							|  |  |  | 		await Flags.deleteNote(flagId, datetime); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 	datetime = datetime || Date.now(); | 
					
						
							| 
									
										
										
										
											2020-07-14 13:38:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 	const payload = JSON.stringify([uid, note]); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	await db.sortedSetAdd(`flag:${flagId}:notes`, datetime, payload); | 
					
						
							| 
									
										
										
										
											2019-09-28 15:08:51 -04:00
										 |  |  | 	await Flags.appendHistory(flagId, uid, { | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 		notes: null, | 
					
						
							|  |  |  | 		datetime: datetime, | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-11-23 19:41:35 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | Flags.notify = async function (flagObj, uid) { | 
					
						
							|  |  |  | 	const [admins, globalMods] = await Promise.all([ | 
					
						
							|  |  |  | 		groups.getMembers('administrators', 0, -1), | 
					
						
							|  |  |  | 		groups.getMembers('Global Moderators', 0, -1), | 
					
						
							|  |  |  | 	]); | 
					
						
							|  |  |  | 	let uids = admins.concat(globalMods); | 
					
						
							|  |  |  | 	let notifObj = null; | 
					
						
							|  |  |  | 	if (flagObj.type === 'post') { | 
					
						
							|  |  |  | 		const [title, cid] = await Promise.all([ | 
					
						
							|  |  |  | 			topics.getTitleByPid(flagObj.targetId), | 
					
						
							|  |  |  | 			posts.getCidByPid(flagObj.targetId), | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const modUids = await categories.getModeratorUids([cid]); | 
					
						
							|  |  |  | 		const titleEscaped = utils.decodeHTMLEntities(title).replace(/%/g, '%').replace(/,/g, ','); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		notifObj = await notifications.create({ | 
					
						
							|  |  |  | 			type: 'new-post-flag', | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			bodyShort: `[[notifications:user_flagged_post_in, ${flagObj.reports[flagObj.reports.length - 1].reporter.username}, ${titleEscaped}]]`, | 
					
						
							| 
									
										
										
										
											2021-03-15 17:55:14 -04:00
										 |  |  | 			bodyLong: await plugins.hooks.fire('filter:parse.raw', String(flagObj.description || '')), | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 			pid: flagObj.targetId, | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			path: `/flags/${flagObj.flagId}`, | 
					
						
							|  |  |  | 			nid: `flag:post:${flagObj.targetId}`, | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 			from: uid, | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			mergeId: `notifications:user_flagged_post_in|${flagObj.targetId}`, | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 			topicTitle: title, | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 		uids = uids.concat(modUids[0]); | 
					
						
							|  |  |  | 	} else if (flagObj.type === 'user') { | 
					
						
							|  |  |  | 		notifObj = await notifications.create({ | 
					
						
							|  |  |  | 			type: 'new-user-flag', | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			bodyShort: `[[notifications:user_flagged_user, ${flagObj.reports[flagObj.reports.length - 1].reporter.username}, ${flagObj.target.username}]]`, | 
					
						
							| 
									
										
										
										
											2021-03-15 17:55:14 -04:00
										 |  |  | 			bodyLong: await plugins.hooks.fire('filter:parse.raw', String(flagObj.description || '')), | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			path: `/flags/${flagObj.flagId}`, | 
					
						
							|  |  |  | 			nid: `flag:user:${flagObj.targetId}`, | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 			from: uid, | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			mergeId: `notifications:user_flagged_user|${flagObj.targetId}`, | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		throw new Error('[[error:invalid-data]]'); | 
					
						
							| 
									
										
										
										
											2016-12-06 16:11:56 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 10:08:07 -05:00
										 |  |  | 	plugins.hooks.fire('action:flags.notify', { | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 		flag: flagObj, | 
					
						
							| 
									
										
										
										
											2021-03-02 10:08:07 -05:00
										 |  |  | 		notification: notifObj, | 
					
						
							|  |  |  | 		from: uid, | 
					
						
							|  |  |  | 		to: uids, | 
					
						
							| 
									
										
										
										
											2019-09-28 14:37:50 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 	uids = uids.filter(_uid => parseInt(_uid, 10) !== parseInt(uid, 10)); | 
					
						
							|  |  |  | 	await notifications.push(notifObj, uids); | 
					
						
							| 
									
										
										
										
											2016-12-06 16:11:56 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-07-22 18:16:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 20:40:55 -04:00
										 |  |  | async function mergeBanHistory(history, targetUid, uids) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	let recentBans = await db.getSortedSetRevRange(`uid:${targetUid}:bans:timestamp`, 0, 19); | 
					
						
							| 
									
										
										
										
											2020-05-26 20:40:55 -04:00
										 |  |  | 	recentBans = await db.getObjects(recentBans); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return history.concat(recentBans.reduce((memo, cur) => { | 
					
						
							|  |  |  | 		uids.push(cur.fromUid); | 
					
						
							|  |  |  | 		memo.push({ | 
					
						
							|  |  |  | 			uid: cur.fromUid, | 
					
						
							|  |  |  | 			meta: [ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					key: '[[user:banned]]', | 
					
						
							|  |  |  | 					value: cur.reason, | 
					
						
							|  |  |  | 					labelClass: 'danger', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					key: '[[user:info.banned-expiry]]', | 
					
						
							|  |  |  | 					value: new Date(parseInt(cur.expire, 10)).toISOString(), | 
					
						
							|  |  |  | 					labelClass: 'default', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], | 
					
						
							|  |  |  | 			datetime: parseInt(cur.timestamp, 10), | 
					
						
							|  |  |  | 			datetimeISO: utils.toISOString(parseInt(cur.timestamp, 10)), | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return memo; | 
					
						
							|  |  |  | 	}, [])); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function mergeUsernameEmailChanges(history, targetUid, uids) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	const usernameChanges = await user.getHistory(`user:${targetUid}:usernames`); | 
					
						
							|  |  |  | 	const emailChanges = await user.getHistory(`user:${targetUid}:emails`); | 
					
						
							| 
									
										
										
										
											2020-05-26 20:40:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return history.concat(usernameChanges.reduce((memo, changeObj) => { | 
					
						
							|  |  |  | 		uids.push(targetUid); | 
					
						
							|  |  |  | 		memo.push({ | 
					
						
							|  |  |  | 			uid: targetUid, | 
					
						
							|  |  |  | 			meta: [ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					key: '[[user:change_username]]', | 
					
						
							|  |  |  | 					value: changeObj.value, | 
					
						
							|  |  |  | 					labelClass: 'primary', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], | 
					
						
							|  |  |  | 			datetime: changeObj.timestamp, | 
					
						
							|  |  |  | 			datetimeISO: changeObj.timestampISO, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return memo; | 
					
						
							|  |  |  | 	}, [])).concat(emailChanges.reduce((memo, changeObj) => { | 
					
						
							|  |  |  | 		uids.push(targetUid); | 
					
						
							|  |  |  | 		memo.push({ | 
					
						
							|  |  |  | 			uid: targetUid, | 
					
						
							|  |  |  | 			meta: [ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					key: '[[user:change_email]]', | 
					
						
							|  |  |  | 					value: changeObj.value, | 
					
						
							|  |  |  | 					labelClass: 'primary', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], | 
					
						
							|  |  |  | 			datetime: changeObj.timestamp, | 
					
						
							|  |  |  | 			datetimeISO: changeObj.timestampISO, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return memo; | 
					
						
							|  |  |  | 	}, [])); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 18:16:18 -04:00
										 |  |  | require('./promisify')(Flags); |