From 5fcffcd608089d1a0f0f77e405e92be6eccaed5b Mon Sep 17 00:00:00 2001 From: si458 Date: Sat, 24 May 2025 12:47:28 +0100 Subject: [PATCH] fix email/sms/messaging customised templates #6994 Signed-off-by: si458 --- meshmail.js | 21 +++++++++++++++++++++ meshmessaging.js | 22 ++++++++++++++++++++-- meshsms.js | 18 ++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/meshmail.js b/meshmail.js index 2b4fcdee..2c88051e 100644 --- a/meshmail.js +++ b/meshmail.js @@ -116,6 +116,27 @@ module.exports.CreateMeshMail = function (parent, domain) { } } + // If no email template found, use the default translated email template + if (((htmlfile == null) || (txtfile == null)) && (lang != null) && (lang != 'en')) { + var translationsPath = obj.parent.path.join(obj.parent.webEmailsPath, 'translations'); + var translationsPathHtml = obj.parent.path.join(obj.parent.webEmailsPath, 'translations', name + '_' + lang + '.html'); + var translationsPathTxt = obj.parent.path.join(obj.parent.webEmailsPath, 'translations', name + '_' + lang + '.txt'); + if (obj.parent.fs.existsSync(translationsPath) && obj.parent.fs.existsSync(translationsPathHtml) && obj.parent.fs.existsSync(translationsPathTxt)) { + htmlfile = obj.parent.fs.readFileSync(translationsPathHtml).toString(); + txtfile = obj.parent.fs.readFileSync(translationsPathTxt).toString(); + } + } + + // If no translated email template found, use the default email template + if ((htmlfile == null) || (txtfile == null)) { + var pathHtml = obj.parent.path.join(obj.parent.webEmailsPath, name + '.html'); + var pathTxt = obj.parent.path.join(obj.parent.webEmailsPath, name + '.txt'); + if (obj.parent.fs.existsSync(pathHtml) && obj.parent.fs.existsSync(pathTxt)) { + htmlfile = obj.parent.fs.readFileSync(pathHtml).toString(); + txtfile = obj.parent.fs.readFileSync(pathTxt).toString(); + } + } + // No email templates if ((htmlfile == null) || (txtfile == null)) { return null; } diff --git a/meshmessaging.js b/meshmessaging.js index 646ae4a2..9a722792 100644 --- a/meshmessaging.js +++ b/meshmessaging.js @@ -397,6 +397,7 @@ module.exports.CreateServer = function (parent) { function getTemplate(templateNumber, domain, lang) { parent.debug('email', 'Getting SMS template #' + templateNumber + ', lang: ' + lang); if (Array.isArray(lang)) { lang = lang[0]; } // TODO: For now, we only use the first language given. + if (lang != null) { lang = lang.split('-')[0]; } // Take the first part of the language, "xx-xx" var r = {}, emailsPath = null; if ((domain != null) && (domain.webemailspath != null)) { emailsPath = domain.webemailspath; } @@ -404,7 +405,7 @@ module.exports.CreateServer = function (parent) { else if (obj.parent.webEmailsPath != null) { emailsPath = obj.parent.webEmailsPath; } if ((emailsPath == null) || (obj.parent.fs.existsSync(emailsPath) == false)) { return null } - // Get the non-english email if needed + // Get the non-english sms if needed var txtfile = null; if ((lang != null) && (lang != 'en')) { var translationsPath = obj.parent.path.join(emailsPath, 'translations'); @@ -414,7 +415,7 @@ module.exports.CreateServer = function (parent) { } } - // Get the english email + // Get the english sms if (txtfile == null) { var pathTxt = obj.parent.path.join(emailsPath, 'sms-messages.txt'); if (obj.parent.fs.existsSync(pathTxt)) { @@ -422,6 +423,23 @@ module.exports.CreateServer = function (parent) { } } + // If no english sms and a non-english language is requested, try to get the default translated sms + if (txtfile == null && (lang != null) && (lang != 'en')) { + var translationsPath = obj.parent.path.join(obj.parent.webEmailsPath, 'translations'); + var translationsPathTxt = obj.parent.path.join(obj.parent.webEmailsPath, 'translations', 'sms-messages_' + lang + '.txt'); + if (obj.parent.fs.existsSync(translationsPath) && obj.parent.fs.existsSync(translationsPathTxt)) { + txtfile = obj.parent.fs.readFileSync(translationsPathTxt).toString(); + } + } + + // If no default translated sms, try to get the default english sms + if (txtfile == null) { + var pathTxt = obj.parent.path.join(obj.parent.webEmailsPath, 'sms-messages.txt'); + if (obj.parent.fs.existsSync(pathTxt)) { + txtfile = obj.parent.fs.readFileSync(pathTxt).toString(); + } + } + // No email templates if (txtfile == null) { return null; } diff --git a/meshsms.js b/meshsms.js index 1746c23e..441e7c4d 100644 --- a/meshsms.js +++ b/meshsms.js @@ -169,6 +169,7 @@ module.exports.CreateMeshSMS = function (parent) { function getTemplate(templateNumber, domain, lang) { parent.debug('email', 'Getting SMS template #' + templateNumber + ', lang: ' + lang); if (Array.isArray(lang)) { lang = lang[0]; } // TODO: For now, we only use the first language given. + if (lang != null) { lang = lang.split('-')[0]; } // Take the first part of the language, "xx-xx" var r = {}, emailsPath = null; if ((domain != null) && (domain.webemailspath != null)) { emailsPath = domain.webemailspath; } @@ -194,6 +195,23 @@ module.exports.CreateMeshSMS = function (parent) { } } + // If no english sms and a non-english language is requested, try to get the default translated sms + if (txtfile == null && (lang != null) && (lang != 'en')) { + var translationsPath = obj.parent.path.join(obj.parent.webEmailsPath, 'translations'); + var translationsPathTxt = obj.parent.path.join(obj.parent.webEmailsPath, 'translations', 'sms-messages_' + lang + '.txt'); + if (obj.parent.fs.existsSync(translationsPath) && obj.parent.fs.existsSync(translationsPathTxt)) { + txtfile = obj.parent.fs.readFileSync(translationsPathTxt).toString(); + } + } + + // If no default translated sms, try to get the default english sms + if (txtfile == null) { + var pathTxt = obj.parent.path.join(obj.parent.webEmailsPath, 'sms-messages.txt'); + if (obj.parent.fs.existsSync(pathTxt)) { + txtfile = obj.parent.fs.readFileSync(pathTxt).toString(); + } + } + // No email templates if (txtfile == null) { return null; }