[bug] Customize notification title; fix bug 70772

This commit is contained in:
Sergey Konovalov
2024-10-10 14:29:27 +03:00
parent 2652dd20c5
commit 199e8f9f5e
4 changed files with 30 additions and 25 deletions

View File

@ -42,7 +42,7 @@
"email"
],
"template": {
"title": "License expiration warning",
"title": "%s Docs license expiration warning",
"body": "Attention! Your license is about to expire on %s.\nUpon reaching this date, you will no longer be entitled to receive personal technical support and install new Docs versions released after this date."
},
"policies": {
@ -55,7 +55,7 @@
"email"
],
"template": {
"title": "License expiration warning",
"title": "%s Docs license expiration warning",
"body": "Attention! Your license expired on %s.\nYou are no longer entitled to receive personal technical support and install new Docs versions released after this date.\nPlease contact sales@onlyoffice.com to discuss license renewal."
},
"policies": {
@ -68,7 +68,7 @@
"email"
],
"template": {
"title": "License connection limit warning",
"title": "%s Docs license connection limit warning",
"body": "Attention! You have reached %s%% of the %s limit set by your license."
},
"policies": {
@ -81,7 +81,7 @@
"email"
],
"template": {
"title": "License connection limit warning",
"title": "%s Docs license connection limit warning",
"body": "Attention! You have reached %s%% of the live viewer %s limit set by your license."
},
"policies": {

View File

@ -53,7 +53,7 @@ const notificationTypes = {
class TransportInterface {
async send(ctx, message) {}
contentGeneration(template, message) {}
contentGeneration(title, message) {}
}
class MailTransport extends TransportInterface {
@ -72,9 +72,9 @@ class MailTransport extends TransportInterface {
return mailService.send(this.host, this.auth.user, message);
}
contentGeneration(template, message) {
contentGeneration(title, message) {
return {
subject: template.title,
subject: title,
text: message
};
}
@ -106,13 +106,13 @@ class Transport {
}
}
async function notify(ctx, notificationType, message, opt_cacheKey = undefined) {
async function notify(ctx, notificationType, title, message, opt_cacheKey = undefined) {
const tenRule = ctx.getCfg(`notification.rules.${notificationType}`, config.get(`notification.rules.${notificationType}`));
if (tenRule?.enable) {
ctx.logger.debug('Notification service: notify "%s"', notificationType);
let checkRes = await checkRulePolicies(ctx, notificationType, tenRule, opt_cacheKey);
if (checkRes) {
await notifyRule(ctx, tenRule, message);
await notifyRule(ctx, tenRule, title, message);
}
}
}
@ -132,11 +132,11 @@ async function checkRulePolicies(ctx, notificationType, tenRule, opt_cacheKey) {
return isLock;
}
async function notifyRule(ctx, tenRule, message) {
async function notifyRule(ctx, tenRule, title, message) {
const transportObjects = tenRule.transportType.map(transport => new Transport(ctx, transport));
for (const transportObject of transportObjects) {
try {
const mail = transportObject.transport.contentGeneration(tenRule.template, message);
const mail = transportObject.transport.contentGeneration(title, message);
await transportObject.transport.send(ctx, mail);
} catch (error) {
ctx.logger.error('Notification service: error: %s', error.stack);

View File

@ -138,8 +138,8 @@ const cfgForgottenFiles = config.get('services.CoAuthoring.server.forgottenfiles
const cfgForgottenFilesName = config.get('services.CoAuthoring.server.forgottenfilesname');
const cfgMaxRequestChanges = config.get('services.CoAuthoring.server.maxRequestChanges');
const cfgWarningLimitPercents = config.get('license.warning_limit_percents');
const cfgNotificationRuleLicenseLimitEdit = config.get('notification.rules.licenseLimitEdit.template.body');
const cfgNotificationRuleLicenseLimitLiveViewer = config.get('notification.rules.licenseLimitLiveViewer.template.body');
const cfgNotificationRuleLicenseLimitEdit = config.get('notification.rules.licenseLimitEdit.template');
const cfgNotificationRuleLicenseLimitLiveViewer = config.get('notification.rules.licenseLimitLiveViewer.template');
const cfgErrorFiles = config.get('FileConverter.converter.errorfiles');
const cfgOpenProtectedFile = config.get('services.CoAuthoring.server.openProtectedFile');
const cfgIsAnonymousSupport = config.get('services.CoAuthoring.server.isAnonymousSupport');
@ -3479,8 +3479,8 @@ exports.install = function(server, callbackFunction) {
function* _checkLicenseAuth(ctx, licenseInfo, userId, isLiveViewer) {
const tenWarningLimitPercents = ctx.getCfg('license.warning_limit_percents', cfgWarningLimitPercents) / 100;
const tenNotificationRuleLicenseLimitEdit = ctx.getCfg(`notification.rules.licenseLimitEdit.template.body`, cfgNotificationRuleLicenseLimitEdit);
const tenNotificationRuleLicenseLimitLiveViewer = ctx.getCfg(`notification.rules.licenseLimitLiveViewer.template.body`, cfgNotificationRuleLicenseLimitLiveViewer);
const tenNotificationRuleLicenseLimitEdit = ctx.getCfg(`notification.rules.licenseLimitEdit.template`, cfgNotificationRuleLicenseLimitEdit);
const tenNotificationRuleLicenseLimitLiveViewer = ctx.getCfg(`notification.rules.licenseLimitLiveViewer.template`, cfgNotificationRuleLicenseLimitLiveViewer);
const c_LR = constants.LICENSE_RESULT;
let licenseType = licenseInfo.type;
if (c_LR.Success === licenseType || c_LR.SuccessLimit === licenseType) {
@ -3531,14 +3531,16 @@ exports.install = function(server, callbackFunction) {
}
}
if ((c_LR.Success !== licenseType && c_LR.SuccessLimit !== licenseType) || 100 !== notificationPercent) {
const message = util.format(notificationTemplate, notificationPercent, notificationLimit);
const applicationName = (process.env.APPLICATION_NAME || "").toUpperCase();
const title = util.format(notificationTemplate.title, applicationName);
const message = util.format(notificationTemplate.body, notificationPercent, notificationLimit);
if (100 !== notificationPercent) {
ctx.logger.warn(message);
} else {
ctx.logger.error(message);
}
//todo with yield service could throw error
void notificationService.notify(ctx, notificationType, message, notificationType + notificationPercent);
void notificationService.notify(ctx, notificationType, title, message, notificationType + notificationPercent);
}
}
return licenseType;

View File

@ -43,8 +43,8 @@ const tenantManager = require('../../Common/sources/tenantManager');
const { notificationTypes, ...notificationService } = require('../../Common/sources/notificationService');
const cfgStartNotifyFrom = ms(config.get('license.warning_license_expiration'));
const cfgNotificationRuleLicenseExpirationWarning = config.get('notification.rules.licenseExpirationWarning.template.body');
const cfgNotificationRuleLicenseExpirationError = config.get('notification.rules.licenseExpirationError.template.body');
const cfgNotificationRuleLicenseExpirationWarning = config.get('notification.rules.licenseExpirationWarning.template');
const cfgNotificationRuleLicenseExpirationError = config.get('notification.rules.licenseExpirationError.template');
async function fixImageExifRotation(ctx, buffer) {
if (!buffer) {
@ -130,16 +130,19 @@ async function notifyLicenseExpiration(ctx, endDate) {
endDate = currentDate;
}
const formattedExpirationTime = humanFriendlyExpirationTime(endDate);
const applicationName = (process.env.APPLICATION_NAME || "").toUpperCase();
if (endDate <= currentDate) {
const tenNotificationRuleLicenseExpirationError = ctx.getCfg('notification.rules.licenseExpirationError.template.body', cfgNotificationRuleLicenseExpirationError);
const message = util.format(tenNotificationRuleLicenseExpirationError, formattedExpirationTime);
const tenNotificationRuleLicenseExpirationError = ctx.getCfg('notification.rules.licenseExpirationError.template', cfgNotificationRuleLicenseExpirationError);
const title = util.format(tenNotificationRuleLicenseExpirationError.title, applicationName);
const message = util.format(tenNotificationRuleLicenseExpirationError.body, formattedExpirationTime);
ctx.logger.error(message);
await notificationService.notify(ctx, notificationTypes.LICENSE_EXPIRATION_ERROR, message);
await notificationService.notify(ctx, notificationTypes.LICENSE_EXPIRATION_ERROR, title, message);
} else {
const tenNotificationRuleLicenseExpirationWarning = ctx.getCfg('notification.rules.licenseExpirationWarning.template.body', cfgNotificationRuleLicenseExpirationWarning);
const message = util.format(tenNotificationRuleLicenseExpirationWarning, formattedExpirationTime);
const tenNotificationRuleLicenseExpirationWarning = ctx.getCfg('notification.rules.licenseExpirationWarning.template', cfgNotificationRuleLicenseExpirationWarning);
const title = util.format(tenNotificationRuleLicenseExpirationWarning.title, applicationName);
const message = util.format(tenNotificationRuleLicenseExpirationWarning.body, formattedExpirationTime);
ctx.logger.warn(message);
await notificationService.notify(ctx, notificationTypes.LICENSE_EXPIRATION_WARNING, message);
await notificationService.notify(ctx, notificationTypes.LICENSE_EXPIRATION_WARNING, title, message);
}
}
}