-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): simplify vote webhook handling
This change adds a middleware function to the API that handles incoming top.gg webhook requests. The middleware verifies the request authorization header, checks the validity of the payload, and then processes the vote event. This includes incrementing the user's vote count, adding the voter role, and announcing the vote in the support server. The changes also remove the previously used `VoteManager` class, as its functionality has been consolidated into the new middleware.
- Loading branch information
Showing
5 changed files
with
99 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
import dblRouter from '#main/api/routes/dbl.js'; | ||
import { VoteManager } from '#main/managers/VoteManager.js'; | ||
import Logger from '#utils/Logger.js'; | ||
import express from 'express'; | ||
|
||
const app = express(); | ||
|
||
export const startApi = (voteManager: VoteManager) => { | ||
app.use(express.json()); | ||
app.use(dblRouter(voteManager)); | ||
app.use(express.json()); | ||
app.get('/', (req, res) => res.redirect('https://interchat.fun')); | ||
app.use('/dbl', dblRouter); | ||
|
||
app.get('/', (req, res) => res.redirect('https://interchat.fun')); | ||
|
||
// run da server | ||
app.listen(process.env.PORT, () => | ||
Logger.info(`API listening on http://localhost:${process.env.PORT}`), | ||
); | ||
}; | ||
// run da server | ||
app.listen(process.env.PORT, () => | ||
Logger.info(`API listening on http://localhost:${process.env.PORT}`), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,9 @@ | ||
// express route boyeee | ||
import { VoteManager } from '#main/managers/VoteManager.js'; | ||
import type { WebhookPayload } from '#types/topgg.d.ts'; | ||
import Logger from '#utils/Logger.js'; | ||
import { Router } from 'express'; | ||
|
||
const isValidVotePayload = (payload: WebhookPayload) => { | ||
const payloadTypes = ['upvote', 'test']; | ||
const isValidData = | ||
typeof payload.user === 'string' && | ||
typeof payload.bot === 'string' && | ||
payloadTypes.includes(payload.type); | ||
const dblRouter: Router = Router(); | ||
const voteManager = new VoteManager(); | ||
|
||
const isValidWeekendType = | ||
typeof payload.isWeekend === 'boolean' || typeof payload.isWeekend === 'undefined'; | ||
dblRouter.post('/', voteManager.middleware.bind(voteManager)); | ||
|
||
return isValidData && isValidWeekendType; | ||
}; | ||
|
||
const router: Router = Router(); | ||
|
||
export default (voteManager: VoteManager) => { | ||
router.post('/dbl', (req, res) => { | ||
const dblHeader = req.header('Authorization'); | ||
if (dblHeader !== process.env.TOPGG_WEBHOOK_SECRET) { | ||
return res.status(401).json({ message: 'Unauthorized' }); | ||
} | ||
|
||
const payload: WebhookPayload = req.body; | ||
|
||
if (!isValidVotePayload(payload)) { | ||
Logger.error('Invalid payload received from top.gg, possible untrusted request: %O', payload); | ||
return res.status(400).json({ message: 'Invalid payload' }); | ||
} | ||
|
||
voteManager.emit('vote', payload); | ||
|
||
return res.status(204).send(); | ||
}); | ||
|
||
return router; | ||
}; | ||
export default dblRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters