diff --git a/lib/peertubeRequester.ts b/lib/peertubeRequester.ts index 338a90f..ae3d985 100644 --- a/lib/peertubeRequester.ts +++ b/lib/peertubeRequester.ts @@ -27,21 +27,21 @@ class PeerTubeRequester { this.password = config.password; } - private async requestAuthToken(): Promise { + async requestAuthToken(): Promise { let response = await fetch( new URL(`/api/v1/oauth-clients/local`, this.domainName) ); if (!response.ok) { - throw new Error(response.statusText); // CRASH + throw new Error("Cannot get client credentials : " + response.statusText); // CRASH } const { client_id: clientId, client_secret: clientSecret } = await response.json(); const clientInfo: { [key: string]: string } = { - clientId, - clientSecret, - grantType: "password", - responseType: "code", + client_id: clientId, + client_secret: clientSecret, + grant_type: "password", + response_type: "code", username: this.username, password: this.password, }; @@ -54,7 +54,7 @@ class PeerTubeRequester { body: myParams, }); if (!response.ok) { - throw new Error(response.statusText); // CRASH + throw new Error("Cannot get access Token : " + response.statusText); // CRASH } const { access_token: accessToken } = await response.json(); return accessToken; diff --git a/src/main.ts b/src/main.ts index 520c030..54927b9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,7 +10,8 @@ type ListenerData = ListenerRss.Config & { let myManager: ListenerRssAggregator; let listenersDataBinding = new Map(); let logger: any; -let peertube: PeerTubeRequester; +let peertube: PeerTubeRequester | undefined = undefined; +let goodPeertubeCredential: boolean = false; import * as path from "path"; import fs from "fs"; @@ -29,6 +30,18 @@ async function register({ type: "input-textarea", }); + registerSetting({ + name: "admin-name", + label: "Admin Username", + type: "input", + }); + + registerSetting({ + name: "admin-password", + label: "Admin Password", + type: "input-password", + }); + logger.debug("setting register"); fs.appendFileSync(path.join(basePath, "/storage.bd"), ""); @@ -37,20 +50,36 @@ async function register({ ); myManager = new ListenerRssAggregator(configAggregator); - peertube = new PeerTubeRequester({ - domainName: "http://localhost:9000", - username: "root", - password: "test", - }); - logger.debug("Aggregator created"); - const inputs = await settingsManager.getSetting("ytb-urls"); - if (inputs) await addListeners(inputs); + const settingYtbUrls = await settingsManager.getSetting("ytb-urls"); + if (settingYtbUrls) await addListeners(settingYtbUrls); - logger.debug("Config loaded"); + const settingCredentials: any = await settingsManager.getSettings([ + "admin-name", + "admin-password", + ]); + + if (settingCredentials["admin-name"] && settingCredentials["admin-password"]) + apiRequestInitializer({ + domainName: peertubeHelpers.config.getWebserverUrl(), + username: settingCredentials["admin-name"], + password: settingCredentials["admin-password"], + }); + logger.debug("Actual config loaded"); settingsManager.onSettingsChange(async (settings: any) => { + if ( + !peertube || + peertube.username != settings["admin-name"] || + peertube.password != settings["admin-password"] + ) + apiRequestInitializer({ + domainName: peertubeHelpers.config.getWebserverUrl(), + username: settings["admin-name"], + password: settings["admin-password"], + }); + await addListeners(settings["ytb-urls"]); }); @@ -64,13 +93,31 @@ async function register({ JSON.stringify(entries) ); for (const item of entries.items) - await peertube.uploadFromUrl({ - channelId: datas.channelId, - targetUrl: item.link, - }); + if (peertube) + await peertube.uploadFromUrl({ + channelId: datas.channelId, + targetUrl: item.link, + }); + else { + logger.warn("Bad credential provides. New entries Skipped."); + } }); } +async function apiRequestInitializer(data: PeerTubeRequester.Config) { + peertube = new PeerTubeRequester(data); + + try { + await peertube.requestAuthToken(); + goodPeertubeCredential = true; + logger.debug("credential ok"); + } catch (error) { + logger.warn("Error during the credential validation : " + error); + peertube = undefined; + goodPeertubeCredential = false; + } +} + async function addListeners(listenerInput: string) { let listeners: ListenerData[]; try { @@ -96,9 +143,9 @@ async function addListeners(listenerInput: string) { myManager.stopAll(); await myManager.saveOverride(listeners); - if (logger) logger.warn("Configuration changed: " + listenerInput); + if (logger) logger.debug("Configuration changed: " + listenerInput); - myManager.startAll(); + if (goodPeertubeCredential) myManager.startAll(); } async function unregister() {