import { CommentProcessor } from "../rdrama/services/CommentProcessor"; 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 { shouldNotifyUser } from "../utils/ShouldNotify"; import { Comment } from "../rdrama/models/Comment"; class WorkflowOrchestrator { /** * Executes the defined workflow for processing comments. */ async executeWorkflow() { try { const comments = await this.fetchAndLogComments(); for (const comment of comments) { await this.processComment(comment); } console.log('Workflow executed successfully.'); } catch (error) { console.error('An error occurred during workflow execution:', error); } } /** * Fetches comments and logs the count. * @returns {Promise} The fetched comments. */ async fetchAndLogComments(): Promise { 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. */ async processComment(comment: Comment) { const redditUsers = CommentParser.extractUsernames(comment); 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); } /** * 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. 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); if (!redditMessage) throw new Error('No comments for Reddit found'); await DatabaseService.insertUserMention({ rdrama_comment_id: comment.id, username: redditUser, message: redditMessage, }); await RedditService.sendMessage(redditUser, 'Crosstalk PM Notification', redditMessage); } } export default WorkflowOrchestrator;