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

refactored src/posts/votes.js #162

Open
wants to merge 6 commits into
base: main
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
44 changes: 21 additions & 23 deletions public/src/modules/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ import { fire as fireHook } from 'hooks';
import { confirm } from 'bootbox';

const baseUrl = config.relative_path + '/api/v3';

async function call(options, callback) {
options.url = options.url.startsWith('/api') ?
config.relative_path + options.url :
baseUrl + options.url;

if (typeof callback === 'function') {
xhr(options).then(result => callback(null, result), err => callback(err));
return;
}

try {
const result = await xhr(options);
return result;
Expand All @@ -33,19 +30,28 @@ async function call(options, callback) {
}
}

async function parseResponse(res, isJson) {
return isJson ? await res.json : await res.text();
}

function handleErrorResponse(res, response, isJSON) {
if (response) {
throw new Error(isJSON ? response.status.message : response);
}
throw new Error(res.statusText);
}
async function xhr(options) {
// Normalize body based on type
console.log('Test Log: Code execution reached here - Evelyn Lo');

const { url } = options;
delete options.url;

if (options.data && !(options.data instanceof FormData)) {
options.data = JSON.stringify(options.data || {});
options.headers['content-type'] = 'application/json; charset=utf-8';
}

// Allow options to be modified by plugins, etc.
({ options } = await fireHook('filter:api.options', { options }));

/**
* Note: pre-v4 backwards compatibility
*
Expand All @@ -59,46 +65,41 @@ async function xhr(options) {
options.body = options.data;
delete options.data;
}

const res = await fetch(url, options);
const { headers } = res;
const contentType = headers.get('content-type');
const isJSON = contentType && contentType.startsWith('application/json');

let response;
if (options.method !== 'HEAD') {
if (isJSON) {
response = await res.json();
} else {
response = await res.text();
}
response = await parseResponse(res, isJSON);
}
// if (!res.ok) {
// if (response) {
// throw new Error(isJSON ? response.status.message : response);
// }
// throw new Error(res.statusText);
// }

if (!res.ok) {
if (response) {
throw new Error(isJSON ? response.status.message : response);
}
throw new Error(res.statusText);
handleErrorResponse(res, response, isJSON);
}

return isJSON && response && response.hasOwnProperty('status') && response.hasOwnProperty('response') ?
response.response :
response;
}


export function get(route, data, onSuccess) {
return call({
url: route + (data && Object.keys(data).length ? ('?' + $.param(data)) : ''),
}, onSuccess);
}

export function head(route, data, onSuccess) {
return call({
url: route + (data && Object.keys(data).length ? ('?' + $.param(data)) : ''),
method: 'HEAD',
}, onSuccess);
}

export function post(route, data, onSuccess) {
return call({
url: route,
Expand All @@ -109,7 +110,6 @@ export function post(route, data, onSuccess) {
},
}, onSuccess);
}

export function patch(route, data, onSuccess) {
return call({
url: route,
Expand All @@ -120,7 +120,6 @@ export function patch(route, data, onSuccess) {
},
}, onSuccess);
}

export function put(route, data, onSuccess) {
return call({
url: route,
Expand All @@ -131,7 +130,6 @@ export function put(route, data, onSuccess) {
},
}, onSuccess);
}

export function del(route, data, onSuccess) {
return call({
url: route,
Expand Down
23 changes: 21 additions & 2 deletions src/posts/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,33 @@ module.exports = function (Posts) {
if (isPrivileged) {
return;
}
if (reputation < meta.config[`min:rep:${type}`]) {
throw new Error(`[[error:not-enough-reputation-to-${type}, ${meta.config[`min:rep:${type}`]}]]`);

console.log('Reputation:', reputation);
const minReputationRequired = meta.config[`min:rep:${type}`];
console.log('Min Reputation Required:', minReputationRequired);
const errorMessage = `[[error:not-enough-reputation-to-${type}, ${minReputationRequired}]]`;
console.log('Error Message:', errorMessage);

if (reputation < minReputationRequired) {
console.log('here');
throw new Error(errorMessage);
}
console.log('**Evelyn**');
console.log('Voted PIDs Today:', votedPidsToday);
console.log('Target UID:', targetUid);
/*
if (reputation < meta.config[`min:rep:${type}`]) {
throw new Error(`[[error:not-enough-reputation-to-${type}, ${meta.config[`min:rep:${type}`]}]]`);
} */
const votesToday = meta.config[`${type}sPerDay`];
console.log('Votes Today Limit:', votesToday);

if (votesToday && votedPidsToday.length >= votesToday) {
throw new Error(`[[error:too-many-${type}s-today, ${votesToday}]]`);
}
const voterPerUserToday = meta.config[`${type}sPerUserPerDay`];
console.log('Voter Per User Today:', voterPerUserToday);

if (voterPerUserToday) {
const postData = await Posts.getPostsFields(votedPidsToday, ['uid']);
const targetUpVotes = postData.filter(p => p.uid === targetUid).length;
Expand Down
Loading