@ -319,16 +319,32 @@ function sendSlackNotification({body, title, type, url, thumbnail}) {
// Generic
function isLikelyAppriseWebhookURL ( webhook _url ) {
try {
const url = new URL ( webhook _url ) ;
const path = url . pathname . toLowerCase ( ) ;
const host = url . hostname . toLowerCase ( ) ;
function parseWebhookURL ( webhook _url ) {
if ( typeof webhook _url !== 'string' ) return null ;
const trimmed _url = webhook _url . trim ( ) ;
if ( ! trimmed _url ) return null ;
let request _url = trimmed _url ;
let explicit _apprise = false ;
const apprises _match = trimmed _url . match ( /^apprises:\/\/(.+)$/i ) ;
const apprise _match = trimmed _url . match ( /^apprise:\/\/(.+)$/i ) ;
if ( apprises _match ) {
request _url = ` https:// ${ apprises _match [ 1 ] } ` ;
explicit _apprise = true ;
} else if ( apprise _match ) {
request _url = ` http:// ${ apprise _match [ 1 ] } ` ;
explicit _apprise = true ;
}
// Apprise commonly exposes /notify[/<key>], while reverse proxies often include /apprise.
return /(^|\/)notify(\/[\w-]{1,128})?\/?$/ . test ( path ) || /(^|\/)apprise(\/|$)/ . test ( path ) || host . includes ( 'apprise' ) ;
try {
return {
request _url : request _url ,
parsed _url : new URL ( request _url ) ,
explicit _apprise : explicit _apprise
} ;
} catch {
return false ;
return null ;
}
}
@ -336,9 +352,21 @@ function mapNotificationTypeToAppriseType(type) {
return NOTIFICATION _TYPE _TO _APPRISE _TYPE [ type ] ? NOTIFICATION _TYPE _TO _APPRISE _TYPE [ type ] : 'info' ;
}
function getWebhookPayload ( webhook _url , data ) {
if ( ! isLikelyAppriseWebhookURL ( webhook _url ) ) return data ;
return {
function isLikelyAppriseWebhookURL ( webhook _info ) {
if ( ! webhook _info || ! webhook _info [ 'parsed_url' ] ) return false ;
if ( webhook _info [ 'explicit_apprise' ] ) return true ;
const path = webhook _info [ 'parsed_url' ] . pathname . toLowerCase ( ) ;
const host = webhook _info [ 'parsed_url' ] . hostname . toLowerCase ( ) ;
// Apprise commonly exposes /notify[/<key>], while reverse proxies often include /apprise.
return /(^|\/)notify(\/[\w-]{1,128})?\/?$/ . test ( path ) || /(^|\/)apprise(\/|$)/ . test ( path ) || host . includes ( 'apprise' ) ;
}
function getWebhookPayload ( webhook _info , data ) {
if ( ! isLikelyAppriseWebhookURL ( webhook _info ) ) return data ;
const payload = {
title : data [ 'title' ] ,
body : data [ 'body' ] ,
type : mapNotificationTypeToAppriseType ( data [ 'type' ] ) ,
@ -346,13 +374,20 @@ function getWebhookPayload(webhook_url, data) {
url : data [ 'url' ] ,
thumbnail : data [ 'thumbnail' ]
} ;
const apprise _tag = webhook _info [ 'parsed_url' ] . searchParams . get ( 'tag' ) || webhook _info [ 'parsed_url' ] . searchParams . get ( 'tags' ) ;
if ( apprise _tag ) payload [ 'tag' ] = apprise _tag ;
return payload ;
}
function sendGenericNotification ( data ) {
const webhook _url = config _api . getConfigItem ( 'ytdl_webhook_url' ) ;
logger . verbose ( ` Sending generic notification to ${ webhook _url } ` ) ;
const payload = getWebhookPayload ( webhook _url , data ) ;
fetch ( webhook _url , {
const webhook _info = parseWebhookURL ( webhook _url ) ;
const request _url = webhook _info ? webhook _info [ 'request_url' ] : webhook _url ;
logger . verbose ( ` Sending generic notification to ${ request _url } ` ) ;
const payload = getWebhookPayload ( webhook _info , data ) ;
fetch ( request _url , {
method : 'POST' ,
headers : {
"Content-Type" : "application/json"