From 9f5fd261c3d284882b5d3bca549b47c58465d4ed Mon Sep 17 00:00:00 2001 From: deadvey Date: Thu, 5 Mar 2026 13:53:10 +0000 Subject: [PATCH] Changing how data is requested in DEV branch --- src/data.js | 43 +++++-------------------------- src/functions.js | 13 +++++++--- src/routes/standard_pages.js | 31 +++++++++++----------- src/routes/syndication.js | 11 +++++--- views/pages/timeline.ejs | 1 + views/syndication/global_atom.ejs | 2 +- views/syndication/global_rss.ejs | 3 ++- views/syndication/user_rss.ejs | 3 ++- 8 files changed, 45 insertions(+), 62 deletions(-) diff --git a/src/data.js b/src/data.js index 937e7e9..2b096b3 100644 --- a/src/data.js +++ b/src/data.js @@ -86,7 +86,7 @@ export function getdata(table_name, key=-1, value=-1) { { // id is the index if (value < result.length && value >= 0) { - return result[value] + return [result[value]] } else { @@ -94,11 +94,10 @@ export function getdata(table_name, key=-1, value=-1) { return 1 } } - return result[func.find_key_value_pair(result, key, value)] - return -1 // This index doesn't exist + let return_list = func.find_key_value_pair(result,key,value) + return return_list } - return result.slice(- config['data_request_limit']) - break; + return result case 'hitcount': result = func.require_module('../data/data.json') // This file is actually called data.json return result["hitcount"] @@ -109,37 +108,9 @@ export function getdata(table_name, key=-1, value=-1) { break; } break; - // NOT YET WORKING! - case 'mysql': - const mysql = require('mysql'); - let con = mysql.createConnection({ - host: config.database.host, - user: config.database.user, - password: config.database.password, - database: config.database.database, - }); - - con.connect(function(err) { - if (err) throw err; - - if (data == "posts" || data == 'users' || data == 'comments') { - con.query(`SELECT * FROM ${data}`, function (err, result, fields) { - if (err) throw err; - result = Object.values(JSON.parse(JSON.stringify(result))) - console.log(result) - return result; - }); - } - else if (data == 'hitcount') { - con.query(`SELECT paramValue FROM params WHERE paramName = '${data}'`, function (err, result, fields) { - if (err) throw err; - result = Object.values(JSON.parse(JSON.stringify(result))) - console.log(result) - return result; - }); - - } - }); + default: + console.log("Error, invalid database management system") + return 1 } } diff --git a/src/functions.js b/src/functions.js index 54c3e40..a196e76 100644 --- a/src/functions.js +++ b/src/functions.js @@ -57,8 +57,9 @@ export function unix_time_to_atom_date(unix_time) // eg "string1, string2, string3" // this is so you can have a list of tags that each point to their individual tag page // returns: string -export function render_tags(tags) +export function render_tags(tags_string) { + let tags = tags_string.split(',') tags = tags.filter((item, index) => tags.indexOf(item) === index) // Remove duplicate tags let string = "" // Initialises the string if (tags.length == 1 && tags[0] == "") @@ -140,11 +141,15 @@ export function render_md(content) return md.render(content) }; +// Returns a list of matches export function find_key_value_pair(data_array, key, value) { + let return_list = [] for (let i = 0; i < data_array.length; i++) { - if (data_array[i][key] == value) { - return i + let element = data_array[i][key] + if (element == value + || (Array.isArray(element) && element.includes(value))) { + return_list.push(data_array[i]) } } - return -1 + return return_list }; diff --git a/src/routes/standard_pages.js b/src/routes/standard_pages.js index 6133347..2823a8a 100644 --- a/src/routes/standard_pages.js +++ b/src/routes/standard_pages.js @@ -10,30 +10,31 @@ const router = express.Router(); // Timeline router.get("/", (req,res) => { + let posts = data.getdata('posts') // 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, - }) + res.render("pages/timeline", + { + config, + locale, + 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', 'id', userID) + let user = data.getdata('users', 'id', userID)[0] if (userID != -1) { res.render("pages/user", { @@ -61,7 +62,7 @@ router.get("/user/:username", (req, res) => { // Posts router.get("/post/:post_index", (req, res) => { const postID = parseInt(req.params.post_index) - let post = data.getdata('posts','id', postID) + 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, diff --git a/src/routes/syndication.js b/src/routes/syndication.js index 9e8bb80..72a1240 100644 --- a/src/routes/syndication.js +++ b/src/routes/syndication.js @@ -18,9 +18,10 @@ router.get("/rss", (req,res) => { } else { res.setHeader('content-type', 'application/rss+xml'); - res.render("syndication/global_rss", { + res.render("syndication/rss", { config, posts: data.getdata('posts'), + users: data.getdata('users'), func, }) }; @@ -28,7 +29,7 @@ router.get("/rss", (req,res) => { // user RSS protocol gets router.get("/user/:username/rss", (req,res) => { const username = req.params.username; - const userID = func.get_userID(username); + const user = data.getdata('users', 'username', username); if (config.rss == false) { res.render("partials/message", { message: locale.rss_disabled, @@ -37,11 +38,11 @@ router.get("/user/:username/rss", (req,res) => { } else { res.setHeader('content-type', 'application/rss+xml'); - res.render("syndication/user_rss", { + res.render("syndication/rss", { config, posts: data.getdata('posts'), + users: user, func, - userID, }) }; }); @@ -58,6 +59,7 @@ router.get("/atom", (req,res) => { res.render("syndication/global_atom", { config, posts: data.getdata('posts'), + users: data.getdata('users'), func, getUnixTime, }) @@ -78,6 +80,7 @@ router.get("/user/:username/atom", (req,res) => { res.render("syndication/user_atom", { config, posts: data.getdata('posts'), + users: data.getdata('users'), func, userID, getUnixTime, diff --git a/views/pages/timeline.ejs b/views/pages/timeline.ejs index 9577879..5a27e1a 100644 --- a/views/pages/timeline.ejs +++ b/views/pages/timeline.ejs @@ -13,6 +13,7 @@
<% for (let index = posts.length - 1; index >= 0; index--) { %> <% if (posts[index]["deleted"] != true) { %> + <%- console.log(posts[index]) %> <%- include('../posts/timeline', {post: posts[index], postID: index, user: users[posts[index].userID], comments: comments[index]}); %> <% } %> <% } %> diff --git a/views/syndication/global_atom.ejs b/views/syndication/global_atom.ejs index c68b04e..6a758cf 100644 --- a/views/syndication/global_atom.ejs +++ b/views/syndication/global_atom.ejs @@ -1,7 +1,7 @@ " ?> <%= config.site_name %> - <%= config.site_url %> + <%= config.site_url %> <%= config.site_description %> <%= func.unix_time_to_atom_date(getUnixTime(new Date())) %> <%= config.site_url %> diff --git a/views/syndication/global_rss.ejs b/views/syndication/global_rss.ejs index bdb0ea0..dc8bdfc 100644 --- a/views/syndication/global_rss.ejs +++ b/views/syndication/global_rss.ejs @@ -2,7 +2,7 @@ <%= config.site_name %> - <%= config.site_url %> + <%= config.site_url %> <%= config.site_description %> <% for (let postID = posts.length-1; postID >= 0; postID--) { %> <% if (posts[postID]["deleted"] != true) { %> @@ -12,6 +12,7 @@ ]]> <%= config.site_url %>/post/<%= postID %> <%= func.unix_time_to_rss_date(posts[postID]['pubdate']) %> + <%= users[posts[postID]['userID']]['prettyname'] %> <% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %> ]]> <% } %> diff --git a/views/syndication/user_rss.ejs b/views/syndication/user_rss.ejs index 383ccb7..94a6034 100644 --- a/views/syndication/user_rss.ejs +++ b/views/syndication/user_rss.ejs @@ -10,9 +10,10 @@ <%= posts[postID]["title"] %> <%= config.site_url %>/post/<%= postID %> - ]]> + ]]> <%= config.site_url %>/post/<%= postID %> <%= func.unix_time_to_rss_date(posts[postID]['pubdate']) %> + <%= users[posts[postID]['userID']]['prettyname'] %> <% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %> ]]> <% } %>