mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-26 05:10:41 +03:00
Routes are fixed
This commit is contained in:
parent
c9766ad967
commit
4f4bd93baf
36 changed files with 414 additions and 352 deletions
|
@ -17,9 +17,7 @@ And, you can learn informations about API in `APIDOCS.md`.
|
|||
* [Camroku](https://github.com/Camroku) - Made stylesheets
|
||||
|
||||
## To Do (Backend, bug fixes) :
|
||||
- Better method for params in URL. Router, or etc.
|
||||
- Middleware. For logged in etc.
|
||||
- Page template. (Not onlt module.exports) config, file, etc.
|
||||
- Better error codes, example 400 for bad request
|
||||
- Ratelimit
|
||||
- Database change. (To MongoDB)
|
||||
|
|
81
api/index.js
81
api/index.js
|
@ -1,81 +0,0 @@
|
|||
const { User, Message, Thread } = require("../classes");
|
||||
const db = require("quick.db");
|
||||
|
||||
|
||||
class ApiResponse {
|
||||
constructor(status, result) {
|
||||
this.status = status;
|
||||
this.result = result;
|
||||
}
|
||||
}
|
||||
|
||||
const { request, response } = require("express");
|
||||
|
||||
/**
|
||||
* For intellisense
|
||||
* @param {request} req
|
||||
* @param {response} res
|
||||
*/
|
||||
|
||||
module.exports = (req, res) => {
|
||||
|
||||
const error = (status, error) =>
|
||||
res.status(status).json(new ApiResponse(403, { error }))
|
||||
|
||||
|
||||
/**
|
||||
* AUTH TYPE:
|
||||
|
||||
headers:
|
||||
{
|
||||
username: "Username for client",
|
||||
password: "Password of selected username for client"
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
const { username = null, password = null } = req.headers;
|
||||
|
||||
if (!username || !password)
|
||||
return error(403, "Headers are missing")
|
||||
|
||||
const user = db.get("secret." + username);
|
||||
|
||||
if (!user)
|
||||
return error(403, "We have not got any user has got this name")
|
||||
|
||||
if (user.key !== password)
|
||||
return error(403, 'Incorrect Password!')
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* REQUEST TYPE:
|
||||
* GET /api/action/id
|
||||
*
|
||||
* @example message action:
|
||||
* GET /api/message/0
|
||||
*
|
||||
*/
|
||||
const { action } = req.params;
|
||||
|
||||
switch (action) {
|
||||
case "message":
|
||||
const { id = null } = req.params;
|
||||
if (!id) return error(403, "Missing id in query")
|
||||
const message = new Message().getId(id);
|
||||
|
||||
if (!message || message.deleted) return error(403, "We have not got any message declared as this id.");
|
||||
|
||||
res.status(200).json(new ApiResponse(200, message));
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
return error(403, "Missing/undefined param: action");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -39,12 +39,12 @@ example_response = {
|
|||
"messages": [0], # ids of messages
|
||||
"time": 1647178870047,
|
||||
"deleted": False,
|
||||
"id": "0"
|
||||
"id": 0
|
||||
},
|
||||
# Other informations about message
|
||||
"deleted": False,
|
||||
"edited": False,
|
||||
"react": {},
|
||||
"id": "1"
|
||||
"id": 1
|
||||
}
|
||||
}
|
||||
|
|
18
index.js
18
index.js
|
@ -8,7 +8,7 @@ const error = require("./errors/error.js")
|
|||
const app = express();
|
||||
|
||||
app.use(session({ secret: 'secret', resave: true, saveUninitialized: true }));
|
||||
app.use(express.static(path.join(__dirname, "public")));
|
||||
app.use(express.static("public"));
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
|
||||
|
@ -18,18 +18,16 @@ app.set("view engine", "ejs");
|
|||
//Temp:
|
||||
app.get("/", (req, res) => res.redirect("/index"));
|
||||
|
||||
/**
|
||||
* API:
|
||||
*/
|
||||
app.get("/api/:action/:id", require("./api"));
|
||||
|
||||
for (const type of fs.readdirSync("./routes"))
|
||||
for (const file of fs.readdirSync("./routes/" + type))
|
||||
app[type](`/${file.replace(".js", "")}*`, require(`./routes/${type}/${file}`))
|
||||
|
||||
app.get('*', (req, res) => error(res, 404, "We have not got this page."));
|
||||
app.post('*', (req, res) => error(res, 404, "We have not got this page."));
|
||||
|
||||
|
||||
|
||||
for (const file of fs.readdirSync("./routes/"))
|
||||
app.use(`/${file.replace(".js", "")}`, require(`./routes/${file}`))
|
||||
|
||||
|
||||
app.all("*", (req, res) => error(res, 404, "We have not got this page."))
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
app.listen(port, () => console.log("SERVER ON PORT:", port));
|
||||
|
|
10
middlewares/login.js
Normal file
10
middlewares/login.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
const { Router } = require("express");
|
||||
|
||||
const app = Router();
|
||||
|
||||
app.get("/", (req, res, next) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
next();
|
||||
});
|
||||
|
||||
module.exports = app;
|
1
reset.js
1
reset.js
|
@ -3,4 +3,5 @@ set("users", new Array());
|
|||
set("threads", new Array());
|
||||
set("secret", new Object());
|
||||
set("messages", new Array());
|
||||
set("timeouts", new Array());
|
||||
|
||||
|
|
39
routes/admin.js
Normal file
39
routes/admin.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
const { User } = require("../classes");
|
||||
|
||||
const { Router } = require("express")
|
||||
const error = require("../errors/error")
|
||||
|
||||
const app = Router();
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const user = new User().getId(req.session.userid)
|
||||
|
||||
if (!user.admin) return error(res, 404, "You have not got permissions for view to this page.");
|
||||
|
||||
res.render("admin", { user, user2: false })
|
||||
}
|
||||
);
|
||||
|
||||
app.post("/", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
const user = new User().getId(req.session.userid)
|
||||
|
||||
if (!user.admin) return error(res, 404, "You have not got permissions for view to this page.");
|
||||
const user2 = new User().getId(req.body.userid)
|
||||
|
||||
if (!user2)
|
||||
return error(res, 404, "We have not got this user in all of the forum. Vesselam.");
|
||||
|
||||
else {
|
||||
user2.admin = true;
|
||||
user2.write()
|
||||
}
|
||||
|
||||
res.render("admin", { user, user2 })
|
||||
|
||||
|
||||
});
|
||||
|
||||
module.exports = app;
|
91
routes/api/index.js
Normal file
91
routes/api/index.js
Normal file
|
@ -0,0 +1,91 @@
|
|||
const { User, Message, Thread } = require("../../classes");
|
||||
const db = require("quick.db");
|
||||
|
||||
|
||||
const { Router } = require("express")
|
||||
|
||||
const app = Router();
|
||||
|
||||
|
||||
class ApiResponse {
|
||||
constructor(status, result) {
|
||||
this.status = status;
|
||||
this.result = result;
|
||||
}
|
||||
}
|
||||
|
||||
const { request, response } = require("express");
|
||||
|
||||
/**
|
||||
* For intellisense
|
||||
* @param {request} req
|
||||
* @param {response} res
|
||||
*/
|
||||
|
||||
|
||||
app.get("/:action/:id", (req, res) => {
|
||||
|
||||
const error = (status, error) =>
|
||||
res.status(status).json(new ApiResponse(status, { error }))
|
||||
|
||||
|
||||
/**
|
||||
* AUTH TYPE:
|
||||
|
||||
headers:
|
||||
{
|
||||
username: "Username for client",
|
||||
password: "Password of selected username for client"
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
const { username = null, password = null } = req.headers;
|
||||
|
||||
if (!username || !password)
|
||||
return error(401, "Headers are missing")
|
||||
|
||||
const user = db.get("secret." + username);
|
||||
|
||||
if (!user)
|
||||
return error(401, "We have not got any user has got this name")
|
||||
|
||||
if (user.key !== password)
|
||||
return error(401, 'Incorrect Password!')
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* REQUEST TYPE:
|
||||
* GET /api/action/id
|
||||
*
|
||||
* @example message action:
|
||||
* GET /api/message/0
|
||||
*
|
||||
*/
|
||||
const { action } = req.params;
|
||||
|
||||
if (action === "message") {
|
||||
|
||||
const { id = null } = req.params;
|
||||
if (!id) return error(400, "Missing id in query")
|
||||
const message = new Message().getId(id);
|
||||
|
||||
if (!message || message.deleted) return error(404, "We have not got any message declared as this id.");
|
||||
|
||||
res.status(200).json(new ApiResponse(200, message));
|
||||
} else if (action === "user") {
|
||||
const { id = null } = req.params;
|
||||
if (!id) return error(400, "Missing id in query")
|
||||
const member = new User().getId(id);
|
||||
if (!member || member.deleted) return error(404, "We have not got any user declared as this id.");
|
||||
|
||||
res.status(200).json(new ApiResponse(200, member));
|
||||
}
|
||||
else
|
||||
return error(400, "Missing/undefined param in action section: " + action);
|
||||
});
|
||||
|
||||
|
||||
|
||||
module.exports = app;
|
|
@ -1,11 +0,0 @@
|
|||
const { User } = require("../../classes/index");
|
||||
const error = require("../../errors/error.js");
|
||||
|
||||
module.exports = (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const user = new User().getId(req.session.userid)
|
||||
|
||||
if (!user.admin) return error(res, 404, "You have not got permissions for view to this page.");
|
||||
|
||||
res.render("admin", { user, user2: false })
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
const { User } = require("../../classes/index");
|
||||
|
||||
|
||||
module.exports = (req, res) => {
|
||||
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const user = new User().getId(req.session.userid)
|
||||
res.render("openThread", { user })
|
||||
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
module.exports = (req,res)=> res.render("login");
|
|
@ -1,13 +0,0 @@
|
|||
const { Message } = require("../../classes/index");
|
||||
const error = require("../../errors/error.js");
|
||||
|
||||
module.exports = (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const id = req.url.slice(9);
|
||||
const message = new Message().getId(id)
|
||||
|
||||
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
|
||||
|
||||
res.redirect("/threads/" + message.thread.id);
|
||||
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
module.exports = (req,res)=> res.render("register");
|
|
@ -1,25 +0,0 @@
|
|||
const { Thread, Message, User } = require("../../classes/index");
|
||||
const db = require("quick.db");
|
||||
|
||||
const error = require("../../errors/error.js");
|
||||
|
||||
module.exports = (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
const id = req.url.slice(9);
|
||||
const user = new User().getId(req.session.userid);
|
||||
|
||||
if (!id) {
|
||||
const threads = db.get("threads").slice(0, 10)
|
||||
const links = threads.map(thread => "/threads/" + threads.indexOf(thread))
|
||||
|
||||
return res.render("threads", { threads, links, user})
|
||||
}
|
||||
const thread = new Thread().getId(id);
|
||||
|
||||
if (thread) {
|
||||
const messages = thread.messages.filter(id => !new Message().getId(id).deleted).map(id => new Message().getId(id));
|
||||
res.render("thread", { thread, messages, user })
|
||||
} else
|
||||
error(res, 404, "We have not got this thread.");
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
const { User } = require("../../classes/index");
|
||||
module.exports = (req, res) => {
|
||||
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
const user = new User().getId(req.session.userid);
|
||||
res.render("userEdit", { user })
|
||||
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
const { User } = require("../../classes/index");
|
||||
const db = require("quick.db");
|
||||
const error = require("../../errors/error.js")
|
||||
|
||||
module.exports = (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const user = new User().getId(req.session.userid)
|
||||
|
||||
const id = req.url.slice(7);
|
||||
|
||||
if (!id) {
|
||||
|
||||
const users = db.get("users").slice(0);
|
||||
|
||||
const links = users.filter(user=> !user.deleted).map(user => "/users/" + user.id)
|
||||
return res.render("users", { users, links, user })
|
||||
|
||||
}
|
||||
|
||||
const member = new User().getId(id);
|
||||
|
||||
|
||||
if (member && (user.admin || !member.deleted)) {
|
||||
const message = db.get("messages").filter(message => message.author.id === Number(id)).length
|
||||
const thread = db.get("threads").filter(thread => thread.author.id === Number(id)).length
|
||||
|
||||
const counts = { message, thread }
|
||||
res.render("user", { user, member, counts })
|
||||
}
|
||||
else
|
||||
error(res, 404, "We have not got this user.");
|
||||
|
||||
}
|
|
@ -1,12 +1,26 @@
|
|||
const { User } = require("../../classes/index");
|
||||
const { User } = require("../classes");
|
||||
const { get } = require("quick.db")
|
||||
module.exports = (req, res) => {
|
||||
|
||||
const { Router } = require("express");
|
||||
const app = Router();
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
const mem = process.memoryUsage().heapUsed / Math.pow(2, 20);
|
||||
|
||||
const users = get("users").length;
|
||||
|
||||
const threads = get("threads").length;
|
||||
|
||||
const messages = get("messages").length;
|
||||
|
||||
const user = new User().getId(req.session.userid)
|
||||
|
||||
res.render("index", { mem, user, users, threads, messages })
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
module.exports = app;
|
|
@ -1,13 +1,19 @@
|
|||
const db = require("quick.db");
|
||||
const error = require("../../errors/error.js")
|
||||
const { User } = require("../../classes/index");
|
||||
const { User } = require("../classes");
|
||||
const db = require("quick.db")
|
||||
|
||||
module.exports = (req, res) => {
|
||||
const { Router } = require("express")
|
||||
const error = require("../errors/error")
|
||||
|
||||
const app = Router();
|
||||
|
||||
app.get("/", (req, res) => res.render("login"));
|
||||
|
||||
app.post("/", (req, res) => {
|
||||
req.session.loggedin = false;
|
||||
req.session.username = null;
|
||||
req.session.userid = null;
|
||||
let username = req.body.username;
|
||||
let password = req.body.password;
|
||||
const { username = null, password = null } = req.body;
|
||||
|
||||
if (username && password) {
|
||||
const user = db.get("secret." + username)
|
||||
if (user) {
|
||||
|
@ -24,8 +30,11 @@ module.exports = (req, res) => {
|
|||
|
||||
|
||||
} else
|
||||
error(res, 403, "You forgot entering some values")
|
||||
error(res, 400, "You forgot entering some values")
|
||||
|
||||
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
module.exports = app;
|
71
routes/message.js
Normal file
71
routes/message.js
Normal file
|
@ -0,0 +1,71 @@
|
|||
const { Thread, Message, User } = require("../classes");
|
||||
const error = require("../errors/error")
|
||||
|
||||
const { Router } = require("express");
|
||||
|
||||
const app = Router();
|
||||
|
||||
app.get("/:id", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const message = new Message().getId(req.params.id);
|
||||
|
||||
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
|
||||
|
||||
res.redirect("/threads/" + message.thread.id);
|
||||
|
||||
});
|
||||
|
||||
app.post("/", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
|
||||
const thread = new Thread().getId(req.body.threadID);
|
||||
|
||||
if (thread) {
|
||||
const message = new Message(req.body.content, new User().getId(req.session.userid), thread).takeId().write();
|
||||
thread.push(message.id)
|
||||
thread.write();
|
||||
res.redirect('/threads/' + req.body.threadID);
|
||||
|
||||
}
|
||||
else
|
||||
error(res, 404, "We have not got this thread.");
|
||||
|
||||
});
|
||||
|
||||
|
||||
app.post("/delete/:id", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
const message = new Message().getId(req.params.id)
|
||||
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
|
||||
const user = new User().getId(req.session.userid);
|
||||
if (user.id != message.author.id && !user.admin)
|
||||
return error(res, 403, "You have not got permission for this.");
|
||||
message.deleted = true;
|
||||
message.write();
|
||||
|
||||
|
||||
res.status(200).redirect("/threads/" + message.thread.id);
|
||||
|
||||
})
|
||||
app.post("/react/:id", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
const { id = null } = req.params;
|
||||
const info = req.body;
|
||||
const message = new Message().getId(id);
|
||||
if (message) {
|
||||
if (!(req.session.userid in message.react))
|
||||
message.react[req.session.userid] = "like" in info;
|
||||
else
|
||||
delete message.react[req.session.userid];
|
||||
|
||||
message.write();
|
||||
res.redirect("/threads/" + message.thread.id);
|
||||
} else error(res, 404, "We have not got this Message for reacting.");
|
||||
|
||||
|
||||
});
|
||||
|
||||
module.exports = app;
|
|
@ -1,22 +0,0 @@
|
|||
const { User } = require("../../classes/index");
|
||||
const error = require("../../errors/error.js");
|
||||
|
||||
module.exports = (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
const user = new User().getId(req.session.userid)
|
||||
|
||||
if (!user.admin) return error(res, 404, "You have not got permissions for view to this page.");
|
||||
const user2 = new User().getId(req.body.userid)
|
||||
|
||||
if (!user2) return error(res, 404, "We have not got this user in all of forum. Vesselam.");
|
||||
|
||||
else {
|
||||
user2.admin = true;
|
||||
user2.write()
|
||||
}
|
||||
|
||||
res.render("admin", { user, user2 })
|
||||
|
||||
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
const { User, Thread, Message } = require("../../classes/index");
|
||||
|
||||
|
||||
module.exports = (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const user = new User().getId(req.session.userid);
|
||||
const info = req.body;
|
||||
const thread = new Thread(info.title, user).takeId().write();
|
||||
thread.push(new Message(info.content, user, thread).takeId().write().id)
|
||||
thread.write();
|
||||
|
||||
res.redirect('/threads/' + thread.id);
|
||||
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
const { User, Message } = require("../../classes/index");
|
||||
const error = require("../../errors/error.js");
|
||||
|
||||
module.exports = (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const id = req.url.slice(9 + 6)
|
||||
const message = new Message().getId(id)
|
||||
|
||||
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
|
||||
const user = new User().getId(req.session.userid);
|
||||
if (user.id != message.author.id && !user.admin)
|
||||
return error(res, 403, "You have not got permission for this.");
|
||||
message.deleted = true;
|
||||
message.write();
|
||||
|
||||
|
||||
res.redirect("/threads/" + message.thread.id);
|
||||
|
||||
}
|
23
routes/post/react.js
vendored
23
routes/post/react.js
vendored
|
@ -1,23 +0,0 @@
|
|||
const error = require("../../errors/error.js")
|
||||
const { Message, User } = require("../../classes/index");
|
||||
|
||||
module.exports = (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const id = req.url.slice(7);
|
||||
if (!id) error(res, 404, "Id of request is missing");
|
||||
|
||||
const info = req.body;
|
||||
const user = new User().getId(req.session.userid);
|
||||
const message = new Message().getId(id);
|
||||
if (message) {
|
||||
if (!(user.id in message.react))
|
||||
message.react[user.id] = "like" in info;
|
||||
else
|
||||
delete message.react[user.id];
|
||||
|
||||
message.write();
|
||||
res.redirect("/threads/" + message.thread.id);
|
||||
} else error(res, 404, "We have not got this Message for reacting.");
|
||||
|
||||
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
const { Thread, Message, User } = require("../../classes/index");
|
||||
const error = require("../../errors/error.js")
|
||||
|
||||
module.exports = (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
const id = req.url.slice(9);
|
||||
const thread = new Thread().getId(id);
|
||||
|
||||
if (thread) {
|
||||
const message = new Message(req.body.content, new User().getId(req.session.userid), thread).takeId().write();
|
||||
thread.push(message.id)
|
||||
thread.write();
|
||||
res.redirect('/threads/' + id);
|
||||
|
||||
}
|
||||
else
|
||||
error(res, 404, "We have not got this thread.");
|
||||
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
const { User, Message } = require("../../classes/index");
|
||||
const error = require("../../errors/error.js");
|
||||
|
||||
module.exports = (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const user = new User().getId(req.session.userid);
|
||||
if (!user.admin)
|
||||
return error(res, 403, "You have not got permission for this.");
|
||||
|
||||
const id = req.url.slice(9 + 3)
|
||||
const member = new User().getId(id);
|
||||
if (!member || member.deleted) return error(res, 404, "We have not got any user declared as this id.");
|
||||
|
||||
member.deleted = true;
|
||||
member.write();
|
||||
|
||||
res.redirect("/admin");
|
||||
}
|
|
@ -1,23 +1,28 @@
|
|||
const db = require("quick.db");
|
||||
const error = require("../../errors/error.js")
|
||||
const { User } = require("../../classes/index");
|
||||
const { User } = require("../classes");
|
||||
const db = require("quick.db")
|
||||
|
||||
module.exports = (req, res) => {
|
||||
const { Router } = require("express")
|
||||
const error = require("../errors/error")
|
||||
|
||||
const app = Router();
|
||||
|
||||
app.get("/", (req, res) => res.render("register"));
|
||||
|
||||
app.post("/", (req, res) => {
|
||||
req.session.loggedin = false;
|
||||
req.session.username = null;
|
||||
req.session.userid = null;
|
||||
let username = req.body.username;
|
||||
let password = req.body.password;
|
||||
const { username = null, password = null } = req.body;
|
||||
|
||||
|
||||
if (username && password) {
|
||||
const user = db.get("secret." + username)
|
||||
|
||||
if (user) {
|
||||
if (user)
|
||||
error(res, 404, `We have got an user named ${username}!`)
|
||||
|
||||
} else {
|
||||
let avatar = req.body.avatar || "/images/guest.png"
|
||||
const user2 = new User(req.body.username, avatar).takeId()
|
||||
else {
|
||||
const user2 = new User(req.body.username, req.body.avatar).takeId()
|
||||
db.set("secret." + username, { id: user2.id, key: password })
|
||||
req.session.loggedin = true;
|
||||
req.session.username = username;
|
||||
|
@ -27,7 +32,10 @@ module.exports = (req, res) => {
|
|||
}
|
||||
|
||||
} else
|
||||
error(res, 404, "You forgot entering some values")
|
||||
error(res, 400, "You forgot entering some values")
|
||||
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
module.exports = app;
|
65
routes/threads.js
Normal file
65
routes/threads.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
const { Thread, Message, User } = require("../classes");
|
||||
const error = require("../errors/error")
|
||||
const db = require("quick.db")
|
||||
|
||||
const { Router } = require("express");
|
||||
|
||||
const app = Router();
|
||||
|
||||
app.get("/open*", (req, res) => {
|
||||
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const user = new User().getId(req.session.userid)
|
||||
res.render("openThread", { user })
|
||||
|
||||
});
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
const user = new User().getId(req.session.userid);
|
||||
|
||||
const threads = db.get("threads").slice(0, 10)
|
||||
const links = threads.map(thread => "/threads/" + threads.indexOf(thread))
|
||||
|
||||
return res.render("threads", { threads, links, user })
|
||||
|
||||
});
|
||||
|
||||
|
||||
app.get("/:id", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
const { id } = req.params;
|
||||
|
||||
const thread = new Thread().getId(id);
|
||||
|
||||
if (thread) {
|
||||
const user = new User().getId(req.session.userid);
|
||||
const messages = thread.messages.filter(id => !new Message().getId(id).deleted).map(id => new Message().getId(id));
|
||||
res.render("thread", { thread, messages, user })
|
||||
} else
|
||||
error(res, 404, "We have not got this thread.");
|
||||
});
|
||||
|
||||
app.post("/", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
|
||||
const user = new User().getId(req.session.userid);
|
||||
|
||||
const { title = null, content = null } = req.body;
|
||||
|
||||
if (!title || !content) return error(res, 400, "Title and content is missing");
|
||||
|
||||
const thread = new Thread(title, user).takeId().write();
|
||||
|
||||
thread
|
||||
.push(new Message(content, user, thread).takeId().write().id)
|
||||
.write();
|
||||
|
||||
res.redirect('/threads/' + thread.id);
|
||||
|
||||
})
|
||||
|
||||
|
||||
module.exports = app;
|
55
routes/users.js
Normal file
55
routes/users.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
const { User } = require("../classes");
|
||||
const db = require("quick.db")
|
||||
|
||||
const { Router } = require("express")
|
||||
const error = require("../errors/error")
|
||||
|
||||
const app = Router();
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const user = new User().getId(req.session.userid)
|
||||
|
||||
|
||||
const users = db.get("users").slice(0);
|
||||
|
||||
const links = users.filter(user => !user.deleted).map(user => "/users/" + user.id);
|
||||
return res.render("users", { users, links, user })
|
||||
|
||||
|
||||
|
||||
});
|
||||
app.get("/:id", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const user = new User().getId(req.session.userid)
|
||||
const { id = null } = req.params;
|
||||
const member = new User().getId(req.params.id);
|
||||
|
||||
|
||||
if (member && (user.admin || !member.deleted)) {
|
||||
const message = db.get("messages").filter(message => message.author.id === Number(id)).length
|
||||
const thread = db.get("threads").filter(thread => thread.author.id === Number(id)).length
|
||||
|
||||
const counts = { message, thread }
|
||||
res.render("user", { user, member, counts })
|
||||
}
|
||||
else error(res, 404, "We have not got this user.");
|
||||
|
||||
});
|
||||
app.post("/delete/:id", (req, res) => {
|
||||
if (!req.session.loggedin) return res.redirect('/login');
|
||||
const user = new User().getId(req.session.userid);
|
||||
if (!user.admin)
|
||||
return error(res, 403, "You have not got permission for this.");
|
||||
|
||||
const id = req.url.slice(9 + 3)
|
||||
const member = new User().getId(id);
|
||||
if (!member || member.deleted) return error(res, 404, "We have not got any user declared as this id.");
|
||||
|
||||
member.deleted = true;
|
||||
member.write();
|
||||
|
||||
res.redirect("/admin");
|
||||
});
|
||||
|
||||
module.exports = app;
|
|
@ -19,7 +19,7 @@
|
|||
<br> <button class="buyuk" onclick="window.location.href = '/threads'">THREADS</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/search'">SEARCH</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/users'">USERS</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/createThread/'">OPEN THREAD</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/threads/open/'">OPEN THREAD</button>
|
||||
<h1 style="display:inline; float:right;"><a href=<%=user.getLink() %>> <%= user.name %></a>
|
||||
<img class="yuvarlak" src=<%=user.avatar %> alt=<%= user.name %>>
|
||||
</h1>
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
|
||||
<button class="buyuk" onclick="window.location.href = '/search'">SEARCH</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/users'">USERS</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/createThread/'">OPEN THREAD</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/threads/open/'">OPEN THREAD</button>
|
||||
<h1 style="display:inline; float:right;"><a href=<%=user.getLink() %>> <%= user.name %></a>
|
||||
<img class="yuvarlak" src=<%=user.avatar %> alt=<%= user.name %>>
|
||||
</h1>
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
|
||||
|
||||
|
||||
<form action="/createThread/" method="POST">
|
||||
<form action="/threads/" method="POST">
|
||||
<h2>Title:</h2>
|
||||
<input name="title"></input>
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
|
||||
<button class="buyuk" onclick="window.location.href = '/search'">SEARCH</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/users'">USERS</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/createThread/'">OPEN THREAD</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/threads/open/'">OPEN THREAD</button>
|
||||
<h1 style="display:inline; float:right;"><a href=<%=user.getLink() %>> <%= user.name %></a>
|
||||
<img class="yuvarlak" src=<%=user.avatar %> alt=<%= user.name %>>
|
||||
</h1>
|
||||
|
@ -50,12 +50,14 @@
|
|||
<h2>
|
||||
<%= message.content %>
|
||||
</h2>
|
||||
<form style="text-align:right;display:inline;" action="/messageDelete/<%= message.id %>" method="post">
|
||||
|
||||
<form style="text-align:right;display:inline;" action="/message/delete/<%= message.id %>" method="post">
|
||||
<button style="display:inline;" class="button" type="submit">DELETE</button>
|
||||
</form>
|
||||
|
||||
<form style="text-align:right;" action="/react/<%= message.id %>" method="POST">
|
||||
|
||||
<form style="display:inline;" action="/message/<%= message.id %>" method="delete">
|
||||
</form>
|
||||
<form style="text-align:right;" action="/message/react/<%= message.id %>" method="POST">
|
||||
|
||||
<h3 style="display:inline;">
|
||||
<%= Object.values(message.react).filter(Boolean).length - Object.values(message.react).filter(x=>!x).length %>
|
||||
|
@ -73,12 +75,13 @@
|
|||
<br>
|
||||
<% }); %>
|
||||
|
||||
|
||||
<hr>
|
||||
|
||||
|
||||
<form action="/threads/<%= thread.id %>" method="POST">
|
||||
<form action="/message" method="POST">
|
||||
<textarea rows="4" cols="50" name="content"></textarea>
|
||||
<input name="threadID" type="hidden" value="<%= thread.id %>"></input>
|
||||
|
||||
<br>
|
||||
|
||||
<button class="button" type="submit">Send!</button>
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
<button class="buyuk" onclick="window.location.href = '/users'">USERS</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/search'">SEARCH</button>
|
||||
|
||||
<button class="buyuk" onclick="window.location.href = '/createThread/'">OPEN THREAD</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/threads/open/'">OPEN THREAD</button>
|
||||
<h1 style="display:inline; float:right;"><a href=<%=user.getLink() %>> <%= user.name %></a>
|
||||
<img class="yuvarlak" src=<%=user.avatar %> alt=<%= user.name %>>
|
||||
</h1>
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
<button class="buyuk" onclick="window.location.href = '/threads'">THREADS</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/search'">SEARCH</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/users'">USERS</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/createThread/'">OPEN THREAD</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/threads/open/'">OPEN THREAD</button>
|
||||
<h1 style="display:inline; float:right;"><a href=<%=user.getLink() %>> <%= user.name %></a>
|
||||
<img class="yuvarlak" src=<%=user.avatar %> alt=<%= user.name %>>
|
||||
</h1>
|
||||
|
@ -70,7 +70,7 @@
|
|||
|
||||
</form>
|
||||
|
||||
<form action="/userDelete/<%= member.id %>" method="POST">
|
||||
<form action="/users/delete/<%= member.id %>" method="POST">
|
||||
<button class="buyuk" type="submit">Delete user!</button>
|
||||
</form>
|
||||
<% }; %>
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
<button class="buyuk" onclick="window.location.href = '/search'">SEARCH</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/users'">USERS</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/createThread/'">OPEN THREAD</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/threads/open/'">OPEN THREAD</button>
|
||||
<h1 style="display:inline; float:right;"><a href=<%=user.getLink() %>> <%= user.name %></a>
|
||||
<img class="yuvarlak" src=<%=user.avatar %> alt=<%= user.name %>>
|
||||
</h1>
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
<br>
|
||||
<button class="buyuk" onclick="window.location.href = '/threads'">THREADS</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/search'">SEARCH</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/createThread/'">OPEN THREAD</button>
|
||||
<button class="buyuk" onclick="window.location.href = '/threads/open/'">OPEN THREAD</button>
|
||||
<h1 style="display:inline; float:right;"><a href=<%=user.getLink() %>> <%= user.name %></a>
|
||||
<img class="yuvarlak" src=<%=user.avatar %> alt=<%= user.name %>>
|
||||
</h1>
|
||||
|
|
Loading…
Reference in a new issue