-
-
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.
feat: Vote annoucements, and storage of topgg votes in db
- Loading branch information
Showing
26 changed files
with
1,203 additions
and
253 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,58 +1,17 @@ | ||
import http from 'http'; | ||
import { node } from '@tensorflow/tfjs-node'; | ||
import { load } from 'nsfwjs'; | ||
import { VoteManager } from '../managers/VoteManager.js'; | ||
import Logger from '../utils/Logger.js'; | ||
import { captureException } from '@sentry/node'; | ||
import express from 'express'; | ||
import dblRoute from './routes/dbl.js'; | ||
import nsfwRouter from './routes/nsfw.js'; | ||
|
||
const model = await load(); | ||
const port = 3000; | ||
// to start the server | ||
export const startApi = (data: { voteManager: VoteManager }) => { | ||
const app = express(); | ||
|
||
export default function start() { | ||
const server = http.createServer(async (req, res) => { | ||
if (req.method === 'GET' && req.url?.startsWith('/nsfw')) { | ||
const url = new URL(req.url, `http://${req.headers.host}`); | ||
const imageUrl = url.searchParams.get('url'); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: true })); | ||
app.use(nsfwRouter); | ||
if (data.voteManager) app.use(dblRoute(data.voteManager)); | ||
|
||
if (!imageUrl || typeof imageUrl !== 'string') { | ||
res.writeHead(400, { 'Content-Type': 'application/json' }); | ||
res.end(JSON.stringify({ error: 'Missing url query parameter.' })); | ||
return; | ||
} | ||
|
||
const regex = /\bhttps?:\/\/\S+?\.(?:png|jpe?g)(?:\?\S+)?\b/; | ||
if (!regex.test(imageUrl)) { | ||
res.writeHead(400, { 'Content-Type': 'application/json' }); | ||
res.end( | ||
JSON.stringify({ | ||
error: 'Invalid url parameter. Must be a valid PNG, JPG, or JPEG image URL.', | ||
}), | ||
); | ||
return; | ||
} | ||
|
||
try { | ||
const imageBuffer = await (await fetch(imageUrl)).arrayBuffer(); | ||
const imageTensor = (await node.decodeImage(Buffer.from(imageBuffer), 3)) as any; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
const predictions = await model.classify(imageTensor); | ||
imageTensor.dispose(); | ||
|
||
res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
res.end(JSON.stringify(predictions)); | ||
} | ||
catch (error) { | ||
Logger.error(error); | ||
captureException(error); | ||
res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
res.end('500 Internal Server Error'); | ||
} | ||
} | ||
else { | ||
res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
res.end('Why the hell are we even here? This is a 404.'); | ||
} | ||
}); | ||
|
||
server.listen(port, () => { | ||
Logger.info(`API listening on port http://localhost:${port}.`); | ||
}); | ||
} | ||
app.listen(8080, () => Logger.info('API listening on port http://localhost:8080.')); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Webhook } from '@top-gg/sdk'; | ||
import { Router } from 'express'; | ||
import { VoteManager } from '../../managers/VoteManager.js'; | ||
|
||
// NOTE: testing this against top.gg only works in the production server | ||
// to test locally use postman or something similar to send a POST request to http://localhost:8080/dbl | ||
const router: Router = Router(); | ||
const TopggWebhook = new Webhook(process.env.TOPGG_AUTH); | ||
|
||
export default (voteManager: VoteManager) => { | ||
router.post( | ||
'/dbl', | ||
TopggWebhook.listener((vote) => { | ||
// emit the vote event to use in other files | ||
voteManager?.emit('vote', vote); | ||
}), | ||
); | ||
return router; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { load } from 'nsfwjs'; | ||
import { Router } from 'express'; | ||
import { captureException } from '@sentry/node'; | ||
import { node } from '@tensorflow/tfjs-node'; | ||
import Logger from '../../utils/Logger.js'; | ||
|
||
const nsfwModel = await load(); | ||
const router: Router = Router(); | ||
|
||
router.get('/nsfw', async (req, res) => { | ||
const url = new URL(req.url, `http://${req.headers.host}`); | ||
const imageUrl = url.searchParams.get('url'); | ||
|
||
if (!imageUrl || typeof imageUrl !== 'string') { | ||
res.writeHead(400, { 'Content-Type': 'application/json' }); | ||
res.end(JSON.stringify({ error: 'Missing url query parameter.' })); | ||
return; | ||
} | ||
|
||
const regex = /\bhttps?:\/\/\S+?\.(?:png|jpe?g)(?:\?\S+)?\b/; | ||
if (!regex.test(imageUrl)) { | ||
res.writeHead(400, { 'Content-Type': 'application/json' }); | ||
res.end( | ||
JSON.stringify({ | ||
error: 'Invalid url parameter. Must be a valid PNG, JPG, or JPEG image URL.', | ||
}), | ||
); | ||
return; | ||
} | ||
|
||
try { | ||
const imageBuffer = await (await fetch(imageUrl)).arrayBuffer(); | ||
const imageTensor = (await node.decodeImage(Buffer.from(imageBuffer), 3)) as any; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
const predictions = await nsfwModel.classify(imageTensor); | ||
imageTensor.dispose(); | ||
|
||
res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
res.end(JSON.stringify(predictions)); | ||
} | ||
catch (error) { | ||
Logger.error(error); | ||
captureException(error); | ||
res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
res.end('500 Internal Server Error'); | ||
} | ||
}); | ||
|
||
export default router; |
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
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
Oops, something went wrong.