migrate to typescript

This commit is contained in:
Dainius Daukševičius
2020-10-17 21:56:54 +03:00
committed by Dainius Dauksevicius
parent c462323dd5
commit 08d27265fc
120 changed files with 5002 additions and 4711 deletions

56
src/utils/webhookUtils.ts Normal file
View File

@@ -0,0 +1,56 @@
import {config} from '../config';
import {Logger} from '../utils/logger';
import request from 'request';
function getVoteAuthorRaw(submissionCount: number, isVIP: boolean, isOwnSubmission: boolean): string {
if (isOwnSubmission) {
return "self";
} else if (isVIP) {
return "vip";
} else if (submissionCount === 0) {
return "new";
} else {
return "other";
}
}
function getVoteAuthor(submissionCount: number, isVIP: boolean, isOwnSubmission: boolean): string {
if (submissionCount === 0) {
return "Report by New User";
} else if (isOwnSubmission) {
return "Report by Submitter";
} else if (isVIP) {
return "Report by VIP User";
}
return "";
}
function dispatchEvent(scope: string, data: any): void {
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;
// TODO TYPESCRIPT deprecated
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.message);
});
});
}
export {
getVoteAuthorRaw,
getVoteAuthor,
dispatchEvent,
};