Compare commits
No commits in common. "f87c2da980127c31ed738fb17cac1b77a9c0b296" and "00f0765ef8aab48f22e8116146941a7c8c4e2957" have entirely different histories.
f87c2da980
...
00f0765ef8
|
@ -6,7 +6,6 @@ import rDramaSession from './rdrama/session/SessionManager';
|
||||||
import { DatabaseInitializer } from './db/initializeDatabase';
|
import { DatabaseInitializer } from './db/initializeDatabase';
|
||||||
import RedisSessionManager from './redis/session/SessionManager';
|
import RedisSessionManager from './redis/session/SessionManager';
|
||||||
import { Comment } from './rdrama/models/Comment';
|
import { Comment } from './rdrama/models/Comment';
|
||||||
import { CommentParser } from './rdrama/services/CommentParser';
|
|
||||||
|
|
||||||
// Import other necessary services or configurations
|
// Import other necessary services or configurations
|
||||||
|
|
||||||
|
@ -35,8 +34,6 @@ async function startApplication() {
|
||||||
|
|
||||||
const workflowOrchestrator = new WorkflowOrchestrator();
|
const workflowOrchestrator = new WorkflowOrchestrator();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
redisManager.client.on('message', async (channel, message) => {
|
redisManager.client.on('message', async (channel, message) => {
|
||||||
if (channel === 'newCommentsChannel') {
|
if (channel === 'newCommentsChannel') {
|
||||||
const comment: Comment = JSON.parse(message)
|
const comment: Comment = JSON.parse(message)
|
||||||
|
|
|
@ -1,29 +1,85 @@
|
||||||
import { CommentParser } from "../rdrama/services/CommentParser";
|
import { CommentParser } from "../rdrama/services/CommentParser";
|
||||||
|
import { CommentPoster } from "../rdrama/services/CommentPoster";
|
||||||
|
import { MessageFileName, MessageService } from "../utils/MessageService";
|
||||||
|
import { DatabaseService } from "../db/services/Database";
|
||||||
|
import { shouldNotifyUser } from "../utils/ShouldNotify";
|
||||||
import { Comment } from "../rdrama/models/Comment";
|
import { Comment } from "../rdrama/models/Comment";
|
||||||
import GameState from "../game/gameState";
|
|
||||||
import GameFlow from "../game/gameFlow";
|
|
||||||
|
|
||||||
class WorkflowOrchestrator {
|
class WorkflowOrchestrator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the defined workflow for processing comments.
|
* Executes the defined workflow for processing comments.
|
||||||
*
|
|
||||||
* @param comment - The comment object to process
|
|
||||||
*/
|
*/
|
||||||
async executeWorkflow(comment: Comment) {
|
async executeWorkflow(comment: Comment) {
|
||||||
try {
|
try {
|
||||||
const parsedComment = CommentParser.parseCommand(comment)
|
const canSend = await DatabaseService.canSendNotification();
|
||||||
if (parsedComment.command === "") return
|
const coolDownHours = process.env.NOTIFICATION_COOLDOWN_HOURS
|
||||||
const gameState = await GameState.load(comment)
|
if (!canSend) {
|
||||||
const gameFlow = new GameFlow(gameState)
|
console.log(`Last Message Sent less than ${coolDownHours ? coolDownHours : 4} hours ago. Set NOTIFICATION_COOLDOWN_HOURS to change this`)
|
||||||
await gameFlow.executeCurrentPhase(parsedComment.command)
|
return;
|
||||||
|
}
|
||||||
|
await this.processComment(comment);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('An error occurred during workflow execution:', error);
|
console.error('An error occurred during workflow execution:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
await this.postCommentAndNotify(comment, redditUsers[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
async postCommentAndNotify(comment: Comment, redditUser: string) {
|
||||||
|
const placeholdersRdrama = { author_name: comment.author_name };
|
||||||
|
|
||||||
|
const userMentionExists = await DatabaseService.userMentionExists(redditUser);
|
||||||
|
if (userMentionExists) {
|
||||||
|
const commentPreviouslyMessaged = MessageService.getRandomMessage(MessageFileName.RdramaPreviousMessage, placeholdersRdrama);
|
||||||
|
if (!commentPreviouslyMessaged) throw new Error('No comments for previous Message found');
|
||||||
|
const postedComment = await CommentPoster.postComment(`c_${comment.id}`, `${commentPreviouslyMessaged}`);
|
||||||
|
console.log(`Sent Comment to`, JSON.stringify(postedComment, null, 4));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const resultshouldNotifyUser = await shouldNotifyUser(redditUser);
|
||||||
|
if (!resultshouldNotifyUser) {
|
||||||
|
const commentShouldntNotify = MessageService.getRandomMessage(MessageFileName.RdramaShouldntNotify, placeholdersRdrama);
|
||||||
|
if (!commentShouldntNotify) throw new Error('No comments for Shouldnt Notify found');
|
||||||
|
const postedComment = await CommentPoster.postComment(`c_${comment.id}`, `${commentShouldntNotify}`);
|
||||||
|
console.log(`Sent Comment to`, JSON.stringify(postedComment, null, 4));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const commentResponseRdrama = MessageService.getRandomMessage(MessageFileName.RdramaMessages, placeholdersRdrama);
|
||||||
|
if (!commentResponseRdrama) throw new Error('No comments for Rdrama found');
|
||||||
|
const postedComment = await CommentPoster.postComment(`c_${comment.id}`, `${commentResponseRdrama}`);
|
||||||
|
console.log(`Sent Comment to`, JSON.stringify(postedComment, null, 4));
|
||||||
|
|
||||||
|
const placeholdersReddit = {
|
||||||
|
author_name: comment.author_name,
|
||||||
|
username: redditUser,
|
||||||
|
permalink: comment.permalink
|
||||||
|
};
|
||||||
|
const redditMessage = MessageService.getRandomMessage(MessageFileName.RedditMessages, placeholdersReddit);
|
||||||
|
if (!redditMessage) throw new Error('No comments for Reddit found');
|
||||||
|
|
||||||
|
await DatabaseService.insertUserMention({
|
||||||
|
rdrama_comment_id: comment.id,
|
||||||
|
username: redditUser,
|
||||||
|
message: redditMessage,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default WorkflowOrchestrator;
|
export default WorkflowOrchestrator;
|
Loading…
Reference in New Issue