Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor Flags.validate in src/flags.js #29

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "nodebb"
"extends": "nodebb",
"parserOptions": {
"ecmaVersion": 2020 // This change in configuration is suggested by Copilot
}
}
2 changes: 1 addition & 1 deletion .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
reporter: dot
timeout: 25000
exit: true
bail: true
bail: false
13 changes: 9 additions & 4 deletions src/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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]]');
Expand All @@ -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']}]]`);
Expand Down
31 changes: 31 additions & 0 deletions test/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down