From d62cc582c9a700f23603952550ad1d6fe3f4997d Mon Sep 17 00:00:00 2001 From: Nikky Date: Tue, 30 Nov 2021 05:05:16 +0300 Subject: [PATCH] wip --- src/config/database.config.js | 2 ++ src/logic/spam.js | 34 ++++++++++++++++++++++++++++++++++ src/logic/utils.js | 22 +++++++--------------- src/model/user.model.js | 10 ++++++++++ 4 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 src/logic/spam.js diff --git a/src/config/database.config.js b/src/config/database.config.js index 00bd703..235d9c3 100644 --- a/src/config/database.config.js +++ b/src/config/database.config.js @@ -1,4 +1,6 @@ +// https://sequelize.org/master/manual/getting-started.html#connecting-to-a-database + export const DatabaseConfig = { dialect: 'sqlite', storage: './database.sqlite', diff --git a/src/logic/spam.js b/src/logic/spam.js new file mode 100644 index 0000000..e12262d --- /dev/null +++ b/src/logic/spam.js @@ -0,0 +1,34 @@ + +export class CacheTable{ + + constructor(threshold){ + this.entries = {}; + this.count = 0; + this.threshold = threshold || 1024; + } + set(key, val){ + if(this.entries[key] === undefined){ + if(this.count < this.threshold){ + this.count++; + }else{ + for(var otherKey in this.entries){ + delete this.entries[otherKey]; + break; + } + } + } + this.entries[key] = val; + } + remove(key){ + if(this.entries[key] !== undefined){ + delete this.entries[key]; + this.count--; + } + } + get(key){ return this.entries[key]; } + allEntries(){ return this.entries; } +} + +export class SpamTable{ +} + diff --git a/src/logic/utils.js b/src/logic/utils.js index 9ee93c3..20423bc 100644 --- a/src/logic/utils.js +++ b/src/logic/utils.js @@ -1,34 +1,26 @@ function isString(val){ - return (typeof val) === 'string'; + return (typeof val) === 'string'; } export function checkStringParam(param, min, max){ - return isString(param) && (param.length >= min) && (param.length <= max); + return isString(param) && (param.length >= min) && (param.length <= max); } export function errorOut(reply, msg, code){ reply.code(code || 400); - reply.send(msg) || 'Bad request.'; + reply.send(msg || 'Bad request.'); } export function reverseString(str){ - return str.split("").reverse().join(""); + return str.split("").reverse().join(""); } export function randomElement(arr){ - return arr[Math.floor(Math.random()*arr.length)]; + return arr[Math.floor(Math.random()*arr.length)]; } export function notYet(date){ - if(!date) return false; - return (new Date() <= date); + if(!date) return false; + return (new Date() <= date); } - -// export class CacheTable{ - -// constructor(){ -// this.data = {}; -// } - -// } diff --git a/src/model/user.model.js b/src/model/user.model.js index ffa2432..3ff49c8 100644 --- a/src/model/user.model.js +++ b/src/model/user.model.js @@ -1,5 +1,10 @@ import Sequelize from 'sequelize'; +/** + * @param {Object} props + * @param {import('sequelize/types').Sequelize} props.db + */ + const UserEntity = ({db}) => ( db.define('Users', { uuid: {type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.UUIDV4}, @@ -15,6 +20,11 @@ const UserEntity = ({db}) => ( restoreCode: Sequelize.TEXT, restoreExpiry: Sequelize.DATE + }, { + indexes: [ + { fields: ['token'], unique: true }, + { fields: ['email'], unique: true } + ] }) );