Compare commits

1 Commits
dev ... master

Author SHA1 Message Date
4955ebe7bd Update README.md 2026-03-25 18:09:23 +01:00
15 changed files with 227 additions and 139 deletions

View File

@@ -29,20 +29,14 @@ Read the [configuation guide](docs/CONFIG.md) for configuration help (in config.
* Search functionality * Search functionality
* Page indexes * Page indexes
# Bugs:
* probably scales like shit
* probably insecure as hell
# Planned features/todo list: # Planned features/todo list:
* federation (looks tricky) * federation
* inline comments and docs
* clean up code a bit * clean up code a bit
* /postID and /userID pages * /postID and /userID pages
* Make EJS modification more user friendly (half done) * Make EJS modification more user friendly (half done)
* API for returning posts, users, comments, tags other?... * API for returning posts, users, comments, tags other?...
* Moderation tools including a keyword blacklist * Moderation tools including a keyword blacklist
* Request account function? Not sure how this should be implemented. * Request account function? Not sure how this should be implemented.
* optional SQL
* initialisation has prompts for setup process. * initialisation has prompts for setup process.
# Docs: # Docs:

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)[0]; let post = getdata('posts','id', postID);
if (post == 1) // Does not exist if (post == 1) // Does not exist
{ {
return 1 return 1
@@ -86,7 +86,7 @@ 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
{ {
@@ -94,10 +94,11 @@ export function getdata(table_name, key=-1, value=-1) {
return 1 return 1
} }
} }
let return_list = func.find_key_value_pair(result,key,value) return result[func.find_key_value_pair(result, key, value)]
return return_list return -1 // This index doesn't exist
} }
return result return result.slice(- config['data_request_limit'])
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
return result["hitcount"] return result["hitcount"]
@@ -108,9 +109,37 @@ export function getdata(table_name, key=-1, value=-1) {
break; break;
} }
break; break;
default: // NOT YET WORKING!
console.log("Error, invalid database management system") case 'mysql':
return 1 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;
});
}
});
} }
} }

View File

@@ -57,9 +57,8 @@ export function unix_time_to_atom_date(unix_time)
// eg "<a href="/tag/string1">string1</a>, <a href="/tag/string2">string2</a>, <a href="/tag/string3">string3</a>" // eg "<a href="/tag/string1">string1</a>, <a href="/tag/string2">string2</a>, <a href="/tag/string3">string3</a>"
// this is so you can have a list of tags that each point to their individual tag page // this is so you can have a list of tags that each point to their individual tag page
// returns: string // returns: string
export function render_tags(tags_string) export function render_tags(tags)
{ {
let tags = tags_string.split(',')
tags = tags.filter((item, index) => tags.indexOf(item) === index) // Remove duplicate tags tags = tags.filter((item, index) => tags.indexOf(item) === index) // Remove duplicate tags
let string = "" // Initialises the string let string = "" // Initialises the string
if (tags.length == 1 && tags[0] == "") if (tags.length == 1 && tags[0] == "")
@@ -75,6 +74,24 @@ export function render_tags(tags_string)
} }
return 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 // This escapes some potentially dangerous HTML characters with their HTML entities
// https://www.freeformatter.com/html-entities.html // https://www.freeformatter.com/html-entities.html
// accepts a string // accepts a string
@@ -123,14 +140,11 @@ export function render_md(content)
return md.render(content) return md.render(content)
}; };
// Returns a list of matches
export function find_key_value_pair(data_array, key, value) { export function find_key_value_pair(data_array, key, value) {
let return_list = []
for (let i = 0; i < data_array.length; i++) { for (let i = 0; i < data_array.length; i++) {
let element = data_array[i][key] if (data_array[i][key] == value) {
if (element == value || (key == 'tags' && element.includes(value))) { return i
return_list.push(data_array[i])
} }
} }
return return_list return -1
}; };

View File

@@ -10,45 +10,46 @@ const router = express.Router();
// Timeline // Timeline
router.get("/", (req,res) => { router.get("/", (req,res) => {
let posts = data.getdata('posts')
// Increment the hitcount // Increment the hitcount
if (config.enable_hitcount) { if (config.enable_hitcount) {
data.increment_hitcount() data.increment_hitcount()
} }
res.render("pages/timeline", res.render("pages/timeline",
{ {
config, config,
locale, locale,
posts, posts: data.getdata("posts"),
users: data.getdata("users"), users: data.getdata("users"),
comments: data.getdata("comments"), comments: data.getdata("comments"),
hitcount: data.getdata("hitcount"), hitcount: data.getdata("hitcount"),
fromUnixTime, fromUnixTime,
format, format,
getUnixTime, getUnixTime,
func, func,
}) })
}); // / }); // /
// Users // Users
router.get("/user/:username", (req, res) => { router.get("/user/:username", (req, res) => {
let user = data.getdata('users', 'username', req.params.username) const userID = func.get_userID(req.params.username)
if (user.length > 0) { let user = data.getdata('users', 'id', userID)
if (userID != -1) {
res.render("pages/user", res.render("pages/user",
{ {
config, config,
locale, locale,
posts: data.getdata('posts','userID',user[0].id), posts: data.getdata('posts'),
user: user[0], user,
comments: data.getdata('comments'), userID: userID,
fromUnixTime, comments: data.getdata('comments'),
format, fromUnixTime,
getUnixTime, format,
func, getUnixTime,
}) func,
})
} }
else { else if (userID == -1) {
res.render("partials/message", res.render("partials/message",
{ {
message: locale.user_doesnt_exist, message: locale.user_doesnt_exist,
@@ -60,30 +61,34 @@ router.get("/user/:username", (req, res) => {
// Posts // Posts
router.get("/post/:post_index", (req, res) => { router.get("/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)[0] let post = data.getdata('posts','id', postID)
if (post.length == 0) { // 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,
config, config,
}) })
} }
else { else if (typeof post["deleted"] == "undefined" || post["deleted"] == false) {
if (config.enable_hitcount) { if (config.enable_hitcount) {
data.increment_hitcount(postID) data.increment_hitcount(postID)
} }
res.render("pages/post", res.render("pages/post",
{ {
config, config,
locale, locale,
post, post,
postID, postID,
user: data.getdata('users','id', post.userID)[0], user: data.getdata('users','id', post.userID),
comments: data.getdata('comments','id', postID)[0]["comments"], comments: data.getdata('comments','id', postID)["comments"],
fromUnixTime, fromUnixTime,
format, format,
getUnixTime, getUnixTime,
func, func,
}) })
}
else {
console.log("Error loading page")
res.redirect(301,"/")
} }
}); // /post/:post_index }); // /post/:post_index
@@ -91,23 +96,19 @@ router.get("/post/:post_index", (req, res) => {
// Tags // Tags
router.get("/tag/:tag", (req,res) => { router.get("/tag/:tag", (req,res) => {
const tag = req.params.tag 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", res.render("pages/tag",
{ {
config, config,
locale, locale,
tag, tag,
posts, posts: data.getdata('posts'),
users, users: data.getdata('users'),
comments, comments: data.getdata('comments'),
fromUnixTime, fromUnixTime,
format, format,
getUnixTime, getUnixTime,
func, func,
}) })
}); // /tag/:tag }); // /tag/:tag

View File

@@ -18,10 +18,9 @@ router.get("/rss", (req,res) => {
} }
else { else {
res.setHeader('content-type', 'application/rss+xml'); res.setHeader('content-type', 'application/rss+xml');
res.render("syndication/rss", { res.render("syndication/global_rss", {
config, config,
posts: data.getdata('posts'), posts: data.getdata('posts'),
users: data.getdata('users'),
func, func,
}) })
}; };
@@ -29,7 +28,7 @@ router.get("/rss", (req,res) => {
// user RSS protocol gets // user RSS protocol gets
router.get("/user/:username/rss", (req,res) => { router.get("/user/:username/rss", (req,res) => {
const username = req.params.username; const username = req.params.username;
const user = data.getdata('users', 'username', username); const userID = func.get_userID(username);
if (config.rss == false) { if (config.rss == false) {
res.render("partials/message", { res.render("partials/message", {
message: locale.rss_disabled, message: locale.rss_disabled,
@@ -38,11 +37,11 @@ router.get("/user/:username/rss", (req,res) => {
} }
else { else {
res.setHeader('content-type', 'application/rss+xml'); res.setHeader('content-type', 'application/rss+xml');
res.render("syndication/rss", { res.render("syndication/user_rss", {
config, config,
posts: data.getdata('posts'), posts: data.getdata('posts'),
users: user,
func, func,
userID,
}) })
}; };
}); });
@@ -59,7 +58,6 @@ router.get("/atom", (req,res) => {
res.render("syndication/global_atom", { res.render("syndication/global_atom", {
config, config,
posts: data.getdata('posts'), posts: data.getdata('posts'),
users: data.getdata('users'),
func, func,
getUnixTime, getUnixTime,
}) })
@@ -80,7 +78,6 @@ router.get("/user/:username/atom", (req,res) => {
res.render("syndication/user_atom", { res.render("syndication/user_atom", {
config, config,
posts: data.getdata('posts'), posts: data.getdata('posts'),
users: data.getdata('users'),
func, func,
userID, userID,
getUnixTime, getUnixTime,

View File

@@ -4,7 +4,7 @@
</h1> </h1>
</div> </div>
<p><%- func.render_md(user.description) %></p> <p><%- func.render_md(user.description) %></p>
<a if='edit-account-link' href="<%= config.edit_account_base_url %>/<%= user.id %>"> <a if='edit-account-link' href="<%= config.edit_account_base_url %>/<%= userID %>">
<img class='icon' src='/icons/edit.png' alt='<%= locale.edit_account %>' title='<%= locale.edit_account %>'> <img class='icon' src='/icons/edit.png' alt='<%= locale.edit_account %>' title='<%= locale.edit_account %>'>
</a> </a>
| |

View File

@@ -12,7 +12,13 @@
</header> </header>
<div id="posts"> <div id="posts">
<% for (let index = posts.length - 1; index >= 0; index--) { %> <% for (let index = posts.length - 1; index >= 0; index--) { %>
<%- include('../posts/tag', {post: posts[index], postID: index, user: users[posts[index].userID], comments: comments[index]}); %> <% if ( posts[index].deleted != true) { %>
<% posts[index].tags.forEach((current_tag, tag_index) => { %>
<% if (current_tag.toLowerCase() == tag.toLowerCase()) { %>
<%- include('../posts/tag', {post: posts[index], postID: index, user: users[posts[index].userID], comments: comments[index]}); %>
<% } %>
<% }) %>
<% } %>
<% } %> <% } %>
</div> </div>
<footer id='footer'> <footer id='footer'>

View File

@@ -13,7 +13,6 @@
<div id="posts"> <div id="posts">
<% for (let index = posts.length - 1; index >= 0; index--) { %> <% for (let index = posts.length - 1; index >= 0; index--) { %>
<% if (posts[index]["deleted"] != true) { %> <% 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]}); %> <%- include('../posts/timeline', {post: posts[index], postID: index, user: users[posts[index].userID], comments: comments[index]}); %>
<% } %> <% } %>
<% } %> <% } %>

View File

@@ -12,7 +12,9 @@
</header> </header>
<div id="posts"> <div id="posts">
<% for (let index = posts.length - 1; index >= 0; index--) { %> <% for (let index = posts.length - 1; index >= 0; index--) { %>
<%- include('../posts/user', {post: posts[index], postID: index, user: user, comments: comments[index]}); %> <% if (posts[index].userID == userID) { %>
<%- include('../posts/user', {post: posts[index], postID: index, user: user, comments: comments[index]}); %>
<% } %>
<% } %> <% } %>
</div> </div>
<footer id='footer'> <footer id='footer'>

View File

@@ -1,23 +0,0 @@
<?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>
<% tags = posts[postID]['tags'].split(',') %>
<% for (let tag_index = 0; tag_index < tags.length; tag_index++) { %>
<category><![CDATA[<%= tags[tag_index] %>]]></category>
<% } %>
</entry>
<% } %>
<% } %>
</feed>

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_atom_date(getUnixTime(new Date())) %></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[<%- func.render_md(posts[postID]["content"]) %>]]></summary>
<guid isPermaLink="true"><%= config.site_url %>/post/<%= postID %></guid>
<pubDate><%# func.unix_time_to_atom_date(posts[postID]['pubdate']) %></pubDate>
<% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %>
<category><![CDATA[<%= posts[postID]['tags'][tag_index] %>]]></category>
<% } %>
</entry>
<% } %>
<% } %>
</feed>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="<%= config.charset %>" ?>
<rss version="2.0">
<channel>
<title><%= config.site_name %></title>
<link><%= config.site_url %></title>
<description><%= config.site_description %></description>
<% for (let postID = posts.length-1; postID >= 0; postID--) { %>
<% if (posts[postID]["deleted"] != true) { %>
<item>
<title><%= posts[postID]["title"] %></title>
<link><%= config.site_url %>/post/<%= postID %></link>
<description><![CDATA[<%- func.render_md(posts[postID]["content"]) %>]]></description>
<guid isPermaLink="true"><%= config.site_url %>/post/<%= postID %></guid>
<pubDate><%= func.unix_time_to_rss_date(posts[postID]['pubdate']) %></pubDate>
<% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %>
<category><![CDATA[<%= posts[postID]['tags'][tag_index] %>]]></category>
<% } %>
</item>
<% } %>
<% } %>
</channel>
</rss>

View File

@@ -1,23 +0,0 @@
<rss version="2.0">
<channel>
<title><%= config.site_name %></title>
<link><%= config.site_url %></link>
<description><%= config.site_description %></description>
<% for (let postID = posts.length-1; postID >= 0; postID--) { %>
<% if (posts[postID]["deleted"] != true) { %>
<item>
<title><%= posts[postID]["title"] %></title>
<link><%= config.site_url %>/post/<%= postID %></link>
<description><![CDATA[<%- (posts[postID]["content"]) %>]]></description>
<guid isPermaLink="true"><%= config.site_url %>/post/<%= postID %></guid>
<pubDate><%= func.unix_time_to_date_format(posts[postID]['pubdate'],'EEE, dd MMM yyyy HH:mm:ss') %></pubDate>
<author><%= users[posts[postID]['userID']]['prettyname'] %></author>
<% tags = posts[postID]['tags'].split(',') %>
<% for (let tag_index = 0; tag_index < tags.length; tag_index++) { %>
<category><![CDATA[<%= tags[tag_index] %>]]></category>
<% } %>
</item>
<% } %>
<% } %>
</channel>
</rss>

View File

@@ -0,0 +1,24 @@
<?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_atom_date(getUnixTime(new Date())) %></updated>
<id><%= config.site_url %></id>
<% for (let postID = posts.length-1; postID >= 0; postID--) { %>
<% if (posts[postID]["userID"] == userID) { %>
<% if (posts[postID]["deleted"] != true) { %>
<entry>
<title><%= posts[postID]["title"] %></title>
<link><%= config.site_url %>/post/<%= postID %></link>
<summary><![CDATA[<%- func.render_md(posts[postID]["content"]) %>]]></summary>
<guid isPermaLink="true"><%= config.site_url %>/post/<%= postID %></guid>
<pubDate><%# func.unix_time_to_atom_date(posts[postID]['pubdate']) %></pubDate>
<% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %>
<category><![CDATA[<%= posts[postID]['tags'][tag_index] %>]]></category>
<% } %>
</entry>
<% } %>
<% } %>
<% } %>
</feed>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="<%= config.charset %>" ?>
<rss version="2.0">
<channel>
<title><%= config.site_name %></title>
<link><%= config.site_url %></title>
<description><%= config.site_description %></description>
<% for (let postID = posts.length-1; postID >= 0; postID--) { %>
<% if (posts[postID]["userID"] == userID) { %>
<% if (posts[postID]["deleted"] != true) { %>
<item>
<title><%= posts[postID]["title"] %></title>
<link><%= config.site_url %>/post/<%= postID %></link>
<description><![CDATA[<%- func.render_md(posts[postID]["content"]) %>]]></description>
<guid isPermaLink="true"><%= config.site_url %>/post/<%= postID %></guid>
<pubDate><%= func.unix_time_to_rss_date(posts[postID]['pubdate']) %></pubDate>
<% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %>
<category><![CDATA[<%= posts[postID]['tags'][tag_index] %>]]></category>
<% } %>
</item>
<% } %>
<% } %>
<% } %>
</channel>
</rss>