From 1603c7bf07429dc6dd69268236b871ab54cd0201 Mon Sep 17 00:00:00 2001 From: sloppyjosh Date: Wed, 6 Mar 2024 00:17:42 -0500 Subject: [PATCH] thoroughly testing notifications and timing --- src/db/services/database.ts | 10 ++++++---- src/index.ts | 1 + src/rdrama/services/CommentParser.ts | 8 ++++---- src/reddit/services/reddit.ts | 2 +- src/utils/ShouldNotify.ts | 2 +- src/workflows/WorkflowOrchestrator.ts | 4 +++- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/db/services/database.ts b/src/db/services/database.ts index b144790..8cf346c 100644 --- a/src/db/services/database.ts +++ b/src/db/services/database.ts @@ -133,7 +133,7 @@ export class DatabaseService { const { access_token, token_type, expires_in, scope } = tokenData; const expiryTimestamp = Math.floor(Date.now() / 1000) + expires_in; console.log('token_identifier', token_identifier) - console.log('access_token', access_token) + console.log('access_token', `${access_token.substring(0, 5)}XXXXX`) console.log('token_type', token_type) console.log('expires_in', expires_in) console.log('scope', scope) @@ -170,7 +170,7 @@ export class DatabaseService { * @returns {Promise} True if the cooldown has passed, allowing new notifications to be sent. */ public async canSendNotification(): Promise { - const cooldownHours = parseInt(process.env.NOTIFICATION_COOLDOWN_HOURS || '4', 10); + const cooldownHours = process.env.NOTIFICATION_COOLDOWN_HOURS || 4; const sql = ` SELECT MAX(sent_time) as last_notification_time FROM user_mentions @@ -183,9 +183,11 @@ export class DatabaseService { } const lastNotificationTime = new Date(result.last_notification_time).getTime(); - const currentTime = Date.now(); + const currentTime = new Date(new Date().toISOString().slice(0, 19).replace('T', ' ')).getTime(); const timeElapsed = currentTime - lastNotificationTime; - const cooldownPeriod = cooldownHours * 60 * 60 * 1000; // Convert hours to milliseconds + console.log('timeElapsed', timeElapsed) + const cooldownPeriod = +cooldownHours * 60 * 60 * 1000; // Convert hours to milliseconds + console.log('cooldownPeriod', cooldownPeriod) return timeElapsed >= cooldownPeriod; } diff --git a/src/index.ts b/src/index.ts index d7ef158..6f47b5a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,6 +25,7 @@ async function startApplication() { 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`) + return; } console.log('RDrama Session Start') diff --git a/src/rdrama/services/CommentParser.ts b/src/rdrama/services/CommentParser.ts index 4412d12..aa67a97 100644 --- a/src/rdrama/services/CommentParser.ts +++ b/src/rdrama/services/CommentParser.ts @@ -10,20 +10,20 @@ export class CommentParser { */ public extractUsernames(comment: Comment): string[] { const foundUsernames: Set = new Set(); - + const matches = comment.body.match(this.regexPattern); if (matches) { matches.forEach(match => { // Ensure the username is captured in a standardized format const usernameMatch = match.trim().match(/\/?u\/([a-zA-Z0-9_]+)/); if (usernameMatch) { - // Standardize to "u/username" format - const username = `u/${usernameMatch[1].toLowerCase()}`; + // Standardize to "username" format + const username = `${usernameMatch[1].toLowerCase()}`; foundUsernames.add(username); } }); } - + return Array.from(foundUsernames); } } diff --git a/src/reddit/services/reddit.ts b/src/reddit/services/reddit.ts index 641cf8f..6ee9f3c 100644 --- a/src/reddit/services/reddit.ts +++ b/src/reddit/services/reddit.ts @@ -1,6 +1,6 @@ import axios, { AxiosError } from 'axios'; import SessionManager from '../session/SessionManager'; -import { RedditUser } from '../model/user'; +import { RedditUser } from '../model/User'; export class RedditService { private sessionManager: SessionManager; diff --git a/src/utils/ShouldNotify.ts b/src/utils/ShouldNotify.ts index d47f2c2..250be7f 100644 --- a/src/utils/ShouldNotify.ts +++ b/src/utils/ShouldNotify.ts @@ -1,5 +1,5 @@ import dotenv from 'dotenv'; -import { RedditService } from '../reddit/services/reddit'; +import { RedditService } from '../reddit/services/Reddit'; import { DatabaseService } from '../db/services/Database'; // Load environment variables from .env file diff --git a/src/workflows/WorkflowOrchestrator.ts b/src/workflows/WorkflowOrchestrator.ts index 852bb53..5bdeadc 100644 --- a/src/workflows/WorkflowOrchestrator.ts +++ b/src/workflows/WorkflowOrchestrator.ts @@ -3,7 +3,7 @@ import { CommentParser } from "../rdrama/services/CommentParser"; import { CommentPoster } from "../rdrama/services/CommentPoster"; import { MessageService } from "../utils/MessageService"; import { DatabaseService } from "../db/services/Database"; -import { RedditService } from "../reddit/services/reddit"; +import { RedditService } from "../reddit/services/Reddit"; import RedditSessionManager from "../reddit/session/SessionManager"; import { shouldNotifyUser } from "../utils/ShouldNotify"; @@ -50,6 +50,8 @@ class WorkflowOrchestrator { if (!resultshouldNotifyUser) continue const placeholdersReddit = { author_name: comment.author_name, + username: redditUser, + permalink: comment.permalink }; const redditMessage = this.messageService.getRandomRedditMessage(placeholdersReddit) await this.databaseService.insertUserMention({