CrossTalkPM/src/workflows/WorkflowOrchestrator.ts

74 lines
3.8 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 RedditSessionManager from "../reddit/session/SessionManager";
import { shouldNotifyUser } from "../utils/ShouldNotify";
2024-02-23 06:18:00 +00:00
class WorkflowOrchestrator {
constructor(
private commentProcessor: CommentProcessor,
2024-02-23 07:02:46 +00:00
private commentParser: CommentParser,
2024-03-01 06:40:34 +00:00
private commentPoster: CommentPoster,
2024-02-23 07:02:46 +00:00
) { }
2024-02-23 06:18:00 +00:00
/**
* Executes the defined workflow for processing comments.
*/
async executeWorkflow() {
try {
// Fetch comments from the source
const comments = await this.commentProcessor.processComments();
2024-02-23 06:18:00 +00:00
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`);
2024-03-01 06:40:34 +00:00
for (const comment of comments) {
const redditUsers = this.commentParser.extractUsernames(comment)
if (redditUsers.length === 0) continue
console.log('found:', redditUsers)
2024-03-05 06:25:05 +00:00
const placeholdersRdrama = {
2024-03-01 06:40:34 +00:00
author_name: comment.author_name,
};
2024-03-05 06:25:05 +00:00
for (const redditUser of redditUsers) {
const userMentionExists = await DatabaseService.userMentionExists(redditUser)
2024-03-05 06:25:05 +00:00
if (userMentionExists) continue
const commentResponseRdrama = MessageService.getRandomRdramaMessage(placeholdersRdrama)
if (!commentResponseRdrama) throw new Error('No comments for Rdrama found')
2024-03-05 06:25:05 +00:00
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()
2024-03-05 06:25:05 +00:00
const redditService = new RedditService(redditSession)
const resultshouldNotifyUser = await shouldNotifyUser(redditUser, redditService)
2024-03-05 06:25:05 +00:00
if (!resultshouldNotifyUser) continue
const placeholdersReddit = {
author_name: comment.author_name,
username: redditUser,
permalink: comment.permalink
2024-03-05 06:25:05 +00:00
};
const redditMessage = MessageService.getRandomRedditMessage(placeholdersReddit)
if (!redditMessage) throw new Error('No comments for Reddit found')
await DatabaseService.insertUserMention({
2024-03-05 06:25:05 +00:00
rdrama_comment_id: comment.id,
username: redditUser,
message: redditMessage,
})
await redditService.sendMessage(redditUser, 'Crosstalk PM Notification', redditMessage)
return;
2024-02-23 06:18:00 +00:00
2024-03-05 06:25:05 +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);
}
}
}
export default WorkflowOrchestrator;