Included user ID in table, Modified to rate limit snappy seperately from rdrama
parent
5345d182b7
commit
ad3f827a99
|
@ -6,6 +6,9 @@ CREATE TABLE IF NOT EXISTS user_mentions (
|
|||
-- 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.
|
||||
username TEXT NOT NULL,
|
||||
|
|
|
@ -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<void> {
|
||||
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<void> {
|
||||
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<boolean>} 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<boolean> {
|
||||
public static async canSendNotification(comment: Comment): Promise<boolean> {
|
||||
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.
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue