diff --git a/src/index.ts b/src/index.ts index 676e843..830a2be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,14 +46,12 @@ async function startApplication() { const commentFetcher = new CommentProcessor(); const commentParser = new CommentParser(); const commentPoster = new CommentPoster() - const messageService = new MessageService(); // Initialize and start your workflow const workflowOrchestrator = new WorkflowOrchestrator( commentFetcher, commentParser, - commentPoster, - messageService + commentPoster ); await workflowOrchestrator.executeWorkflow(); } diff --git a/src/utils/MessageService.ts b/src/utils/MessageService.ts index c42c63d..fc8bb30 100644 --- a/src/utils/MessageService.ts +++ b/src/utils/MessageService.ts @@ -1,41 +1,93 @@ import fs from 'fs'; import path from 'path'; +/** + * Manages the retrieval and formatting of messages stored in text files. + * This class provides functionality to load messages for rDrama and Reddit, + * select a random message, and replace placeholders within that message + * with specified values. + */ export class MessageService { - private redditMessages: string[] = []; - private rdramaMessages: string[] = []; - - constructor() { - this.loadMessages(); - } - - private loadMessages() { - try { - const redditMessagesPath = path.join(__dirname, '..', 'messages', 'reddit_messages.txt'); - this.redditMessages = fs.readFileSync(redditMessagesPath, 'utf-8').split('---END---').filter(line => line.trim()); - } catch (error) { - console.error('Failed to load Reddit messages:', error); - } + /** + * Loads rDrama messages from a text file, splitting by a specific delimiter. + * Each message is separated by '---END---' in the text file. + * + * @example + * const rdramaMessages = MessageService.loadRdramaMessages(); + * + * @returns {string[] | undefined} An array of rDrama messages, or undefined if there was an error loading the messages. + */ + private static loadRdramaMessages(): string[] | undefined { try { const rdramaMessagesPath = path.join(__dirname, '..', 'messages', 'rdrama_messages.txt'); - this.rdramaMessages = fs.readFileSync(rdramaMessagesPath, 'utf-8').split('---END---').filter(line => line.trim()); + return fs.readFileSync(rdramaMessagesPath, 'utf-8').split('---END---').filter(line => line.trim()); } catch (error) { console.error('Failed to load rDrama messages:', error); } } - public getRandomRedditMessage(placeholders: { [key: string]: string }): string { - const message = this.redditMessages[Math.floor(Math.random() * this.redditMessages.length)]; + /** + * Loads Reddit messages from a text file, splitting by a specific delimiter. + * Each message is separated by '---END---' in the text file. + * + * @example + * const redditMessages = MessageService.loadRedditMessages(); + * + * @returns {string[] | undefined} An array of Reddit messages, or undefined if there was an error loading the messages. + */ + private static loadRedditMessages(): string[] | undefined { + try { + const redditMessagesPath = path.join(__dirname, '..', 'messages', 'reddit_messages.txt'); + return fs.readFileSync(redditMessagesPath, 'utf-8').split('---END---').filter(line => line.trim()); + } catch (error) { + console.error('Failed to load Reddit messages:', error); + } + } + + /** + * Selects a random Reddit message from the loaded messages and replaces placeholders within it. + * + * @example + * const message = MessageService.getRandomRedditMessage({ username: 'exampleUser' }); + * + * @param {Object} placeholders - A mapping of placeholder names to their replacement values. + * @returns {string | undefined} A formatted Reddit message with placeholders replaced, or undefined if messages couldn't be loaded. + */ + public static getRandomRedditMessage(placeholders: { [key: string]: string }): string | undefined { + const redditMessages = this.loadRedditMessages() + if (!redditMessages) return + const message = redditMessages[Math.floor(Math.random() * redditMessages.length)]; return this.replacePlaceholders(message, placeholders); } - public getRandomRdramaMessage(placeholders: { [key: string]: string }): string { - const message = this.rdramaMessages[Math.floor(Math.random() * this.rdramaMessages.length)]; + /** + * Selects a random rDrama message from the loaded messages and replaces placeholders within it. + * + * @example + * const message = MessageService.getRandomRdramaMessage({ username: 'exampleUser' }); + * + * @param {Object} placeholders - A mapping of placeholder names to their replacement values. + * @returns {string | undefined} A formatted rDrama message with placeholders replaced, or undefined if messages couldn't be loaded. + */ + public static getRandomRdramaMessage(placeholders: { [key: string]: string }): string | undefined { + const rdramaMessages = this.loadRdramaMessages() + if (!rdramaMessages) return + const message = rdramaMessages[Math.floor(Math.random() * rdramaMessages.length)]; return this.replacePlaceholders(message, placeholders); } - private replacePlaceholders(message: string, placeholders: { [key: string]: string }): string { + /** + * Replaces placeholders in a message with values from a provided mapping. + * + * @example + * const formattedMessage = MessageService.replacePlaceholders('Hello, {username}!', { username: 'exampleUser' }); + * + * @param {string} message - The message containing placeholders. + * @param {Object} placeholders - A mapping of placeholder names to their replacement values. + * @returns {string} The message with placeholders replaced by actual values. + */ + private static replacePlaceholders(message: string, placeholders: { [key: string]: string }): string { return Object.keys(placeholders).reduce((acc, key) => { const regex = new RegExp(`{${key}}`, 'g'); return acc.replace(regex, placeholders[key]); diff --git a/src/workflows/WorkflowOrchestrator.ts b/src/workflows/WorkflowOrchestrator.ts index 5c56896..cad7332 100644 --- a/src/workflows/WorkflowOrchestrator.ts +++ b/src/workflows/WorkflowOrchestrator.ts @@ -12,7 +12,6 @@ class WorkflowOrchestrator { private commentProcessor: CommentProcessor, private commentParser: CommentParser, private commentPoster: CommentPoster, - private messageService: MessageService, ) { } /** @@ -39,7 +38,8 @@ class WorkflowOrchestrator { for (const redditUser of redditUsers) { const userMentionExists = await DatabaseService.userMentionExists(redditUser) if (userMentionExists) continue - const commentResponseRdrama = this.messageService.getRandomRdramaMessage(placeholdersRdrama) + 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)) @@ -52,7 +52,8 @@ class WorkflowOrchestrator { username: redditUser, permalink: comment.permalink }; - const redditMessage = this.messageService.getRandomRedditMessage(placeholdersReddit) + 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,