diff --git a/src/routes/voteOnSponsorTime.js b/src/routes/voteOnSponsorTime.js index 9d9b458..458c914 100644 --- a/src/routes/voteOnSponsorTime.js +++ b/src/routes/voteOnSponsorTime.js @@ -5,8 +5,7 @@ var getHash = require('../utils/getHash.js'); var getIP = require('../utils/getIP.js'); var getFormattedTime = require('../utils/getFormattedTime.js'); var isUserTrustworthy = require('../utils/isUserTrustworthy.js'); -const dispatchWebhooks = require('../utils/dispatchWebhooks.js'); -const {getVoteAuthor, getVoteAuthorRaw} = require('../utils/webhookUtils.js'); +const {getVoteAuthor, getVoteAuthorRaw, dispatchEvent} = require('../utils/webhookUtils.js'); var databases = require('../databases/databases.js'); @@ -214,7 +213,7 @@ async function voteOnSponsorTime(req, res) { } let isUpvote = incrementAmount > 0; // Send custom webhooks - dispatchWebhooks(isUpvote ? "vote.up" : "vote.down", { + dispatchEvent(isUpvote ? "vote.up" : "vote.down", { "user": { "status": getVoteAuthorRaw(userSubmissionCountRow.submissionCount, isVIP, isOwnSubmission) }, diff --git a/src/utils/dispatchWebhooks.js b/src/utils/dispatchWebhooks.js deleted file mode 100644 index 625cbd7..0000000 --- a/src/utils/dispatchWebhooks.js +++ /dev/null @@ -1,22 +0,0 @@ -const config = require("../config.js"); -const logger = require('../utils/logger.js'); -const request = require('request'); - - -function dispatchEvent(scope, data) { - let webhooks = config.webhooks; - if (webhooks === undefined || webhooks.length === 0) return; - logger.debug("Dispatching webhooks"); - webhooks.forEach(webhook => { - let webhookURL = webhook.url; - let authKey = webhook.key; - let scopes = webhook.scopes || []; - if (!scopes.includes(scope.toLowerCase())) return; - request.post(webhookURL, {json: data, headers: { - "Authorization": authKey, - "Event-Type": scope // Maybe change this in the future? - }}); - }); -} - -module.exports = dispatchEvent; \ No newline at end of file diff --git a/src/utils/webhookUtils.js b/src/utils/webhookUtils.js index 34f8651..17383bf 100644 --- a/src/utils/webhookUtils.js +++ b/src/utils/webhookUtils.js @@ -1,3 +1,7 @@ +const config = require('../config.js'); +const logger = require('../utils/logger.js'); +const request = require('request'); + function getVoteAuthorRaw(submissionCount, isVIP, isOwnSubmission) { if (isOwnSubmission) { return "self"; @@ -22,5 +26,24 @@ function getVoteAuthor(submissionCount, isVIP, isOwnSubmission) { return ""; } -module.exports.getVoteAuthorRaw = getVoteAuthorRaw; -module.exports.getVoteAuthor = getVoteAuthor; \ No newline at end of file +function dispatchEvent(scope, data) { + let webhooks = config.webhooks; + if (webhooks === undefined || webhooks.length === 0) return; + logger.debug("Dispatching webhooks"); + webhooks.forEach(webhook => { + let webhookURL = webhook.url; + let authKey = webhook.key; + let scopes = webhook.scopes || []; + if (!scopes.includes(scope.toLowerCase())) return; + request.post(webhookURL, {json: data, headers: { + "Authorization": authKey, + "Event-Type": scope // Maybe change this in the future? + }}); + }); +} + +module.exports = { + getVoteAuthorRaw, + getVoteAuthor, + dispatchEvent +} \ No newline at end of file