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) {
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() {

View File

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

View File

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

View File

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

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"),
MessageModel = require("./Message"),
ThreadModel = require("./Thread"),
SecretModel = require("./Secret"),
TimeoutModel = require("./Timeout");
SecretModel = require("./Secret");
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) => {
// 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);
}

View File

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