1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-24 15:49:46 +02:00

ref: Refactoring

This commit is contained in:
Maksim Eltyshev 2024-10-30 22:28:25 +01:00
parent e4fd0eda16
commit 036cdc058a
7 changed files with 53 additions and 39 deletions

View file

@ -14,7 +14,10 @@ const valuesValidator = (value) => {
return true;
};
const buildAndSendMessage = async (card, action, actorUser, send) => {
const truncateString = (string, maxLength = 30) =>
string.length > maxLength ? `${string.substring(0, 30)}...` : string;
const buildAndSendMarkdownMessage = async (card, action, actorUser, send) => {
const cardLink = `<${sails.config.custom.baseUrl}/cards/${card.id}|${card.name}>`;
let markdown;
@ -28,6 +31,7 @@ const buildAndSendMessage = async (card, action, actorUser, send) => {
break;
case Action.Types.COMMENT_CARD:
// TODO: truncate text?
markdown = `*${actorUser.name}* commented on ${cardLink}:\n>${action.data.text}`;
break;
@ -38,7 +42,7 @@ const buildAndSendMessage = async (card, action, actorUser, send) => {
await send(markdown);
};
const buildAndSendMessageForTelegramBot = async (card, action, actorUser, send) => {
const buildAndSendHtmlMessage = async (card, action, actorUser, send) => {
const cardLink = `<a href="${sails.config.custom.baseUrl}/cards/${card.id}">${card.name}</a>`;
let html;
@ -52,15 +56,14 @@ const buildAndSendMessageForTelegramBot = async (card, action, actorUser, send)
break;
case Action.Types.COMMENT_CARD: {
const commentedText =
action.data.text.length > 30 ? `${action.data.text.substring(0, 30)}...` : action.data.text;
html = `<b>${actorUser.name}</b> commented on ${cardLink}: <i>${commentedText}</i>`;
html = `<b>${actorUser.name}</b> commented on ${cardLink}:\n<i>${truncateString(action.data.text)}</i>`;
break;
}
default:
return;
}
await send(html);
};
@ -142,11 +145,25 @@ module.exports = {
);
if (sails.config.custom.slackBotToken) {
buildAndSendMessage(values.card, action, values.user, sails.helpers.utils.sendSlackMessage);
buildAndSendMarkdownMessage(
values.card,
action,
values.user,
sails.helpers.utils.sendSlackMessage,
);
}
if (sails.config.custom.telegramChatId) {
buildAndSendMessageForTelegramBot(
if (sails.config.custom.googleChatWebhookUrl) {
buildAndSendMarkdownMessage(
values.card,
action,
values.user,
sails.helpers.utils.sendGoogleChatMessage,
);
}
if (sails.config.custom.telegramBotToken) {
buildAndSendHtmlMessage(
values.card,
action,
values.user,
@ -154,14 +171,6 @@ module.exports = {
);
}
if (sails.config.custom.googleChatWebhookUrl) {
buildAndSendMessage(
values.card,
action,
values.user,
sails.helpers.utils.sendGoogleChatMessage,
);
}
return action;
},
};

View file

@ -1,8 +1,8 @@
const buildAndSendMessage = async (card, actorUser, send) => {
const buildAndSendMarkdownMessage = async (card, actorUser, send) => {
await send(`*${card.name}* was deleted by ${actorUser.name}`);
};
const buildAndSendMessageForTelegramBot = async (card, actorUser, send) => {
const buildAndSendHtmlMessage = async (card, actorUser, send) => {
await send(`<b>${card.name}</b> was deleted by ${actorUser.name}`);
};
@ -60,19 +60,19 @@ module.exports = {
});
if (sails.config.custom.slackBotToken) {
buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendSlackMessage);
}
if (sails.config.custom.telegramChatId) {
buildAndSendMessageForTelegramBot(
card,
inputs.actorUser,
sails.helpers.utils.sendTelegramMessage,
);
buildAndSendMarkdownMessage(card, inputs.actorUser, sails.helpers.utils.sendSlackMessage);
}
if (sails.config.custom.googleChatWebhookUrl) {
buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendGoogleChatMessage);
buildAndSendMarkdownMessage(
card,
inputs.actorUser,
sails.helpers.utils.sendGoogleChatMessage,
);
}
if (sails.config.custom.telegramBotToken) {
buildAndSendHtmlMessage(card, inputs.actorUser, sails.helpers.utils.sendTelegramMessage);
}
}

View file

@ -1,4 +1,4 @@
const POST_MESSAGE_API_URL = (telegramBotToken) =>
const buildSendMessageApiUrl = (telegramBotToken) =>
`https://api.telegram.org/bot${telegramBotToken}/sendMessage`;
module.exports = {
@ -8,6 +8,7 @@ module.exports = {
required: true,
},
},
async fn(inputs) {
const headers = {
'Content-Type': 'application/json; charset=utf-8',
@ -25,7 +26,7 @@ module.exports = {
let response;
try {
response = await fetch(POST_MESSAGE_API_URL(sails.config.custom.telegramBotToken), {
response = await fetch(buildSendMessageApiUrl(sails.config.custom.telegramBotToken), {
headers,
method: 'POST',
body: JSON.stringify(body),
@ -36,8 +37,8 @@ module.exports = {
}
if (!response.ok) {
const responseErrorJson = await response.json();
sails.log.error(`Error sending to Telegram: ${responseErrorJson.description}`);
const responseJson = await response.json();
sails.log.error(`Error sending to Telegram: ${responseJson.description}`);
}
},
};