Updated storage for feeds and posts
This commit is contained in:
137
libCore.js
137
libCore.js
@@ -19,31 +19,21 @@ const openai = new OpenAIApi(configuration);
|
|||||||
|
|
||||||
// Data Structures
|
// Data Structures
|
||||||
var feeds = [];
|
var feeds = [];
|
||||||
let rssFeedMap = [];
|
var rssFeedMap = [];
|
||||||
let rssFeedCategories = [];
|
var rssFeedCategories = [];
|
||||||
|
|
||||||
// Precess Vars
|
// Setup Storage handlers
|
||||||
var userTable = process.env.DB_TABLE
|
var feedStorage = new storageHandler.feedStorage();
|
||||||
|
|
||||||
// Setup Storage handler
|
|
||||||
var storageSource = new storageHandler.Storage(userTable);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new source url to configured storage
|
* Adds or updates new source url to configured storage
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {string} title - Title/Name of the RSS feed.
|
* @param {string} title - Title/Name of the RSS feed.
|
||||||
* @param {string} link - URL of RSS feed.
|
* @param {string} link - URL of RSS feed.
|
||||||
* @param {string} category - Category of RSS feed.
|
* @param {string} category - Category of RSS feed.
|
||||||
*/
|
*/
|
||||||
exports.addSource = function (title, link, category, guildId, channelId, callback) {
|
exports.addSource = async (title, link, category, guildId, channelId, callback) => {
|
||||||
|
feedStorage.create([{
|
||||||
for (i = 0; i < feeds.length; i++) {
|
|
||||||
if (feeds[i].link == link) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
storageSource.create([{
|
|
||||||
"fields": {
|
"fields": {
|
||||||
"title": title,
|
"title": title,
|
||||||
"link": link,
|
"link": link,
|
||||||
@@ -54,9 +44,11 @@ exports.addSource = function (title, link, category, guildId, channelId, callbac
|
|||||||
}], function (err, record) {
|
}], function (err, record) {
|
||||||
if (err) {
|
if (err) {
|
||||||
log.ERROR("Error in create:", err);
|
log.ERROR("Error in create:", err);
|
||||||
callback(err, undefined);
|
return callback(err, undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!record) return callback(undefined, false);
|
||||||
|
|
||||||
log.DEBUG("Record ID:", record.getId());
|
log.DEBUG("Record ID:", record.getId());
|
||||||
|
|
||||||
var linkData = {
|
var linkData = {
|
||||||
@@ -71,7 +63,7 @@ exports.addSource = function (title, link, category, guildId, channelId, callbac
|
|||||||
feeds.push(linkData);
|
feeds.push(linkData);
|
||||||
|
|
||||||
log.DEBUG("pushed item to feeds");
|
log.DEBUG("pushed item to feeds");
|
||||||
callback(undefined, true);
|
return callback(undefined, record);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -88,13 +80,13 @@ exports.deleteSource = function (title, callback) {
|
|||||||
deleteRecord = feeds[i].id;
|
deleteRecord = feeds[i].id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
storageSource.destroy(deleteRecord, function (err, deletedRecord) {
|
feedStorage.destroy(deleteRecord, function (err, deletedRecord) {
|
||||||
if (err) {
|
if (err) {
|
||||||
log.ERROR(err);
|
log.ERROR(err);
|
||||||
callback(err, undefined);
|
return callback(err, undefined);
|
||||||
}
|
}
|
||||||
log.DEBUG(deletedRecord.id);
|
log.DEBUG(deletedRecord.id);
|
||||||
callback(undefined, deletedRecord);
|
return callback(undefined, deletedRecord);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,17 +96,17 @@ exports.deleteSource = function (title, callback) {
|
|||||||
* @param {string} feedType - Category to select Feed by.
|
* @param {string} feedType - Category to select Feed by.
|
||||||
*/
|
*/
|
||||||
exports.getFeeds = function (feedType) {
|
exports.getFeeds = function (feedType) {
|
||||||
var linkFlayerFilteredMap = [];
|
var rssFeedFilteredMap = [];
|
||||||
if (feedType == null || feedType == undefined || feedType == "" || feedType.toLowerCase() == "all") {
|
if (feedType == null || feedType == undefined || feedType == "" || feedType.toLowerCase() == "all") {
|
||||||
return rssFeedMap;
|
return rssFeedMap;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
rssFeedMap.forEach(linkFlay => {
|
rssFeedMap.forEach(rssFeed => {
|
||||||
if (linkFlay.category.toLowerCase().indexOf(feedType.toLowerCase()) > -1) {
|
if (rssFeed.category.toLowerCase().indexOf(feedType.toLowerCase()) > -1) {
|
||||||
linkFlayerFilteredMap.push(linkFlay);
|
rssFeedFilteredMap.push(rssFeed);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return linkFlayerFilteredMap;
|
return rssFeedFilteredMap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,47 +118,39 @@ exports.loadFeeds = function () {
|
|||||||
rssFeedMap = [];
|
rssFeedMap = [];
|
||||||
rssFeedCategories = [];
|
rssFeedCategories = [];
|
||||||
|
|
||||||
storageSource.getAllRecords(function (err, records) {
|
feedStorage.getAllRecords((err, records) => {
|
||||||
records.forEach(function (record) {
|
for (const record of records){
|
||||||
try {
|
try {
|
||||||
log.DEBUG('Retrieved title: ', record.get('title'));
|
log.DEBUG('Retrieved title: ', record.title);
|
||||||
log.DEBUG('Retrieved link:', record.get('link'));
|
log.DEBUG('Retrieved link: ', record.link);
|
||||||
log.DEBUG('Retrieved category:', record.get('category'));
|
log.DEBUG('Retrieved category: ', record.category);
|
||||||
|
log.DEBUG('Retrieved guild ID: ', record.guild_id);
|
||||||
var feedData = {
|
log.DEBUG('Retrieved channel ID: ', record.channel_id);
|
||||||
title: `${unescape(record.get('title'))}`,
|
|
||||||
link: `${unescape(record.get('link'))}`,
|
// Check to see if this source has been loaded already
|
||||||
category: `${unescape(record.get('category'))}`,
|
feeds.forEach(feedSource => {
|
||||||
id: record.getId()
|
if (feedSource.link != record.link) {
|
||||||
}
|
log.DEBUG("Loading new source: ", feedSource)
|
||||||
|
feeds.push(feedSource);
|
||||||
var foundMatch = false;
|
|
||||||
feeds.forEach(feedBlock => {
|
|
||||||
if (feedBlock.link == feedData.link) {
|
|
||||||
foundMatch = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!foundMatch) {
|
|
||||||
feeds.push(feedData);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let foundCat = false;
|
// Update the known categories if new category is not present
|
||||||
rssFeedCategories.forEach(cat => {
|
if (!rssFeedCategories.includes(record.category)) {
|
||||||
if (cat == record.get('category')) {
|
log.DEBUG("Added new category to running known categories: ", record.category)
|
||||||
foundCat = true;
|
rssFeedCategories.push(record.category);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!foundCat) {
|
|
||||||
rssFeedCategories.push(record.get('category'));
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
log.DEBUG(error);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
log.DEBUG(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
feeds.forEach(feedBlock => {
|
feeds.forEach(feedBlock => {
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
@@ -177,10 +161,12 @@ exports.loadFeeds = function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (feed != undefined && feed.items != undefined) {
|
if (feed != undefined && feed.items != undefined) {
|
||||||
|
//log.DEBUG("Feed: ", feed);
|
||||||
feed.items.forEach(item => {
|
feed.items.forEach(item => {
|
||||||
var foundFeed = false;
|
var foundFeed = false;
|
||||||
rssFeedMap.forEach(linkFlay => {
|
rssFeedMap.forEach(rssFeed => {
|
||||||
if (linkFlay.link == item.link) {
|
if (rssFeed.link == item.link) {
|
||||||
|
log.DEBUG("Found matching post?: ", rssFeed)
|
||||||
foundFeed = true;
|
foundFeed = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -202,14 +188,9 @@ exports.loadFeeds = function () {
|
|||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.DEBUG(error);
|
log.DEBUG(error);
|
||||||
}
|
}
|
||||||
})().then();
|
|
||||||
});
|
//log.DEBUG("RSS Feed Map: ", rssFeedMap);
|
||||||
return;
|
|
||||||
//fetchNextPage();
|
|
||||||
}, function done(error) {
|
|
||||||
log.DEBUG(error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -372,4 +353,12 @@ exports.getQuotes = async function (quote_url) {
|
|||||||
*/
|
*/
|
||||||
exports.getCategories = function () {
|
exports.getCategories = function () {
|
||||||
return rssFeedCategories;
|
return rssFeedCategories;
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.addPost = (postObject, callback) => {
|
||||||
|
if(!postObject.post_link_hash) return callback(new Error("No hash included"), undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.checkIfPostExists = (postObject, callback) => {
|
||||||
|
if(!postObject.post_id || !postObject.post_link_hash) return callback(new Error("No hash included"), undefined);
|
||||||
}
|
}
|
||||||
215
libStorage.js
215
libStorage.js
@@ -4,11 +4,14 @@
|
|||||||
// Import modules
|
// Import modules
|
||||||
const { DebugBuilder } = require("./utilities/debugBuilder");
|
const { DebugBuilder } = require("./utilities/debugBuilder");
|
||||||
const log = new DebugBuilder("server", "libStorage");
|
const log = new DebugBuilder("server", "libStorage");
|
||||||
|
const { RSSSourceRecord, RSSPostRecord } = require("./utilities/recordHelper");
|
||||||
|
|
||||||
// Storage Specific Modules
|
// Storage Specific Modules
|
||||||
// MySQL
|
// MySQL
|
||||||
const mysql = require("mysql");
|
const mysql = require("mysql");
|
||||||
|
|
||||||
|
const rssFeedsTable = process.env.DB_RSS_FEEDS_TABLE;
|
||||||
|
const rssPostsTable = process.env.DB_RSS_POSTS_TABLE;
|
||||||
|
|
||||||
// Helper Functions
|
// Helper Functions
|
||||||
// Function to run and handle SQL errors
|
// Function to run and handle SQL errors
|
||||||
@@ -17,87 +20,100 @@ function runSQL(sqlQuery, connection, callback = (err, rows) => {
|
|||||||
throw err;
|
throw err;
|
||||||
}) {
|
}) {
|
||||||
// Start the MySQL Connection
|
// Start the MySQL Connection
|
||||||
//connection.connect();
|
|
||||||
connection.query(sqlQuery, (err, rows) => {
|
connection.query(sqlQuery, (err, rows) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
log.ERROR("SQL Error:", err)
|
log.ERROR("SQL Error:", err)
|
||||||
callback(err, undefined);
|
return callback(err, undefined);
|
||||||
}
|
}
|
||||||
log.DEBUG("RunSQL Returned Rows:", rows);
|
log.VERBOSE("RunSQL Returned Rows:", rows);
|
||||||
callback(undefined, rows);
|
return callback(undefined, rows);
|
||||||
})
|
})
|
||||||
//connection.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class RSSRecord {
|
class Storage {
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {*} _id
|
|
||||||
* @param {*} _title
|
|
||||||
* @param {*} _link
|
|
||||||
* @param {*} _category
|
|
||||||
* @param {*} _guild_id
|
|
||||||
* @param {*} _channel_id
|
|
||||||
*/
|
|
||||||
constructor(_id, _title, _link, _category, _guild_id, _channel_id) {
|
|
||||||
this.id = _id;
|
|
||||||
this.title = _title;
|
|
||||||
this.link= _link;
|
|
||||||
this.category = _category;
|
|
||||||
this.guild_id = _guild_id;
|
|
||||||
this.channel_id = _channel_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
getId() {
|
|
||||||
return this.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
get(key) {
|
|
||||||
if (!Object.keys(this).includes(key)) throw new Error("Key is invalid", key);
|
|
||||||
return this[key]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.RSSRecord = RSSRecord;
|
|
||||||
|
|
||||||
exports.Storage = class Storage {
|
|
||||||
constructor(_dbTable) {
|
constructor(_dbTable) {
|
||||||
this.connection = mysql.createPool({
|
this.connection = mysql.createPool({
|
||||||
host: process.env.DB_HOST,
|
host: process.env.DB_HOST,
|
||||||
user: process.env.DB_USER,
|
user: process.env.DB_USER,
|
||||||
password: process.env.DB_PASS,
|
password: process.env.DB_PASS,
|
||||||
database: process.env.DB_NAME
|
database: process.env.DB_NAME
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set the DB Table for later use
|
|
||||||
this.dbTable = _dbTable
|
this.dbTable = _dbTable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a record by a specified key
|
||||||
|
* @param {*} key The key to search for
|
||||||
|
* @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);
|
||||||
|
|
||||||
|
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]);
|
||||||
|
else return callback(undefined, false);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all records stored
|
||||||
|
* @param {function} callback
|
||||||
|
*/
|
||||||
|
getAllRecords(callback) {
|
||||||
|
log.INFO("Getting all records");
|
||||||
|
const sqlQuery = `SELECT * FROM ${this.dbTable}`
|
||||||
|
|
||||||
|
let rssRecords = [];
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
log.VERBOSE("All records:", rssRecords);
|
||||||
|
return callback(undefined, rssRecords);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.feedStorage = class feedStorage extends Storage {
|
||||||
|
constructor() {
|
||||||
|
super(rssFeedsTable);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper to save a new entry using the storage method configured
|
* Wrapper to save a new entry using the storage method configured
|
||||||
* @param {Array} toBeSaved Entry or Entries to be added
|
* @param {Array} toBeSaved Entry or Entries to be added
|
||||||
* @param {function} callback The callback function to be called with the record when saved
|
* @param {function} callback The callback function to be called with the record when saved
|
||||||
*/
|
*/
|
||||||
create(toBeSaved, callback) {
|
create(toBeSaved, callback) {
|
||||||
log.DEBUG("To be saved:", toBeSaved);
|
log.DEBUG("To be saved:", toBeSaved);
|
||||||
log.DEBUG("to be saved length:", toBeSaved.length);
|
log.DEBUG("to be saved length:", toBeSaved.length);
|
||||||
if (!toBeSaved[0].fields?.title) callback(Error("No title given"), undefined);
|
// If the request was for the Feeds Table
|
||||||
|
if (!toBeSaved[0].fields?.title) return callback(Error("No title given"), undefined);
|
||||||
let newRecords = []
|
let newRecords = []
|
||||||
for (var entry of toBeSaved) {
|
for (var entry of toBeSaved) {
|
||||||
entry = entry.fields
|
entry = entry.fields;
|
||||||
log.DEBUG("Entry:", entry);
|
log.DEBUG("Entry:", entry);
|
||||||
this.returnRecord(undefined, entry.title, entry.link, entry.category, entry.guild_id, entry.channel_id, (err, record) => {
|
this.returnRecord(undefined, entry.title, entry.link, entry.category, entry.guild_id, entry.channel_id, (err, record) => {
|
||||||
if (err) callback(err, undefined);
|
if (err) return callback(err, undefined);
|
||||||
newRecords.push(record);
|
newRecords.push(record);
|
||||||
if (toBeSaved.length === 1) {
|
if (toBeSaved.length === 1) {
|
||||||
log.DEBUG("One record to callback with:", record);
|
log.DEBUG("One record to callback with:", record);
|
||||||
callback(undefined, record);
|
return callback(undefined, record);
|
||||||
}
|
}
|
||||||
})
|
}, false)
|
||||||
}
|
}
|
||||||
if (!toBeSaved.length === 1) {
|
if (!toBeSaved.length === 1) {
|
||||||
callback(undefined, newRecords);
|
return callback(undefined, newRecords);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -106,12 +122,12 @@ exports.Storage = class Storage {
|
|||||||
* @param {function} callback The callback function to be called with the record when deleted
|
* @param {function} callback The callback function to be called with the record when deleted
|
||||||
*/
|
*/
|
||||||
destroy(entryID, callback) {
|
destroy(entryID, callback) {
|
||||||
if (!entryID) callback(Error("No entry ID given"), undefined);
|
if (!entryID) return callback(Error("No entry ID given"), undefined);
|
||||||
|
|
||||||
this.getRecordBy('id', entryID, (err, entryRecord) => {
|
this.getRecordBy('id', entryID, (err, entryRecord) => {
|
||||||
this.removeEntry(entryRecord.id, (err, results) => {
|
this.removeEntry(entryRecord.id, (err, results) => {
|
||||||
if (err) callback(err, undefined);
|
if (err) return callback(err, undefined);
|
||||||
callback(undefined, results);
|
return callback(undefined, results);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -122,29 +138,11 @@ exports.Storage = class Storage {
|
|||||||
* @returns {true|false|*}
|
* @returns {true|false|*}
|
||||||
*/
|
*/
|
||||||
checkForTitle(title, callback) {
|
checkForTitle(title, callback) {
|
||||||
if (!title) callback(new Error("No title given when checking for title"), undefined);
|
if (!title) return callback(new Error("No title given when checking for title"), undefined)
|
||||||
|
|
||||||
this.getRecordBy("title", title, callback);
|
this.getRecordBy("title", title, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a record by a specified key
|
|
||||||
* @param {*} key The key to search for
|
|
||||||
* @param {*} keyValue The value of the key to search for
|
|
||||||
*/
|
|
||||||
getRecordBy(key, keyValue, callback) {
|
|
||||||
const validKeys = ["link", "title", "category", "id"];
|
|
||||||
if (!validKeys.includes(key)) callback(new Error("Given key not valid"), undefined);
|
|
||||||
|
|
||||||
const sqlQuery = `SELECT * FROM ${this.dbTable} WHERE ${key} = '${keyValue}'`;
|
|
||||||
|
|
||||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
|
||||||
if (err) callback(err, undefined);
|
|
||||||
if (rows[0]?.title) callback(undefined, rows[0]);
|
|
||||||
else callback(undefined, false);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save the given entry to the storage medium
|
* Save the given entry to the storage medium
|
||||||
* @param {Object} entryObject The entry object to be saved
|
* @param {Object} entryObject The entry object to be saved
|
||||||
@@ -153,7 +151,7 @@ exports.Storage = class Storage {
|
|||||||
saveEntry(entryObject, callback) {
|
saveEntry(entryObject, callback) {
|
||||||
log.DEBUG("Saving entry:", entryObject);
|
log.DEBUG("Saving entry:", entryObject);
|
||||||
if (!entryObject?.title || !entryObject?.link || !entryObject?.category) {
|
if (!entryObject?.title || !entryObject?.link || !entryObject?.category) {
|
||||||
callback(new Error("Entry object malformed, check the object before saving it"), undefined)
|
return callback(new Error("Entry object malformed, check the object before saving it"), undefined)
|
||||||
}
|
}
|
||||||
|
|
||||||
const sqlQuery = `INSERT INTO ${this.dbTable} (title, link, category, guild_id, channel_id) VALUES ('${entryObject.title}', '${entryObject.link}', '${entryObject.category}', '${entryObject.guild_id}', '${entryObject.channel_id}');`;
|
const sqlQuery = `INSERT INTO ${this.dbTable} (title, link, category, guild_id, channel_id) VALUES ('${entryObject.title}', '${entryObject.link}', '${entryObject.category}', '${entryObject.guild_id}', '${entryObject.channel_id}');`;
|
||||||
@@ -161,8 +159,8 @@ exports.Storage = class Storage {
|
|||||||
log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`)
|
log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`)
|
||||||
|
|
||||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||||
if (err) callback(err, undefined);
|
if (err) return callback(err, undefined);
|
||||||
callback(undefined, rows);
|
return callback(undefined, rows);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,9 +171,9 @@ exports.Storage = class Storage {
|
|||||||
*/
|
*/
|
||||||
updateEntry(entryObject, callback) {
|
updateEntry(entryObject, callback) {
|
||||||
let queryParams = [];
|
let queryParams = [];
|
||||||
if (!entryObject.title) callback(new Error("No title given before updating"), undefined);
|
if (!entryObject.title) return callback(new Error("No title given before updating"), undefined);
|
||||||
queryParams.push(`title = '${entryObject.title}'`);
|
queryParams.push(`title = '${entryObject.title}'`);
|
||||||
if (!entryObject.link) callback(new Error("No link given before updating"), undefined);
|
if (!entryObject.link) return callback(new Error("No link given before updating"), undefined);
|
||||||
queryParams.push(`link = '${entryObject.link}'`);
|
queryParams.push(`link = '${entryObject.link}'`);
|
||||||
if (entryObject.category) queryParams.push(`category = '${entryObject.category}'`);
|
if (entryObject.category) queryParams.push(`category = '${entryObject.category}'`);
|
||||||
if (entryObject.guild_id) queryParams.push(`guild_id = '${entryObject.guild_id}'`);
|
if (entryObject.guild_id) queryParams.push(`guild_id = '${entryObject.guild_id}'`);
|
||||||
@@ -200,8 +198,8 @@ exports.Storage = class Storage {
|
|||||||
log.DEBUG(`Updating entry with SQL query: '${sqlQuery}'`)
|
log.DEBUG(`Updating entry with SQL query: '${sqlQuery}'`)
|
||||||
|
|
||||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||||
if (err) callback(err, undefined);
|
if (err) return callback(err, undefined);
|
||||||
callback(undefined, rows);
|
return callback(undefined, rows);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,14 +210,14 @@ exports.Storage = class Storage {
|
|||||||
*/
|
*/
|
||||||
removeEntry(title, callback) {
|
removeEntry(title, callback) {
|
||||||
if (!title) {
|
if (!title) {
|
||||||
callback(new Error("No entry title given before deleting"), undefined)
|
return callback(new Error("No entry title given before deleting"), undefined)
|
||||||
}
|
}
|
||||||
|
|
||||||
const sqlQuery = `DELETE FROM ${this.dbTable} WHERE title = '${title}';`;
|
const sqlQuery = `DELETE FROM ${this.dbTable} WHERE title = '${title}';`;
|
||||||
|
|
||||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||||
if (err) callback(err, undefined);
|
if (err) return callback(err, undefined);
|
||||||
callback(undefined, rows[0]);
|
return callback(undefined, rows[0]);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,9 +229,9 @@ exports.Storage = class Storage {
|
|||||||
* @param {*} _category The category of the record
|
* @param {*} _category The category of the record
|
||||||
* @param {*} callback Callback function to return an error or the record
|
* @param {*} callback Callback function to return an error or the record
|
||||||
*/
|
*/
|
||||||
returnRecord(_id, _title, _link, _category, _guild_id, _channel_id, callback) {
|
returnRecord(_id, _title, _link, _category, _guild_id, _channel_id, callback, updateEnabled = true) {
|
||||||
log.DEBUG(`Return record for these values: ID: '${_id}', Title: '${_title}', Category: '${_category}', Link: '${_link}', Guild: '${_guild_id}', Channel:'${_channel_id}'`)
|
log.DEBUG(`Return record for these values: ID: '${_id}', Title: '${_title}', Category: '${_category}', Link: '${_link}', Guild: '${_guild_id}', Channel:'${_channel_id}', Update Enabled: `, updateEnabled)
|
||||||
if (!_link && !_title && !_guild_id && !_channel_id) callback(new Error("No link or title given when creating a record"), undefined);
|
if (!_link && !_title && !_guild_id && !_channel_id) return callback(new Error("No link or title given when creating a record"), undefined);
|
||||||
let entryObject = {
|
let entryObject = {
|
||||||
"title": _title,
|
"title": _title,
|
||||||
"link": _link,
|
"link": _link,
|
||||||
@@ -244,58 +242,41 @@ exports.Storage = class Storage {
|
|||||||
|
|
||||||
if (_id) {
|
if (_id) {
|
||||||
entryObject.id = _id;
|
entryObject.id = _id;
|
||||||
|
if (!updateEnabled) return callback(undefined, undefined);
|
||||||
|
|
||||||
this.updateEntry(entryObject, (err, rows) => {
|
this.updateEntry(entryObject, (err, rows) => {
|
||||||
if (err) callback(err, undefined);
|
if (err) return callback(err, undefined);
|
||||||
this.getRecordBy('id', entryObject.id, (err, record) => {
|
this.getRecordBy('id', entryObject.id, (err, record) => {
|
||||||
if (err) callback(err, undefined);
|
if (err) return callback(err, undefined);
|
||||||
callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id));
|
return callback(undefined, new RSSSourceRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id));
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.checkForTitle(_title, (err, titleExists) => {
|
this.checkForTitle(_title, (err, titleExists) => {
|
||||||
if (!titleExists) {
|
if (!titleExists) {
|
||||||
log.DEBUG("Entry doesn't exist, making one now", entryObject);
|
log.DEBUG("Entry doesn't exist, making one now", entryObject);
|
||||||
this.saveEntry(entryObject, (err, rows) => {
|
this.saveEntry(entryObject, (err, rows) => {
|
||||||
if (err) callback(err, undefined);
|
if (err) return callback(err, undefined);
|
||||||
this.getRecordBy("title", entryObject.title, (err, record) => {
|
this.getRecordBy("title", entryObject.title, (err, record) => {
|
||||||
if (err) callback(err, undefined);
|
if (err) return callback(err, undefined);
|
||||||
callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id));
|
return callback(undefined, new RSSSourceRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id));
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
if (!updateEnabled) return callback(undefined, undefined);
|
||||||
|
|
||||||
this.updateEntry(entryObject, (err, rows) => {
|
this.updateEntry(entryObject, (err, rows) => {
|
||||||
if (err) callback(err, undefined);
|
if (err) return callback(err, undefined);
|
||||||
this.getRecordBy('title', entryObject.title, (err, record) => {
|
this.getRecordBy('title', entryObject.title, (err, record) => {
|
||||||
if (err) callback(err, undefined);
|
if (err) return callback(err, undefined);
|
||||||
callback(undefined, new RSSRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id));
|
return callback(undefined, new RSSSourceRecord(record.id, record.title, record.link, record.category, record.guild_id, record.channel_id));
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get all records stored
|
|
||||||
* @param {function} callback
|
|
||||||
*/
|
|
||||||
getAllRecords(callback) {
|
|
||||||
log.INFO("Getting all records");
|
|
||||||
const sqlQuery = `SELECT * FROM ${this.dbTable}`
|
|
||||||
|
|
||||||
let rssRecords = [];
|
|
||||||
|
|
||||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
|
||||||
if (err) callback(err, undefined);
|
|
||||||
for (const row of rows) {
|
|
||||||
log.DEBUG("Row from SQL query:", row);
|
|
||||||
rssRecords.push(new RSSRecord(row.id, row.title, row.link, row.category, row.guild_id, row.channel_id));
|
|
||||||
}
|
|
||||||
log.DEBUG("All records:", rssRecords);
|
|
||||||
callback(undefined, rssRecords);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
72
utilities/recordHelper.js
Normal file
72
utilities/recordHelper.js
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
// This is for record builders
|
||||||
|
|
||||||
|
const { DebugBuilder } = require("./debugBuilder");
|
||||||
|
const log = new DebugBuilder("server", "recordHelper");
|
||||||
|
|
||||||
|
class baseRSSRecord {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {*} _id The ID of the post from the DB
|
||||||
|
* @param {*} _title The title of the post
|
||||||
|
* @param {*} _link The link to the post
|
||||||
|
* @param {*} _category The category of the post
|
||||||
|
*/
|
||||||
|
constructor(_id, _title, _link, _category) {
|
||||||
|
this.id = _id;
|
||||||
|
this.title = _title;
|
||||||
|
this.link= _link;
|
||||||
|
this.category = _category;
|
||||||
|
}
|
||||||
|
|
||||||
|
getId() {
|
||||||
|
return this.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
get(key) {
|
||||||
|
log.DEBUG(`Getting key '${key}' from: `, Object(this));
|
||||||
|
if (!Object.keys(this).includes(key)) throw new Error(`Key is invalid ${key}`);
|
||||||
|
return this[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.baseRSSRecord = baseRSSRecord;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a Source record.
|
||||||
|
*
|
||||||
|
* A source record is the record of an RSS feed itself. Something like "https://www.leafly.com/feed".
|
||||||
|
*/
|
||||||
|
exports.RSSSourceRecord = class RSSSourceRecord extends baseRSSRecord{
|
||||||
|
/**
|
||||||
|
* @param {*} _id The ID of the post from the DB
|
||||||
|
* @param {*} _title The title of the post
|
||||||
|
* @param {*} _link The link to the post
|
||||||
|
* @param {*} _category The category of the post
|
||||||
|
* @param {*} _guild_id The guild id to receive updates on this source
|
||||||
|
* @param {*} _channel_id The channel to send updates from this source
|
||||||
|
*/
|
||||||
|
constructor(_id, _title, _link, _category, _guild_id, _channel_id) {
|
||||||
|
super(_id, _title, _link, _category);
|
||||||
|
this.guild_id = _guild_id;
|
||||||
|
this.channel_id = _channel_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a Post record.
|
||||||
|
*
|
||||||
|
* A post is an individual article/post/link from an RSS feed. Each individual post will be added to the database to be recalled and sent later.
|
||||||
|
*/
|
||||||
|
exports.RSSPostRecord = class RSSPostRecord extends baseRSSRecord{
|
||||||
|
/**
|
||||||
|
* @param {*} _id The ID of the post from the DB
|
||||||
|
* @param {*} _title The title of the post
|
||||||
|
* @param {*} _link The link to the post
|
||||||
|
* @param {*} _category The category of the post
|
||||||
|
* @param {*} _rssSourceID The ID of the RSS source that created this post
|
||||||
|
*/
|
||||||
|
constructor(_id, _title, _link, _category, _rssSourceID) {
|
||||||
|
super(_id, _title, _link, _category);
|
||||||
|
this.source_id = _rssSourceID;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user