From 375ea3a9634f4d09c3ac2d3fd40e8682d3464f36 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Sun, 20 Sep 2020 22:33:56 -0400 Subject: [PATCH] score remodel with frontend --- src/api/loc.sh/routes/internal.ts | 89 +++++++++++++++++++++++++++++++ src/commands/score.ts | 12 +++++ src/intervals/score.ts | 4 +- src/models/Score.ts | 2 + 4 files changed, 106 insertions(+), 1 deletion(-) diff --git a/src/api/loc.sh/routes/internal.ts b/src/api/loc.sh/routes/internal.ts index 3836e58..cdba43a 100644 --- a/src/api/loc.sh/routes/internal.ts +++ b/src/api/loc.sh/routes/internal.ts @@ -2,8 +2,11 @@ import { Route, Server } from '../../../class'; // import acknowledgements from '../../../configs/acknowledgements.json'; export default class Internal extends Route { + public timeout: Set; + constructor(server: Server) { super(server); + this.timeout = new Set(); this.conf = { path: '/int', }; @@ -28,5 +31,91 @@ export default class Internal extends Route { return this.handleError(err, res); } }); + + this.router.get('/score', async (req, res) => { + try { + res.setHeader('Access-Control-Allow-Origin', '*'); + if (this.timeout.has(req.ip)) return res.status(429).json({ code: this.constants.codes.PERMISSION_DENIED, message: this.constants.messages.PERMISSION_DENIED }); + if (!req.query.pin) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR }); + const args = req.query.pin.toString(); + this.timeout.add(req.ip); + setTimeout(() => this.timeout.delete(req.ip), 1800000); + let score = await this.server.client.db.Score.findOne({ pin: [Number(args.split('-')[0]), Number(args.split('-')[1]), Number(args.split('-')[2])] }).lean().exec(); + if (!score) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); + const member = await this.server.client.getRESTGuildMember(this.constants.discord.SERVER_ID, score.userID); + if (!member) return res.status(404).json({ code: this.constants.codes.ACCOUNT_NOT_FOUND }); + await this.server.client.db.Score.updateOne({ userID: score.userID }, { $addToSet: { softInquiries: { name: `${member.username} via report.libraryofcode.org @ IP ${req.ip}`, date: new Date() } } }); + score = await this.server.client.db.Score.findOne({ pin: [Number(args.split('-')[0]), Number(args.split('-')[1]), Number(args.split('-')[2])] }).lean().exec(); + + let totalScore = '0'; + let activityScore = '0'; + let moderationScore = '0'; + let roleScore = '0'; + let cloudServicesScore = '0'; + let otherScore = '0'; + let miscScore = '0'; + + if (score.total < 200) totalScore = '---'; + else if (score.total > 800) totalScore = '800'; + else totalScore = `${score.total}`; + + if (score.activity < 10) activityScore = '---'; + else if (score.activity > Math.floor((Math.log1p(5000 + 300 + 200 + 100) * 12))) activityScore = String(Math.floor((Math.log1p(5000 + 300 + 200 + 100) * 12))); + else activityScore = `${score.activity}`; + + if (score.roles <= 0) roleScore = '---'; + else if (score.roles > 54) roleScore = '54'; + else roleScore = `${score.roles}`; + + moderationScore = `${score.moderation}`; + + if (score.other === 0) otherScore = '---'; + else otherScore = `${score.other}`; + + if (score.staff <= 0) miscScore = '---'; + else miscScore = `${score.staff}`; + + if (score.cloudServices === 0) cloudServicesScore = '---'; + else if (score.cloudServices > 10) cloudServicesScore = '10'; + else cloudServicesScore = `${score.cloudServices}`; + + const moderations = await this.server.client.db.Moderation.find({ userID: score.userID }); + + + return res.status(200).json({ + name: `${member.username}#${member.discriminator}`, + userID: score.userID, + pin: score.pin?.join('-'), + score: totalScore, + activityScore, + cloudServicesScore, + moderationScore, + roleScore, + otherScore, + miscScore, + notify: score.notify, + locked: !!score.locked, + totalModerations: moderations?.length > 0 ? moderations.length : 0, + inquiries: score.inquiries?.length > 0 ? score.inquiries : [], + softInquiries: score.softInquiries?.length > 0 ? score.softInquiries : [], + lastUpdated: score.lastUpdate, + }); + } catch (err) { + return this.handleError(err, res); + } + }); + + this.router.get('/id', async (req, res) => { + try { + if (req.query?.auth.toString() !== this.server.client.config.internalKey) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); + if (!req.query.pin) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR }); + const args = req.query.pin.toString(); + const user = await this.server.client.db.Score.findOne({ pin: [Number(args.split('-')[0]), Number(args.split('-')[1]), Number(args.split('-')[2])] }).lean().exec(); + if (!user) return res.status(404).json({ code: this.constants.codes.ACCOUNT_NOT_FOUND, message: this.constants.messages.NOT_FOUND }); + return res.status(200).json({ userID: user.userID }); + } catch (err) { + return this.handleError(err, res); + } + }); } } diff --git a/src/commands/score.ts b/src/commands/score.ts index 6a54cad..cdbed50 100644 --- a/src/commands/score.ts +++ b/src/commands/score.ts @@ -15,6 +15,7 @@ export default class Score extends Command { public async run(message: Message, args: string[]) { try { + let check = false; let user: User; if (args[0] === 'notify') { user = message.author; @@ -51,6 +52,8 @@ export default class Score extends Command { if (!args[0] || !this.checkCustomPermissions(this.client.util.resolveMember(message.author.id, this.mainGuild), 6)) { user = message.author; if (!user) return this.error(message.channel, 'Member not found.'); + await this.client.db.Score.updateOne({ userID: user.id }, { $addToSet: { softInquiries: { name: `${user.username} via Discord`, date: new Date() } } }); + check = true; } else { user = this.client.util.resolveMember(args[0], this.mainGuild).user; if (!user) { @@ -157,6 +160,15 @@ export default class Score extends Command { // eslint-disable-next-line no-mixed-operators if ((args[1] === 'hard' || args[1] === 'soft') && this.checkCustomPermissions(this.client.util.resolveMember(message.author.id, this.mainGuild), 6)) { if (score.pin?.length > 0) embed.addField('PIN', score.pin.join('-'), true); + + if (args[1] === 'soft' && !check) { + let name = ''; + for (const role of this.client.util.resolveMember(message.author.id, this.mainGuild).roles.map((r) => this.mainGuild.roles.get(r)).sort((a, b) => b.position - a.position)) { + name = `Library of Code sp-us | ${role.name}`; + break; + } + await this.client.db.Score.updateOne({ userID: user.id }, { $addToSet: { softInquiries: { name, date: new Date() } } }); + } } if (args[1] === 'hard' && this.checkCustomPermissions(this.client.util.resolveMember(message.author.id, this.mainGuild), 6)) { await message.channel.createMessage({ embed }); diff --git a/src/intervals/score.ts b/src/intervals/score.ts index fc88a5d..978f38b 100644 --- a/src/intervals/score.ts +++ b/src/intervals/score.ts @@ -40,7 +40,8 @@ export default async function calculateScore(client: Client): Promise