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

Refactoring code in src/flags.js #478

Closed
wants to merge 4 commits into from
Closed
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
Binary file added dump.rdb
Binary file not shown.
16 changes: 13 additions & 3 deletions src/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,17 @@ Flags.validate = async function (payload) {

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]]');
}

Expand All @@ -294,20 +300,24 @@ Flags.validate = async function (payload) {
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']}]]`);
}
Expand Down