streamlined orchestrator, included new message banks to prompt users

pull/2/head
j 2024-03-23 01:03:35 -04:00
parent 689df79ab0
commit be40118b1a
1 changed files with 32 additions and 45 deletions

View File

@ -1,7 +1,6 @@
import { CommentProcessor } from "../rdrama/services/CommentProcessor";
import { CommentParser } from "../rdrama/services/CommentParser";
import { CommentPoster } from "../rdrama/services/CommentPoster";
import { MessageService } from "../utils/MessageService";
import { MessageFileName, MessageService } from "../utils/MessageService";
import { DatabaseService } from "../db/services/Database";
import { RedditService } from "../reddit/services/Reddit";
import { shouldNotifyUser } from "../utils/ShouldNotify";
@ -12,30 +11,20 @@ class WorkflowOrchestrator {
/**
* Executes the defined workflow for processing comments.
*/
async executeWorkflow() {
async executeWorkflow(comment: Comment) {
try {
const comments = await this.fetchAndLogComments();
for (const comment of comments) {
await this.processComment(comment);
const canSend = await DatabaseService.canSendNotification();
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('Workflow executed successfully.');
await this.processComment(comment);
} catch (error) {
console.error('An error occurred during workflow execution:', error);
}
}
/**
* Fetches comments and logs the count.
* @returns {Promise<Array>} The fetched comments.
*/
async fetchAndLogComments(): Promise<Comment[]> {
const comments = await CommentProcessor.processComments();
console.log(`Fetched ${comments.length} comments`);
return comments;
}
/**
* Processes a single comment, including posting responses and sending notifications.
* @param {Object} comment The comment to process.
@ -45,47 +34,45 @@ class WorkflowOrchestrator {
if (redditUsers.length === 0) return;
console.log('found:', redditUsers);
for (const redditUser of redditUsers) {
await this.handleUserMention(comment, redditUser);
}
}
/**
* Handles a mention of a user in a comment, including checking for previous mentions, posting a response, and sending a notification.
* @param {Object} comment The comment mentioning the user.
* @param {string} redditUser The mentioned Reddit user's username.
*/
async handleUserMention(comment: Comment, redditUser: string) {
const userMentionExists = await DatabaseService.userMentionExists(redditUser);
if (userMentionExists) return;
const placeholdersRdrama = { author_name: comment.author_name };
const commentResponseRdrama = MessageService.getRandomRdramaMessage(placeholdersRdrama);
if (!commentResponseRdrama) throw new Error('No comments for Rdrama found');
await this.postCommentAndNotify(comment, redditUser, commentResponseRdrama);
await this.postCommentAndNotify(comment, redditUsers[0]);
}
/**
* Posts a comment response and sends a notification if the user should be notified.
* @param {Object} comment The original comment.
* @param {string} redditUser The Reddit user to notify.
* @param {string} commentResponseRdrama The response to post.
*/
async postCommentAndNotify(comment: Comment, redditUser: string, commentResponseRdrama: string) {
// Placeholder for posting a comment. Uncomment and implement as needed.
async postCommentAndNotify(comment: Comment, redditUser: string) {
const placeholdersRdrama = { author_name: comment.author_name };
const userMentionExists = await DatabaseService.userMentionExists(redditUser);
if (userMentionExists) {
const commentPreviouslyMessaged = MessageService.getRandomMessage(MessageFileName.RdramaPreviousMessage, placeholdersRdrama);
if (!commentPreviouslyMessaged) throw new Error('No comments for previous Message found');
const postedComment = await CommentPoster.postComment(`c_${comment.id}`, `${commentPreviouslyMessaged}`);
console.log(`Sent Comment to`, JSON.stringify(postedComment, null, 4));
return;
}
const resultshouldNotifyUser = await shouldNotifyUser(redditUser);
if (!resultshouldNotifyUser) {
const commentShouldntNotify = MessageService.getRandomMessage(MessageFileName.RdramaShouldntNotify, placeholdersRdrama);
if (!commentShouldntNotify) throw new Error('No comments for Shouldnt Notify found');
const postedComment = await CommentPoster.postComment(`c_${comment.id}`, `${commentShouldntNotify}`);
console.log(`Sent Comment to`, JSON.stringify(postedComment, null, 4));
return;
}
const commentResponseRdrama = MessageService.getRandomMessage(MessageFileName.RdramaMessages, placeholdersRdrama);
if (!commentResponseRdrama) throw new Error('No comments for Rdrama found');
const postedComment = await CommentPoster.postComment(`c_${comment.id}`, `${commentResponseRdrama}`);
console.log(`Sent Comment to`, JSON.stringify(postedComment, null, 4));
const resultshouldNotifyUser = await shouldNotifyUser(redditUser);
if (!resultshouldNotifyUser) return;
const placeholdersReddit = {
author_name: comment.author_name,
username: redditUser,
permalink: comment.permalink
};
const redditMessage = MessageService.getRandomRedditMessage(placeholdersReddit);
const redditMessage = MessageService.getRandomMessage(MessageFileName.RedditMessages, placeholdersReddit);
if (!redditMessage) throw new Error('No comments for Reddit found');
await DatabaseService.insertUserMention({