67 lines
2.8 KiB
TypeScript
67 lines
2.8 KiB
TypeScript
import dotenv from 'dotenv';
|
|
import { RedditService } from '../reddit/services/Reddit';
|
|
import { DatabaseService } from '../db/services/Database';
|
|
|
|
// Load environment variables from .env file
|
|
dotenv.config();
|
|
|
|
/**
|
|
* Determines whether a user should be notified based on various criteria.
|
|
*
|
|
* This function checks a Reddit user's information against a set of conditions
|
|
* defined by environment variables and user attributes. These conditions include
|
|
* whether the user is a moderator, an employee, accepts private messages, has
|
|
* karma below a certain threshold, and has not been notified before.
|
|
*
|
|
* @param {string} username - The Reddit username of the user to check.
|
|
* @returns {Promise<boolean>} - A promise that resolves to `true` if the user meets
|
|
* the criteria for notification, otherwise `false`.
|
|
*
|
|
* @throws {Error} Throws an error if there's a problem fetching user information
|
|
* from Reddit or checking the user's notification status in the database.
|
|
*
|
|
* @example
|
|
* // Example of checking if a user should be notified
|
|
* shouldNotifyUser('exampleUser').then(shouldNotify => {
|
|
* if (shouldNotify) {
|
|
* console.log('User should be notified.');
|
|
* } else {
|
|
* console.log('User should not be notified.');
|
|
* }
|
|
* }).catch(error => {
|
|
* console.error('Error checking if user should be notified:', error);
|
|
* });
|
|
*
|
|
* @example
|
|
* // Environment variables used in this function
|
|
* // .env file
|
|
* EXCLUDE_MODS=true
|
|
* EXCLUDE_EMPLOYEES=true
|
|
* KARMA_THRESHOLD=100000
|
|
*
|
|
* These environment variables control the behavior of the function, such as whether
|
|
* to exclude moderators or employees from notifications, and the karma threshold for
|
|
* notifications.
|
|
*/
|
|
export async function shouldNotifyUser(username: string): Promise<boolean> {
|
|
const userInfo = await RedditService.getUserInfo(username);
|
|
if (!userInfo) return false;
|
|
|
|
const { is_mod, is_employee, accept_pms, total_karma } = userInfo.data;
|
|
|
|
const excludeMods = process.env.EXCLUDE_MODS !== 'false'; // Defaults to true unless explicitly set to 'false'
|
|
const excludeEmployees = process.env.EXCLUDE_EMPLOYEES !== 'false'; // Defaults to true unless explicitly set to 'false'
|
|
const notifyAcceptPms = accept_pms !== false; // Notify if accept_pms is true or undefined
|
|
const karmaThreshold = parseInt(process.env.KARMA_THRESHOLD || '100000', 10);
|
|
const hasBeenNotifiedBefore = await DatabaseService.userMentionExists(username);
|
|
|
|
const meetsCriteria =
|
|
(!excludeMods || !is_mod) && // Notify unless we're excluding mods and the user is a mod
|
|
(!excludeEmployees || !is_employee) && // Notify unless we're excluding employees and the user is an employee
|
|
notifyAcceptPms &&
|
|
total_karma < karmaThreshold &&
|
|
!hasBeenNotifiedBefore;
|
|
|
|
return meetsCriteria;
|
|
}
|