1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00

ref: Refactoring

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

View file

@ -68,11 +68,13 @@ services:
# - SLACK_BOT_TOKEN=
# - SLACK_CHANNEL_ID=
# - GOOGLE_CHAT_WEBHOOK_URL=
# - TELEGRAM_BOT_TOKEN=
# - TELEGRAM_CHAT_ID=
# - TELEGRAM_THREAD_ID=
working_dir: /app
command: ['sh', '-c', 'npm run start']
command: ["sh", "-c", "npm run start"]
depends_on:
postgres:
condition: service_healthy
@ -105,7 +107,7 @@ services:
# - DEFAULT_ADMIN_NAME=Demo Demo
# - DEFAULT_ADMIN_USERNAME=demo
working_dir: /app
command: ['sh', '-c', 'npm run db:init']
command: ["sh", "-c", "npm run db:init"]
volumes:
- ./server:/app
- /app/node_modules
@ -122,10 +124,10 @@ services:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
ports:
- '5432:5432'
- "5432:5432"
restart: unless-stopped
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U user -d planka_db']
test: ["CMD-SHELL", "pg_isready -U user -d planka_db"]
interval: 10s
timeout: 5s
retries: 5
@ -133,7 +135,7 @@ services:
proxy:
image: nginx:alpine
ports:
- '3000:80'
- "3000:80"
volumes:
- ./config/development/nginx.conf:/etc/nginx/nginx.conf
depends_on:

View file

@ -74,6 +74,7 @@ services:
# - SLACK_BOT_TOKEN=
# - SLACK_CHANNEL_ID=
# - GOOGLE_CHAT_WEBHOOK_URL=
# - TELEGRAM_BOT_TOKEN=
@ -92,7 +93,7 @@ services:
- POSTGRES_DB=planka
- POSTGRES_HOST_AUTH_METHOD=trust
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres -d planka']
test: ["CMD-SHELL", "pg_isready -U postgres -d planka"]
interval: 10s
timeout: 5s
retries: 5

View file

@ -65,13 +65,13 @@ SECRET_KEY=notsecretkey
# SLACK_BOT_TOKEN=
# SLACK_CHANNEL_ID=
# GOOGLE_CHAT_WEBHOOK_URL=
# TELEGRAM_BOT_TOKEN=
# TELEGRAM_CHAT_ID=
# TELEGRAM_THREAD_ID=
## Do not edit this
TZ=UTC

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}`);
}
},
};

View file

@ -82,6 +82,7 @@ module.exports.custom = {
slackBotToken: process.env.SLACK_BOT_TOKEN,
slackChannelId: process.env.SLACK_CHANNEL_ID,
googleChatWebhookUrl: process.env.GOOGLE_CHAT_WEBHOOK_URL,
telegramBotToken: process.env.TELEGRAM_BOT_TOKEN,