From 5ae9602f0a853e3a5b1963a30ae5a3d8c8e87afd Mon Sep 17 00:00:00 2001 From: Matthew R Date: Tue, 29 Sep 2020 23:44:20 -0400 Subject: [PATCH] fixes to api for reports --- .../comm.libraryofcode.org/routes/report.ts | 42 ++++++++++++++++--- src/class/AutoMod.ts | 31 ++++++++++++++ src/class/Route.ts | 1 + src/class/Server.ts | 4 +- src/class/Util.ts | 18 ++++++++ 5 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 src/class/AutoMod.ts diff --git a/src/api/comm.libraryofcode.org/routes/report.ts b/src/api/comm.libraryofcode.org/routes/report.ts index d6b7619..8582337 100644 --- a/src/api/comm.libraryofcode.org/routes/report.ts +++ b/src/api/comm.libraryofcode.org/routes/report.ts @@ -11,17 +11,17 @@ export default class Report extends Route { public bind() { this.router.post('/hard', async (req, res) => { try { - if (!req.headers.authorization) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.codes.UNAUTHORIZED }); - if (!req.body.pin || !req.body.userID || !req.body.reason) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR, message: this.constants.codes.CLIENT_ERROR }); + if (!req.headers.authorization) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); + if (!req.body.pin || !req.body.userID || !req.body.reason) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR, message: this.constants.messages.CLIENT_ERROR }); if (req.body.reason?.length < 1) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR, message: this.constants.codes.CLIENT_ERROR }); const merchant = await this.server.client.db.Merchant.findOne({ key: req.headers.authorization }).lean().exec(); - if (!merchant) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.codes.UNAUTHORIZED }); + if (!merchant) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); const member = await this.server.client.db.Score.findOne({ userID: req.body.userID, 'pin.2': req.body.pin }).lean().exec(); - if (!member) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.codes.UNAUTHORIZED }); + if (!member) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); - if (member.locked) return res.status(403).json({ code: this.constants.codes.PERMISSION_DENIED, message: this.constants.codes.PERMISSION_DENIED }); + if (member.locked) return res.status(403).json({ code: this.constants.codes.PERMISSION_DENIED, message: this.constants.messages.PERMISSION_DENIED }); let totalScore: number; let activityScore: number; @@ -88,5 +88,37 @@ export default class Report extends Route { return this.handleError(err, res); } }); + + this.router.post('/soft', async (req, res) => { + try { + if (!req.headers.authorization) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); + if (!req.body.pin || !req.body.userID) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR, message: this.constants.messages.CLIENT_ERROR }); + + const merchant = await this.server.client.db.Merchant.findOne({ key: req.headers.authorization }).lean().exec(); + if (!merchant) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); + + const member = await this.server.client.db.Score.findOne({ userID: req.body.userID, 'pin.2': req.body.pin }).lean().exec(); + if (!member) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); + + let totalScore: number; + + if (member.total < 200) totalScore = 0; + else if (member.total > 800) totalScore = 800; + else totalScore = Math.round(member.total); + + await this.server.client.db.Merchant.updateOne({ key: req.headers.authorization }, { $addToSet: { pulls: { type: 1, reason: 'N/A', date: new Date() } } }); + await this.server.client.db.Score.updateOne({ userID: member.userID }, { $addToSet: { softInquiries: { name: merchant.name, date: new Date() } } }); + + return res.status(200).json({ + code: this.constants.codes.SUCCESS, + message: { + userID: member.userID, + totalScore, + }, + }); + } catch (err) { + return this.handleError(err, res); + } + }); } } diff --git a/src/class/AutoMod.ts b/src/class/AutoMod.ts new file mode 100644 index 0000000..3aebbad --- /dev/null +++ b/src/class/AutoMod.ts @@ -0,0 +1,31 @@ +/* eslint-disable no-bitwise */ +import { Member, Message, User } from 'eris'; +import { randomBytes } from 'crypto'; +import moment, { unitOfTime } from 'moment'; +import { Client, Moderation, RichEmbed } from '.'; +import { Moderation as ModerationModel, ModerationInterface } from '../models'; +import { moderation as channels } from '../configs/channels.json'; + +export default class AutoMod extends Moderation { + public client: Client; + + public list: Set + + // eslint-disable-next-line @typescript-eslint/no-useless-constructor + constructor(client: Client) { + super(client); + this.list = new Set(); + } + + public run(message: Message, member: Member) { + try { + if (member) + } + } + + public warn(message: Message, member: Member) { + try { + await message.channel.createMessage(`__**`) + } + } +} diff --git a/src/class/Route.ts b/src/class/Route.ts index 0d90eaa..c3b20ff 100644 --- a/src/class/Route.ts +++ b/src/class/Route.ts @@ -64,6 +64,7 @@ export default class Route { PERMISSION_DENIED: ['PERMISSION_DENIED', 'You do not have valid credentials to access this resource.'], NOT_FOUND: ['NOT_FOUND', 'The resource you requested cannot be located.'], ENDPOINT_NOT_FOUND: ['ENDPOINT_NOT_FOUND', 'The endpoint you requested does not exist or cannot be located.'], + CLIENT_ERROR: ['CLIENT_ERROR', 'The information provided to this endpoint via headers, body, query, or parameters are invalid.'], SERVER_ERROR: ['INTERNAL_ERROR', 'An internal error has occurred, Engineers have been notified.'], DEPRECATED: ['ENDPOINT_OR_RESOURCE_DEPRECATED', 'The endpoint or resource you\'re trying to access has been deprecated.'], MAINTENANCE_OR_UNAVAILABLE: ['SERVICE_UNAVAILABLE', 'The endpoint or resource you\'re trying to access is either in maintenance or is not available.'], diff --git a/src/class/Server.ts b/src/class/Server.ts index 7cfd4d3..ab4f576 100644 --- a/src/class/Server.ts +++ b/src/class/Server.ts @@ -22,8 +22,8 @@ export default class Server { this.port = port; this.root = routeRoot; - this.loadRoutes(); this.init(); + this.loadRoutes(); } get client() { @@ -50,8 +50,8 @@ export default class Server { } public init() { - this.app.set('trust proxy', 'loopback'); this.app.use(bodyParser.json()); + this.app.set('trust proxy', 'loopback'); this.app.use(helmet({ hsts: false, hidePoweredBy: false, diff --git a/src/class/Util.ts b/src/class/Util.ts index a49fbaa..aa5432a 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -151,4 +151,22 @@ export default class Util { public encode(arg: string) { return arg.split('').map((x) => x.charCodeAt(0) / 400); } + + public normalize(string) { + const input = []; + // eslint-disable-next-line no-plusplus + for (let i = 0; i < string.length; i++) { + input.push(string.charCodeAt(i) / 1000); + } + return input; + } + + public convert_ascii(ascii: []) { + let string = ''; + // eslint-disable-next-line no-plusplus + for (let i = 0; i < ascii.length; i++) { + string += String.fromCharCode(ascii[i] * 4000); + } + return string; + } }