thoroughly testing notifications and timing

pull/1/head
sloppyjosh 2024-03-06 00:17:42 -05:00
parent 35956bb7b1
commit 1603c7bf07
6 changed files with 16 additions and 11 deletions

View File

@ -133,7 +133,7 @@ export class DatabaseService {
const { access_token, token_type, expires_in, scope } = tokenData; const { access_token, token_type, expires_in, scope } = tokenData;
const expiryTimestamp = Math.floor(Date.now() / 1000) + expires_in; const expiryTimestamp = Math.floor(Date.now() / 1000) + expires_in;
console.log('token_identifier', token_identifier) 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('token_type', token_type)
console.log('expires_in', expires_in) console.log('expires_in', expires_in)
console.log('scope', scope) console.log('scope', scope)
@ -170,7 +170,7 @@ export class DatabaseService {
* @returns {Promise<boolean>} True if the cooldown has passed, allowing new notifications to be sent. * @returns {Promise<boolean>} True if the cooldown has passed, allowing new notifications to be sent.
*/ */
public async canSendNotification(): Promise<boolean> { public async canSendNotification(): Promise<boolean> {
const cooldownHours = parseInt(process.env.NOTIFICATION_COOLDOWN_HOURS || '4', 10); const cooldownHours = process.env.NOTIFICATION_COOLDOWN_HOURS || 4;
const sql = ` const sql = `
SELECT MAX(sent_time) as last_notification_time SELECT MAX(sent_time) as last_notification_time
FROM user_mentions FROM user_mentions
@ -183,9 +183,11 @@ export class DatabaseService {
} }
const lastNotificationTime = new Date(result.last_notification_time).getTime(); 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 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; return timeElapsed >= cooldownPeriod;
} }

View File

@ -25,6 +25,7 @@ async function startApplication() {
const coolDownHours = process.env.NOTIFICATION_COOLDOWN_HOURS const coolDownHours = process.env.NOTIFICATION_COOLDOWN_HOURS
if (!canSend) { if (!canSend) {
console.log(`Last Message Sent less than ${coolDownHours ? coolDownHours : 4} hours ago. Set NOTIFICATION_COOLDOWN_HOURS to change this`) 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') console.log('RDrama Session Start')

View File

@ -10,20 +10,20 @@ export class CommentParser {
*/ */
public extractUsernames(comment: Comment): string[] { public extractUsernames(comment: Comment): string[] {
const foundUsernames: Set<string> = new Set(); const foundUsernames: Set<string> = new Set();
const matches = comment.body.match(this.regexPattern); const matches = comment.body.match(this.regexPattern);
if (matches) { if (matches) {
matches.forEach(match => { matches.forEach(match => {
// Ensure the username is captured in a standardized format // Ensure the username is captured in a standardized format
const usernameMatch = match.trim().match(/\/?u\/([a-zA-Z0-9_]+)/); const usernameMatch = match.trim().match(/\/?u\/([a-zA-Z0-9_]+)/);
if (usernameMatch) { if (usernameMatch) {
// Standardize to "u/username" format // Standardize to "username" format
const username = `u/${usernameMatch[1].toLowerCase()}`; const username = `${usernameMatch[1].toLowerCase()}`;
foundUsernames.add(username); foundUsernames.add(username);
} }
}); });
} }
return Array.from(foundUsernames); return Array.from(foundUsernames);
} }
} }

View File

@ -1,6 +1,6 @@
import axios, { AxiosError } from 'axios'; import axios, { AxiosError } from 'axios';
import SessionManager from '../session/SessionManager'; import SessionManager from '../session/SessionManager';
import { RedditUser } from '../model/user'; import { RedditUser } from '../model/User';
export class RedditService { export class RedditService {
private sessionManager: SessionManager; private sessionManager: SessionManager;

View File

@ -1,5 +1,5 @@
import dotenv from 'dotenv'; import dotenv from 'dotenv';
import { RedditService } from '../reddit/services/reddit'; import { RedditService } from '../reddit/services/Reddit';
import { DatabaseService } from '../db/services/Database'; import { DatabaseService } from '../db/services/Database';
// Load environment variables from .env file // Load environment variables from .env file

View File

@ -3,7 +3,7 @@ import { CommentParser } from "../rdrama/services/CommentParser";
import { CommentPoster } from "../rdrama/services/CommentPoster"; import { CommentPoster } from "../rdrama/services/CommentPoster";
import { MessageService } from "../utils/MessageService"; import { MessageService } from "../utils/MessageService";
import { DatabaseService } from "../db/services/Database"; 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 RedditSessionManager from "../reddit/session/SessionManager";
import { shouldNotifyUser } from "../utils/ShouldNotify"; import { shouldNotifyUser } from "../utils/ShouldNotify";
@ -50,6 +50,8 @@ class WorkflowOrchestrator {
if (!resultshouldNotifyUser) continue if (!resultshouldNotifyUser) continue
const placeholdersReddit = { const placeholdersReddit = {
author_name: comment.author_name, author_name: comment.author_name,
username: redditUser,
permalink: comment.permalink
}; };
const redditMessage = this.messageService.getRandomRedditMessage(placeholdersReddit) const redditMessage = this.messageService.getRandomRedditMessage(placeholdersReddit)
await this.databaseService.insertUserMention({ await this.databaseService.insertUserMention({