mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-14 07:27:01 +03:00
Merge pull request #118 from TAG-Epic/custom-webhooks
Add custom webhooks
This commit is contained in:
@@ -17,5 +17,6 @@
|
|||||||
"dbSchema": "./databases/_sponsorTimes.db.sql",
|
"dbSchema": "./databases/_sponsorTimes.db.sql",
|
||||||
"privateDBSchema": "./databases/_private.db.sql",
|
"privateDBSchema": "./databases/_private.db.sql",
|
||||||
"mode": "development",
|
"mode": "development",
|
||||||
"readOnly": false
|
"readOnly": false,
|
||||||
|
"webhooks": []
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,59 +11,89 @@ var isoDurations = require('iso8601-duration');
|
|||||||
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 { dispatchEvent } = require('../utils/webhookUtils.js');
|
||||||
|
|
||||||
function sendDiscordNotification(userID, videoID, UUID, segmentInfo) {
|
function sendWebhookNotification(userID, videoID, UUID, submissionCount, youtubeData, {submissionStart, submissionEnd}, segmentInfo) {
|
||||||
//check if they are a first time user
|
let row = db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]);
|
||||||
//if so, send a notification to discord
|
let userName = row !== undefined ? row.userName : null;
|
||||||
if (config.youtubeAPIKey !== null && config.discordFirstTimeSubmissionsWebhookURL !== null) {
|
let video = youtubeData.items[0];
|
||||||
|
|
||||||
|
let scopeName = "submissions.other";
|
||||||
|
if (submissionCount <= 1) {
|
||||||
|
scopeName = "submissions.new";
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatchEvent(scopeName, {
|
||||||
|
"video": {
|
||||||
|
"id": videoID,
|
||||||
|
"title": video.snippet.title,
|
||||||
|
"thumbnail": video.snippet.thumbnails.maxres ? video.snippet.thumbnails.maxres : null,
|
||||||
|
"url": "https://www.youtube.com/watch?v=" + videoID
|
||||||
|
},
|
||||||
|
"submission": {
|
||||||
|
"UUID": UUID,
|
||||||
|
"category": segmentInfo.category,
|
||||||
|
"startTime": submissionStart,
|
||||||
|
"endTime": submissionEnd,
|
||||||
|
"user": {
|
||||||
|
"UUID": userID,
|
||||||
|
"username": userName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendWebhooks(userID, videoID, UUID, segmentInfo) {
|
||||||
|
if (config.youtubeAPIKey !== null) {
|
||||||
let userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [userID]);
|
let userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [userID]);
|
||||||
|
|
||||||
// If it is a first time submission
|
YouTubeAPI.videos.list({
|
||||||
if (userSubmissionCountRow.submissionCount <= 1) {
|
part: "snippet",
|
||||||
YouTubeAPI.videos.list({
|
id: videoID
|
||||||
part: "snippet",
|
}, function (err, data) {
|
||||||
id: videoID
|
if (err || data.items.length === 0) {
|
||||||
}, function (err, data) {
|
err && logger.error(err);
|
||||||
if (err || data.items.length === 0) {
|
return;
|
||||||
err && logger.error(err);
|
}
|
||||||
return;
|
|
||||||
}
|
let startTime = parseFloat(segmentInfo.segment[0]);
|
||||||
|
let endTime = parseFloat(segmentInfo.segment[1]);
|
||||||
let startTime = parseFloat(segmentInfo.segment[0]);
|
sendWebhookNotification(userID, videoID, UUID, userSubmissionCountRow.submissionCount, data, {submissionStart: startTime, submissionEnd: endTime}, segmentInfo);
|
||||||
let endTime = parseFloat(segmentInfo.segment[1]);
|
|
||||||
|
// If it is a first time submission
|
||||||
request.post(config.discordFirstTimeSubmissionsWebhookURL, {
|
// Then send a notification to discord
|
||||||
json: {
|
if (config.discordFirstTimeSubmissionsWebhookURL === null) return;
|
||||||
"embeds": [{
|
request.post(config.discordFirstTimeSubmissionsWebhookURL, {
|
||||||
"title": data.items[0].snippet.title,
|
json: {
|
||||||
"url": "https://www.youtube.com/watch?v=" + videoID + "&t=" + (startTime.toFixed(0) - 2),
|
"embeds": [{
|
||||||
"description": "Submission ID: " + UUID +
|
"title": data.items[0].snippet.title,
|
||||||
"\n\nTimestamp: " +
|
"url": "https://www.youtube.com/watch?v=" + videoID + "&t=" + (startTime.toFixed(0) - 2),
|
||||||
getFormattedTime(startTime) + " to " + getFormattedTime(endTime) +
|
"description": "Submission ID: " + UUID +
|
||||||
"\n\nCategory: " + segmentInfo.category,
|
"\n\nTimestamp: " +
|
||||||
"color": 10813440,
|
getFormattedTime(startTime) + " to " + getFormattedTime(endTime) +
|
||||||
"author": {
|
"\n\nCategory: " + segmentInfo.category,
|
||||||
"name": userID
|
"color": 10813440,
|
||||||
},
|
"author": {
|
||||||
"thumbnail": {
|
"name": userID
|
||||||
"url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "",
|
},
|
||||||
}
|
"thumbnail": {
|
||||||
}]
|
"url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "",
|
||||||
}
|
|
||||||
}, (err, res) => {
|
|
||||||
if (err) {
|
|
||||||
logger.error("Failed to send first time submission Discord hook.");
|
|
||||||
logger.error(JSON.stringify(err));
|
|
||||||
logger.error("\n");
|
|
||||||
} else if (res && res.statusCode >= 400) {
|
|
||||||
logger.error("Error sending first time submission Discord hook");
|
|
||||||
logger.error(JSON.stringify(res));
|
|
||||||
logger.error("\n");
|
|
||||||
}
|
}
|
||||||
});
|
}]
|
||||||
|
}
|
||||||
|
}, (err, res) => {
|
||||||
|
if (err) {
|
||||||
|
logger.error("Failed to send first time submission Discord hook.");
|
||||||
|
logger.error(JSON.stringify(err));
|
||||||
|
logger.error("\n");
|
||||||
|
} else if (res && res.statusCode >= 400) {
|
||||||
|
logger.error("Error sending first time submission Discord hook");
|
||||||
|
logger.error(JSON.stringify(res));
|
||||||
|
logger.error("\n");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,6 +226,9 @@ module.exports = async function postSkipSegments(req, res) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Will be filled when submitting
|
||||||
|
let UUIDs = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//check if this user is on the vip list
|
//check if this user is on the vip list
|
||||||
let vipRow = db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID]);
|
let vipRow = db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID]);
|
||||||
@@ -271,8 +304,7 @@ module.exports = async function postSkipSegments(req, res) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Discord notification
|
UUIDs.push(UUID);
|
||||||
sendDiscordNotification(userID, videoID, UUID, segmentInfo);
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
@@ -283,4 +315,8 @@ module.exports = async function postSkipSegments(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
|
|
||||||
|
for (let i = 0; i < segments.length; i++) {
|
||||||
|
sendWebhooks(userID, videoID, UUIDs[i], segments[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ 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 {getVoteAuthor, getVoteAuthorRaw, dispatchEvent} = require('../utils/webhookUtils.js');
|
||||||
|
|
||||||
var databases = require('../databases/databases.js');
|
var databases = require('../databases/databases.js');
|
||||||
var db = databases.db;
|
var db = databases.db;
|
||||||
@@ -13,16 +14,124 @@ var YouTubeAPI = require('../utils/youtubeAPI.js');
|
|||||||
var request = require('request');
|
var request = require('request');
|
||||||
const logger = require('../utils/logger.js');
|
const logger = require('../utils/logger.js');
|
||||||
|
|
||||||
function getVoteAuthor(submissionCount, isVIP, isOwnSubmission) {
|
const voteTypes = {
|
||||||
if (submissionCount === 0) {
|
normal: 0,
|
||||||
return "Report by New User";
|
incorrect: 1
|
||||||
} else if (isVIP) {
|
}
|
||||||
return "Report by VIP User";
|
|
||||||
} else if (isOwnSubmission) {
|
|
||||||
return "Report by Submitter";
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
/**
|
||||||
|
* @param {Object} voteData
|
||||||
|
* @param {string} voteData.UUID
|
||||||
|
* @param {string} voteData.nonAnonUserID
|
||||||
|
* @param {number} voteData.voteTypeEnum
|
||||||
|
* @param {boolean} voteData.isVIP
|
||||||
|
* @param {boolean} voteData.isOwnSubmission
|
||||||
|
* @param voteData.row
|
||||||
|
* @param {string} voteData.category
|
||||||
|
* @param {number} voteData.incrementAmount
|
||||||
|
* @param {number} voteData.oldIncrementAmount
|
||||||
|
*/
|
||||||
|
function sendWebhooks(voteData) {
|
||||||
|
let submissionInfoRow = db.prepare('get', "SELECT s.videoID, s.userID, s.startTime, s.endTime, s.category, u.userName, " +
|
||||||
|
"(select count(1) from sponsorTimes where userID = s.userID) count, " +
|
||||||
|
"(select count(1) from sponsorTimes where userID = s.userID and votes <= -2) disregarded " +
|
||||||
|
"FROM sponsorTimes s left join userNames u on s.userID = u.userID where s.UUID=?",
|
||||||
|
[voteData.UUID]);
|
||||||
|
|
||||||
|
let userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [voteData.nonAnonUserID]);
|
||||||
|
|
||||||
|
if (submissionInfoRow !== undefined && userSubmissionCountRow != undefined) {
|
||||||
|
let webhookURL = null;
|
||||||
|
if (voteData.voteTypeEnum === voteTypes.normal) {
|
||||||
|
webhookURL = config.discordReportChannelWebhookURL;
|
||||||
|
} else if (voteData.voteTypeEnum === voteTypes.incorrect) {
|
||||||
|
webhookURL = config.discordCompletelyIncorrectReportWebhookURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.youtubeAPIKey !== null) {
|
||||||
|
YouTubeAPI.videos.list({
|
||||||
|
part: "snippet",
|
||||||
|
id: submissionInfoRow.videoID
|
||||||
|
}, function (err, data) {
|
||||||
|
if (err || data.items.length === 0) {
|
||||||
|
err && logger.error(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let isUpvote = voteData.incrementAmount > 0;
|
||||||
|
// Send custom webhooks
|
||||||
|
dispatchEvent(isUpvote ? "vote.up" : "vote.down", {
|
||||||
|
"user": {
|
||||||
|
"status": getVoteAuthorRaw(userSubmissionCountRow.submissionCount, voteData.isVIP, voteData.isOwnSubmission)
|
||||||
|
},
|
||||||
|
"video": {
|
||||||
|
"id": submissionInfoRow.videoID,
|
||||||
|
"title": data.items[0].snippet.title,
|
||||||
|
"url": "https://www.youtube.com/watch?v=" + submissionInfoRow.videoID,
|
||||||
|
"thumbnail": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : ""
|
||||||
|
},
|
||||||
|
"submission": {
|
||||||
|
"UUID": voteData.UUID,
|
||||||
|
"views": voteData.row.views,
|
||||||
|
"category": voteData.category,
|
||||||
|
"startTime": submissionInfoRow.startTime,
|
||||||
|
"endTime": submissionInfoRow.endTime,
|
||||||
|
"user": {
|
||||||
|
"UUID": submissionInfoRow.userID,
|
||||||
|
"username": submissionInfoRow.userName,
|
||||||
|
"submissions": {
|
||||||
|
"total": submissionInfoRow.count,
|
||||||
|
"ignored": submissionInfoRow.disregarded
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"votes": {
|
||||||
|
"before": voteData.row.votes,
|
||||||
|
"after": (voteData.row.votes + voteData.incrementAmount - voteData.oldIncrementAmount)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Send discord message
|
||||||
|
if (webhookURL !== null && !isUpvote) {
|
||||||
|
request.post(webhookURL, {
|
||||||
|
json: {
|
||||||
|
"embeds": [{
|
||||||
|
"title": data.items[0].snippet.title,
|
||||||
|
"url": "https://www.youtube.com/watch?v=" + submissionInfoRow.videoID
|
||||||
|
+ "&t=" + (submissionInfoRow.startTime.toFixed(0) - 2),
|
||||||
|
"description": "**" + voteData.row.votes + " Votes Prior | " +
|
||||||
|
(voteData.row.votes + voteData.incrementAmount - voteData.oldIncrementAmount) + " Votes Now | " + voteData.row.views
|
||||||
|
+ " Views**\n\n**Submission ID:** " + voteData.UUID
|
||||||
|
+ "\n**Category:** " + submissionInfoRow.category
|
||||||
|
+ "\n\n**Submitted by:** "+submissionInfoRow.userName+"\n " + submissionInfoRow.userID
|
||||||
|
+ "\n\n**Total User Submissions:** "+submissionInfoRow.count
|
||||||
|
+ "\n**Ignored User Submissions:** "+submissionInfoRow.disregarded
|
||||||
|
+"\n\n**Timestamp:** " +
|
||||||
|
getFormattedTime(submissionInfoRow.startTime) + " to " + getFormattedTime(submissionInfoRow.endTime),
|
||||||
|
"color": 10813440,
|
||||||
|
"author": {
|
||||||
|
"name": getVoteAuthor(userSubmissionCountRow.submissionCount, voteData.isVIP, voteData.isOwnSubmission)
|
||||||
|
},
|
||||||
|
"thumbnail": {
|
||||||
|
"url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "",
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}, (err, res) => {
|
||||||
|
if (err) {
|
||||||
|
logger.error("Failed to send reported submission Discord hook.");
|
||||||
|
logger.error(JSON.stringify(err));
|
||||||
|
logger.error("\n");
|
||||||
|
} else if (res && res.statusCode >= 400) {
|
||||||
|
logger.error("Error sending reported submission Discord hook");
|
||||||
|
logger.error(JSON.stringify(res));
|
||||||
|
logger.error("\n");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function categoryVote(UUID, userID, isVIP, category, hashedIP, res) {
|
function categoryVote(UUID, userID, isVIP, category, hashedIP, res) {
|
||||||
@@ -126,11 +235,6 @@ async function voteOnSponsorTime(req, res) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let voteTypes = {
|
|
||||||
normal: 0,
|
|
||||||
incorrect: 1
|
|
||||||
}
|
|
||||||
|
|
||||||
let voteTypeEnum = (type == 0 || type == 1) ? voteTypes.normal : voteTypes.incorrect;
|
let voteTypeEnum = (type == 0 || type == 1) ? voteTypes.normal : voteTypes.incorrect;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -194,74 +298,6 @@ async function voteOnSponsorTime(req, res) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send discord message
|
|
||||||
if (incrementAmount < 0) {
|
|
||||||
// Get video ID
|
|
||||||
let submissionInfoRow = db.prepare('get', "SELECT s.videoID, s.userID, s.startTime, s.endTime, s.category, u.userName, " +
|
|
||||||
"(select count(1) from sponsorTimes where userID = s.userID) count, " +
|
|
||||||
"(select count(1) from sponsorTimes where userID = s.userID and votes <= -2) disregarded " +
|
|
||||||
"FROM sponsorTimes s left join userNames u on s.userID = u.userID where s.UUID=?",
|
|
||||||
[UUID]);
|
|
||||||
|
|
||||||
let userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [nonAnonUserID]);
|
|
||||||
|
|
||||||
if (submissionInfoRow !== undefined && userSubmissionCountRow != undefined) {
|
|
||||||
let webhookURL = null;
|
|
||||||
if (voteTypeEnum === voteTypes.normal) {
|
|
||||||
webhookURL = config.discordReportChannelWebhookURL;
|
|
||||||
} else if (voteTypeEnum === voteTypes.incorrect) {
|
|
||||||
webhookURL = config.discordCompletelyIncorrectReportWebhookURL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.youtubeAPIKey !== null && webhookURL !== null) {
|
|
||||||
YouTubeAPI.videos.list({
|
|
||||||
part: "snippet",
|
|
||||||
id: submissionInfoRow.videoID
|
|
||||||
}, function (err, data) {
|
|
||||||
if (err || data.items.length === 0) {
|
|
||||||
err && logger.error(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
request.post(webhookURL, {
|
|
||||||
json: {
|
|
||||||
"embeds": [{
|
|
||||||
"title": data.items[0].snippet.title,
|
|
||||||
"url": "https://www.youtube.com/watch?v=" + submissionInfoRow.videoID
|
|
||||||
+ "&t=" + (submissionInfoRow.startTime.toFixed(0) - 2),
|
|
||||||
"description": "**" + row.votes + " Votes Prior | " + (row.votes + incrementAmount - oldIncrementAmount) + " Votes Now | " + row.views
|
|
||||||
+ " Views**\n\n**Submission ID:** " + UUID
|
|
||||||
+ "\n**Category:** " + submissionInfoRow.category
|
|
||||||
+ "\n\n**Submitted by:** "+submissionInfoRow.userName+"\n " + submissionInfoRow.userID
|
|
||||||
+ "\n\n**Total User Submissions:** "+submissionInfoRow.count
|
|
||||||
+ "\n**Ignored User Submissions:** "+submissionInfoRow.disregarded
|
|
||||||
+"\n\n**Timestamp:** " +
|
|
||||||
getFormattedTime(submissionInfoRow.startTime) + " to " + getFormattedTime(submissionInfoRow.endTime),
|
|
||||||
"color": 10813440,
|
|
||||||
"author": {
|
|
||||||
"name": getVoteAuthor(userSubmissionCountRow.submissionCount, isVIP, isOwnSubmission)
|
|
||||||
},
|
|
||||||
"thumbnail": {
|
|
||||||
"url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "",
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
}, (err, res) => {
|
|
||||||
if (err) {
|
|
||||||
logger.error("Failed to send reported submission Discord hook.");
|
|
||||||
logger.error(JSON.stringify(err));
|
|
||||||
logger.error("\n");
|
|
||||||
} else if (res && res.statusCode >= 400) {
|
|
||||||
logger.error("Error sending reported submission Discord hook");
|
|
||||||
logger.error(JSON.stringify(res));
|
|
||||||
logger.error("\n");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only change the database if they have made a submission before and haven't voted recently
|
// Only change the database if they have made a submission before and haven't voted recently
|
||||||
let ableToVote = isVIP
|
let ableToVote = isVIP
|
||||||
|| (db.prepare("get", "SELECT userID FROM sponsorTimes WHERE userID = ?", [nonAnonUserID]) !== undefined
|
|| (db.prepare("get", "SELECT userID FROM sponsorTimes WHERE userID = ?", [nonAnonUserID]) !== undefined
|
||||||
@@ -314,6 +350,18 @@ async function voteOnSponsorTime(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
|
|
||||||
|
sendWebhooks({
|
||||||
|
UUID,
|
||||||
|
nonAnonUserID,
|
||||||
|
voteTypeEnum,
|
||||||
|
isVIP,
|
||||||
|
isOwnSubmission,
|
||||||
|
row,
|
||||||
|
category,
|
||||||
|
incrementAmount,
|
||||||
|
oldIncrementAmount
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ const settings = {
|
|||||||
if (config.mode === 'development') {
|
if (config.mode === 'development') {
|
||||||
settings.INFO = true;
|
settings.INFO = true;
|
||||||
settings.DEBUG = true;
|
settings.DEBUG = true;
|
||||||
|
} else if (config.mode === 'test') {
|
||||||
|
settings.WARN = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function log(level, string) {
|
function log(level, string) {
|
||||||
|
|||||||
52
src/utils/webhookUtils.js
Normal file
52
src/utils/webhookUtils.js
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
const config = require('../config.js');
|
||||||
|
const logger = require('../utils/logger.js');
|
||||||
|
const request = require('request');
|
||||||
|
|
||||||
|
function getVoteAuthorRaw(submissionCount, isVIP, isOwnSubmission) {
|
||||||
|
if (isOwnSubmission) {
|
||||||
|
return "self";
|
||||||
|
} else if (isVIP) {
|
||||||
|
return "vip";
|
||||||
|
} else if (submissionCount === 0) {
|
||||||
|
return "new";
|
||||||
|
} else {
|
||||||
|
return "other";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
function getVoteAuthor(submissionCount, isVIP, isOwnSubmission) {
|
||||||
|
if (submissionCount === 0) {
|
||||||
|
return "Report by New User";
|
||||||
|
} else if (isVIP) {
|
||||||
|
return "Report by VIP User";
|
||||||
|
} else if (isOwnSubmission) {
|
||||||
|
return "Report by Submitter";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
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?
|
||||||
|
}}).on('error', (e) => {
|
||||||
|
logger.warn('Couldn\'t send webhook to ' + webhook.url);
|
||||||
|
logger.warn(e);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getVoteAuthorRaw,
|
||||||
|
getVoteAuthor,
|
||||||
|
dispatchEvent
|
||||||
|
}
|
||||||
33
test.json
33
test.json
@@ -15,5 +15,36 @@
|
|||||||
"dbSchema": "./databases/_sponsorTimes.db.sql",
|
"dbSchema": "./databases/_sponsorTimes.db.sql",
|
||||||
"privateDBSchema": "./databases/_private.db.sql",
|
"privateDBSchema": "./databases/_private.db.sql",
|
||||||
"mode": "test",
|
"mode": "test",
|
||||||
"readOnly": false
|
"readOnly": false,
|
||||||
|
"webhooks": [
|
||||||
|
{
|
||||||
|
"url": "http://127.0.0.1:8081/CustomWebhook",
|
||||||
|
"key": "superSecretKey",
|
||||||
|
"scopes": [
|
||||||
|
"vote.up",
|
||||||
|
"vote.down"
|
||||||
|
]
|
||||||
|
}, {
|
||||||
|
"url": "http://127.0.0.1:8081/FailedWebhook",
|
||||||
|
"key": "superSecretKey",
|
||||||
|
"scopes": [
|
||||||
|
"vote.up",
|
||||||
|
"vote.down"
|
||||||
|
]
|
||||||
|
}, {
|
||||||
|
"url": "http://127.0.0.1:8099/WrongPort",
|
||||||
|
"key": "superSecretKey",
|
||||||
|
"scopes": [
|
||||||
|
"vote.up",
|
||||||
|
"vote.down"
|
||||||
|
]
|
||||||
|
}, {
|
||||||
|
"url": "http://unresolvable.host:8081/FailedWebhook",
|
||||||
|
"key": "superSecretKey",
|
||||||
|
"scopes": [
|
||||||
|
"vote.up",
|
||||||
|
"vote.down"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,12 @@ app.post('/CompletelyIncorrectReportWebhook', (req, res) => {
|
|||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
app.post('/CustomWebhook', (req, res) => {
|
||||||
|
res.sendStatus(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
module.exports = function createMockServer(callback) {
|
module.exports = function createMockServer(callback) {
|
||||||
return app.listen(config.mockPort, callback);
|
return app.listen(config.mockPort, callback);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user