You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/flags.js

924 lines
27 KiB
JavaScript

8 years ago
'use strict';
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');
const categories = require('./categories');
const topics = require('./topics');
const posts = require('./posts');
const privileges = require('./privileges');
const plugins = require('./plugins');
Webpack5 (#10311) * feat: webpack 5 part 1 * fix: gruntfile fixes * fix: fix taskbar warning add app.importScript copy public/src/modules to build folder * refactor: remove commented old code * feat: reenable admin * fix: acp settings pages, fix sortable on manage categories embedded require in html not allowed * fix: bundle serialize/deserizeli so plugins dont break * test: fixe util tests * test: fix require path * test: more test fixes * test: require correct utils module * test: require correct utils * test: log stack * test: fix db require blowing up tests * test: move and disable bundle test * refactor: add aliases * test: disable testing route * fix: move webpack modules necessary for build, into `dependencies` * test: fix one more test remove 500-embed.tpl * fix: restore use of assets/nodebb.min.js, at least for now * fix: remove unnecessary line break * fix: point to proper ACP bundle * test: maybe fix build test * test: composer * refactor: dont need dist * refactor: more cleanup use everything from build/public folder * get rid of conditional import in app.js * fix: ace * refactor: cropper alias * test: lint and test fixes * lint: fix * refactor: rename function to app.require * refactor: go back to using app.require * chore: use github branch * chore: use webpack branch * feat: webpack webinstaller * feat: add chunkFile name with contenthash * refactor: move hooks to top * refactor: get rid of template500Function * fix(deps): use webpack5 branch of 2factor plugin * chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket: * refactor: disable cache on templates loadTemplate is called once by benchpress and the result is cache internally * refactor: add server side helpers.js * feat: deprecate /plugins shorthand route, closes #10343 * refactor: use build/public for webpack * test: fix filename * fix: more specific selector * lint: ignore * refactor: fix comments * test: add debug for random failing test * refactor: cleanup remove test page, remove dupe functions in utils.common * lint: use relative path for now * chore: bump prerelease version * feat: add translateKeys * fix: optional params * fix: get rid of extra timeago files * refactor: cleanup, require timeago locale earlier remove translator.prepareDOM, it is in header.tpl html tag * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * feat: allow app.require('bootbox'/'benchpressjs') * refactor: require server side utils * test: jquery ready * change istaller to use build/public * test: use document.addEventListener * refactor: closes #10301 * refactor: generateTopicClass * fix: column counts for other privileges * fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking] * fix: typo in hook name * refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags) * fix: crash if `delay` not passed in (as it cannot be destructured) * refactor: replace substr * feat: set --panel-offset style in html element based on stored value in localStorage * refactor: addDropupHandler() logic to be less naive - Take into account height of the menu - Don't apply dropUp logic if there's nothing in the dropdown - Remove 'hidden' class (added by default in Persona for post tools) when menu items are added closes #10423 * refactor: simplify utils.params [breaking] Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do. * feat: add support for returning full URLSearchParams for utils.params * fix: utils.params() fallback handling * fix: default empty obj for params() * fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end * fix: utils.params() not allowing relative paths to be passed in * refactor(DRY): new assertPasswordValidity utils method * fix: incorrect error message returned on insufficient privilege on flag edit * fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate - added failing tests and patched up middleware.assert.flags to fix * refactor: flag api v3 tests to create new post and flags on every round * fix: missing error:no-flag language key * refactor: flags.canView to check flag existence, simplify middleware.assert.flag * feat: flag deletion API endpoint, #10426 * feat: UI for flag deletion, closes #10426 * chore: update plugin versions * chore: up emoji * chore: update markdown * chore: up emoji-android * fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check Co-authored-by: Julian Lam <[email protected]>
3 years ago
const utils = require('./utils');
const batch = require('./batch');
const Flags = module.exports;
Flags._constants = {
states: ['open', 'wip', 'resolved', 'rejected'],
state_class: {
open: 'info',
wip: 'warning',
resolved: 'success',
rejected: 'danger',
},
};
Flags.init = async function () {
8 years ago
// Query plugins for custom filter strategies and merge into core filter strategies
function prepareSets(sets, orSets, prefix, value) {
8 years ago
if (!Array.isArray(value)) {
sets.push(prefix + value);
} else if (value.length) {
if (value.length === 1) {
sets.push(prefix + value[0]);
} else {
orSets.push(value.map(x => prefix + x));
}
8 years ago
}
}
8 years ago
const hookData = {
8 years ago
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);
},
page: function () { /* noop */ },
perPage: function () { /* noop */ },
8 years ago
quick: function (sets, orSets, key, uid) {
switch (key) {
case 'mine':
sets.push(`flags:byAssignee:${uid}`);
break;
case 'unresolved':
prepareSets(sets, orSets, 'flags:byState:', ['open', 'wip']);
break;
8 years ago
}
},
},
helpers: {
prepareSets: prepareSets,
},
};
8 years ago
try {
const data = await plugins.hooks.fire('filter:flags.getFilters', hookData);
8 years ago
Flags._filters = data.filters;
} catch (err) {
winston.error(`[flags/init] Could not retrieve filters\n${err.stack}`);
Flags._filters = {};
}
8 years ago
};
Flags.get = async function (flagId) {
const [base, notes, reports] = await Promise.all([
db.getObject(`flag:${flagId}`),
Flags.getNotes(flagId),
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
Flags.getReports(flagId),
]);
if (!base) {
return;
}
const flagObj = {
state: 'open',
assignee: null,
...base,
datetimeISO: utils.toISOString(base.datetime),
target_readable: `${base.type.charAt(0).toUpperCase() + base.type.slice(1)} ${base.targetId}`,
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
target: await Flags.getTarget(base.type, base.targetId, 0),
notes,
reports,
};
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
const data = await plugins.hooks.fire('filter:flags.get', {
flag: flagObj,
});
return data.flag;
};
Flags.getCount = async function ({ uid, filters, query }) {
filters = filters || {};
const flagIds = await Flags.getFlagIdsWithFilters({ filters, uid, query });
return flagIds.length;
};
Flags.getFlagIdsWithFilters = async function ({ filters, uid, query }) {
let sets = [];
const orSets = [];
7 years ago
// 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;
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}`);
}
}
sets = (sets.length || orSets.length) ? sets : ['flags:datetime']; // No filter default
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' });
}
if (orSets.length) {
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
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);
}
}
const result = await plugins.hooks.fire('filter:flags.getFlagIdsWithFilters', {
filters,
uid,
query,
flagIds,
});
return result.flagIds;
};
Flags.list = async function (data) {
const filters = data.filters || {};
let flagIds = await Flags.getFlagIdsWithFilters({
filters,
uid: data.uid,
query: data.query,
});
flagIds = await Flags.sort(flagIds, data.sort);
// 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);
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
const reportCounts = await db.sortedSetsCard(flagIds.map(flagId => `flag:${flagId}:reports`));
const flags = await Promise.all(flagIds.map(async (flagId, idx) => {
let flagObj = await db.getObject(`flag:${flagId}`);
flagObj = {
state: 'open',
assignee: null,
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
heat: reportCounts[idx],
...flagObj,
};
flagObj.labelClass = Flags._constants.state_class[flagObj.state];
return Object.assign(flagObj, {
target_readable: `${flagObj.type.charAt(0).toUpperCase() + flagObj.type.slice(1)} ${flagObj.targetId}`,
datetimeISO: utils.toISOString(flagObj.datetime),
});
}));
const payload = await plugins.hooks.fire('filter:flags.list', {
flags: flags,
page: filters.page,
uid: data.uid,
});
return {
flags: payload.flags,
page: payload.page,
pageCount: pageCount,
};
};
Flags.sort = async function (flagIds, sort) {
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');
};
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,
}));
mapped.sort((a, b) => b.heat - a.heat);
flagIds = mapped.map(obj => flagIds[obj.index]);
break;
}
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]);
}
}
return flagIds;
};
Flags.validate = async function (payload) {
const [target, reporter] = await Promise.all([
Flags.getTarget(payload.type, payload.id, payload.uid),
user.getUserData(payload.uid),
]);
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]]');
}
// 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]]');
}
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, ${meta.config['min:rep:flag']}]]`);
}
} else if (payload.type === 'user') {
if (parseInt(payload.id, 10) === parseInt(payload.uid, 10)) {
throw new Error('[[error:cant-flag-self]]');
}
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, ${meta.config['min:rep:flag']}]]`);
}
} else {
throw new Error('[[error:invalid-data]]');
}
};
Flags.getNotes = async function (flagId) {
let notes = await db.getSortedSetRevRangeWithScores(`flag:${flagId}:notes`, 0, -1);
notes = await modifyNotes(notes);
return notes;
};
Flags.getNote = async function (flagId, datetime) {
datetime = parseInt(datetime, 10);
if (isNaN(datetime)) {
throw new Error('[[error:invalid-data]]');
}
let notes = await db.getSortedSetRangeByScoreWithScores(`flag:${flagId}:notes`, 0, 1, datetime, datetime);
if (!notes.length) {
throw new Error('[[error:invalid-data]]');
}
notes = await modifyNotes(notes);
return notes[0];
};
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
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');
};
async function modifyNotes(notes) {
const uids = [];
notes = notes.map((note) => {
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']);
return notes.map((note, idx) => {
note.user = userData[idx];
note.content = validator.escape(note.content);
return note;
});
}
Flags.deleteNote = async function (flagId, datetime) {
datetime = parseInt(datetime, 10);
if (isNaN(datetime)) {
throw new Error('[[error:invalid-data]]');
}
const note = await db.getSortedSetRangeByScore(`flag:${flagId}:notes`, 0, 1, datetime, datetime);
if (!note.length) {
throw new Error('[[error:invalid-data]]');
}
await db.sortedSetRemove(`flag:${flagId}:notes`, note[0]);
};
Flags.create = async function (type, id, uid, reason, timestamp, forceFlag = false) {
let doHistoryAppend = false;
if (!timestamp) {
timestamp = Date.now();
doHistoryAppend = true;
}
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
const [flagExists, targetExists,, targetFlagged, targetUid, targetCid] = await Promise.all([
// Sanity checks
Flags.exists(type, id, uid),
Flags.targetExists(type, id),
Flags.canFlag(type, id, uid, forceFlag),
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
Flags.targetFlagged(type, id),
// Extra data for zset insertion
Flags.getTargetUid(type, id),
Flags.getTargetCid(type, id),
]);
if (!forceFlag && flagExists) {
throw new Error(`[[error:${type}-already-flagged]]`);
} else if (!targetExists) {
throw new Error('[[error:invalid-data]]');
}
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
// If the flag already exists, just add the report
if (targetFlagged) {
const flagId = await Flags.getFlagIdByTarget(type, id);
await Promise.all([
Flags.addReport(flagId, type, id, uid, reason, timestamp),
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
Flags.update(flagId, uid, { state: 'open' }),
]);
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
return await Flags.get(flagId);
}
const flagId = await db.incrObjectField('global', 'nextFlagId');
const batched = [];
batched.push(
db.setObject(`flag:${flagId}`, {
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
flagId: flagId,
type: type,
targetId: id,
targetUid: targetUid,
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
datetime: timestamp,
}),
Flags.addReport(flagId, type, id, uid, reason, timestamp),
db.sortedSetAdd('flags:datetime', timestamp, flagId), // by time, the default
db.sortedSetAdd(`flags:byType:${type}`, timestamp, flagId), // by flag type
db.sortedSetIncrBy('flags:byTarget', 1, [type, id].join(':')), // by flag target (score is count)
analytics.increment('flags') // some fancy analytics
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
);
if (targetUid) {
batched.push(db.sortedSetAdd(`flags:byTargetUid:${targetUid}`, timestamp, flagId)); // by target uid
}
if (targetCid) {
batched.push(db.sortedSetAdd(`flags:byCid:${targetCid}`, timestamp, flagId)); // by target cid
}
if (type === 'post') {
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
batched.push(
db.sortedSetAdd(`flags:byPid:${id}`, timestamp, flagId), // by target pid
posts.setPostField(id, 'flagId', flagId)
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
);
if (targetUid && parseInt(targetUid, 10) !== parseInt(uid, 10)) {
batched.push(user.incrementUserFlagsBy(targetUid, 1));
}
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
} else if (type === 'user') {
batched.push(user.setUserField(id, 'flagId', flagId));
}
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
// Run all the database calls in one single batched call...
await Promise.all(batched);
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
if (doHistoryAppend) {
await Flags.update(flagId, uid, { state: 'open' });
}
const flagObj = await Flags.get(flagId);
plugins.hooks.fire('action:flags.create', { flag: flagObj });
return flagObj;
8 years ago
};
Flags.purge = async function (flagIds) {
const flagData = (await db.getObjects(flagIds.map(flagId => `flag:${flagId}`))).filter(Boolean);
const postFlags = flagData.filter(flagObj => flagObj.type === 'post');
const userFlags = flagData.filter(flagObj => flagObj.type === 'user');
const assignedFlags = flagData.filter(flagObj => !!flagObj.assignee);
const [allReports, cids] = await Promise.all([
db.getSortedSetsMembers(flagData.map(flagObj => `flag:${flagObj.flagId}:reports`)),
categories.getAllCidsFromSet('categories:cid'),
]);
const allReporterUids = allReports.map(flagReports => flagReports.map(report => report && report.split(';')[0]));
const removeReporters = [];
flagData.forEach((flagObj, i) => {
if (Array.isArray(allReporterUids[i])) {
allReporterUids[i].forEach((uid) => {
removeReporters.push([`flags:hash`, [flagObj.type, flagObj.targetId, uid].join(':')]);
removeReporters.push([`flags:byReporter:${uid}`, flagObj.flagId]);
});
}
});
await Promise.all([
db.sortedSetRemoveBulk([
...flagData.map(flagObj => ([`flags:byType:${flagObj.type}`, flagObj.flagId])),
...flagData.map(flagObj => ([`flags:byState:${flagObj.state}`, flagObj.flagId])),
...removeReporters,
...postFlags.map(flagObj => ([`flags:byPid:${flagObj.targetId}`, flagObj.flagId])),
...assignedFlags.map(flagObj => ([`flags:byAssignee:${flagObj.assignee}`, flagObj.flagId])),
...userFlags.map(flagObj => ([`flags:byTargetUid:${flagObj.targetUid}`, flagObj.flagId])),
]),
db.deleteObjectFields(postFlags.map(flagObj => `post:${flagObj.targetId}`, ['flagId'])),
db.deleteObjectFields(userFlags.map(flagObj => `user:${flagObj.targetId}`, ['flagId'])),
db.deleteAll([
...flagIds.map(flagId => `flag:${flagId}`),
...flagIds.map(flagId => `flag:${flagId}:notes`),
...flagIds.map(flagId => `flag:${flagId}:reports`),
...flagIds.map(flagId => `flag:${flagId}:history`),
]),
db.sortedSetRemove(cids.map(cid => `flags:byCid:${cid}`), flagIds),
db.sortedSetRemove('flags:datetime', flagIds),
db.sortedSetRemove(
'flags:byTarget',
flagData.map(flagObj => [flagObj.type, flagObj.targetId].join(':'))
),
]);
};
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
Flags.getReports = async function (flagId) {
const payload = await db.getSortedSetRevRangeWithScores(`flag:${flagId}:reports`, 0, -1);
const [reports, uids] = payload.reduce((memo, cur) => {
const value = cur.value.split(';');
memo[1].push(value.shift());
cur.value = validator.escape(String(value.join(';')));
memo[0].push(cur);
return memo;
}, [[], []]);
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
await Promise.all(reports.map(async (report, idx) => {
report.timestamp = report.score;
report.timestampISO = new Date(report.score).toISOString();
delete report.score;
report.reporter = await user.getUserFields(uids[idx], ['username', 'userslug', 'picture', 'reputation']);
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
}));
return reports;
};
Flags.addReport = async function (flagId, type, id, uid, reason, timestamp) {
await db.sortedSetAddBulk([
[`flags:byReporter:${uid}`, timestamp, flagId],
[`flag:${flagId}:reports`, timestamp, [uid, reason].join(';')],
['flags:hash', flagId, [type, id, uid].join(':')],
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
]);
plugins.hooks.fire('action:flags.addReport', { flagId, type, id, uid, reason, timestamp });
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
};
Flags.exists = async function (type, id, uid) {
return await db.isSortedSetMember('flags:hash', [type, id, uid].join(':'));
8 years ago
};
Webpack5 (#10311) * feat: webpack 5 part 1 * fix: gruntfile fixes * fix: fix taskbar warning add app.importScript copy public/src/modules to build folder * refactor: remove commented old code * feat: reenable admin * fix: acp settings pages, fix sortable on manage categories embedded require in html not allowed * fix: bundle serialize/deserizeli so plugins dont break * test: fixe util tests * test: fix require path * test: more test fixes * test: require correct utils module * test: require correct utils * test: log stack * test: fix db require blowing up tests * test: move and disable bundle test * refactor: add aliases * test: disable testing route * fix: move webpack modules necessary for build, into `dependencies` * test: fix one more test remove 500-embed.tpl * fix: restore use of assets/nodebb.min.js, at least for now * fix: remove unnecessary line break * fix: point to proper ACP bundle * test: maybe fix build test * test: composer * refactor: dont need dist * refactor: more cleanup use everything from build/public folder * get rid of conditional import in app.js * fix: ace * refactor: cropper alias * test: lint and test fixes * lint: fix * refactor: rename function to app.require * refactor: go back to using app.require * chore: use github branch * chore: use webpack branch * feat: webpack webinstaller * feat: add chunkFile name with contenthash * refactor: move hooks to top * refactor: get rid of template500Function * fix(deps): use webpack5 branch of 2factor plugin * chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket: * refactor: disable cache on templates loadTemplate is called once by benchpress and the result is cache internally * refactor: add server side helpers.js * feat: deprecate /plugins shorthand route, closes #10343 * refactor: use build/public for webpack * test: fix filename * fix: more specific selector * lint: ignore * refactor: fix comments * test: add debug for random failing test * refactor: cleanup remove test page, remove dupe functions in utils.common * lint: use relative path for now * chore: bump prerelease version * feat: add translateKeys * fix: optional params * fix: get rid of extra timeago files * refactor: cleanup, require timeago locale earlier remove translator.prepareDOM, it is in header.tpl html tag * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * feat: allow app.require('bootbox'/'benchpressjs') * refactor: require server side utils * test: jquery ready * change istaller to use build/public * test: use document.addEventListener * refactor: closes #10301 * refactor: generateTopicClass * fix: column counts for other privileges * fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking] * fix: typo in hook name * refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags) * fix: crash if `delay` not passed in (as it cannot be destructured) * refactor: replace substr * feat: set --panel-offset style in html element based on stored value in localStorage * refactor: addDropupHandler() logic to be less naive - Take into account height of the menu - Don't apply dropUp logic if there's nothing in the dropdown - Remove 'hidden' class (added by default in Persona for post tools) when menu items are added closes #10423 * refactor: simplify utils.params [breaking] Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do. * feat: add support for returning full URLSearchParams for utils.params * fix: utils.params() fallback handling * fix: default empty obj for params() * fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end * fix: utils.params() not allowing relative paths to be passed in * refactor(DRY): new assertPasswordValidity utils method * fix: incorrect error message returned on insufficient privilege on flag edit * fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate - added failing tests and patched up middleware.assert.flags to fix * refactor: flag api v3 tests to create new post and flags on every round * fix: missing error:no-flag language key * refactor: flags.canView to check flag existence, simplify middleware.assert.flag * feat: flag deletion API endpoint, #10426 * feat: UI for flag deletion, closes #10426 * chore: update plugin versions * chore: up emoji * chore: update markdown * chore: up emoji-android * fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check Co-authored-by: Julian Lam <[email protected]>
3 years ago
Flags.canView = async (flagId, uid) => {
const exists = await db.isSortedSetMember('flags:datetime', flagId);
if (!exists) {
return false;
}
const [{ type, targetId }, isAdminOrGlobalMod] = await Promise.all([
db.getObject(`flag:${flagId}`),
user.isAdminOrGlobalMod(uid),
]);
if (type === 'post') {
const cid = await Flags.getTargetCid(type, targetId);
const isModerator = await user.isModerator(uid, cid);
return isAdminOrGlobalMod || isModerator;
}
return isAdminOrGlobalMod;
};
Flags.canFlag = async function (type, id, uid, skipLimitCheck = false) {
const limit = meta.config['flags:limitPerTarget'];
if (!skipLimitCheck && limit > 0) {
const score = await db.sortedSetScore('flags:byTarget', `${type}:${id}`);
if (score >= limit) {
throw new Error(`[[error:${type}-flagged-too-many-times]]`);
}
}
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]]');
}
};
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]]');
};
Flags.targetExists = async function (type, id) {
if (type === 'post') {
return await posts.exists(id);
} else if (type === 'user') {
return await user.exists(id);
8 years ago
}
throw new Error('[[error:invalid-data]]');
8 years ago
};
feat: consolidation of flags to reduce flagspam, #8510 Squashed commit of the following: commit c6d09396208a10c244d7b3d22ffd2d7dd1274d3a Author: Julian Lam <[email protected]> Date: Fri Jul 24 13:41:32 2020 -0400 fix: more tests commit 32f9af2a87a81fa62ecca01e71d6f0d5b9d37ba1 Merge: e50907535 4eae927d1 Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:53:04 2020 -0400 Merge remote-tracking branch 'origin/master' into singleton-flags commit e50907535109dbdbe8f15c3e2fcdf22d90b1332a Author: Julian Lam <[email protected]> Date: Fri Jul 24 10:52:46 2020 -0400 fix: controllers-admin test commit fd5af99e303de48a80b0ccc166eee19175cf232b Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:26:55 2020 -0400 fix(tests): dummy commit to trigger travisCI commit c452a6ffcfaef91403de084c4ae16795cb23c60e Author: Julian Lam <[email protected]> Date: Fri Jul 17 17:05:09 2020 -0400 fix(openapi): openapi spec changes commit 8089a74e89128141ab1e6f8ff83447114b3b846b Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:48:00 2020 -0400 fix: reversing the order of reports for display purposes commit a099892b377333561c72f1ad5b6b20ddb4ce8a96 Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:45:44 2020 -0400 refactor: run all flag creation calls in a single batch commit b24999682f9d5a33a08a049749c1f0eb4f00facc Author: Julian Lam <[email protected]> Date: Fri Jul 17 15:08:23 2020 -0400 feat: handling multiple reporters per flag, #8510 commit 08c75c020021ada754bf0e39eae77d631b01dee5 Author: Julian Lam <[email protected]> Date: Thu Jul 16 20:53:18 2020 -0400 feat: upgrade script for #8510
5 years ago
Flags.targetFlagged = async function (type, id) {
return await db.sortedSetScore('flags:byTarget', [type, id].join(':')) >= 1;
};
Flags.getTargetUid = async function (type, id) {
if (type === 'post') {
return await posts.getPostField(id, 'uid');
}
return id;
};
Flags.getTargetCid = async function (type, id) {
if (type === 'post') {
return await posts.getCidByPid(id);
}
return null;
};
Flags.update = async function (flagId, uid, changeset) {
const current = await db.getObjectFields(`flag:${flagId}`, ['uid', 'state', 'assignee', 'type', 'targetId']);
if (!current.type) {
return;
}
const now = changeset.datetime || Date.now();
const notifyAssignee = async function (assigneeId) {
if (assigneeId === '' || parseInt(uid, 10) === parseInt(assigneeId, 10)) {
return;
}
const notifObj = await notifications.create({
type: 'my-flags',
bodyShort: `[[notifications:flag_assigned_to_you, ${flagId}]]`,
bodyLong: '',
path: `/flags/${flagId}`,
nid: `flags:assign:${flagId}:uid:${assigneeId}`,
from: uid,
});
await notifications.push(notifObj, [assigneeId]);
};
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;
};
8 years ago
// Retrieve existing flag data to compare for history-saving/reference purposes
const tasks = [];
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])) {
delete changeset[prop];
} 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}`));
}
8 years ago
}
} else if (prop === 'assignee') {
if (changeset[prop] === '') {
tasks.push(db.sortedSetRemove(`flags:byAssignee:${changeset[prop]}`, flagId));
/* eslint-disable-next-line */
} else 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]));
}
}
}
8 years ago
if (!Object.keys(changeset).length) {
return;
}
tasks.push(db.setObject(`flag:${flagId}`, changeset));
tasks.push(Flags.appendHistory(flagId, uid, changeset));
await Promise.all(tasks);
plugins.hooks.fire('action:flags.update', { flagId: flagId, changeset: changeset, uid: uid });
};
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' });
}
8 years ago
};
Flags.resolveUserPostFlags = async function (uid, callerUid) {
if (meta.config['flags:autoResolveOnBan']) {
await batch.processSortedSet(`uid:${uid}:posts`, async (pids) => {
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' });
}
}
}, {
batch: 500,
});
}
};
Flags.getHistory = async function (flagId) {
const uids = [];
let history = await db.getSortedSetRevRangeWithScores(`flag:${flagId}:history`, 0, -1);
const targetUid = await db.getObjectField(`flag:${flagId}`, 'targetUid');
history = history.map((entry) => {
entry.value = JSON.parse(entry.value);
uids.push(entry.value[0]);
// Deserialise changeset
const changeset = entry.value[1];
if (changeset.hasOwnProperty('state')) {
changeset.state = changeset.state === undefined ? '' : `[[flags:state-${changeset.state}]]`;
}
return {
uid: entry.value[0],
fields: changeset,
datetime: entry.score,
datetimeISO: utils.toISOString(entry.score),
};
});
// Append ban history and username change data
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]; });
// Resort by date
history = history.sort((a, b) => b.datetime - a.datetime);
return history;
};
Flags.appendHistory = async function (flagId, uid, changeset) {
const datetime = changeset.datetime || Date.now();
delete changeset.datetime;
const payload = JSON.stringify([uid, changeset, datetime]);
await db.sortedSetAdd(`flag:${flagId}:history`, datetime, payload);
};
8 years ago
Flags.appendNote = async function (flagId, uid, note, datetime) {
if (datetime) {
try {
await Flags.deleteNote(flagId, datetime);
} catch (e) {
// Do not throw if note doesn't exist
if (!e.message === '[[error:invalid-data]]') {
throw e;
}
}
}
datetime = datetime || Date.now();
const payload = JSON.stringify([uid, note]);
await db.sortedSetAdd(`flag:${flagId}:notes`, datetime, payload);
await Flags.appendHistory(flagId, uid, {
notes: null,
datetime: datetime,
});
8 years ago
};
Flags.notify = async function (flagObj, uid, notifySelf = false) {
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;
const { displayname } = flagObj.reports[flagObj.reports.length - 1].reporter;
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, '&#37;').replace(/,/g, '&#44;');
notifObj = await notifications.create({
type: 'new-post-flag',
bodyShort: `[[notifications:user_flagged_post_in, ${displayname}, ${titleEscaped}]]`,
bodyLong: await plugins.hooks.fire('filter:parse.raw', String(flagObj.description || '')),
pid: flagObj.targetId,
path: `/flags/${flagObj.flagId}`,
nid: `flag:post:${flagObj.targetId}`,
from: uid,
mergeId: `notifications:user_flagged_post_in|${flagObj.targetId}`,
topicTitle: title,
});
uids = uids.concat(modUids[0]);
} else if (flagObj.type === 'user') {
notifObj = await notifications.create({
type: 'new-user-flag',
bodyShort: `[[notifications:user_flagged_user, ${displayname}, ${flagObj.target.user.displayname}]]`,
bodyLong: await plugins.hooks.fire('filter:parse.raw', String(flagObj.description || '')),
path: `/flags/${flagObj.flagId}`,
nid: `flag:user:${flagObj.targetId}`,
from: uid,
mergeId: `notifications:user_flagged_user|${flagObj.targetId}`,
});
} else {
throw new Error('[[error:invalid-data]]');
}
plugins.hooks.fire('action:flags.notify', {
flag: flagObj,
notification: notifObj,
from: uid,
to: uids,
});
if (!notifySelf) {
uids = uids.filter(_uid => parseInt(_uid, 10) !== parseInt(uid, 10));
}
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: validator.escape(String(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);