From d37ef9ae2a0869ab5d881997cf3ed3bbf9fd6cb3 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Sun, 13 Sep 2020 20:47:41 -0400 Subject: [PATCH] score calculations for users --- src/api/routes/Webhook.ts | 42 +++++++++++++++++++++++++++++++++++++++ src/class/Security.ts | 3 ++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/api/routes/Webhook.ts b/src/api/routes/Webhook.ts index 65c5fe4..a0e1766 100644 --- a/src/api/routes/Webhook.ts +++ b/src/api/routes/Webhook.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-continue */ import { TextChannel } from 'eris'; import { Server } from '..'; import { Route, RichEmbed } from '../../class'; @@ -28,5 +29,46 @@ export default class Webhook extends Route { return this.handleError(err, res); } }); + + this.router.get('/score', async (req, res) => { + try { + if (req.query?.authorization !== this.server.security.keys.iv.toString('hex')) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); + if (!req.query?.id) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR }); + + const account = await this.server.client.db.Account.findOne({ userID: req.query.id.toString() }).lean().exec(); + if (!account) return res.status(200).send({ error: true }); + const moderations = await this.server.client.db.Moderation.find({ userID: req.query.id.toString() }).lean().exec(); + + const response: { + error: boolean, + tier: number, + totalReferrals?: number, + createdAt?: Date, + warns?: Date[], + locks?: Date[], + deletes?: Date[], + } = { + error: false, + tier: account.tier, + totalReferrals: 0, + createdAt: account.createdAt, + warns: [], + locks: [], + deletes: [], + }; + if (account.totalReferrals && account.totalReferrals > 0) response.totalReferrals = account.totalReferrals; + if (moderations.length > 0) { + for (const moderation of moderations) { + if (moderation.reason?.includes('DN/C')) continue; + if (moderation.type === 1) response.warns.push(moderation.date); + if (moderation.type === 2) response.locks.push(moderation.date); + if (moderation.type === 4) response.deletes.push(moderation.date); + } + } + return res.status(200).json(response); + } catch (err) { + return this.handleError(err, res); + } + }); } } diff --git a/src/class/Security.ts b/src/class/Security.ts index bd00631..a59f9a2 100644 --- a/src/class/Security.ts +++ b/src/class/Security.ts @@ -7,7 +7,7 @@ import { AccountInterface } from '../models'; export default class Security { public client: Client; - protected readonly keyBase: { key: string, iv: string }; + protected readonly keyBase: { key: string, iv: string, internal: string }; constructor(client: Client) { this.client = client; @@ -18,6 +18,7 @@ export default class Security { return { key: Buffer.from(this.keyBase.key, 'base64'), iv: Buffer.from(this.keyBase.iv, 'base64'), + internal: Buffer.from(this.keyBase.internal, 'hex'), }; }