Files
blogger-nodejs/src/routes/standard_pages.js
T

153 lines
3.3 KiB
JavaScript

const express = require('express');
const config = require('../../config')
const data = require('../data')
const func = require('../functions')
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
const router = express.Router();
///////////////////// Standard Pages //////////////////////
// Timeline
router.get(config.site_path, (req,res) => {
// Increment the hitcount
if (config.enable_hitcount) {
data.increment_hitcount()
}
res.render("pages/timeline",
{
config,
locale,
posts: data.getdata("posts"),
users: data.getdata("users"),
comments: data.getdata("comments"),
hitcount: data.getdata("hitcount"),
fromUnixTime,
format,
getUnixTime,
func,
})
}); // /
// Users
router.get(`${config.site_path}/user/:username`, (req, res) => {
const userID = func.get_userID(req.params.username)
let posts = data.getdata('posts', 'userID', userID);
let user = data.getdata('users', 'id', userID)[0];
if (userID != -1) {
res.render("pages/user",
{
config,
locale,
posts,
user,
userID: userID,
comments: data.getdata('comments', 'postID'),
fromUnixTime,
format,
getUnixTime,
func,
})
}
else if (userID == -1) {
res.render("partials/message",
{
message: locale.user_doesnt_exist,
config,
})
}
}); // /user/:username
// Posts
router.get(`${config.site_path}/post/:post_index`, (req, res) => {
const postID = parseInt(req.params.post_index)
let post = data.getdata('posts','id', postID)[0]
if (post == 1) { // data.getdata returns error code 1 if nothing is available
res.render("partials/message", {
message: locale.post_doesnt_exist,
config,
})
}
else if (typeof post["deleted"] == "undefined" || post["deleted"] == false) {
if (config.enable_hitcount) {
data.increment_hitcount(postID)
}
res.render("pages/post",
{
config,
locale,
post,
postID,
user: data.getdata('users','id', post.userID)[0],
comments: data.getdata('comments','id', postID)[0]["comments"],
fromUnixTime,
format,
getUnixTime,
func,
})
}
else {
console.log("Error loading page")
res.redirect(301,"/")
}
}); // /post/:post_index
// Tags
router.get(`${config.site_path}/tag/:tag`, (req,res) => {
const tag = req.params.tag
res.render("pages/tag",
{
config,
locale,
tag,
posts: data.getdata('posts'),
users: data.getdata('users'),
comments: data.getdata('comments'),
fromUnixTime,
format,
getUnixTime,
func,
})
}); // /tag/:tag
// Comments
router.get(`${config.site_path}/comment/:postID-:commentID`, (req,res) => {
const commentID = parseInt(req.params.commentID);
const postID = parseInt(req.params.postID);
let posts_comments = data.getdata('comments', 'id', postID)[0]["comments"]
let comment = 1
// For loop to find the comment with matching ID
posts_comments.forEach((current_comment, index) => {
if (current_comment.id == commentID) {
comment = posts_comments[index]
}
})
// If comment doesn't exist, show error
if (comment == 1 || posts_comments == 1) { // Comment of this ID was not found
res.render("partials/message", {
config,
message: locale.comment_doesnt_exist,
})
}
else {
res.render("pages/comment",
{
config,
locale,
comment,
postID,
commentID,
fromUnixTime,
format,
getUnixTime,
func,
})
}
});
module.exports = router;