From 11d79cd7ade8aeb803f8b4c27536e10a832eb4a6 Mon Sep 17 00:00:00 2001 From: Akif9748 Date: Wed, 6 Apr 2022 22:21:50 +0300 Subject: [PATCH] Timeout removed --- classes/message.js | 30 +++++++++++++++----------- classes/thread.js | 30 +++++++++++++++----------- classes/user.js | 49 +++++++++++++++++++++++++----------------- index.js | 1 - middlewares/timeout.js | 15 ------------- models/Timeout.js | 8 ------- models/index.js | 5 ++--- routes/message.js | 4 ---- routes/threads.js | 1 - 9 files changed, 65 insertions(+), 78 deletions(-) delete mode 100644 middlewares/timeout.js delete mode 100644 models/Timeout.js diff --git a/classes/message.js b/classes/message.js index b9db078..714ec20 100644 --- a/classes/message.js +++ b/classes/message.js @@ -17,21 +17,25 @@ module.exports = class Message { } async getById(id = this.id) { - this.id = Number(id); + try { + this.id = Number(id); - const message = await MessageModel.findOne({ id }); - if (!message) return null; + const message = await MessageModel.findOne({ id }); + if (!message) return null; - const { content, authorID, author = null, threadID = null, time = Date.now(), deleted = false, edited = false, react = {} } = message; - this.content = content; - this.threadID = threadID; - this.author = author; - this.authorID = authorID; - this.time = time; - this.deleted = deleted; - this.edited = edited; - this.react = react; - return this; + const { content, authorID, author = null, threadID = null, time = Date.now(), deleted = false, edited = false, react = {} } = message; + this.content = content; + this.threadID = threadID; + this.author = author; + this.authorID = authorID; + this.time = time; + this.deleted = deleted; + this.edited = edited; + this.react = react; + return this; + } catch (e) { + return null; + } } async takeId() { diff --git a/classes/thread.js b/classes/thread.js index 25563e3..b169ea9 100644 --- a/classes/thread.js +++ b/classes/thread.js @@ -17,28 +17,32 @@ module.exports = class Thread { } async getById(id = this.id) { - this.id = Number(id); + try { + this.id = Number(id); - - const thread = await ThreadModel.findOne({ id }); - if (!thread) return null; - const { title, authorID, author, messages = [], time = Date.now(), deleted = false } = thread; - this.title = title - this.author = author; - this.authorID = authorID; - this.messages = messages; - this.time = time; - this.deleted = deleted; + const thread = await ThreadModel.findOne({ id }); + if (!thread) return null; - return this; + const { title, authorID, author, messages = [], time = Date.now(), deleted = false } = thread; + this.title = title + this.author = author; + this.authorID = authorID; + this.messages = messages; + this.time = time; + this.deleted = deleted; + + return this; + } catch (e) { + return null; + } } push(messageID) { this.messages.push(messageID) return this; } - + async takeId() { this.id = await ThreadModel.count({}) || 0; return this; diff --git a/classes/user.js b/classes/user.js index b379cf0..abf1ad8 100644 --- a/classes/user.js +++ b/classes/user.js @@ -14,34 +14,43 @@ module.exports = class User { } async getById(id = this.id) { - this.id = Number(id); - const user = await UserModel.findOne({ id }); - if (!user) return null; + try { + this.id = Number(id); + const user = await UserModel.findOne({ id }); + if (!user) return null; - const { name = "guest", avatar = "/images/guest.png", time = Date.now(), admin = false, deleted = false } = user; - this.name = name; - this.avatar = avatar; - this.time = time; - this.admin = admin; - this.deleted = deleted; - return this; + const { name = "guest", avatar = "/images/guest.png", time = Date.now(), admin = false, deleted = false } = user; + this.name = name; + this.avatar = avatar; + this.time = time; + this.admin = admin; + this.deleted = deleted; + return this; + } catch (e) { + return null; + } } async getByName(Name = this.name) { + try { + const user = await UserModel.findOne({ name: Name }); - const user = await UserModel.findOne({ name: Name }); - if (!user) return null; + if (!user) return null; - const { id, name = "guest", avatar = "/images/guest.png", time = Date.now(), admin = false, deleted = false } = user; + const { id, name = "guest", avatar = "/images/guest.png", time = Date.now(), admin = false, deleted = false } = user; + + this.id = Number(id); + this.name = name; + this.avatar = avatar; + this.time = time; + this.admin = admin; + this.deleted = deleted; + return this; + } catch (e) { + return null; + } - this.id = Number(id); - this.name = name; - this.avatar = avatar; - this.time = time; - this.admin = admin; - this.deleted = deleted; - return this; } diff --git a/index.js b/index.js index 6baeae8..61923fe 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,6 @@ app.use(express.static("public")); app.set("view engine", "ejs"); app.use(express.json()); app.use(require("./middlewares/user")); -app.use(require("./middlewares/timeout")); for (const file of fs.readdirSync("./routes")) app.use("/" + file.replace(".js", ""), require(`./routes/${file}`)); diff --git a/middlewares/timeout.js b/middlewares/timeout.js deleted file mode 100644 index d0d5275..0000000 --- a/middlewares/timeout.js +++ /dev/null @@ -1,15 +0,0 @@ -const { TimeoutModel } = require("../models"); -module.exports = async (req, res, next) => { - if (!req.user || req.user.admin) return next(); - - const timeout = await TimeoutModel.findOne({ id: req.user.id }) || new TimeoutModel({ until: Date.now() - 1000, id: req.user.id }); - - req.timeout = timeout; - - if (timeout.until > Date.now()) - req.ratelimit = true; - - - next(); - -} \ No newline at end of file diff --git a/models/Timeout.js b/models/Timeout.js deleted file mode 100644 index 2b20aff..0000000 --- a/models/Timeout.js +++ /dev/null @@ -1,8 +0,0 @@ -const { Schema, model } = require("mongoose") - - -module.exports = model('timeout', new Schema({ - id: { type: Number, unique: true }, - until: Number - -})) \ No newline at end of file diff --git a/models/index.js b/models/index.js index b60fe23..78d66db 100644 --- a/models/index.js +++ b/models/index.js @@ -1,7 +1,6 @@ const UserModel = require("./User"), MessageModel = require("./Message"), ThreadModel = require("./Thread"), - SecretModel = require("./Secret"), - TimeoutModel = require("./Timeout"); + SecretModel = require("./Secret"); -module.exports = { UserModel, MessageModel, ThreadModel, SecretModel, TimeoutModel }; \ No newline at end of file +module.exports = { UserModel, MessageModel, ThreadModel, SecretModel }; \ No newline at end of file diff --git a/routes/message.js b/routes/message.js index e4deadc..3eaaf05 100644 --- a/routes/message.js +++ b/routes/message.js @@ -17,7 +17,6 @@ app.use(require("../middlewares/login")); app.post("/", async (req, res) => { - // if (req.ratelimit) return error(res, 429, "Wait until " + new Date(req.timeout.until).toLocaleTimeString("tr") + ", you are too quick for send.") const thread = await new Thread().getById(req.body.threadID); if (thread) { @@ -26,9 +25,6 @@ app.post("/", async (req, res) => { thread.push(message.id); thread.write(); - // req.timeout.until += 1000 * 30; - // await req.timeout.save() - res.redirect('/threads/' + req.body.threadID); } diff --git a/routes/threads.js b/routes/threads.js index c66fada..64e2a3a 100644 --- a/routes/threads.js +++ b/routes/threads.js @@ -50,7 +50,6 @@ app.use(require("../middlewares/login")); app.post("/", async (req, res) => { -// if (req.ratelimit) return error(res, 429, "Wait until " + new Date(req.timeout.until).toLocaleTimeString("tr") + ", you are too quick for send.") const { title = null, content = null } = req.body;