diff --git a/public/src/modules/api.js b/public/src/modules/api.js index fcee6b94b0..249ef65c0a 100644 --- a/public/src/modules/api.js +++ b/public/src/modules/api.js @@ -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; @@ -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 * @@ -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, @@ -109,7 +110,6 @@ export function post(route, data, onSuccess) { }, }, onSuccess); } - export function patch(route, data, onSuccess) { return call({ url: route, @@ -120,7 +120,6 @@ export function patch(route, data, onSuccess) { }, }, onSuccess); } - export function put(route, data, onSuccess) { return call({ url: route, @@ -131,7 +130,6 @@ export function put(route, data, onSuccess) { }, }, onSuccess); } - export function del(route, data, onSuccess) { return call({ url: route, diff --git a/src/posts/votes.js b/src/posts/votes.js index bfe5e1e47f..13ca1daff0 100644 --- a/src/posts/votes.js +++ b/src/posts/votes.js @@ -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;