Refactor custom webhooks

This commit is contained in:
TAG-Epic
2020-08-23 21:44:50 +02:00
parent a268f9a892
commit e5aa631da0
2 changed files with 56 additions and 49 deletions

View File

@@ -4,7 +4,9 @@ var config = require('../config.js');
var getHash = require('../utils/getHash.js'); var getHash = require('../utils/getHash.js');
var getIP = require('../utils/getIP.js'); var getIP = require('../utils/getIP.js');
var getFormattedTime = require('../utils/getFormattedTime.js'); var getFormattedTime = require('../utils/getFormattedTime.js');
var isUserTrustworthy = require('../utils/isUserTrustworthy.js') var isUserTrustworthy = require('../utils/isUserTrustworthy.js');
const dispatchWebhooks = require('../utils/dispatchWebhooks.js')
var databases = require('../databases/databases.js'); var databases = require('../databases/databases.js');
var db = databases.db; var db = databases.db;
@@ -217,22 +219,12 @@ async function voteOnSponsorTime(req, res) {
id: submissionInfoRow.videoID id: submissionInfoRow.videoID
}, function (err, data) { }, function (err, data) {
if (err || data.items.length === 0) { if (err || data.items.length === 0) {
err && console.log(err); err && logger.error(err);
return; return;
} }
let isUpvote = incrementAmount > 0 let isUpvote = incrementAmount > 0;
// Send custom webhooks // Send custom webhooks
if (config.webhooks.size !== 0) { dispatchWebhooks(isUpvote ? "vote.up" : "vote.down", {
logger.debug("Dispatching webhooks");
config.webhooks.forEach(customWebhook => {
let customWebhookURL = customWebhook.url;
let scopes = customWebhook.scopes;
let key = customWebhook.key;
if ((!isUpvote && !scopes.includes("vote.down")) || (isUpvote && !scopes.includes("vote.up"))) {
return;
}
request.post(customWebhookURL, {
json: {
"user": { "user": {
"status": userSubmissionCountRow.submissionCount === 0 ? "new" : (isVIP ? "vip" : "normal") "status": userSubmissionCountRow.submissionCount === 0 ? "new" : (isVIP ? "vip" : "normal")
}, },
@@ -249,7 +241,7 @@ async function voteOnSponsorTime(req, res) {
"startTime": submissionInfoRow.startTime, "startTime": submissionInfoRow.startTime,
"endTime": submissionInfoRow.endTime, "endTime": submissionInfoRow.endTime,
"user": { "user": {
"uuid": submissionInfoRow.userID, "UUID": submissionInfoRow.userID,
"username": submissionInfoRow.userName, "username": submissionInfoRow.userName,
"submissions": { "submissions": {
"total": submissionInfoRow.count, "total": submissionInfoRow.count,
@@ -261,14 +253,7 @@ async function voteOnSponsorTime(req, res) {
"before": row.votes, "before": row.votes,
"after": (row.votes + incrementAmount - oldIncrementAmount) "after": (row.votes + incrementAmount - oldIncrementAmount)
} }
},
headers: {
"Authorization": key,
"Event-Type": isUpvote ? "upvote" : "downvote"
}
}); });
});
}
// Send discord message // Send discord message
if (webhookURL !== null && !isUpvote) { if (webhookURL !== null && !isUpvote) {
request.post(webhookURL, { request.post(webhookURL, {

View File

@@ -0,0 +1,22 @@
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.size === 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;