Skip to content

Commit

Permalink
ref: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
meltyshev committed Oct 30, 2024
1 parent cc95032 commit 38bc4cb
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 31 deletions.
12 changes: 7 additions & 5 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -122,18 +124,18 @@ 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

proxy:
image: nginx:alpine
ports:
- '3000:80'
- "3000:80"
volumes:
- ./config/development/nginx.conf:/etc/nginx/nginx.conf
depends_on:
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ services:

# - SLACK_BOT_TOKEN=
# - SLACK_CHANNEL_ID=

# - GOOGLE_CHAT_WEBHOOK_URL=

# - TELEGRAM_BOT_TOKEN=
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 21 additions & 12 deletions server/api/helpers/actions/create-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
};

Expand Down Expand Up @@ -142,26 +145,32 @@ module.exports = {
);

if (sails.config.custom.slackBotToken) {
buildAndSendMessage(values.card, action, values.user, sails.helpers.utils.sendSlackMessage);
}

if (sails.config.custom.telegramChatId) {
buildAndSendMessageForTelegramBot(
buildAndSendMarkdownMessage(
values.card,
action,
values.user,
sails.helpers.utils.sendTelegramMessage,
sails.helpers.utils.sendSlackMessage,
);
}

if (sails.config.custom.googleChatWebhookUrl) {
buildAndSendMessage(
buildAndSendMarkdownMessage(
values.card,
action,
values.user,
sails.helpers.utils.sendGoogleChatMessage,
);
}

if (sails.config.custom.telegramBotToken) {
buildAndSendHtmlMessage(
values.card,
action,
values.user,
sails.helpers.utils.sendTelegramMessage,
);
}

return action;
},
};
16 changes: 8 additions & 8 deletions server/api/helpers/cards/delete-one.js
Original file line number Diff line number Diff line change
@@ -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}`);
};

Expand Down Expand Up @@ -60,19 +60,19 @@ module.exports = {
});

if (sails.config.custom.slackBotToken) {
buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendSlackMessage);
buildAndSendMarkdownMessage(card, inputs.actorUser, sails.helpers.utils.sendSlackMessage);
}

if (sails.config.custom.telegramChatId) {
buildAndSendMessageForTelegramBot(
if (sails.config.custom.googleChatWebhookUrl) {
buildAndSendMarkdownMessage(
card,
inputs.actorUser,
sails.helpers.utils.sendTelegramMessage,
sails.helpers.utils.sendGoogleChatMessage,
);
}

if (sails.config.custom.googleChatWebhookUrl) {
buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendGoogleChatMessage);
if (sails.config.custom.telegramBotToken) {
buildAndSendHtmlMessage(card, inputs.actorUser, sails.helpers.utils.sendTelegramMessage);
}
}

Expand Down
9 changes: 5 additions & 4 deletions server/api/helpers/utils/send-telegram-message.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const POST_MESSAGE_API_URL = (telegramBotToken) =>
const buildSendMessageApiUrl = (telegramBotToken) =>
`https://api.telegram.org/bot${telegramBotToken}/sendMessage`;

module.exports = {
Expand All @@ -8,6 +8,7 @@ module.exports = {
required: true,
},
},

async fn(inputs) {
const headers = {
'Content-Type': 'application/json; charset=utf-8',
Expand All @@ -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),
Expand All @@ -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}`);
}
},
};
1 change: 1 addition & 0 deletions server/config/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 38bc4cb

Please sign in to comment.