Files
blogger-nodejs/src/routes/standard_pages.js
2025-10-24 13:10:53 +01:00

152 lines
4.4 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("/", (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("/user/:username", (req, res) => {
const userID = func.get_userID(req.params.username)
let user = data.getdata('users', userID)
if (userID != -1) {
res.render("pages/user",
{
config,
locale,
posts: data.getdata('posts'),
user,
userID: userID,
comments: data.getdata('comments'),
fromUnixTime,
format,
getUnixTime,
func,
})
}
else if (userID == -1) {
res.render("partials/message",
{
message: locale.user_doesnt_exist,
config,
})
}
}); // /user/:username
// Posts
router.get("/post/:post_index", (req, res) => {
const postID = parseInt(req.params.post_index)
let post = data.getdata('posts', postID)
if (post == 1) { // data.getdata returns error code 1 if nothing is available
res.render("partials/message", {
message: locale.post_doesnt_exist,
config,
})
}
if (config.enable_hitcount) {
data.increment_hitcount(postID)
}
if (typeof post["deleted"] == "undefined" || post["deleted"] == false) {
res.render("pages/post",
{
config,
locale,
post,
postID,
user: data.getdata('users', post.userID),
comments: data.getdata('comments', postID),
fromUnixTime,
format,
getUnixTime,
func,
})
}
else {
console.log("Error loading page")
res.redirect(301,"/")
}
}); // /post/:post_index
// Tags
router.get("/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("/comment/:postID-:commentID", (req,res) => {
const commentID = parseInt(req.params.commentID);
const postID = parseInt(req.params.postID);
let posts_comments = data.getdata('comments', postID)
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;