Timeout removed

This commit is contained in:
Akif9748 2022-04-06 22:21:50 +03:00
parent 0144741a97
commit 11d79cd7ad
9 changed files with 65 additions and 78 deletions

View file

@ -17,21 +17,25 @@ module.exports = class Message {
} }
async getById(id = this.id) { async getById(id = this.id) {
this.id = Number(id); try {
this.id = Number(id);
const message = await MessageModel.findOne({ id }); const message = await MessageModel.findOne({ id });
if (!message) return null; if (!message) return null;
const { content, authorID, author = null, threadID = null, time = Date.now(), deleted = false, edited = false, react = {} } = message; const { content, authorID, author = null, threadID = null, time = Date.now(), deleted = false, edited = false, react = {} } = message;
this.content = content; this.content = content;
this.threadID = threadID; this.threadID = threadID;
this.author = author; this.author = author;
this.authorID = authorID; this.authorID = authorID;
this.time = time; this.time = time;
this.deleted = deleted; this.deleted = deleted;
this.edited = edited; this.edited = edited;
this.react = react; this.react = react;
return this; return this;
} catch (e) {
return null;
}
} }
async takeId() { async takeId() {

View file

@ -17,28 +17,32 @@ module.exports = class Thread {
} }
async getById(id = this.id) { 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; const thread = await ThreadModel.findOne({ id });
this.title = title if (!thread) return null;
this.author = author;
this.authorID = authorID;
this.messages = messages;
this.time = time;
this.deleted = deleted;
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) { push(messageID) {
this.messages.push(messageID) this.messages.push(messageID)
return this; return this;
} }
async takeId() { async takeId() {
this.id = await ThreadModel.count({}) || 0; this.id = await ThreadModel.count({}) || 0;
return this; return this;

View file

@ -14,34 +14,43 @@ module.exports = class User {
} }
async getById(id = this.id) { async getById(id = this.id) {
this.id = Number(id); try {
const user = await UserModel.findOne({ id }); this.id = Number(id);
if (!user) return null; 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; const { name = "guest", avatar = "/images/guest.png", time = Date.now(), admin = false, deleted = false } = user;
this.name = name; this.name = name;
this.avatar = avatar; this.avatar = avatar;
this.time = time; this.time = time;
this.admin = admin; this.admin = admin;
this.deleted = deleted; this.deleted = deleted;
return this; return this;
} catch (e) {
return null;
}
} }
async getByName(Name = this.name) { 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;
} }

View file

@ -15,7 +15,6 @@ app.use(express.static("public"));
app.set("view engine", "ejs"); app.set("view engine", "ejs");
app.use(express.json()); app.use(express.json());
app.use(require("./middlewares/user")); app.use(require("./middlewares/user"));
app.use(require("./middlewares/timeout"));
for (const file of fs.readdirSync("./routes")) for (const file of fs.readdirSync("./routes"))
app.use("/" + file.replace(".js", ""), require(`./routes/${file}`)); app.use("/" + file.replace(".js", ""), require(`./routes/${file}`));

View file

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

View file

@ -1,8 +0,0 @@
const { Schema, model } = require("mongoose")
module.exports = model('timeout', new Schema({
id: { type: Number, unique: true },
until: Number
}))

View file

@ -1,7 +1,6 @@
const UserModel = require("./User"), const UserModel = require("./User"),
MessageModel = require("./Message"), MessageModel = require("./Message"),
ThreadModel = require("./Thread"), ThreadModel = require("./Thread"),
SecretModel = require("./Secret"), SecretModel = require("./Secret");
TimeoutModel = require("./Timeout");
module.exports = { UserModel, MessageModel, ThreadModel, SecretModel, TimeoutModel }; module.exports = { UserModel, MessageModel, ThreadModel, SecretModel };

View file

@ -17,7 +17,6 @@ app.use(require("../middlewares/login"));
app.post("/", async (req, res) => { 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); const thread = await new Thread().getById(req.body.threadID);
if (thread) { if (thread) {
@ -26,9 +25,6 @@ app.post("/", async (req, res) => {
thread.push(message.id); thread.push(message.id);
thread.write(); thread.write();
// req.timeout.until += 1000 * 30;
// await req.timeout.save()
res.redirect('/threads/' + req.body.threadID); res.redirect('/threads/' + req.body.threadID);
} }

View file

@ -50,7 +50,6 @@ app.use(require("../middlewares/login"));
app.post("/", async (req, res) => { 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; const { title = null, content = null } = req.body;