72 lines
3.7 KiB
TypeScript
72 lines
3.7 KiB
TypeScript
import { CommentProcessor } from "../rdrama/services/CommentProcessor";
|
|
import { CommentParser } from "../rdrama/services/CommentParser";
|
|
import { CommentPoster } from "../rdrama/services/CommentPoster";
|
|
import { MessageService } from "../utils/MessageService";
|
|
import { DatabaseService } from "../db/services/Database";
|
|
import { RedditService } from "../reddit/services/reddit";
|
|
import RedditSessionManager from "../reddit/session/SessionManager";
|
|
import { shouldNotifyUser } from "../utils/ShouldNotify";
|
|
|
|
class WorkflowOrchestrator {
|
|
constructor(
|
|
private commentProcessor: CommentProcessor,
|
|
private commentParser: CommentParser,
|
|
private commentPoster: CommentPoster,
|
|
private messageService: MessageService,
|
|
private databaseService: DatabaseService,
|
|
) { }
|
|
|
|
/**
|
|
* Executes the defined workflow for processing comments.
|
|
*/
|
|
async executeWorkflow() {
|
|
try {
|
|
// Fetch comments from the source
|
|
const comments = await this.commentProcessor.processComments();
|
|
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`);
|
|
|
|
for (const comment of comments) {
|
|
const redditUsers = this.commentParser.extractUsernames(comment)
|
|
if (redditUsers.length === 0) continue
|
|
console.log('found:', redditUsers)
|
|
const placeholdersRdrama = {
|
|
author_name: comment.author_name,
|
|
};
|
|
for (const redditUser of redditUsers) {
|
|
const userMentionExists = await this.databaseService.userMentionExists(redditUser)
|
|
if (userMentionExists) continue
|
|
const commentResponseRdrama = this.messageService.getRandomRdramaMessage(placeholdersRdrama)
|
|
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(this.databaseService)
|
|
const redditService = new RedditService(redditSession)
|
|
const resultshouldNotifyUser = await shouldNotifyUser(redditUser, redditService, this.databaseService)
|
|
if (!resultshouldNotifyUser) continue
|
|
const placeholdersReddit = {
|
|
author_name: comment.author_name,
|
|
};
|
|
const redditMessage = this.messageService.getRandomRedditMessage(placeholdersReddit)
|
|
await this.databaseService.insertUserMention({
|
|
rdrama_comment_id: comment.id,
|
|
username: redditUser,
|
|
message: redditMessage,
|
|
})
|
|
await redditService.sendMessage(redditUser, 'Crosstalk PM Notification', redditMessage)
|
|
return;
|
|
|
|
}
|
|
}
|
|
console.log('Workflow executed successfully.');
|
|
} catch (error) {
|
|
console.error('An error occurred during workflow execution:', error);
|
|
}
|
|
}
|
|
}
|
|
|
|
export default WorkflowOrchestrator; |