Move dispatchEvent to webhookUtils

This commit is contained in:
TAG-Epic
2020-08-24 09:06:47 +02:00
parent 16777c30ca
commit aca4318351
3 changed files with 27 additions and 27 deletions

View File

@@ -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)
},

View File

@@ -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;

View File

@@ -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;
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
}