changed how data is read from the database and consolodated syndation feeds into just 2 files.

This commit is contained in:
2026-06-01 13:53:58 +01:00
parent 1a0b16feb2
commit 5a14b125d2
7 changed files with 82 additions and 41 deletions
+7 -6
View File
@@ -14,7 +14,7 @@ export function increment_hitcount(postID = -1) { // -1 Means it will increment
writedata('hitcount', hitcount); writedata('hitcount', hitcount);
} }
else { else {
let post = getdata('posts','id', postID); let post = getdata('posts','id', postID)[0];
if (post == 1) // Does not exist if (post == 1) // Does not exist
{ {
return 1 return 1
@@ -86,18 +86,19 @@ export function getdata(table_name, key=-1, value=-1) {
{ // id is the index { // id is the index
if (value < result.length && value >= 0) if (value < result.length && value >= 0)
{ {
return result[value] return [result[value]]
} }
else else
{ {
console.log("No object of this ID exists for the selected table") console.log("No object of this ID exists for the selected table", table_name)
return 1 return 1
} }
} }
return result[func.find_key_value_pair(result, key, value)] let return_value = func.find_key_value_pair(result, key, value)
return -1 // This index doesn't exist return return_value
} }
return result.slice(- config['data_request_limit']) let return_value = result.slice(- config['data_request_limit'])
return return_value
break; break;
case 'hitcount': case 'hitcount':
result = func.require_module('../data/data.json') // This file is actually called data.json result = func.require_module('../data/data.json') // This file is actually called data.json
+16
View File
@@ -140,6 +140,7 @@ export function render_md(content)
return md.render(content) return md.render(content)
}; };
/*
export function find_key_value_pair(data_array, key, value) { export function find_key_value_pair(data_array, key, value) {
for (let i = 0; i < data_array.length; i++) { for (let i = 0; i < data_array.length; i++) {
if (data_array[i][key] == value) { if (data_array[i][key] == value) {
@@ -148,3 +149,18 @@ export function find_key_value_pair(data_array, key, value) {
} }
return -1 return -1
}; };
*/
// 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++) {
let element = data_array[i][key]
if (element == value
|| (Array.isArray(element) && element.includes(value))) {
return_list.push(data_array[i])
}
}
return return_list
};
+1 -1
View File
@@ -71,7 +71,7 @@ router.post(`${config.site_path}/submit_post`, (req,res) => {
fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8'); fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
comments.push({'id': id, 'comments': []}) comments.push({'id': id, 'comments': []})
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`) fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`)
res.redirect(302, config.site_path); res.redirect(302, `/post/${id}`);
} }
else { else {
res.render("partials/message", { res.render("partials/message", {
+7 -6
View File
@@ -33,16 +33,17 @@ router.get(config.site_path, (req,res) => {
// Users // Users
router.get(`${config.site_path}/user/:username`, (req, res) => { router.get(`${config.site_path}/user/:username`, (req, res) => {
const userID = func.get_userID(req.params.username) const userID = func.get_userID(req.params.username)
let user = data.getdata('users', 'id', userID) let posts = data.getdata('posts', 'userID', userID);
let user = data.getdata('users', 'id', userID)[0];
if (userID != -1) { if (userID != -1) {
res.render("pages/user", res.render("pages/user",
{ {
config, config,
locale, locale,
posts: data.getdata('posts'), posts,
user, user,
userID: userID, userID: userID,
comments: data.getdata('comments'), comments: data.getdata('comments', 'postID'),
fromUnixTime, fromUnixTime,
format, format,
getUnixTime, getUnixTime,
@@ -61,7 +62,7 @@ router.get(`${config.site_path}/user/:username`, (req, res) => {
// Posts // Posts
router.get(`${config.site_path}/post/:post_index`, (req, res) => { router.get(`${config.site_path}/post/:post_index`, (req, res) => {
const postID = parseInt(req.params.post_index) 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 if (post == 1) { // data.getdata returns error code 1 if nothing is available
res.render("partials/message", { res.render("partials/message", {
message: locale.post_doesnt_exist, message: locale.post_doesnt_exist,
@@ -78,8 +79,8 @@ router.get(`${config.site_path}/post/:post_index`, (req, res) => {
locale, locale,
post, post,
postID, postID,
user: data.getdata('users','id', post.userID), user: data.getdata('users','id', post.userID)[0],
comments: data.getdata('comments','id', postID), comments: data.getdata('comments','id', postID)[0]["comments"],
fromUnixTime, fromUnixTime,
format, format,
getUnixTime, getUnixTime,
+9 -9
View File
@@ -18,7 +18,8 @@ router.get(`${config.site_path}/rss`, (req,res) => {
} }
else { else {
res.setHeader('content-type', 'application/rss+xml'); res.setHeader('content-type', 'application/rss+xml');
res.render("syndication/global_rss", { res.render("syndication/rss", {
users: data.getdata('users'),
config, config,
posts: data.getdata('posts'), posts: data.getdata('posts'),
func, func,
@@ -26,9 +27,10 @@ router.get(`${config.site_path}/rss`, (req,res) => {
}; };
}); });
// user RSS protocol gets // user RSS protocol gets
// TODO specific title field and descriptions??
router.get(`${config.site_path}/user/:username/rss`, (req,res) => { router.get(`${config.site_path}/user/:username/rss`, (req,res) => {
const username = req.params.username; const username = req.params.username;
const userID = func.get_userID(username); const user = data.getdata('users','username',username)[0]; // Get the user data
if (config.rss == false) { if (config.rss == false) {
res.render("partials/message", { res.render("partials/message", {
message: locale.rss_disabled, message: locale.rss_disabled,
@@ -37,11 +39,11 @@ router.get(`${config.site_path}/user/:username/rss`, (req,res) => {
} }
else { else {
res.setHeader('content-type', 'application/rss+xml'); res.setHeader('content-type', 'application/rss+xml');
res.render("syndication/user_rss", { res.render("syndication/rss", {
users: [user],
config, config,
posts: data.getdata('posts'), posts: data.getdata('posts','id',user['id']),
func, func,
userID,
}) })
}; };
}); });
@@ -55,7 +57,7 @@ router.get(`${config.site_path}/atom`, (req,res) => {
} }
else { else {
res.setHeader('content-type', 'application/rss+xml'); res.setHeader('content-type', 'application/rss+xml');
res.render("syndication/global_atom", { res.render("syndication/atom", {
config, config,
posts: data.getdata('posts'), posts: data.getdata('posts'),
func, func,
@@ -66,7 +68,6 @@ router.get(`${config.site_path}/atom`, (req,res) => {
// user ATOM protocol gets // user ATOM protocol gets
router.get(`${config.site_path}/user/:username/atom`, (req,res) => { router.get(`${config.site_path}/user/:username/atom`, (req,res) => {
const username = req.params.username; const username = req.params.username;
const userID = func.get_userID(username);
if (config.atom == false) { if (config.atom == false) {
res.render("partials/message", { res.render("partials/message", {
message: locale.atom_disabled, message: locale.atom_disabled,
@@ -75,11 +76,10 @@ router.get(`${config.site_path}/user/:username/atom`, (req,res) => {
} }
else { else {
res.setHeader('content-type', 'application/rss+xml'); res.setHeader('content-type', 'application/rss+xml');
res.render("syndication/user_atom", { res.render("syndication/atom", {
config, config,
posts: data.getdata('posts'), posts: data.getdata('posts'),
func, func,
userID,
getUnixTime, getUnixTime,
}) })
}; };
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="<%= config.charset %>" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><%= config.site_name %></title>
<link><%= config.site_url %></title>
<description><%= config.site_description %></description>
<updated><%= func.unix_time_to_date_format(getUnixTime(new Date()),"yyyy-MM-dd'T'HH:mm:ss'Z'") %></updated>
<id><%= config.site_url %></id>
<% for (let postID = posts.length-1; postID >= 0; postID--) { %>
<% if (posts[postID]["deleted"] != true) { %>
<entry>
<title><%= posts[postID]["title"] %></title>
<link><%= config.site_url %>/post/<%= postID %></link>
<summary><![CDATA[<%- (posts[postID]["content"]) %>]]></summary>
<guid isPermaLink="true"><%= config.site_url %>/post/<%= postID %></guid>
<pubDate><%# func.unix_time_to_date_format(posts[postID]['pubdate'],"yyyy-MM-dd'T'HH:mm:ss'Z'") %></pubDate>
<% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %>
<category><![CDATA[<%= posts[postID]['tags'][tag_index] %>]]></category>
<% } %>
</entry>
<% } %>
<% } %>
</feed>
+20 -19
View File
@@ -1,22 +1,23 @@
<?xml version="1.0" encoding="<%= config.charset %>" ?> <?xml version="1.0" encoding="<%= config.charset %>" ?>
<rss version="2.0"> <rss version="2.0">
<channel> <channel>
<title><%= config.site_name %></title> <title><%= config.site_name %></title>
<link><%= config.site_url %></title> <link><%= config.site_url %></link>
<description><%= config.site_description %></description> <description><%= config.site_description %></description>
<% for (let postID = posts.length-1; postID >= 0; postID--) { %> <% for (let postID = posts.length-1; postID >= 0; postID--) { %>
<% if (posts[postID]["deleted"] != true) { %> <% if (posts[postID]["deleted"] != true) { %>
<item> <item>
<title><%= posts[postID]["title"] %></title> <title><%= posts[postID]["title"] %></title>
<link><%= config.site_url %>/post/<%= postID %></link> <link><%= config.site_url %>/post/<%= postID %></link>
<description><![CDATA[<%- func.render_md(posts[postID]["content"]) %>]]></description> <description><![CDATA[<%- (posts[postID]["content"]) %>]]></description>
<guid isPermaLink="true"><%= config.site_url %>/post/<%= postID %></guid> <guid isPermaLink="true"><%= config.site_url %>/post/<%= postID %></guid>
<pubDate><%= func.unix_time_to_rss_date(posts[postID]['pubdate']) %></pubDate> <pubDate><%= func.unix_time_to_date_format(posts[postID]['pubdate'],'EEE, dd MMM yyyy HH:mm:ss') %></pubDate>
<% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %> <author><%= users[posts[postID]['userID']]['prettyname'] %></author>
<category><![CDATA[<%= posts[postID]['tags'][tag_index] %>]]></category> <% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %>
<% } %> <category><![CDATA[<%= posts[postID]['tags'][tag_index] %>]]></category>
</item> <% } %>
<% } %> </item>
<% } %> <% } %>
</channel> <% } %>
</channel>
</rss> </rss>