Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord webhooks #470

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/actions/deploy/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ inputs:
database_password:
required: true
type: string
discord_webhook_url:
type: string
mail_host:
required: true
type: string
Expand Down Expand Up @@ -85,6 +87,7 @@ runs:
CLOUDFLARE_DNS_API_TOKEN=${{ inputs.cloudflare_api_key }} \
DATABASE_URL=${{ inputs.database_url }} \
DATABASE_PASSWORD=${{ inputs.database_password }} \
DISCORD_WEBHOOK_URL=${{ inputs.discord_webhook_url }} \
REDIS_URL=${{ inputs.redis_url }} \
MAIL_HOST=${{ inputs.mail_host }} \
MAIL_PORT=${{ inputs.mail_port }} \
Expand Down Expand Up @@ -136,6 +139,7 @@ runs:
CLOUDFLARE_DNS_API_TOKEN=${{ inputs.cloudflare_api_key }} \
DATABASE_URL=${{ inputs.database_url }} \
DATABASE_PASSWORD=${{ inputs.database_password }} \
DISCORD_WEBHOOK_URL=${{ inputs.discord_webhook_url }} \
REDIS_URL=${{ inputs.redis_url }} \
MAIL_HOST=${{ inputs.mail_host }} \
MAIL_PORT=${{ inputs.mail_port }} \
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-and-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ jobs:
cloudflare_api_key: ${{ secrets.CLOUDFLARE_API_TOKEN }}
database_password: ${{ secrets.DATABASE_PASSWORD }}
database_url: ${{ secrets.DATABASE_URL }}
discord_webhook_url: ${{ secrets.DISCORD_WEBHOOK_URL }}
mail_host: ${{ secrets.MAIL_HOST }}
mail_password: ${{ secrets.MAIL_PASSWORD }}
mail_port: ${{ secrets.MAIL_PORT }}
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/routes/auth/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isError, isSuccess } from '@dundring/utils';
import * as express from 'express';
import {
mailService,
slackService,
monitoringService,
userService,
validationService,
} from '../../services';
Expand Down Expand Up @@ -68,7 +68,7 @@ router.post<
const fitnessData = await userService.getUserFitnessData(userId);
const ftp = isSuccess(fitnessData)
? fitnessData.data.ftp
: slackService.logAndReturn(
: monitoringService.logAndReturn(
`user [${userId}] does not have any fitnessData stored. Returning {ftp: 200}`,
200
);
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/routes/auth/strava.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { isError, isSuccess } from '@dundring/utils';
import * as express from 'express';
import {
slackService,
monitoringService,
stravaService,
userService,
validationService,
Expand Down Expand Up @@ -61,7 +61,7 @@ router.post<
const fitnessData = await userService.getUserFitnessData(userId);
const ftp = isSuccess(fitnessData)
? fitnessData.data.ftp
: slackService.logAndReturn(
: monitoringService.logAndReturn(
`user [${userId}] does not have any fitnessData stored. Returning {ftp: 200}`,
200
);
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/routes/feedback/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { FeedbackRequestBody } from '@dundring/types';
import * as express from 'express';
import { slackService } from '../../services';
import { monitoringService } from '../../services';
import { success } from '@dundring/utils';

const router = express.Router();

router.post<null, {}, FeedbackRequestBody>('/', (req, res) => {
const { mail, message } = req.body;
slackService.log(`*Feedback${mail ? ` from ${mail}` : ''}*\n${message}`);
monitoringService.log(`*Feedback${mail ? ` from ${mail}` : ''}*\n${message}`);
res.send(success({}));
});

Expand Down
10 changes: 5 additions & 5 deletions apps/backend/src/services/groupSessionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
WebSocketResponse,
} from '@dundring/types';
import { generateRandomString, isSuccess } from '@dundring/utils';
import { slackService } from '.';
import { monitoringService } from '.';
import * as redis from '../redis';
import * as websocket from '../websocket';

Expand Down Expand Up @@ -83,7 +83,7 @@ export const createRoom = async (
id: roomId,
members: [user.username],
};
slackService.logRoomCreation(user.username, roomId);
monitoringService.logRoomCreation(user.username, roomId);
redis.createRoom(roomId, user);
return { type: 'created-group-session', room };
}
Expand All @@ -102,7 +102,7 @@ export const joinRoom = async (roomId: string, member: Member) => {
type: 'joined-group-session',
room,
};
slackService.logRoomJoin(member.username, roomId);
monitoringService.logRoomJoin(member.username, roomId);

// TODO: This is not needed as a broadcast is done to everyone including the sender
websocket.sendMessage(member.username, response);
Expand Down Expand Up @@ -132,9 +132,9 @@ export const leaveRoom = async (username: string, roomId: string) => {
const usersLeftInRoom = leaveRoomStatus.data;

if (usersLeftInRoom === 0) {
slackService.logRoomDeletion(username, roomId);
monitoringService.logRoomDeletion(username, roomId);
} else {
slackService.logRoomLeave(username, roomId);
monitoringService.logRoomLeave(username, roomId);

const membersInRoom = await redis.getRoomMembers(roomId);

Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as groupSessionService from './groupSessionService';
import * as mailService from './mailService';
import * as slackService from './slackService';
import * as monitoringService from './monitoringService';
import * as stravaService from './stravaService';
import * as userService from './userService';
import * as validationService from './validationService';
Expand All @@ -9,7 +9,7 @@ import * as workoutService from './workoutService';
export {
groupSessionService,
mailService,
slackService,
monitoringService,
stravaService,
userService,
validationService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,73 @@ const sendSlackMessage = (message: string) => {
}
};

const getDiscordUrl = (): string | null => {
const url = process.env.DISCORD_WEBHOOK_URL;

if (!url) return null;

return url;
};

const sendDiscordMessage = (message: string) => {
const url = getDiscordUrl();

if (process.env.NODE_ENV !== 'production') {
console.log(`[discord]: ${message}`);
return;
}

if (!url) return;

try {
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ content: message }),
});
} catch (e) {
console.error('[sendSlackMessage]:', e);
}
};

export const logActivityUpload = (activityId: number) => {
const message = `New upload: www.strava.com/activities/${activityId}`;
sendSlackMessage(message);
log(message);
};

export const logUserCreation = (user: { username: string; mail: string }) => {
const message = `New user: *${user.username}* (${user.mail})`;
sendSlackMessage(message);
log(message);
};

export const logRoomJoin = (username: string, roomId: string) => {
const message = `*${username}* joined group session with id *#${roomId}*`;
sendSlackMessage(message);
log(message);
};

export const logRoomCreation = (username: string, roomId: string) => {
const message = `*${username}* created group session with id *#${roomId}*`;
sendSlackMessage(message);
log(message);
};

export const logRoomLeave = (username: string, roomId: string) => {
const message = `*${username}* left group session with id *#${roomId}*.`;
sendSlackMessage(message);
log(message);
};

export const logRoomDeletion = (username: string, roomId: string) => {
const message = `*${username}* left group session with id *#${roomId}*. No more users in group session; deleting it.`;
sendSlackMessage(message);
log(message);
};

export const logAndReturn = <T>(message: string, data: T) => {
sendSlackMessage(message);
log(message);
return data;
};

export const log = (message: string) => {
sendSlackMessage(message);
sendDiscordMessage(message);
};
4 changes: 2 additions & 2 deletions apps/backend/src/services/stravaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { error, isSuccess, success } from '@dundring/utils';
import fetch from 'node-fetch';
import * as FormData from 'form-data';
import { slackService } from './index';
import { monitoringService } from './index';

require('dotenv').config();

Expand Down Expand Up @@ -91,7 +91,7 @@ export const uploadFileToStrava = async (

if (stravaUploadResponse) {
if (isSuccess(stravaUploadResponse)) {
slackService.logActivityUpload(
monitoringService.logActivityUpload(
stravaUploadResponse.data.activity_id
);
}
Expand Down
8 changes: 4 additions & 4 deletions apps/backend/src/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Scopes, UserCreationStrava } from '@dundring/types';
require('dotenv').config();
import * as db from '../db';
import { isSuccess } from '@dundring/utils';
import { slackService } from '.';
import { monitoringService } from '.';

export const getUser = async (query: { username: string } | { id: string }) =>
db.getUser(query);
Expand All @@ -14,7 +14,7 @@ export const updateUser = async (
) => {
const ret = await db.updateUser(userId, data);
if (isSuccess(ret)) {
slackService.log(
monitoringService.log(
`User *${ret.data.id}* updated profile ${
data.username ? `username=*${data.username}* ` : ''
}${data.ftp ? `ftp=*${data.ftp}*` : ''}`
Expand All @@ -40,7 +40,7 @@ export const createUserFromStrava = async (body: UserCreationStrava) => {
const ret = await db.createStravaUserWithRandomUsername(body);
if (isSuccess(ret)) {
const athleteId = ret.data.stravaAuthentication?.athleteId;
slackService.log(
monitoringService.log(
`New user: *${ret.data.id}* created with Strava: athlete id: https://strava.com/athletes/${athleteId}`
);
}
Expand All @@ -50,7 +50,7 @@ export const createUserFromStrava = async (body: UserCreationStrava) => {
export const createUserFromMail = async (mail: string) => {
const ret = await db.createMailUserWithRandomUsername(mail);
if (isSuccess(ret)) {
slackService.log(
monitoringService.log(
`New user: *${ret.data.id}* created with mail: ${ret.data.mailAuthentication?.mail}`
);
}
Expand Down
Loading