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 RedditSessionManager from "../reddit/session/SessionManager"; import { shouldNotifyUser } from "../utils/ShouldNotify"; class WorkflowOrchestrator { constructor( private commentProcessor: CommentProcessor, private commentParser: CommentParser, private commentPoster: CommentPoster, ) { } /** * Executes the defined workflow for processing comments. */ async executeWorkflow() { try { // Fetch comments from the source const comments = await this.commentProcessor.processComments(); console.log(`Fetched ${comments.length} comments`); // Extract and deduplicate usernames from comments const allUsernames = comments.flatMap(comment => this.commentParser.extractUsernames(comment)); const uniqueUsernames = [...new Set(allUsernames)]; console.log(`Extracted ${uniqueUsernames.length} unique usernames`); for (const comment of comments) { const redditUsers = this.commentParser.extractUsernames(comment) if (redditUsers.length === 0) continue console.log('found:', redditUsers) const placeholdersRdrama = { author_name: comment.author_name, }; for (const redditUser of redditUsers) { const userMentionExists = await DatabaseService.userMentionExists(redditUser) if (userMentionExists) continue const commentResponseRdrama = MessageService.getRandomRdramaMessage(placeholdersRdrama) if (!commentResponseRdrama) throw new Error('No comments for Rdrama found') const postedComment = await this.commentPoster.postComment(`c_${comment.id}`, `##### TEST MESSAGE NO REDDITOR PINGED (YET...)\n${commentResponseRdrama}`) //const postedComment = await this.commentPoster.postComment(`c_${comment.id}`, ${commentResponse}`) //TODO uncomment after golive console.log(`Sent Comment to`, JSON.stringify(postedComment, null, 4)) const redditSession = await RedditSessionManager.getInstance() const redditService = new RedditService(redditSession) const resultshouldNotifyUser = await shouldNotifyUser(redditUser, redditService) if (!resultshouldNotifyUser) continue 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) return; } } console.log('Workflow executed successfully.'); } catch (error) { console.error('An error occurred during workflow execution:', error); } } } export default WorkflowOrchestrator;