Skip to content

lib/notifier

Defined in: studiocms/packages/studiocms/src/lib/notifier/index.ts:117^

  • any

new Notifications(): Notifications

Notifications

Effect.Service<Notifications>()(
'studiocms/lib/notifier/Notifications',
{
effect: genLogger('studiocms/lib/notifier/Notifications.effect')(function () {
const MailService = yield Mailer;
const logger = yield makeLogger;
/
Retrieves the configuration settings for StudioCMS.
This function fetches the configuration data from the StudioCMS SDK's database.
If the data is not available, it returns a default configuration with a title of 'StudioCMS'
and mailer functionality disabled.
/
const getConfig = genLogger('studiocms/lib/notifier/Notifications.getConfig')(function () {
const data = yield sdk.GET.siteConfig();
if (!data) {
return {
title: 'StudioCMS',
enableMailer: false,
};
}
return data.data;
});
/
Retrieves users who have enabled a specific notification type and belong to specified user ranks.
@param notification - The notification type to check for each user. It can be of type UserNotification, EditorNotification, or AdminNotification.
@param userRanks - An array of user rank strings to filter users by their rank.
@returns A promise that resolves to an array of CombinedUserData objects representing users who have the specified notification enabled and belong to the specified ranks.
/
const getUsersWithNotifications = (
notification: UserNotification | EditorNotification | AdminNotification,
userRanks: string[]
) =>
genLogger('studiocms/lib/notifier/Notifications.getUsersWithNotifications')(function () {
const userTable = yield sdk.GET.users.all();
const users = userTable.filter(
(user) => user.permissionsData?.rank && userRanks.includes(user.permissionsData?.rank)
);
const usersWithEnabledNotifications: CombinedUserData[] = [];
for (const user of users) {
if (user.notifications) {
const enabledNotifications = user.notifications.split(',');
if (enabledNotifications.includes(notification)) {
usersWithEnabledNotifications.push(user);
}
}
}
return usersWithEnabledNotifications;
});
/
Sends a notification message to a list of users via email.
@param users - An array of user data objects. Each object should contain user information, including an email address.
@param config - Configuration object containing the title of the notification.
@param message - The message to be sent to the users.
@returns A promise that resolves when all emails have been sent.
/
const sendMail = ({
users,
config: { title },
message,
notification,
}: {
users: CombinedUserData[];
config: { title: string };
message: string;
notification: NotificationTitle;
}) =>
genLogger('studiocms/lib/notifier/Notifications.sendMail')(function () {
const htmlTemplate = getTemplate('notification');
for (const { email } of users) {
if (!email) continue;
yield MailService.sendMail({
to: email,
subject: ${title} - New Notification,
html: htmlTemplate({
title: New Notification - ${notificationTitleStrings[notification]},
message,
}),
});
}
});
/
Sends a user notification if the mailer is enabled and the mail connection is verified.
@template T - The type of the user notification.
@param {T} notification - The notification to be sent.
@param {string} userId - The ID of the user to whom the notification will be sent.
/
const sendUserNotification = <T extends UserNotification>(notification: T, userId: string) =>
genLogger('studiocms/lib/notifier/Notifications.sendUserNotification')(function () {
const config = yield getConfig;
if (!config.enableMailer) return;
const testConnection = yield MailService.verifyMailConnection;
if ('error' in testConnection) {
logger.error(Error verifying mail connection: ${testConnection.error});
return;
}
const users = yield getUsersWithNotifications(notification, userRanks);
const user = users.find(({ id }) => id === userId);
if (!user) return;
yield sendMail({
users: [user],
config,
message: userNotificationsnotification,
notification,
});
return;
});
/
Sends an editor notification if the mailer is enabled and the mail connection is verified.
@template T - The type of the editor notification.
@template K - The type of the data required by the notification.
@param {T} notification - The type of notification to send.
@param {K} data - The data to include in the notification.
/
const sendEditorNotification = <
T extends EditorNotification,
K extends Parameters<EditorNotifications[T]>[0],
>(
notification: T,
data: K
) =>
genLogger('studiocms/lib/notifier/Notifications.sendEditorNotification')(function () {
const config = yield getConfig;
if (!config.enableMailer) return;
const testConnection = yield MailService.verifyMailConnection;
if ('error' in testConnection) {
logger.error(Error verifying mail connection: ${testConnection.error});
return;
}
const editors = yield getUsersWithNotifications(notification, editorRanks);
yield sendMail({
users: editors,
config,
message: editorNotificationsnotification,
notification,
});
return;
});
/
Sends an admin notification if the mailer is enabled and the mail connection is verified.
@template T - The type of the admin notification.
@template K - The type of the data required by the notification.
@param {T} notification - The type of notification to send.
@param {K} data - The data to include in the notification.
/
const sendAdminNotification = <
T extends AdminNotification,
K extends Parameters<AdminNotifications[T]>[0],
>(
notification: T,
data: K
) =>
genLogger('studiocms/lib/notifier/Notifications.sendAdminNotification')(function () {
const config = yield getConfig;
if (!config.enableMailer) return;
const testConnection = yield MailService.verifyMailConnection;
if ('error' in testConnection) {
logger.error(Error verifying mail connection: ${testConnection.error});
return;
}
const admins = yield getUsersWithNotifications(notification, adminRanks);
yield sendMail({
users: admins,
config,
message: adminNotificationsnotification,
notification,
});
return;
});
return {
sendUserNotification,
sendEditorNotification,
sendAdminNotification,
};
}),
dependencies: [Mailer.Default],
accessors: true,
}
).constructor

static Provide: any;

Defined in: studiocms/packages/studiocms/src/lib/notifier/index.ts:332^

type AdminNotification = keyof AdminNotifications;

Defined in: studiocms/packages/studiocms/src/lib/notifier/index.ts:97^

The type of the adminNotificationTypes array.


type EditorNotification = keyof EditorNotifications;

Defined in: studiocms/packages/studiocms/src/lib/notifier/index.ts:92^

The type of the editorNotificationTypes array.


type UserNotification = keyof UserNotifications;

Defined in: studiocms/packages/studiocms/src/lib/notifier/index.ts:87^

The type of the userNotificationTypes array.

const makeLogger: any;

Defined in: studiocms/packages/studiocms/src/lib/notifier/index.ts:115^


const notificationTitleStrings: Record<UserNotificationOptions, string>;

Defined in: studiocms/packages/studiocms/src/lib/notifier/index.ts:42^

An object containing notification titles for each notification type.


const notificationTypes: {
admin: string[];
editor: string[];
user: string[];
};

Defined in: studiocms/packages/studiocms/src/lib/notifier/index.ts:78^

An object containing all notification types.

admin: string[];

editor: string[];

user: string[];