This commit is contained in:
Florent 2021-07-06 16:01:06 +02:00 committed by Florent F
parent afdd320056
commit db856aa6e2

View File

@ -1,8 +1,8 @@
import { ListenerRssAggregator } from 'listener-rss-agregator'; import { ListenerRssAggregator } from "listener-rss-agregator";
import { ListenerRSSInfos } from 'listener-rss'; import { ListenerRSSInfos } from "listener-rss";
type ListenerData = { type ListenerData = {
ChannelId: number; ChannelId: number;
firstUpdate: boolean; firstUpdate: boolean;
} & ListenerRSSInfos; } & ListenerRSSInfos;
let myManager: ListenerRssAggregator; let myManager: ListenerRssAggregator;
@ -10,91 +10,91 @@ let listenersDataBinding = new Map<string, ListenerData>();
let logger: any; let logger: any;
let firstUpdate = true; let firstUpdate = true;
import * as path from 'path'; import * as path from "path";
import fs from 'fs'; import fs from "fs";
async function register({ async function register({
registerSetting, registerSetting,
settingsManager, settingsManager,
peertubeHelpers, peertubeHelpers,
}: any) { }: any) {
const basePath = peertubeHelpers.plugin.getDataDirectoryPath(); const basePath = peertubeHelpers.plugin.getDataDirectoryPath();
logger = peertubeHelpers.logger; logger = peertubeHelpers.logger;
registerSetting({ registerSetting({
name: 'ytb-urls', name: "ytb-urls",
label: 'liste des urls youtube a auto-importer', label: "liste des urls youtube a auto-importer",
type: 'input-textarea', type: "input-textarea",
}); });
logger.warn('setting register'); logger.warn("setting register");
fs.appendFileSync(path.join(basePath, '/storage.bd'), ''); // append fs.appendFileSync(path.join(basePath, "/storage.bd"), ""); // append
const configAggregator = await ListenerRssAggregator.instantiateAggregator( const configAggregator = await ListenerRssAggregator.instantiateAggregator(
path.join(basePath, '/storage.bd') path.join(basePath, "/storage.bd")
); );
myManager = new ListenerRssAggregator(configAggregator); myManager = new ListenerRssAggregator(configAggregator);
logger.warn('Aggregator created'); logger.warn("Aggregator created");
const inputs = await settingsManager.getSetting('ytb-urls'); const inputs = await settingsManager.getSetting("ytb-urls");
if (inputs) await addListeners(inputs); if (inputs) await addListeners(inputs);
logger.warn('Config loaded'); logger.warn("Config loaded");
settingsManager.onSettingsChange(async (settings: any) => { settingsManager.onSettingsChange(async (settings: any) => {
await addListeners(settings['ytb-urls']); await addListeners(settings["ytb-urls"]);
}); });
myManager.on('newEntries', (entries: any) => { myManager.on("newEntries", (entries: any) => {
const datas = listenersDataBinding.get(entries.addressListener); const datas = listenersDataBinding.get(entries.addressListener);
if (!datas) return; if (!datas) return;
logger.warn('Nouvelles entrées détéctées: ' + JSON.stringify(entries)); logger.warn("Nouvelles entrées détéctées: " + JSON.stringify(entries));
}); });
} }
async function addListeners(listenerInput: string) { async function addListeners(listenerInput: string) {
let listeners: ListenerData[]; let listeners: ListenerData[];
try { try {
listeners = JSON.parse(listenerInput); listeners = JSON.parse(listenerInput);
} catch { } catch {
logger.warn('Erreur: malformé'); logger.warn("Erreur: malformé");
return; return;
} }
let newListeners = listeners.filter( let newListeners = listeners.filter(
(item) => !listenersDataBinding.has(item.address) (item) => !listenersDataBinding.has(item.address)
); );
let removedUrls = Array.from(listenersDataBinding.keys()).filter( let removedUrls = Array.from(listenersDataBinding.keys()).filter(
(url) => !listeners.some((listener) => listener.address === url) (url) => !listeners.some((listener) => listener.address === url)
); );
for (const newItem of newListeners) { for (const newItem of newListeners) {
listenersDataBinding.set(newItem.address, { listenersDataBinding.set(newItem.address, {
ChannelId: newItem.ChannelId, ChannelId: newItem.ChannelId,
firstUpdate: true, firstUpdate: true,
address: newItem.address, address: newItem.address,
}); });
} }
for (const removedUrl of removedUrls) { for (const removedUrl of removedUrls) {
listenersDataBinding.delete(removedUrl); listenersDataBinding.delete(removedUrl);
} }
myManager.stopAll(); myManager.stopAll();
await myManager.saveOverride(listeners); await myManager.saveOverride(listeners);
firstUpdate = true; firstUpdate = true;
if (logger) logger.warn('Configuration modifiée: ' + listenerInput); if (logger) logger.warn("Configuration modifiée: " + listenerInput);
myManager.startAll(); myManager.startAll();
} }
async function unregister() { async function unregister() {
myManager.stopAll(); myManager.stopAll();
return; return;
} }
module.exports = { module.exports = {
register, register,
unregister, unregister,
}; };