diff --git a/.eslintrc b/.eslintrc index abd292af1b..c1f471f32c 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,6 @@ { - "extends": "nodebb" + "extends": "nodebb", + "parserOptions": { + "ecmaVersion": 2020 // This change in configuration is suggested by Copilot + } } diff --git a/.mocharc.yml b/.mocharc.yml index 16d8518d1b..b9246aa609 100644 --- a/.mocharc.yml +++ b/.mocharc.yml @@ -1,4 +1,4 @@ reporter: dot timeout: 25000 exit: true -bail: true +bail: false diff --git a/src/flags.js b/src/flags.js index 00bce1d9bd..3786b2796a 100644 --- a/src/flags.js +++ b/src/flags.js @@ -273,6 +273,13 @@ Flags.sort = async function (flagIds, sort) { return flagIds; }; +function checkSelfFlagError(payload) { + console.log('CHEYU TU - checkSelfFlagError'); + if (parseInt(payload.id, 10) === parseInt(payload.uid, 10)) { + throw new Error('[[error:cant-flag-self]]'); + } +} + Flags.validate = async function (payload) { const [target, reporter] = await Promise.all([ Flags.getTarget(payload.type, payload.id, payload.uid), @@ -283,7 +290,7 @@ Flags.validate = async function (payload) { throw new Error('[[error:invalid-data]]'); } else if (target.deleted) { throw new Error('[[error:post-deleted]]'); - } else if (!reporter || !reporter.userslug) { + } else if (!reporter?.userslug) { throw new Error('[[error:no-user]]'); } else if (reporter.banned) { throw new Error('[[error:user-banned]]'); @@ -304,9 +311,7 @@ Flags.validate = async function (payload) { 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]]'); - } + checkSelfFlagError(payload); 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']}]]`); diff --git a/test/flags.js b/test/flags.js index ee150a10c4..6b772ceb73 100644 --- a/test/flags.js +++ b/test/flags.js @@ -754,6 +754,37 @@ describe('Flags', () => { done(); }); }); + + function validateUserFlag(done) { + Flags.validate({ + type: 'user', + id: 1, + uid: 3, + }, (err) => { + assert.ok(err); + assert.strictEqual('[[error:not-enough-reputation-to-flag, 50]]', err.message); + Meta.configs.set('min:rep:flag', 0, done); + }); + } + + it('should not pass validation if type is user, flag threshold is set and user rep does not meet it', (done) => { + Meta.configs.set('min:rep:flag', '50', (err) => { + assert.ifError(err); + validateUserFlag(done); + }); + }); + + it('should throw an error if user tries to flag themselves', (done) => { + Flags.validate({ + type: 'user', + id: 1, + uid: 1, + }, (err) => { + assert.ok(err); + assert.strictEqual('[[error:cant-flag-self]]', err.message); + done(); + }); + }); }); describe('.appendNote()', () => {