mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-22 12:00:41 +03:00
Mongoose is fixed & optimized
The dublicate key error fixed, ids as string. classes removed. using mongoose's magic
This commit is contained in:
parent
4d0e2a8594
commit
ae83e014a0
32 changed files with 1346 additions and 507 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,6 +1,9 @@
|
||||||
# Dependency directories
|
# Dependency directories
|
||||||
node_modules/
|
node_modules/
|
||||||
|
|
||||||
|
# env
|
||||||
|
.env
|
||||||
|
|
||||||
# Test files:
|
# Test files:
|
||||||
a.js
|
a.js
|
||||||
db.js
|
db.js
|
|
@ -6,13 +6,13 @@ A forum software written in Node.js.
|
||||||
## Installation
|
## Installation
|
||||||
- Clone or download this repo.
|
- Clone or download this repo.
|
||||||
- Run `npm i` to install **dependencies**.
|
- Run `npm i` to install **dependencies**.
|
||||||
- Run `node reset` to **reset the database**, and `npm start` for run it.
|
- Run `node util/reset` to **reset the database**, and `npm start` for run it.
|
||||||
|
|
||||||
**Note:** Reseting the database is important!
|
**Note:** Reseting the database is important!
|
||||||
|
|
||||||
## API
|
## API
|
||||||
Akf-forum has got an API for other clients etc. You can test api with run apitest.py.
|
Akf-forum has got an API for other clients etc. You can test api with run apitest.py.
|
||||||
And, you can learn about API in `APIDOCS.md`.
|
And, you can learn about API in `util/APIDOCS.md`.
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
* [Akif9748](https://github.com/Akif9748) - Project mainteiner, main developer
|
* [Akif9748](https://github.com/Akif9748) - Project mainteiner, main developer
|
||||||
|
@ -23,6 +23,8 @@ And, you can learn about API in `APIDOCS.md`.
|
||||||
- Redirect query.
|
- Redirect query.
|
||||||
- middleware for timeouts
|
- middleware for timeouts
|
||||||
- DELETED USERS: USERLIST
|
- DELETED USERS: USERLIST
|
||||||
|
- Will fix API
|
||||||
|
- if admin you van see deleted messages.
|
||||||
|
|
||||||
## Roadmap
|
## Roadmap
|
||||||
### User
|
### User
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
const Message = require("./message");
|
|
||||||
const Thread = require("./thread");
|
|
||||||
const User = require("./user");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Classes for akf-forum;
|
|
||||||
*/
|
|
||||||
module.exports = { Message, Thread, User };
|
|
|
@ -1,57 +0,0 @@
|
||||||
const { MessageModel } = require("../models");
|
|
||||||
const User = require("./user");
|
|
||||||
|
|
||||||
module.exports = class Message {
|
|
||||||
|
|
||||||
|
|
||||||
constructor(content, author = User, threadID = null, time = Date.now(), deleted = false, edited = false, react = {}) {
|
|
||||||
|
|
||||||
this.authorID = author?.id;
|
|
||||||
this.content = content;
|
|
||||||
this.author = author;
|
|
||||||
this.time = time;
|
|
||||||
this.threadID = threadID;
|
|
||||||
this.deleted = deleted;
|
|
||||||
this.edited = edited;
|
|
||||||
this.react = react;
|
|
||||||
|
|
||||||
}
|
|
||||||
async getById(id = this.id) {
|
|
||||||
try {
|
|
||||||
this.id = Number(id);
|
|
||||||
|
|
||||||
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;
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async takeId() {
|
|
||||||
this.id = await MessageModel.count({}) || 0;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
async write(id = this.id) {
|
|
||||||
const writing = await MessageModel.findOneAndUpdate({ id }, this);
|
|
||||||
|
|
||||||
if (!writing)
|
|
||||||
await MessageModel.create(this);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
getLink(id = this.id) {
|
|
||||||
return "/messages/" + id;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,63 +0,0 @@
|
||||||
const User = require("./user")
|
|
||||||
|
|
||||||
const { ThreadModel } = require("../models");
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = class Thread {
|
|
||||||
|
|
||||||
constructor(title, author = User, messages = [], time = Date.now(), deleted = false) {
|
|
||||||
|
|
||||||
this.author = author;
|
|
||||||
this.authorID = author?.id;
|
|
||||||
this.title = title;
|
|
||||||
this.messages = messages;
|
|
||||||
this.time = time;
|
|
||||||
this.deleted = deleted;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async getById(id = this.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;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
push(messageID) {
|
|
||||||
this.messages.push(messageID)
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
async takeId() {
|
|
||||||
this.id = await ThreadModel.count({}) || 0;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
async write(id = this.id) {
|
|
||||||
const writing = await ThreadModel.findOneAndUpdate({ id }, this);
|
|
||||||
|
|
||||||
if (!writing)
|
|
||||||
await ThreadModel.create(this);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
getLink(id = this.id) {
|
|
||||||
return "/threads/" + id;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,75 +0,0 @@
|
||||||
const { UserModel } = require("../models");
|
|
||||||
|
|
||||||
module.exports = class User {
|
|
||||||
|
|
||||||
|
|
||||||
constructor(name = "guest", avatar = "/images/guest.png", time = Date.now(), admin = false, deleted = false) {
|
|
||||||
|
|
||||||
this.name = name;
|
|
||||||
this.avatar = avatar;
|
|
||||||
this.time = time;
|
|
||||||
this.admin = admin;
|
|
||||||
this.deleted = deleted;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async getById(id = this.id) {
|
|
||||||
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;
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async getByName(Name = this.name) {
|
|
||||||
try {
|
|
||||||
const user = await UserModel.findOne({ name: Name });
|
|
||||||
|
|
||||||
if (!user) return null;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async takeId() {
|
|
||||||
this.id = await UserModel.count({}) || 0;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
async write(id = this.id) {
|
|
||||||
const writing = await UserModel.findOneAndUpdate({ id }, this);
|
|
||||||
|
|
||||||
if (!writing)
|
|
||||||
await UserModel.create(this);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
getLink(id = this.id) {
|
|
||||||
return "/users/" + id;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
5
index.js
5
index.js
|
@ -1,13 +1,14 @@
|
||||||
const error = require("./errors/error.js"),
|
const error = require("./errors/error.js"),
|
||||||
session = require('express-session'),
|
session = require('express-session'),
|
||||||
bodyParser = require('body-parser'),
|
bodyParser = require('body-parser'),
|
||||||
port = process.env.PORT ?? 3000,
|
port = process.env.PORT || 3000,
|
||||||
mongoose = require("mongoose"),
|
mongoose = require("mongoose"),
|
||||||
express = require('express'),
|
express = require('express'),
|
||||||
fs = require("fs"),
|
fs = require("fs"),
|
||||||
app = express();
|
app = express();
|
||||||
|
|
||||||
mongoose.connect(process.env.MONGO_DB_URL ?? "mongodb://localhost:27017/akf-forum", () => console.log("Database is connected"));
|
require("dotenv").config();
|
||||||
|
mongoose.connect(process.env.MONGO_DB_URL || "mongodb://localhost:27017/akf-forum", () => console.log("Database is connected"));
|
||||||
|
|
||||||
app.use(session({ secret: 'secret', resave: true, saveUninitialized: true }));
|
app.use(session({ secret: 'secret', resave: true, saveUninitialized: true }));
|
||||||
app.use(bodyParser.urlencoded({ extended: true }));
|
app.use(bodyParser.urlencoded({ extended: true }));
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
module.exports = (req, res, next) => {
|
module.exports = (req, res, next) => {
|
||||||
if (!req.session.userid?.toString()) return res.redirect('/login');
|
if (!req.session.userid) return res.redirect('/login');
|
||||||
next();
|
next();
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
const { User } = require("../classes");
|
const { UserModel } = require("../models");
|
||||||
|
|
||||||
module.exports = async (req, res, next) => {
|
module.exports = async (req, res, next) => {
|
||||||
req.user = await new User().getById(req.session.userid);
|
req.user = await UserModel.get(req.session.userid);
|
||||||
next();
|
next();
|
||||||
}
|
}
|
|
@ -1,17 +1,34 @@
|
||||||
const { Schema, model } = require("mongoose")
|
const mongoose = require("mongoose")
|
||||||
|
const UserModel = require("./User");
|
||||||
|
|
||||||
module.exports = model('message', new Schema({
|
const schema = new mongoose.Schema({
|
||||||
id: { type: Number, unique: true },
|
id: { type: String, unique: true },
|
||||||
|
|
||||||
authorID: Number,
|
threadID: String,
|
||||||
threadID: Number,
|
author: UserModel.schema, // user-model
|
||||||
author: Object,
|
|
||||||
|
|
||||||
content: String,
|
content: String,
|
||||||
time: Number,
|
time: { type: Date, default: Date.now },
|
||||||
deleted: { type: Boolean, default: false },
|
deleted: { type: Boolean, default: false },
|
||||||
edited: { type: Boolean, default: false },
|
edited: { type: Boolean, default: false },
|
||||||
messages: [Number],
|
react: { type:Object, default: {} }
|
||||||
react: Object
|
|
||||||
|
})
|
||||||
|
|
||||||
|
schema.virtual('authorID').get(function() { return this.author?.id; });
|
||||||
|
|
||||||
|
schema.methods.takeId = async function () {
|
||||||
|
this.id = String(await model.count() || 0);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
schema.methods.getLink = function (id = this.id) {
|
||||||
|
return "/messages/" + id;
|
||||||
|
}
|
||||||
|
|
||||||
|
const model = mongoose.model('message', schema);
|
||||||
|
|
||||||
|
model.get = id => model.findOne({ id });
|
||||||
|
|
||||||
|
module.exports = model;
|
||||||
|
|
||||||
}))
|
|
|
@ -1,10 +1,12 @@
|
||||||
const { Schema, model } = require("mongoose")
|
const { Schema, model } = require("mongoose")
|
||||||
|
|
||||||
module.exports = model('secret', new Schema({
|
const schema = new Schema({
|
||||||
|
|
||||||
username: { type: String, unique: true },
|
username: { type: String, unique: true },
|
||||||
password: String,
|
password: String,
|
||||||
id: { type: Number, unique: true }
|
id: { type:String, unique: true }
|
||||||
|
|
||||||
|
|
||||||
}))
|
});
|
||||||
|
|
||||||
|
module.exports = model('secret', schema);
|
|
@ -1,15 +1,36 @@
|
||||||
const { Schema, model } = require("mongoose")
|
const mongoose = require("mongoose")
|
||||||
|
const UserModel = require("./User");
|
||||||
|
const schema = new mongoose.Schema({
|
||||||
|
id: { type: String, unique: true },
|
||||||
|
|
||||||
|
author: UserModel.schema,
|
||||||
module.exports = model('thread', new Schema({
|
|
||||||
id: { type: Number, unique: true },
|
|
||||||
|
|
||||||
authorID: Number,
|
|
||||||
author: Object,
|
|
||||||
|
|
||||||
title: String,
|
title: String,
|
||||||
time: Number,
|
time: { type: Date, default: Date.now },
|
||||||
deleted: { type: Boolean, default: false },
|
deleted: { type: Boolean, default: false },
|
||||||
messages: [Number]
|
messages: [String]
|
||||||
|
|
||||||
}))
|
});
|
||||||
|
|
||||||
|
schema.virtual('authorID').get(function() { return this.author?.id; });
|
||||||
|
|
||||||
|
schema.methods.push = function (messageID) {
|
||||||
|
this.messages.push(messageID);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
schema.methods.takeId = async function () {
|
||||||
|
this.id = await model.count() || 0;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
schema.methods.getLink = function (id = this.id) {
|
||||||
|
return "/threads/" + id;
|
||||||
|
}
|
||||||
|
|
||||||
|
const model = mongoose.model('thread', schema);
|
||||||
|
|
||||||
|
model.get = id => model.findOne({ id });
|
||||||
|
|
||||||
|
module.exports = model;
|
|
@ -1,12 +1,28 @@
|
||||||
const { Schema, model } = require("mongoose")
|
const mongoose = require("mongoose")
|
||||||
|
|
||||||
module.exports = model('user', new Schema({
|
const schema = new mongoose.Schema({
|
||||||
id: { type: Number, unique: true },
|
id: { type: String, unique: true },
|
||||||
|
|
||||||
name: String,
|
name: String,
|
||||||
avatar: String,
|
avatar: { type: String, default: "/images/guest.png" },
|
||||||
time: Number,
|
time: { type: Date, default: Date.now },
|
||||||
deleted: { type: Boolean, default: false },
|
deleted: { type: Boolean, default: false },
|
||||||
admin: { type: Boolean, default: false }
|
admin: { type: Boolean, default: false }
|
||||||
|
|
||||||
}))
|
});
|
||||||
|
|
||||||
|
|
||||||
|
schema.methods.takeId = async function () {
|
||||||
|
this.id = String(await model.count() || 0);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
schema.methods.getLink = function (id = this.id) {
|
||||||
|
return "/users/" + id;
|
||||||
|
}
|
||||||
|
|
||||||
|
const model = mongoose.model('user', schema);
|
||||||
|
|
||||||
|
model.get = id => model.findOne({ id });
|
||||||
|
|
||||||
|
module.exports = model;
|
1366
package-lock.json
generated
1366
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -21,10 +21,12 @@
|
||||||
},
|
},
|
||||||
"homepage": "https://akf-forum.herokuapp.com/",
|
"homepage": "https://akf-forum.herokuapp.com/",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"bcrypt": "^5.0.1",
|
||||||
"body-parser": "^1.19.2",
|
"body-parser": "^1.19.2",
|
||||||
|
"dotenv": "^16.0.1",
|
||||||
"ejs": "^3.1.6",
|
"ejs": "^3.1.6",
|
||||||
"express": "^4.17.3",
|
"express": "^4.17.3",
|
||||||
"express-session": "^1.17.2",
|
"express-session": "^1.17.2",
|
||||||
"mongoose": "^6.2.9"
|
"mongoose": "^6.5.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const { User } = require("../classes");
|
const { UserModel } = require("../models")
|
||||||
|
|
||||||
const { Router } = require("express")
|
const { Router } = require("express")
|
||||||
const error = require("../errors/error")
|
const error = require("../errors/error")
|
||||||
|
@ -20,14 +20,14 @@ app.post("/", async (req, res) => {
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
|
|
||||||
if (!user.admin) return error(res, 403, "You have not got permissions for view to this page.");
|
if (!user.admin) return error(res, 403, "You have not got permissions for view to this page.");
|
||||||
const user2 = await new User().getById(req.body.userid)
|
const user2 = await UserModel.get(req.body.userid);
|
||||||
|
|
||||||
if (!user2)
|
if (!user2)
|
||||||
return error(res, 404, "We have not got this user in all of the forum. Vesselam.");
|
return error(res, 404, "We have not got this user in all of the forum. Vesselam.");
|
||||||
|
|
||||||
else {
|
else {
|
||||||
user2.admin = true;
|
user2.admin = true;
|
||||||
user2.write()
|
await user2.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
res.render("admin", { user, user2 })
|
res.render("admin", { user, user2 })
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
const { Router } = require("express")
|
const { Router } = require("express")
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
* for less time
|
||||||
|
*/
|
||||||
const { request, response } = require("express");
|
const { request, response } = require("express");
|
||||||
const { SecretModel } = require("../../models")
|
const { SecretModel } = require("../../models")
|
||||||
const ApiResponse = require("./ApiResponse")
|
const ApiResponse = require("./ApiResponse")
|
||||||
|
@ -31,7 +34,7 @@ const ApiResponse = require("./ApiResponse")
|
||||||
* @param {request} req
|
* @param {request} req
|
||||||
* @param {response} res
|
* @param {response} res
|
||||||
*/
|
*/
|
||||||
|
/*
|
||||||
app.use(async (req, res, next) => {
|
app.use(async (req, res, next) => {
|
||||||
const error = (status, error) =>
|
const error = (status, error) =>
|
||||||
res.status(status).json(new ApiResponse(status, { error }))
|
res.status(status).json(new ApiResponse(status, { error }))
|
||||||
|
@ -54,7 +57,7 @@ app.use(async (req, res, next) => {
|
||||||
app.use("/messages", require("./routes/message"))
|
app.use("/messages", require("./routes/message"))
|
||||||
app.use("/users", require("./routes/user"))
|
app.use("/users", require("./routes/user"))
|
||||||
app.use("/threads", require("./routes/threads"))
|
app.use("/threads", require("./routes/threads"))
|
||||||
|
*/
|
||||||
app.all("*", (req, res) => res.status(400).json(new ApiResponse(400, { error: "Bad request" })));
|
app.all("*", (req, res) => res.status(400).json(new ApiResponse(400, { error: "Bad request" })));
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
|
@ -31,8 +31,8 @@ app.post("/", async (req, res) => {
|
||||||
|
|
||||||
|
|
||||||
const message = await new Message(content, await new User().getByName(req.headers.username), thread.id).takeId()
|
const message = await new Message(content, await new User().getByName(req.headers.username), thread.id).takeId()
|
||||||
message.write();
|
message.save();
|
||||||
thread.push(message.id).write();
|
thread.push(message.id).save();
|
||||||
|
|
||||||
res.status(200).json(new ApiResponse(200, message));
|
res.status(200).json(new ApiResponse(200, message));
|
||||||
|
|
||||||
|
|
|
@ -33,8 +33,8 @@ app.post("/", async (req, res) => {
|
||||||
const user = await new User().getByName(req.headers.username)
|
const user = await new User().getByName(req.headers.username)
|
||||||
const thread = await new Thread(title, user).takeId()
|
const thread = await new Thread(title, user).takeId()
|
||||||
const message = await new Message(content, user, thread.id).takeId()
|
const message = await new Message(content, user, thread.id).takeId()
|
||||||
thread.push(message.id).write();
|
thread.push(message.id).save();
|
||||||
await message.write();
|
await message.save();
|
||||||
|
|
||||||
res.status(200).json(new ApiResponse(200, thread));
|
res.status(200).json(new ApiResponse(200, thread));
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
const { User } = require("../classes");
|
const { UserModel, SecretModel } = require("../models");
|
||||||
const { Router } = require("express");
|
const { Router } = require("express");
|
||||||
const { SecretModel } = require("../models");
|
|
||||||
const error = require("../errors/error");
|
const error = require("../errors/error");
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
|
||||||
|
@ -15,7 +14,7 @@ app.post("/", async (req, res) => {
|
||||||
const user = await SecretModel.findOne({ username });
|
const user = await SecretModel.findOne({ username });
|
||||||
if (user) {
|
if (user) {
|
||||||
if (user.password !== password) return error(res, 403, 'Incorrect Password!')
|
if (user.password !== password) return error(res, 403, 'Incorrect Password!')
|
||||||
const member = await new User().getByName(username)
|
const member = await UserModel.findOne({ name: username });
|
||||||
if (!member || member.deleted) return error(res, 403, 'Incorrect Username and/or Password!')
|
if (!member || member.deleted) return error(res, 403, 'Incorrect Username and/or Password!')
|
||||||
|
|
||||||
req.session.userid = user.id;
|
req.session.userid = user.id;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const { Thread, Message, User } = require("../classes");
|
const { ThreadModel, MessageModel } = require("../models");
|
||||||
const error = require("../errors/error")
|
const error = require("../errors/error")
|
||||||
|
|
||||||
const { Router } = require("express");
|
const { Router } = require("express");
|
||||||
|
@ -6,7 +6,7 @@ const { Router } = require("express");
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
|
||||||
app.get("/:id", async (req, res) => {
|
app.get("/:id", async (req, res) => {
|
||||||
const message = await new Message().getById(req.params.id);
|
const message = await MessageModel.get(req.params.id);
|
||||||
|
|
||||||
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
|
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
|
||||||
res.redirect("/threads/" + message.threadID);
|
res.redirect("/threads/" + message.threadID);
|
||||||
|
@ -18,12 +18,11 @@ app.use(require("../middlewares/login"));
|
||||||
|
|
||||||
app.post("/", async (req, res) => {
|
app.post("/", async (req, res) => {
|
||||||
|
|
||||||
const thread = await new Thread().getById(req.body.threadID);
|
const thread = await ThreadModel.get(req.body.threadID);
|
||||||
if (thread) {
|
if (thread) {
|
||||||
const message = await new Message(req.body.content, req.user, thread.id).takeId()
|
const message = await new MessageModel({ content: req.body.content, author: req.user, threadID: thread.id }).takeId();
|
||||||
await message.write();
|
await message.save();
|
||||||
thread.push(message.id);
|
await thread.push(message.id).save();
|
||||||
thread.write();
|
|
||||||
|
|
||||||
res.redirect('/threads/' + req.body.threadID);
|
res.redirect('/threads/' + req.body.threadID);
|
||||||
|
|
||||||
|
@ -34,29 +33,29 @@ app.post("/", async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/:id/delete", async (req, res) => {
|
app.post("/:id/delete", async (req, res) => {
|
||||||
const message = await new Message().getById(req.params.id)
|
const message = await MessageModel.get(req.params.id);
|
||||||
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
|
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
if (user.id != message.authorID && !user.admin)
|
if (user.id != message.authorID && !user.admin)
|
||||||
return error(res, 403, "You have not got permission for this.");
|
return error(res, 403, "You have not got permission for this.");
|
||||||
message.deleted = true;
|
message.deleted = true;
|
||||||
message.write();
|
await message.save();
|
||||||
|
|
||||||
|
|
||||||
res.status(200).redirect("/threads/" + message.threadID);
|
res.status(200).redirect( "/threads/" + message.threadID);
|
||||||
|
|
||||||
})
|
})
|
||||||
app.post("/:id/react", async (req, res) => {
|
app.post("/:id/react", async (req, res) => {
|
||||||
const { id = null } = req.params;
|
|
||||||
const info = req.body;
|
const info = req.body;
|
||||||
const message = await new Message().getById(id);
|
const message = await MessageModel.get(req.params.id);
|
||||||
if (message) {
|
if (message) {
|
||||||
if (!(req.session.userid in message.react))
|
if (req.user.id in message.react)
|
||||||
message.react[req.session.userid] = "like" in info;
|
|
||||||
else
|
|
||||||
delete message.react[req.session.userid];
|
delete message.react[req.session.userid];
|
||||||
|
else
|
||||||
|
message.react[req.session.userid] = "like" in info;
|
||||||
|
|
||||||
message.write();
|
|
||||||
|
await message.save();
|
||||||
res.redirect("/threads/" + message.threadID);
|
res.redirect("/threads/" + message.threadID);
|
||||||
} else error(res, 404, "We have not got this Message for reacting.");
|
} else error(res, 404, "We have not got this Message for reacting.");
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
const { User } = require("../classes");
|
const { UserModel, SecretModel } = require("../models");
|
||||||
const { SecretModel } = require("../models");
|
|
||||||
|
|
||||||
const { Router } = require("express")
|
const { Router } = require("express")
|
||||||
const error = require("../errors/error")
|
const error = require("../errors/error")
|
||||||
|
|
||||||
|
@ -22,15 +20,13 @@ app.post("/", async (req, res) => {
|
||||||
|
|
||||||
else {
|
else {
|
||||||
|
|
||||||
if (!avatar) avatar ="/images/guest.png";
|
|
||||||
|
|
||||||
const user2 = await new User(req.body.username, avatar).takeId();
|
|
||||||
|
|
||||||
|
const user2 = new UserModel({ name: req.body.username, avatar })
|
||||||
|
await user2.takeId()
|
||||||
|
await user2.save();
|
||||||
await SecretModel.create({ username, password, id: user2.id })
|
await SecretModel.create({ username, password, id: user2.id })
|
||||||
req.session.userid = user2.id;
|
req.session.userid = user2.id;
|
||||||
|
|
||||||
user2.write();
|
|
||||||
|
|
||||||
res.redirect('/');
|
res.redirect('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,26 +1,24 @@
|
||||||
const { Router } = require("express");
|
const { Router } = require("express");
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
|
||||||
const { Thread, Message, User } = require("../classes");
|
|
||||||
const error = require("../errors/error")
|
const error = require("../errors/error")
|
||||||
const { ThreadModel } = require("../models")
|
const { ThreadModel, MessageModel } = require("../models")
|
||||||
|
|
||||||
|
|
||||||
app.get("/", async (req, res) => {
|
app.get("/", async (req, res) => {
|
||||||
|
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
|
|
||||||
const threads = await Promise.all((await ThreadModel.find({}).limit(10))
|
const threads = await ThreadModel.find({}).limit(10);
|
||||||
.map(async threads => await new Thread().getById(threads.id)));
|
|
||||||
|
|
||||||
return res.render("threads", { threads, user });
|
return res.render("threads", { threads, user });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.get("/open*", async (req, res) => {
|
app.get("/create*", async (req, res) => {
|
||||||
|
|
||||||
const user = req.user
|
const user = req.user
|
||||||
res.render("openThread", { user })
|
res.render("createThread", { user })
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -28,13 +26,13 @@ app.get("/:id", async (req, res) => {
|
||||||
|
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
const thread = await new Thread().getById(id);
|
const thread = await ThreadModel.get(id);
|
||||||
|
|
||||||
if (thread) {
|
if (thread) {
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
|
|
||||||
const messages = await Promise.all(thread.messages.map(async id => {
|
const messages = await Promise.all(thread.messages.map(async id => {
|
||||||
const message = await new Message().getById(id)
|
const message = await MessageModel.get(id)
|
||||||
return (message.deleted || !message) ? null : message;
|
return (message.deleted || !message) ? null : message;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -55,12 +53,13 @@ app.post("/", async (req, res) => {
|
||||||
|
|
||||||
if (!title || !content) return error(res, 400, "Title and/or content is missing");
|
if (!title || !content) return error(res, 400, "Title and/or content is missing");
|
||||||
const user = req.user
|
const user = req.user
|
||||||
const thread = await new Thread(title, user).takeId()
|
const thread = await new ThreadModel({ title, author: user }).takeId()
|
||||||
const message = await new Message(content, user, thread.id).takeId()
|
|
||||||
|
|
||||||
thread.push(message.id).write();
|
const message = await new MessageModel({ content, author: user, threadID: thread.id }).takeId()
|
||||||
|
|
||||||
message.write();
|
await thread.push(message.id).save();
|
||||||
|
|
||||||
|
await message.save();
|
||||||
|
|
||||||
res.redirect('/threads/' + thread.id);
|
res.redirect('/threads/' + thread.id);
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,31 +2,26 @@ const { Router } = require("express");
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
|
||||||
const error = require("../errors/error");
|
const error = require("../errors/error");
|
||||||
|
|
||||||
const { User } = require("../classes");
|
|
||||||
const { UserModel, MessageModel, ThreadModel } = require("../models");
|
const { UserModel, MessageModel, ThreadModel } = require("../models");
|
||||||
|
|
||||||
app.get("/", async (req, res) => {
|
app.get("/", async (req, res) => {
|
||||||
const user = req.user
|
const user = req.user
|
||||||
const users = await UserModel.find({});
|
const users = await UserModel.find({ deleted: false });
|
||||||
|
return res.render("users", { users, user })
|
||||||
const links = users.filter(user => !user.deleted).map(user => "/users/" + user.id);
|
|
||||||
return res.render("users", { users, links, user })
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/:id", async (req, res) => {
|
app.get("/:id", async (req, res) => {
|
||||||
const user = req.user
|
const user = req.user
|
||||||
const { id = null } = req.params;
|
const { id = null } = req.params;
|
||||||
const member = await new User().getById(req.params.id);
|
const member = await UserModel.get(id);
|
||||||
|
|
||||||
|
|
||||||
if (member && (user?.admin || !member.deleted)) {
|
if (member && (user?.admin || !member.deleted)) {
|
||||||
|
|
||||||
const message = await MessageModel.count({ authorID: id });
|
const message = await MessageModel.count({ authorID: id });
|
||||||
const thread = await ThreadModel.count({ authorID: id });
|
const thread = await ThreadModel.count({ authorID: id });
|
||||||
const counts = { message, thread }
|
res.render("user", { user, member, counts:{ message, thread } })
|
||||||
res.render("user", { user, member, counts })
|
|
||||||
}
|
}
|
||||||
else error(res, 404, "We have not got this user.");
|
else error(res, 404, "We have not got this user.");
|
||||||
|
|
||||||
|
@ -41,12 +36,12 @@ app.post("/:id/delete/", async (req, res) => {
|
||||||
return error(res, 403, "You have not got permission for this.");
|
return error(res, 403, "You have not got permission for this.");
|
||||||
|
|
||||||
const { id = null } = req.params;
|
const { id = null } = req.params;
|
||||||
const member = await new User().getById(id);
|
const member = await UserModel.get(id);
|
||||||
|
|
||||||
if (!member || member.deleted) return error(res, 404, "We have not got any user declared as this id.");
|
if (!member || member.deleted) return error(res, 404, "We have not got any user declared as this id.");
|
||||||
|
|
||||||
member.deleted = true;
|
member.deleted = true;
|
||||||
member.write();
|
await member.save();
|
||||||
|
|
||||||
res.redirect("/admin");
|
res.redirect("/admin");
|
||||||
});
|
});
|
||||||
|
|
15
util/admin.js
Normal file
15
util/admin.js
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
require("dotenv").config();
|
||||||
|
|
||||||
|
mongoose.connect(process.env.MONGO_DB_URL, () => console.log("Database is connected"));
|
||||||
|
|
||||||
|
const { UserModel } = require("../models");
|
||||||
|
(async () => {
|
||||||
|
|
||||||
|
const member= await UserModel.get(0);
|
||||||
|
console.log(member);
|
||||||
|
member.admin = true;
|
||||||
|
member.save();
|
||||||
|
})();
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
mongoose.connect('mongodb://localhost:27017/akf-forum', () => console.log("Database is connected"));
|
require("dotenv").config();
|
||||||
|
|
||||||
const { SecretModel, UserModel, MessageModel, ThreadModel } = require("./models");
|
mongoose.connect(process.env.MONGO_DB_URL, () => console.log("Database is connected"));
|
||||||
|
|
||||||
|
const { SecretModel, UserModel, MessageModel, ThreadModel } = require("../models");
|
||||||
(async () => {
|
(async () => {
|
||||||
|
|
||||||
await UserModel.deleteMany({});
|
await UserModel.deleteMany({});
|
|
@ -1,7 +1,7 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<%- include("extra/header", {title: "Open Thread!" }) %>
|
<%- include("extra/header", {title: "Create Thread!" }) %>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
<link rel="stylesheet" href="/css/styles.css" />
|
<link rel="stylesheet" href="/css/styles.css" />
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title><%= title ?? "Akf-forum" %> </title>
|
<title><%= title || "Akf-forum" %> </title>
|
||||||
<meta name="description" content="Akf-forum!">
|
<meta name="description" content="Akf-forum!">
|
||||||
<meta name="author" content="Akif9748">
|
<meta name="author" content="Akif9748">
|
||||||
<link rel="icon" type="image/x-icon" href="/images/favicon.jpg">
|
<link rel="icon" type="image/x-icon" href="/images/favicon.jpg">
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
<a href="/threads">THREADS</a>
|
<a href="/threads">THREADS</a>
|
||||||
<a href="/users">USERS</a>
|
<a href="/users">USERS</a>
|
||||||
<a href="/search">SEARCH</a>
|
<a href="/search">SEARCH</a>
|
||||||
<a href="/threads/open/">CREATE THREAD</a>
|
<a href="/threads/create/">CREATE THREAD</a>
|
||||||
|
|
||||||
<% if (user){ %>
|
<% if (user){ %>
|
||||||
<div style="float: right;" class="user" id="user">
|
<div style="float: right;" class="user" id="user">
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
<ul>
|
<ul>
|
||||||
<% users.forEach(user=>{ %>
|
<% users.forEach(user=>{ %>
|
||||||
<li>
|
<li>
|
||||||
<h1><a href=<%=links[user.id] %> > <%= user.name %></a>
|
<h1><a href=<%= user.getLink() %> > <%= user.name %></a>
|
||||||
<img class="yuvarlak" src=<%=user.avatar %> alt=<%= user.name %>>
|
<img class="yuvarlak" src=<%=user.avatar %> alt=<%= user.name %>>
|
||||||
</h1>
|
</h1>
|
||||||
</li>
|
</li>
|
||||||
|
|
Loading…
Reference in a new issue