CrossTalkPM/src/workflows/WorkflowOrchestrator.ts

100 lines
4.0 KiB
TypeScript
Raw Normal View History

import { CommentProcessor } from "../rdrama/services/CommentProcessor";
2024-02-23 06:18:00 +00:00
import { CommentParser } from "../rdrama/services/CommentParser";
2024-03-01 06:40:34 +00:00
import { CommentPoster } from "../rdrama/services/CommentPoster";
import { MessageService } from "../utils/MessageService";
2024-03-05 06:25:05 +00:00
import { DatabaseService } from "../db/services/Database";
import { RedditService } from "../reddit/services/Reddit";
2024-03-05 06:25:05 +00:00
import { shouldNotifyUser } from "../utils/ShouldNotify";
2024-03-09 07:34:45 +00:00
import { Comment } from "../rdrama/models/Comment";
2024-02-23 06:18:00 +00:00
class WorkflowOrchestrator {
/**
* Executes the defined workflow for processing comments.
*/
async executeWorkflow() {
try {
2024-03-09 07:34:45 +00:00
const comments = await this.fetchAndLogComments();
2024-02-23 06:18:00 +00:00
2024-03-01 06:40:34 +00:00
for (const comment of comments) {
2024-03-09 07:34:45 +00:00
await this.processComment(comment);
2024-03-05 06:25:05 +00:00
}
2024-03-09 07:34:45 +00:00
2024-02-23 06:18:00 +00:00
console.log('Workflow executed successfully.');
} catch (error) {
console.error('An error occurred during workflow execution:', error);
}
}
2024-03-09 07:34:45 +00:00
/**
* 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.
*/
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);
}
2024-02-23 06:18:00 +00:00
}
export default WorkflowOrchestrator;