mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-11-03 20:45:58 +01:00 
			
		
		
		
	refactor: src/flags.js because codeclimate
This commit is contained in:
		
							
								
								
									
										128
									
								
								src/flags.js
									
									
									
									
									
								
							
							
						
						
									
										128
									
								
								src/flags.js
									
									
									
									
									
								
							@@ -455,65 +455,8 @@ Flags.getHistory = async function (flagId) {
 | 
			
		||||
	});
 | 
			
		||||
 | 
			
		||||
	// Append ban history and username change data
 | 
			
		||||
	let recentBans = await db.getSortedSetRevRange('uid:' + targetUid + ':bans:timestamp', 0, 19);
 | 
			
		||||
	const usernameChanges = await user.getHistory('user:' + targetUid + ':usernames');
 | 
			
		||||
	const emailChanges = await user.getHistory('user:' + targetUid + ':emails');
 | 
			
		||||
 | 
			
		||||
	recentBans = await db.getObjects(recentBans);
 | 
			
		||||
	history = 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;
 | 
			
		||||
	}, [])).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;
 | 
			
		||||
	}, []));
 | 
			
		||||
	history = await mergeBanHistory(history, targetUid, uids);
 | 
			
		||||
	history = await mergeUsernameEmailChanges(history, targetUid, uids);
 | 
			
		||||
 | 
			
		||||
	const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']);
 | 
			
		||||
	history.forEach((event, idx) => { event.user = userData[idx]; });
 | 
			
		||||
@@ -590,4 +533,71 @@ Flags.notify = async function (flagObj, uid) {
 | 
			
		||||
	await notifications.push(notifObj, uids);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
async function mergeBanHistory(history, targetUid, uids) {
 | 
			
		||||
	let recentBans = await db.getSortedSetRevRange('uid:' + targetUid + ':bans:timestamp', 0, 19);
 | 
			
		||||
	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) {
 | 
			
		||||
	const usernameChanges = await user.getHistory('user:' + targetUid + ':usernames');
 | 
			
		||||
	const emailChanges = await user.getHistory('user:' + targetUid + ':emails');
 | 
			
		||||
 | 
			
		||||
	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;
 | 
			
		||||
	}, []));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
require('./promisify')(Flags);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user