forked from Outils-PeerTube/listener-rss-aggregator
adding listener rss package + better addNewListener's implementation + better sqlite management
This commit is contained in:
@ -22,24 +22,24 @@ export class ManageListener extends EventEmitter {
|
||||
const configs: ListenerRSSInfos[] = await this.sqliteDb.fetchAll();
|
||||
|
||||
configs.forEach((config) => {
|
||||
const newListener = new ListenerRss(config);
|
||||
this.listenerArray.push(newListener);
|
||||
this.settingEvents(newListener);
|
||||
this.addNewListener(config);
|
||||
});
|
||||
}
|
||||
|
||||
settingEvents(newListener: ListenerRss): void {
|
||||
private addNewListener(info: ListenerRSSInfos) : ListenerRss {
|
||||
const newListener = new ListenerRss(info);
|
||||
this.listenerArray.push(newListener);
|
||||
|
||||
newListener.on("update", (obj) => this.emit("update", obj));
|
||||
newListener.on("newEntries", (obj) => this.emit("newEntries", obj));
|
||||
newListener.on("error", (err) => this.emit("error", err));
|
||||
|
||||
return newListener;
|
||||
}
|
||||
|
||||
async addNewListener(info: ListenerRSSInfos) {
|
||||
const newListener = new ListenerRss(info);
|
||||
this.listenerArray.push(newListener);
|
||||
this.settingEvents(newListener);
|
||||
|
||||
await this.sqliteDb.insertListener(newListener);
|
||||
async registerListener(info: ListenerRSSInfos) {
|
||||
const listener = this.addNewListener(info);
|
||||
await this.sqliteDb.insertListener(listener);
|
||||
}
|
||||
|
||||
async save() {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { ListenerRSSInfos } from "listener-rss";
|
||||
|
||||
import connect, { sql } from "@databases/sqlite";
|
||||
import connect, { DatabaseConnection, sql } from "@databases/sqlite";
|
||||
|
||||
export class SqliteTools {
|
||||
path?: string;
|
||||
@ -9,30 +9,39 @@ export class SqliteTools {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
async ensureTableExists() {
|
||||
async withDB<Type>(callback: (db: DatabaseConnection) => Promise<Type>): Promise<Type> {
|
||||
const db = connect(this.path);
|
||||
try {
|
||||
return callback(db);
|
||||
} finally {
|
||||
await db.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
let req = sql`CREATE TABLE IF NOT EXISTS listeners
|
||||
(
|
||||
address TEXT NOT NULL UNIQUE,
|
||||
customfields TEXT DEFAULT '[]' NOT NULL,
|
||||
timeloop INTEGER DEFAULT 300 NOT NULL,
|
||||
last_entries_links TEXT DEFAULT '[]' NOT NULL,
|
||||
PRIMARY KEY (address),
|
||||
CHECK(timeloop >= 0)
|
||||
);`;
|
||||
|
||||
await db.query(req);
|
||||
await db.dispose();
|
||||
async ensureTableExists() {
|
||||
await this.withDB(async (db) => {
|
||||
let req = sql`CREATE TABLE IF NOT EXISTS listeners
|
||||
(
|
||||
address TEXT NOT NULL,
|
||||
customfields TEXT DEFAULT '[]' NOT NULL,
|
||||
timeloop INTEGER DEFAULT 300 NOT NULL,
|
||||
last_entries_links TEXT DEFAULT '[]' NOT NULL,
|
||||
PRIMARY KEY (address),
|
||||
CHECK(timeloop >= 0)
|
||||
);`;
|
||||
|
||||
await db.query(req);
|
||||
});
|
||||
}
|
||||
|
||||
async fetchAll(): Promise<ListenerRSSInfos[]> {
|
||||
const db = connect(this.path);
|
||||
|
||||
const rows = await this.withDB(async (db) => {
|
||||
let req = sql`SELECT *
|
||||
FROM listeners`;
|
||||
return await db.query(req);
|
||||
});
|
||||
|
||||
let req = sql`SELECT *
|
||||
FROM listeners`;
|
||||
const rows = await db.query(req);
|
||||
await db.dispose();
|
||||
return rows.map((row: any) => ({
|
||||
address: row["address"],
|
||||
customfields: JSON.parse(row["customfields"]),
|
||||
@ -42,30 +51,28 @@ export class SqliteTools {
|
||||
}
|
||||
|
||||
async insertListener(listener: ListenerRSSInfos) {
|
||||
const db = connect(this.path);
|
||||
|
||||
let req = sql`INSERT INTO listeners (address, timeloop, customfields, last_entries_links)
|
||||
VALUES (${listener.address},
|
||||
${listener.timeloop},
|
||||
${JSON.stringify(listener.customfields ? listener.customfields : [])},
|
||||
${JSON.stringify(listener.lastEntriesLinks ? listener.lastEntriesLinks : [])})`;
|
||||
await db.query(req);
|
||||
await db.dispose();
|
||||
await this.withDB(async (db) => {
|
||||
let req = sql`INSERT INTO listeners (address, timeloop, customfields, last_entries_links)
|
||||
VALUES (${listener.address},
|
||||
${listener.timeloop},
|
||||
${JSON.stringify(listener.customfields ?? [])},
|
||||
${JSON.stringify(listener.lastEntriesLinks ?? [])})`;
|
||||
await db.query(req);
|
||||
});
|
||||
}
|
||||
|
||||
async updateAll(listeners: ListenerRSSInfos[]) {
|
||||
const db = connect(this.path);
|
||||
|
||||
await db.tx(async (transaction) => {
|
||||
for (const listener of listeners) {
|
||||
let req = sql`UPDATE listeners
|
||||
SET last_entries_links = ${JSON.stringify(
|
||||
listener.lastEntriesLinks
|
||||
)}
|
||||
WHERE address = ${listener.address}`;
|
||||
await transaction.query(req);
|
||||
}
|
||||
await this.withDB(async (db) => {
|
||||
await db.tx(async (transaction) => {
|
||||
for (const listener of listeners) {
|
||||
let req = sql`UPDATE listeners
|
||||
SET last_entries_links = ${JSON.stringify(
|
||||
listener.lastEntriesLinks
|
||||
)}
|
||||
WHERE address = ${listener.address}`;
|
||||
await transaction.query(req);
|
||||
}
|
||||
});
|
||||
});
|
||||
await db.dispose();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user