2026-02-11 23:51:03 -08:00
|
|
|
const DEFAULT_NOTIFICATION_MESSAGE_MAX_LENGTH = 250;
|
|
|
|
|
|
|
|
|
|
const resolvePositiveNumber = (value, fallback) => {
|
|
|
|
|
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-06 17:40:20 +03:00
|
|
|
const normalizeNotificationPlainText = (text) => {
|
|
|
|
|
if (typeof text !== 'string') {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
.replace(/```[\s\S]*?```/g, ' ')
|
|
|
|
|
.replace(/`([^`]*)`/g, '$1')
|
|
|
|
|
.replace(/^[\t ]*[-*+]\s+/gm, '')
|
|
|
|
|
.replace(/^#{1,6}\s+/gm, '')
|
|
|
|
|
.replace(/\*\*(.*?)\*\*/g, '$1')
|
|
|
|
|
.replace(/__(.*?)__/g, '$1')
|
|
|
|
|
.replace(/\*(.*?)\*/g, '$1')
|
|
|
|
|
.replace(/_(.*?)_/g, '$1')
|
|
|
|
|
.replace(/\[(.*?)\]\((.*?)\)/g, '$1')
|
|
|
|
|
.replace(/\s*\n\s*/g, ' ')
|
|
|
|
|
.replace(/\s+/g, ' ')
|
|
|
|
|
.trim();
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-11 23:51:03 -08:00
|
|
|
export const truncateNotificationText = (text, maxLength = DEFAULT_NOTIFICATION_MESSAGE_MAX_LENGTH) => {
|
|
|
|
|
if (typeof text !== 'string') {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const safeMaxLength = resolvePositiveNumber(maxLength, DEFAULT_NOTIFICATION_MESSAGE_MAX_LENGTH);
|
|
|
|
|
if (text.length <= safeMaxLength) {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return `${text.slice(0, safeMaxLength)}...`;
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-19 02:06:52 +03:00
|
|
|
export const prepareNotificationLastMessage = async ({ message, settings }) => {
|
2026-02-11 23:51:03 -08:00
|
|
|
const originalMessage = typeof message === 'string' ? message : '';
|
|
|
|
|
if (!originalMessage) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const maxLastMessageLength = resolvePositiveNumber(settings?.maxLastMessageLength, DEFAULT_NOTIFICATION_MESSAGE_MAX_LENGTH);
|
2026-05-19 02:06:52 +03:00
|
|
|
const plainTextMessage = normalizeNotificationPlainText(originalMessage);
|
2026-04-06 17:40:20 +03:00
|
|
|
return truncateNotificationText(plainTextMessage, maxLastMessageLength);
|
2026-02-11 23:51:03 -08:00
|
|
|
};
|