2024-02-24 06:20:01 +00:00
|
|
|
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-02-23 06:18:00 +00:00
|
|
|
|
|
|
|
class WorkflowOrchestrator {
|
|
|
|
constructor(
|
2024-02-24 06:20:01 +00:00
|
|
|
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,
|
|
|
|
private messageService: MessageService,
|
2024-02-23 06:18:00 +00:00
|
|
|
//private redditNotifier: RedditNotifier // Handles notifications to Reddit users
|
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
|
2024-02-24 06:20:01 +00:00
|
|
|
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)
|
|
|
|
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?
|
|
|
|
}
|
|
|
|
|
2024-02-23 06:18:00 +00:00
|
|
|
//// Query user information based on usernames
|
|
|
|
//const userInfo = await this.databaseService.queryUsersInfo(uniqueUsernames);
|
|
|
|
//console.log(`Queried information for ${userInfo.length} users`);
|
2024-02-23 07:02:46 +00:00
|
|
|
//
|
2024-02-23 06:18:00 +00:00
|
|
|
//// Filter users who should be notified
|
|
|
|
//const usersToNotify = userInfo.filter(user => this.shouldNotifyUser(user));
|
|
|
|
//console.log(`Identified ${usersToNotify.length} users to notify`);
|
2024-02-23 07:02:46 +00:00
|
|
|
//
|
2024-02-23 06:18:00 +00:00
|
|
|
//// Notify users
|
|
|
|
//for (const user of usersToNotify) {
|
|
|
|
// await this.redditNotifier.notifyUser(user);
|
|
|
|
// console.log(`Notified user: ${user.username}`);
|
|
|
|
//}
|
|
|
|
|
|
|
|
console.log('Workflow executed successfully.');
|
|
|
|
} catch (error) {
|
|
|
|
console.error('An error occurred during workflow execution:', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether a user should be notified based on certain criteria.
|
|
|
|
*
|
|
|
|
* @param user - The user information object.
|
|
|
|
* @returns A boolean indicating whether the user should be notified.
|
|
|
|
*/
|
|
|
|
//private shouldNotifyUser(user: UserInfo): boolean {
|
|
|
|
// // Placeholder for the actual logic to determine if a user should be notified.
|
|
|
|
// // This could involve checking the last notification time against the current time,
|
|
|
|
// // user preferences, or other criteria defined in the business logic.
|
|
|
|
//
|
|
|
|
// // Example logic (to be replaced with actual implementation):
|
|
|
|
// const lastNotifiedTime = new Date(user.lastNotified); // Assuming 'lastNotified' is a Date or string.
|
|
|
|
// const notificationThreshold = 24 * 60 * 60 * 1000; // 24 hours in milliseconds.
|
|
|
|
// return (Date.now() - lastNotifiedTime.getTime()) > notificationThreshold;
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WorkflowOrchestrator;
|