Working RSS Feeds

- Need to get worker in the background to update the sources
This commit is contained in:
Logan Cusano
2023-02-25 19:51:49 -05:00
parent ae071be8b8
commit d33f1ceccc
9 changed files with 218 additions and 250 deletions

View File

@@ -25,7 +25,7 @@ function runSQL(sqlQuery, connection, callback = (err, rows) => {
log.ERROR("SQL Error:", err)
return callback(err, undefined);
}
log.VERBOSE("RunSQL Returned Rows:", rows);
log.VERBOSE(`SQL result for query '${sqlQuery}':`, rows);
return callback(undefined, rows);
})
}
@@ -39,7 +39,37 @@ class Storage {
database: process.env.DB_NAME
});
this.dbTable = _dbTable
this.dbTable = _dbTable;
this.validKeys = [];
var sqlQuery = `SHOW COLUMNS FROM ${this.dbTable};`;
runSQL(sqlQuery, this.connection, (err, rows) => {
if (err) return log.ERROR("Error getting column names: ", err);
if (rows){
for (const validKey of rows){
this.validKeys.push(validKey.Field);
}
log.VERBOSE(`Database rows for '${this.dbTable}': `, rows);
log.DEBUG(`Keys for '${this.dbTable}': `, this.validKeys);
}
})
}
/**
* Wrapper to delete an entry using the storage method configured
* @param {} entryID The ID of the entry to be deleted
* @param {function} callback The callback function to be called with the record when deleted
*/
destroy(entryID, callback) {
if (!entryID) return callback(Error("No entry ID given"), undefined);
this.getRecordBy('id', entryID, (err, entryRecord) => {
this.removeEntry(entryRecord.id, (err, results) => {
if (err) return callback(err, undefined);
return callback(undefined, results);
});
});
}
/**
@@ -48,15 +78,14 @@ class Storage {
* @param {*} keyValue The value of the key to search for
* @param {*} callback The callback function
*/
getRecordBy(key, keyValue, callback) {
const validKeys = ["link", "title", "category", "id"];
if (!validKeys.includes(key)) return callback(new Error("Given key not valid"), undefined);
getRecordBy(key, keyValue, callback) {
if (!this.validKeys.includes(key)) return callback(new Error("Given key not valid", key), undefined);
const sqlQuery = `SELECT * FROM ${this.dbTable} WHERE ${key} = '${keyValue}'`;
runSQL(sqlQuery, this.connection, (err, rows) => {
if (err) return callback(err, undefined);
if (rows[0]?.title) return callback(undefined, rows[0]);
if (rows[0]?.[key]) return callback(undefined, rows[0]);
else return callback(undefined, false);
})
}
@@ -66,19 +95,49 @@ class Storage {
* @param {function} callback
*/
getAllRecords(callback) {
log.INFO("Getting all records");
log.INFO("Getting all records from: ", this.dbTable);
const sqlQuery = `SELECT * FROM ${this.dbTable}`
let rssRecords = [];
let records = [];
runSQL(sqlQuery, this.connection, (err, rows) => {
if (err) return callback(err, undefined);
for (const row of rows) {
log.VERBOSE("Row from SQL query:", row);
rssRecords.push(new RSSSourceRecord(row.id, row.title, row.link, row.category, row.guild_id, row.channel_id));
for (const row of rows) {
if (this.dbTable == rssFeedsTable){
records.push(new RSSSourceRecord(row.id, row.title, row.link, row.category, row.guild_id, row.channel_id));
}
if (this.dbTable == rssPostsTable){
records.push(rows);
}
}
log.VERBOSE("All records:", rssRecords);
return callback(undefined, rssRecords);
log.VERBOSE("All records:", records);
return callback(undefined, records);
});
}
/**
* Gets all unique rows in the given key
* @param {*} key
* @param {*} callback
*/
getUniqueByKey(key, callback){
log.INFO("Getting all unique values in column: ", key);
const sqlQuery = `SELECT DISTINCT ${key} FROM ${this.dbTable}`
let records = [];
runSQL(sqlQuery, this.connection, (err, rows) => {
if (err) return callback(err, undefined);
for (const row of rows) {
if (this.dbTable == rssFeedsTable){
records.push(new RSSSourceRecord(row.id, row.title, row.link, row.category, row.guild_id, row.channel_id));
}
if (this.dbTable == rssPostsTable){
records.push(rows);
}
}
log.VERBOSE("All records:", records);
return callback(undefined, records);
});
}
}
@@ -109,28 +168,12 @@ exports.feedStorage = class feedStorage extends Storage {
log.DEBUG("One record to callback with:", record);
return callback(undefined, record);
}
}, false)
}, false) // Do not update the if it exists
}
if (!toBeSaved.length === 1) {
return callback(undefined, newRecords);
}
}
/**
* Wrapper to delete an entry using the storage method configured
* @param {} entryID The ID of the entry to be deleted
* @param {function} callback The callback function to be called with the record when deleted
*/
destroy(entryID, callback) {
if (!entryID) return callback(Error("No entry ID given"), undefined);
this.getRecordBy('id', entryID, (err, entryRecord) => {
this.removeEntry(entryRecord.id, (err, results) => {
if (err) return callback(err, undefined);
return callback(undefined, results);
});
});
}
}
/**
* Check to see if an entry exists in the storage method configured
@@ -205,15 +248,15 @@ exports.feedStorage = class feedStorage extends Storage {
/**
* Delete the given entry from the storage medium
* @param {string} title The title of the entry to be deleted
* @param {*} id The title of the entry to be deleted
* @param {function} callback The callback to be called with either an error or undefined if successful
*/
removeEntry(title, callback) {
if (!title) {
return callback(new Error("No entry title given before deleting"), undefined)
removeEntry(id, callback) {
if (!id) {
return callback(new Error("No entry id given before deleting"), undefined)
}
const sqlQuery = `DELETE FROM ${this.dbTable} WHERE title = '${title}';`;
const sqlQuery = `DELETE FROM ${this.dbTable} WHERE id = '${id}';`;
runSQL(sqlQuery, this.connection, (err, rows) => {
if (err) return callback(err, undefined);
@@ -280,3 +323,25 @@ exports.feedStorage = class feedStorage extends Storage {
}
}
exports.postStorage = class postStorage extends Storage {
constructor() {
super(rssPostsTable);
}
savePost(_postObject, callback){
const tempCreationDate = new Date().toISOString().slice(0, 19).replace('T', ' ');
log.DEBUG("Saving Post Object:", _postObject);
if (!_postObject?.guid || !_postObject?.link) {
return callback(new Error("Post object malformed, check the object before saving it"), undefined)
}
const sqlQuery = `INSERT INTO ${this.dbTable} (post_guid, post_link, post_sent_date) VALUES ('${_postObject.guid}','${_postObject.link}','${tempCreationDate}');`;
log.DEBUG(`Adding new post with SQL query: '${sqlQuery}'`)
runSQL(sqlQuery, this.connection, (err, rows) => {
if (err) return callback(err, undefined);
return callback(undefined, rows);
})
}
}