diff --git a/src/data.js b/src/data.js index 2b096b3..e57c447 100644 --- a/src/data.js +++ b/src/data.js @@ -14,7 +14,7 @@ export function increment_hitcount(postID = -1) { // -1 Means it will increment writedata('hitcount', hitcount); } else { - let post = getdata('posts','id', postID); + let post = getdata('posts','id', postID)[0]; if (post == 1) // Does not exist { return 1 diff --git a/src/functions.js b/src/functions.js index a196e76..275a389 100644 --- a/src/functions.js +++ b/src/functions.js @@ -75,24 +75,6 @@ export function render_tags(tags_string) } return string } -// The users are stored as a list of objects [ user_object, user_object, user_object ] -// So you cannot easily find the userID (position in list) from the username -// This function returns the username for a given userID by looping over every user -// if the user is present, it returns the index of the user (integer) -// if the user is not present it returns -1 -export function get_userID(username) -{ - const users = require_module("../data/users.json") - for (let i = 0; i < users.length; i++) - { // Loop over every user - if (users[i]['username'] == username) - { - return i // If the username matches then return the index of that user - } - } - return -1 // If user is not present, return -1 -} - // This escapes some potentially dangerous HTML characters with their HTML entities // https://www.freeformatter.com/html-entities.html // accepts a string @@ -146,8 +128,7 @@ export function find_key_value_pair(data_array, key, value) { let return_list = [] for (let i = 0; i < data_array.length; i++) { let element = data_array[i][key] - if (element == value - || (Array.isArray(element) && element.includes(value))) { + if (element == value || (key == 'tags' && element.includes(value))) { return_list.push(data_array[i]) } } diff --git a/src/routes/standard_pages.js b/src/routes/standard_pages.js index b7eccfe..16d4ec3 100644 --- a/src/routes/standard_pages.js +++ b/src/routes/standard_pages.js @@ -33,16 +33,14 @@ router.get("/", (req,res) => { // Users router.get("/user/:username", (req, res) => { - const userID = func.get_userID(req.params.username) - let user = data.getdata('users', 'id', userID)[0] - if (userID != -1) { + let user = data.getdata('users', 'username', req.params.username) + if (user.length > 0) { res.render("pages/user", { config, locale, - posts: data.getdata('posts'), - user, - userID: userID, + posts: data.getdata('posts','userID',user[0].id), + user: user[0], comments: data.getdata('comments'), fromUnixTime, format, @@ -50,7 +48,7 @@ router.get("/user/:username", (req, res) => { func, }) } - else if (userID == -1) { + else { res.render("partials/message", { message: locale.user_doesnt_exist, @@ -79,8 +77,8 @@ router.get("/post/:post_index", (req, res) => { locale, post, postID, - user: data.getdata('users','id', post.userID), - comments: data.getdata('comments','id', postID)["comments"], + user: data.getdata('users','id', post.userID)[0], + comments: data.getdata('comments','id', postID)[0]["comments"], fromUnixTime, format, getUnixTime, @@ -93,19 +91,23 @@ router.get("/post/:post_index", (req, res) => { // Tags router.get("/tag/:tag", (req,res) => { const tag = req.params.tag + const posts = data.getdata('posts','tags',tag); + console.log(posts) + const users = data.getdata('users') + const comments = data.getdata('comments') res.render("pages/tag", - { - config, - locale, - tag, - posts: data.getdata('posts'), - users: data.getdata('users'), - comments: data.getdata('comments'), - fromUnixTime, - format, - getUnixTime, - func, - }) + { + config, + locale, + tag, + posts, + users, + comments, + fromUnixTime, + format, + getUnixTime, + func, + }) }); // /tag/:tag diff --git a/views/headers/user.ejs b/views/headers/user.ejs index f7bb625..d6e52ff 100644 --- a/views/headers/user.ejs +++ b/views/headers/user.ejs @@ -4,7 +4,7 @@
<%- func.render_md(user.description) %>
- +