removed db maintenance and comment storage as these are in redis now

pull/2/head
j 2024-03-23 01:00:35 -04:00
parent 34a3de1fce
commit 6a8bacd265
1 changed files with 2 additions and 111 deletions

View File

@ -26,43 +26,6 @@ export class DatabaseService {
return db return db
} }
/**
* Inserts a new comment into the database.
* This static method constructs an SQL statement to insert all fields of the Comment object
* into the corresponding columns in the 'comments' table.
*
* @example
* await DatabaseService.insertComment({
* id: 1,
* author_id: 123,
* author_name: 'exampleUser',
* body: 'This is a comment.',
* // More fields as per the Comment type
* });
*
* @param {Comment} comment - The comment object to insert.
* @throws {Error} Will throw an error if the insert operation fails.
*/
public static async insertComment(comment: Comment): Promise<void> {
const db = await DatabaseService.getDatabase()
const sql = `
INSERT INTO comments (
id, author_id, author_name, body, body_html, created_utc, deleted_utc,
distinguished, downvotes, edited_utc, is_banned, is_bot, is_nsfw, level,
permalink, pinned, post_id, replies, reports, score, upvotes
) VALUES (
?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?
)
`;
await db.run(sql, [
comment.id, comment.author_id, comment.author_name, comment.body, comment.body_html, comment.created_utc, comment.deleted_utc,
comment.distinguished ? 1 : 0, comment.downvotes, comment.edited_utc, comment.is_banned ? 1 : 0, comment.is_bot ? 1 : 0, comment.is_nsfw ? 1 : 0, comment.level,
comment.permalink, comment.pinned, comment.post_id, JSON.stringify(comment.replies), JSON.stringify(comment.reports), comment.score, comment.upvotes
]);
}
/** /**
* Inserts a new user mention into the database. * Inserts a new user mention into the database.
* This static method adds a record of a user being mentioned in a comment. * This static method adds a record of a user being mentioned in a comment.
@ -86,24 +49,6 @@ export class DatabaseService {
await db.run(sql, [mention.rdrama_comment_id, mention.username, mention.message]); await db.run(sql, [mention.rdrama_comment_id, mention.username, mention.message]);
} }
/**
* Queries the database for an existing comment by its ID.
*
* @example
* const exists = await DatabaseService.commentExists('123');
* console.log(exists ? 'Comment exists.' : 'Comment does not exist.');
*
* @param {string} commentId - The ID of the comment to search for.
* @returns {Promise<boolean>} A boolean indicating whether the comment exists.
* @throws {Error} Will throw an error if the query operation fails.
*/
public static async commentExists(commentId: string): Promise<boolean> {
const db = await DatabaseService.getDatabase()
const sql = `SELECT 1 FROM comments WHERE id = ?`;
const result = await db.get(sql, [commentId]);
return !!result;
}
/** /**
* Queries the database to check if a username has been mentioned. * Queries the database to check if a username has been mentioned.
* *
@ -122,60 +67,6 @@ export class DatabaseService {
return !!result; return !!result;
} }
/**
* Updates the last run timestamp for a maintenance task, using an "upsert" approach.
*
* @example
* await DatabaseService.updateLastRunTimestamp('purgeOldComments');
*
* @param {string} taskName - The name of the maintenance task.
* @throws {Error} Will throw an error if the update operation fails.
*/
public static async getLastRunTimestamp(taskName: string): Promise<Date | null> {
const db = await DatabaseService.getDatabase()
const result = await db.get(`SELECT last_run FROM maintenance_log WHERE task_name = ?`, [taskName]);
return result ? new Date(result.last_run) : null;
}
/**
* Updates the last run timestamp for a maintenance task, using an "upsert" approach.
*
* @example
* await DatabaseService.updateLastRunTimestamp('purgeOldComments');
*
* @param {string} taskName - The name of the maintenance task.
* @throws {Error} Will throw an error if the update operation fails.
*/
public static async updateLastRunTimestamp(taskName: string): Promise<void> {
// Assumes an "upsert" approach for the maintenance_log table
const db = await DatabaseService.getDatabase()
await db.run(
`INSERT INTO maintenance_log (task_name, last_run)
VALUES (?, ?)
ON CONFLICT(task_name)
DO UPDATE SET last_run = ?`,
[taskName, new Date(), new Date()]
);
}
/**
* Deletes comments from the database older than a specified number of days.
*
* @example
* await DatabaseService.purgeOldComments(30); // Purge comments older than 30 days
*
* @param {number} days - The age of comments to be purged, in days.
* @throws {Error} Will throw an error if the purge operation fails.
*/
public static async purgeOldComments(days: number = 1): Promise<void> {
const db = await DatabaseService.getDatabase()
console.log(`Purging comments older than ${days} days...`);
await db.run(`
DELETE FROM comments
WHERE datetime(created_utc, 'unixepoch') < datetime('now', '-${days} days')
`);
}
/** /**
* Inserts or updates the OAuth token in the database for a specific service. * Inserts or updates the OAuth token in the database for a specific service.
* *
@ -261,9 +152,9 @@ export class DatabaseService {
const lastNotificationTime = new Date(result.last_notification_time).getTime(); const lastNotificationTime = new Date(result.last_notification_time).getTime();
const currentTime = new Date(new Date().toISOString().slice(0, 19).replace('T', ' ')).getTime(); const currentTime = new Date(new Date().toISOString().slice(0, 19).replace('T', ' ')).getTime();
const timeElapsed = currentTime - lastNotificationTime; const timeElapsed = currentTime - lastNotificationTime;
console.log('timeElapsed', timeElapsed) //console.log('timeElapsed', timeElapsed)
const cooldownPeriod = +cooldownHours * 60 * 60 * 1000; // Convert hours to milliseconds const cooldownPeriod = +cooldownHours * 60 * 60 * 1000; // Convert hours to milliseconds
console.log('cooldownPeriod', cooldownPeriod) //console.log('cooldownPeriod', cooldownPeriod)
return timeElapsed >= cooldownPeriod; return timeElapsed >= cooldownPeriod;
} }