1
0
Fork 0

score calculations for users

refactor/models
Matthew 2020-09-13 20:47:41 -04:00
parent 01f6a04372
commit d37ef9ae2a
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
2 changed files with 44 additions and 1 deletions

View File

@ -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);
}
});
}
}

View File

@ -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'),
};
}