From ad3f827a99484be39c01a3f25dead19f315867bb Mon Sep 17 00:00:00 2001 From: j Date: Sun, 16 Jun 2024 22:18:40 -0400 Subject: [PATCH] Included user ID in table, Modified to rate limit snappy seperately from rdrama --- .../001_create_user_mentions_table.sql | 3 +++ src/db/services/Database.ts | 27 ++++++++++++------- src/workflows/WorkflowOrchestrator.ts | 3 ++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/db/migrations/001_create_user_mentions_table.sql b/src/db/migrations/001_create_user_mentions_table.sql index bcd5883..e105dd6 100644 --- a/src/db/migrations/001_create_user_mentions_table.sql +++ b/src/db/migrations/001_create_user_mentions_table.sql @@ -5,6 +5,9 @@ CREATE TABLE IF NOT EXISTS user_mentions ( -- The unique identifier of the comment from the r/Drama platform. Not unique in this table -- because a single comment can mention multiple users. rdrama_comment_id TEXT NOT NULL, + + -- The ID of the user who made the comment. + rdrama_user_id INTEGER NOT NULL, -- The mentioned Reddit username in a standardized format (e.g., u/username). Lowercased -- to ensure consistency and prevent duplicate entries due to case differences. diff --git a/src/db/services/Database.ts b/src/db/services/Database.ts index 48537f2..c2c10cd 100644 --- a/src/db/services/Database.ts +++ b/src/db/services/Database.ts @@ -33,22 +33,25 @@ export class DatabaseService { * @example * await DatabaseService.insertUserMention({ * rdrama_comment_id: 456, + * rdrama_user_id: 123, * username: 'mentionedUser', * message: 'You were mentioned in a comment.' * }); * * @param {Object} mention - The user mention object to insert. * @param {number} mention.rdrama_comment_id - The ID of the comment from the r/Drama platform. + * @param {number} mention.rdrama_user_id - The ID of the user who made the comment. * @param {string} mention.username - The mentioned Reddit username. * @param {string} [mention.message] - The content of the message sent to the mentioned user. * @throws {Error} Will throw an error if the insert operation fails. */ - public static async insertUserMention(mention: { rdrama_comment_id: number; username: string; message?: string }): Promise { - const db = await DatabaseService.getDatabase() - const sql = `INSERT INTO user_mentions (rdrama_comment_id, username, message) VALUES (?, ?, ?)`; - await db.run(sql, [mention.rdrama_comment_id, mention.username, mention.message]); + public static async insertUserMention(mention: { rdrama_comment_id: number, rdrama_user_id: number, username: string, message?: string }): Promise { + const db = await DatabaseService.getDatabase(); + const sql = `INSERT INTO user_mentions (rdrama_comment_id, rdrama_user_id, username, message) VALUES (?, ?, ?, ?)`; + await db.run(sql, [mention.rdrama_comment_id, mention.rdrama_user_id, mention.username, mention.message]); } + /** * Queries the database to check if a username has been mentioned. * @@ -135,14 +138,18 @@ export class DatabaseService { * @returns {Promise} True if the cooldown period has passed, allowing new notifications to be sent. * @throws {Error} Will throw an error if the check operation fails. */ - public static async canSendNotification(): Promise { + public static async canSendNotification(comment: Comment): Promise { const db = await DatabaseService.getDatabase() const cooldownHours = process.env.NOTIFICATION_COOLDOWN_HOURS || 4; - const sql = ` - SELECT MAX(sent_time) as last_notification_time - FROM user_mentions - `; - const result = await db.get(sql); + const snappyUID = 261 + + let sql = ` + SELECT MAX(sent_time) as last_notification_time + FROM user_mentions + WHERE rdrama_user_id ${comment.author_id === snappyUID ? '=' : '!='} ? + `; + + const result = await db.get(sql, [snappyUID]) if (!result || !result.last_notification_time) { // No notifications have been sent yet, or unable to retrieve the last sent time. diff --git a/src/workflows/WorkflowOrchestrator.ts b/src/workflows/WorkflowOrchestrator.ts index a5adad4..3a06311 100644 --- a/src/workflows/WorkflowOrchestrator.ts +++ b/src/workflows/WorkflowOrchestrator.ts @@ -13,7 +13,7 @@ class WorkflowOrchestrator { */ async executeWorkflow(comment: Comment) { try { - const canSend = await DatabaseService.canSendNotification(); + const canSend = await DatabaseService.canSendNotification(comment); const coolDownHours = process.env.NOTIFICATION_COOLDOWN_HOURS if (!canSend) { console.log(`Last Message Sent less than ${coolDownHours ? coolDownHours : 4} hours ago. Set NOTIFICATION_COOLDOWN_HOURS to change this`) @@ -77,6 +77,7 @@ class WorkflowOrchestrator { await DatabaseService.insertUserMention({ rdrama_comment_id: comment.id, + rdrama_user_id: comment.author_id, username: redditUser, message: redditMessage, });