mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-14 07:27:01 +03:00
Refactor custom webhooks
This commit is contained in:
@@ -4,7 +4,9 @@ var config = require('../config.js');
|
||||
var getHash = require('../utils/getHash.js');
|
||||
var getIP = require('../utils/getIP.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 db = databases.db;
|
||||
@@ -217,22 +219,12 @@ async function voteOnSponsorTime(req, res) {
|
||||
id: submissionInfoRow.videoID
|
||||
}, function (err, data) {
|
||||
if (err || data.items.length === 0) {
|
||||
err && console.log(err);
|
||||
err && logger.error(err);
|
||||
return;
|
||||
}
|
||||
let isUpvote = incrementAmount > 0
|
||||
let isUpvote = incrementAmount > 0;
|
||||
// Send custom webhooks
|
||||
if (config.webhooks.size !== 0) {
|
||||
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: {
|
||||
dispatchWebhooks(isUpvote ? "vote.up" : "vote.down", {
|
||||
"user": {
|
||||
"status": userSubmissionCountRow.submissionCount === 0 ? "new" : (isVIP ? "vip" : "normal")
|
||||
},
|
||||
@@ -249,7 +241,7 @@ async function voteOnSponsorTime(req, res) {
|
||||
"startTime": submissionInfoRow.startTime,
|
||||
"endTime": submissionInfoRow.endTime,
|
||||
"user": {
|
||||
"uuid": submissionInfoRow.userID,
|
||||
"UUID": submissionInfoRow.userID,
|
||||
"username": submissionInfoRow.userName,
|
||||
"submissions": {
|
||||
"total": submissionInfoRow.count,
|
||||
@@ -261,14 +253,7 @@ async function voteOnSponsorTime(req, res) {
|
||||
"before": row.votes,
|
||||
"after": (row.votes + incrementAmount - oldIncrementAmount)
|
||||
}
|
||||
},
|
||||
headers: {
|
||||
"Authorization": key,
|
||||
"Event-Type": isUpvote ? "upvote" : "downvote"
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
// Send discord message
|
||||
if (webhookURL !== null && !isUpvote) {
|
||||
request.post(webhookURL, {
|
||||
|
||||
22
src/utils/dispatchWebhooks.js
Normal file
22
src/utils/dispatchWebhooks.js
Normal 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;
|
||||
Reference in New Issue
Block a user