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

src/flags.js (Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed.) #60

Open
wants to merge 8 commits into
base: f24
Choose a base branch
from
Open
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
25 changes: 15 additions & 10 deletions src/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,49 +273,54 @@ Flags.sort = async function (flagIds, sort) {
return flagIds;
};

// Chatgpt Assisted Code
Flags.validate = async function (payload) {
console.log('Salman Al-Saigh');
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) {
}
if (target.deleted) {
throw new Error('[[error:post-deleted]]');
} else if (!reporter || !reporter.userslug) {
}
if (!reporter || !reporter.userslug) {
throw new Error('[[error:no-user]]');
} else if (reporter.banned) {
}
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') {
return;
}
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]]');
return;
}
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);
Expand Down
20 changes: 20 additions & 0 deletions test/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,26 @@ describe('Flags', () => {
done();
});
});

// ChatGPT Assisted from below this comment

it('should error out if a 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();
});
});





// Above is chatgpt assisted code above
});

describe('.appendNote()', () => {
Expand Down