From a1d5f75d3c9220b53572131638a74ac8b13107da Mon Sep 17 00:00:00 2001 From: sloppyjosh Date: Fri, 1 Mar 2024 01:40:34 -0500 Subject: [PATCH] Comment Posting Functionality --- package-lock.json | 1 + package.json | 5 ++-- src/index.ts | 8 +++++- src/rdrama/services/CommentPoster.ts | 37 +++++++++++++++++++++++++++ src/utils/MessageService.ts | 4 +-- src/workflows/WorkflowOrchestrator.ts | 18 +++++++++++++ 6 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 src/rdrama/services/CommentPoster.ts diff --git a/package-lock.json b/package-lock.json index e848d86..ee3d053 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "axios-request-throttle": "^1.0.0", "axios-retry": "^4.0.0", "dotenv": "^16.4.5", + "form-data": "^4.0.0", "fs": "^0.0.1-security", "path": "^0.12.7", "sqlite": "^5.1.1", diff --git a/package.json b/package.json index 16f0ecb..9ea42fb 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "axios-request-throttle": "^1.0.0", "axios-retry": "^4.0.0", "dotenv": "^16.4.5", + "form-data": "^4.0.0", "fs": "^0.0.1-security", "path": "^0.12.7", "sqlite": "^5.1.1", @@ -15,7 +16,7 @@ "typescript": "^5.3.3" }, "scripts": { - "build": "tsc && copyfiles -u 3 \"src/db/migrations/*.sql\" dist/db/migrations && copyfiles -u 3 \"src/db/seed/*.sql\" dist/db/seed", + "build": "tsc && copyfiles -u 3 \"src/db/migrations/*.sql\" dist/db/migrations && copyfiles -u 3 \"src/db/seed/*.sql\" dist/db/seed && copyfiles -u 2 \"src/messages/*.txt\" dist/messages", "start": "node dist/index.js" } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index e776d81..1b95e4a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,8 @@ import { DatabaseInitializer } from './db/initializeDatabase'; import { DatabaseService } from './db/services/Database'; import { DatabaseMaintenanceService } from './db/services/DatabaseMaintenance'; import { CommentProcessor } from './rdrama/services/CommentProcessor'; +import { MessageService } from './utils/MessageService'; +import { CommentPoster } from './rdrama/services/CommentPoster'; // Import other necessary services or configurations async function startApplication() { @@ -31,11 +33,15 @@ async function startApplication() { // Initialize services with any required dependencies const commentFetcher = new CommentProcessor(databaseService); const commentParser = new CommentParser(); + const commentPoster = new CommentPoster() + const messageService = new MessageService(); // Initialize and start your workflow const workflowOrchestrator = new WorkflowOrchestrator( commentFetcher, - commentParser + commentParser, + commentPoster, + messageService, ); await workflowOrchestrator.executeWorkflow(); } diff --git a/src/rdrama/services/CommentPoster.ts b/src/rdrama/services/CommentPoster.ts new file mode 100644 index 0000000..f10cb42 --- /dev/null +++ b/src/rdrama/services/CommentPoster.ts @@ -0,0 +1,37 @@ +import SessionManager from '../session/SessionManager'; +import { Comment } from '../models/Comment'; +import FormData from 'form-data'; + +export class CommentPoster { + private sessionManager: SessionManager; + + constructor() { + this.sessionManager = SessionManager.getInstance(); + } + + /** + * Posts a comment as a reply to a given rdrama comment. + * + * @param parentId The ID of the parent comment to reply to. Expected format: 'c_{id}'. + * @param body The body of the comment to post. + * @returns A promise resolving to the Axios response. + */ + public async postComment(parentId: string, body: string): Promise { + const formData = new FormData(); + formData.append('parent_fullname', parentId); + formData.append('body', body); + + try { + const response = await this.sessionManager.axiosInstance.post('/comment', formData, { + headers: { + 'Content-Type': 'multipart/form-data' + } + }); + console.log(`Comment posted successfully to ${parentId}`); + return response.data; + } catch (error) { + console.error(`Failed to post comment to ${parentId}:`, error); + throw error; // Rethrow for handling elsewhere + } + } +} diff --git a/src/utils/MessageService.ts b/src/utils/MessageService.ts index 7172bc5..c42c63d 100644 --- a/src/utils/MessageService.ts +++ b/src/utils/MessageService.ts @@ -11,14 +11,14 @@ export class MessageService { private loadMessages() { try { - const redditMessagesPath = path.join(__dirname, 'messages', 'reddit_messages.txt'); + 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); } try { - const rdramaMessagesPath = path.join(__dirname, 'messages', 'rdrama_messages.txt'); + const rdramaMessagesPath = path.join(__dirname, '..', 'messages', 'rdrama_messages.txt'); this.rdramaMessages = fs.readFileSync(rdramaMessagesPath, 'utf-8').split('---END---').filter(line => line.trim()); } catch (error) { console.error('Failed to load rDrama messages:', error); diff --git a/src/workflows/WorkflowOrchestrator.ts b/src/workflows/WorkflowOrchestrator.ts index e30e284..aee0d04 100644 --- a/src/workflows/WorkflowOrchestrator.ts +++ b/src/workflows/WorkflowOrchestrator.ts @@ -1,10 +1,14 @@ import { CommentProcessor } from "../rdrama/services/CommentProcessor"; import { CommentParser } from "../rdrama/services/CommentParser"; +import { CommentPoster } from "../rdrama/services/CommentPoster"; +import { MessageService } from "../utils/MessageService"; class WorkflowOrchestrator { constructor( private commentProcessor: CommentProcessor, private commentParser: CommentParser, + private commentPoster: CommentPoster, + private messageService: MessageService, //private redditNotifier: RedditNotifier // Handles notifications to Reddit users ) { } @@ -22,6 +26,20 @@ class WorkflowOrchestrator { 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 placeholders = { + author_name: comment.author_name, + }; + const commentResponse = this.messageService.getRandomRdramaMessage(placeholders) + const postedComment = await this.commentPoster.postComment(`c_${comment.id}`, `##### TEST MESSAGE NO REDDITOR PINGED (YET...)\n${commentResponse}`) + //const postedComment = await this.commentPoster.postComment(`c_${comment.id}`, ${commentResponse}`) //TODO make this live + console.log(`Sent Comment to`, JSON.stringify(postedComment, null, 4)) + //FOR now, only reply to one user and we would check against DB with a random interval, we should only message between 2-3 people daily? + } + //// Query user information based on usernames //const userInfo = await this.databaseService.queryUsersInfo(uniqueUsernames); //console.log(`Queried information for ${userInfo.length} users`);