mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-14 15:37:07 +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 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,58 +219,41 @@ 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");
|
"user": {
|
||||||
config.webhooks.forEach(customWebhook => {
|
"status": userSubmissionCountRow.submissionCount === 0 ? "new" : (isVIP ? "vip" : "normal")
|
||||||
let customWebhookURL = customWebhook.url;
|
},
|
||||||
let scopes = customWebhook.scopes;
|
"video": {
|
||||||
let key = customWebhook.key;
|
"id": submissionInfoRow.videoID,
|
||||||
if ((!isUpvote && !scopes.includes("vote.down")) || (isUpvote && !scopes.includes("vote.up"))) {
|
"title": data.items[0].snippet.title,
|
||||||
return;
|
"url": "https://www.youtube.com/watch?v=" + submissionInfoRow.videoID,
|
||||||
}
|
"thumbnail": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : ""
|
||||||
request.post(customWebhookURL, {
|
},
|
||||||
json: {
|
"submission": {
|
||||||
"user": {
|
"id": UUID,
|
||||||
"status": userSubmissionCountRow.submissionCount === 0 ? "new" : (isVIP ? "vip" : "normal")
|
"views": row.views,
|
||||||
},
|
"category": category,
|
||||||
"video": {
|
"startTime": submissionInfoRow.startTime,
|
||||||
"id": submissionInfoRow.videoID,
|
"endTime": submissionInfoRow.endTime,
|
||||||
"title": data.items[0].snippet.title,
|
"user": {
|
||||||
"url": "https://www.youtube.com/watch?v=" + submissionInfoRow.videoID,
|
"UUID": submissionInfoRow.userID,
|
||||||
"thumbnail": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : ""
|
"username": submissionInfoRow.userName,
|
||||||
},
|
"submissions": {
|
||||||
"submission": {
|
"total": submissionInfoRow.count,
|
||||||
"id": UUID,
|
"ignored": submissionInfoRow.disregarded
|
||||||
"views": row.views,
|
|
||||||
"category": category,
|
|
||||||
"startTime": submissionInfoRow.startTime,
|
|
||||||
"endTime": submissionInfoRow.endTime,
|
|
||||||
"user": {
|
|
||||||
"uuid": submissionInfoRow.userID,
|
|
||||||
"username": submissionInfoRow.userName,
|
|
||||||
"submissions": {
|
|
||||||
"total": submissionInfoRow.count,
|
|
||||||
"ignored": submissionInfoRow.disregarded
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"votes": {
|
|
||||||
"before": row.votes,
|
|
||||||
"after": (row.votes + incrementAmount - oldIncrementAmount)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
headers: {
|
|
||||||
"Authorization": key,
|
|
||||||
"Event-Type": isUpvote ? "upvote" : "downvote"
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
});
|
},
|
||||||
}
|
"votes": {
|
||||||
|
"before": row.votes,
|
||||||
|
"after": (row.votes + incrementAmount - oldIncrementAmount)
|
||||||
|
}
|
||||||
|
});
|
||||||
// Send discord message
|
// Send discord message
|
||||||
if (webhookURL !== null && !isUpvote) {
|
if (webhookURL !== null && !isUpvote) {
|
||||||
request.post(webhookURL, {
|
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