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