Created a per-post hitcount as well a writedata() function that can

write to a particular index or to a whole data type
This commit is contained in:
2025-10-02 13:34:55 +01:00
parent 17919e3078
commit 2ada1d970f
5 changed files with 57 additions and 11 deletions

View File

@@ -5,6 +5,26 @@ const func = require('./functions.js')
const config = require("../config.json") const config = require("../config.json")
const fs = require("fs") const fs = require("fs")
// Literally just +1 to the hitcount
export function increment_hitcount(postID = -1) { // -1 Means it will increment the timeline hitcount
if (config.data_storage == 'json') {
if (postID == -1) {
let hitcount = getdata('hitcount');
hitcount += 1
writedata('hitcount', hitcount);
}
else {
let post = getdata('posts', postID);
if (typeof post.hitcount != 'undefined') {
post.hitcount += 1;
writedata('posts', post, postID)
return 0
}
return 1
}
}
};
export function searchdata(term, type) { // Searches users and posts for any matches export function searchdata(term, type) { // Searches users and posts for any matches
let search_results = {"posts": [], "users": []}; let search_results = {"posts": [], "users": []};
// Search users // Search users
@@ -101,3 +121,30 @@ export function getdata(data, index=-1) {
}); });
} }
} }
export function writedata(data, data_to_write, index=-1) {
if (config["data_storage"] == "json") {
if (data == "posts" || data == 'users' || data == 'comments') {
if (index == -1) {
fs.writeFileSync(`../data/${data}.json`, JSON.stringify(data_to_write), 'utf-8')
return 0
}
else if (index >= 0) {
let result = getdata(data);
result[index] = data_to_write;
fs.writeFileSync(`../data/${data}.json`, JSON.stringify(result), 'utf-8')
return 0
}
return 1
}
else if (data == "hitcount") {
let other_data = func.require_module('../data/data.json') // This file is actually called data.json
other_data.hitcount = data_to_write
fs.writeFileSync('../data/data.json', JSON.stringify(other_data), 'utf-8')
}
else {
console.log("Error, invalid requested")
return 1
}
}
}

View File

@@ -124,12 +124,3 @@ export function render_md(content) {
return md.render(content) return md.render(content)
}; };
// Literally just +1 to the hitcount
export function increment_hitcount() {
if (config.data_storage == 'json') {
let other_data = require('../data/data.json');
other_data.hitcount += 1
console.log(`/ Is loaded, hitcount: ${other_data.hitcount}`)
fs.writeFileSync(`../data/data.json`, `${JSON.stringify(other_data)}`, 'utf-8');
}
};

View File

@@ -12,7 +12,7 @@ const router = express.Router();
router.get("/", (req,res) => { router.get("/", (req,res) => {
// Increment the hitcount // Increment the hitcount
if (config.enable_hitcount) { if (config.enable_hitcount) {
func.increment_hitcount() data.increment_hitcount()
} }
res.render("pages/timeline", res.render("pages/timeline",
@@ -62,6 +62,9 @@ router.get("/user/:username", (req, res) => {
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', postID) let post = data.getdata('posts', postID)
if (config.enable_hitcount) {
data.increment_hitcount(postID)
}
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,

View File

@@ -1,6 +1,6 @@
<a href="/"><%= locale.home_page %></a> <a href="/"><%= locale.home_page %></a>
<a href="/index/pages"><%= locale.site_index %></a> <a href="/index/pages"><%= locale.site_index %></a>
<a href="<%= config.new_post_url %>"><%= locale.new_post %></a> <a href="<%= config.new_post_url %>"><%= locale.new_post %></a>
<form method="GET" action="/search" style="display: inline"><label>Search: </label><input type="text" placeholder="🔍" name="q"><input type="submit" value="Submit"></form> <form method="GET" action="/search" style="display: inline"><input type="text" placeholder="🔍" name="q"><input type="submit" value="Search"></form>
<br/> <br/>
<%- config.seperator %> <%- config.seperator %>

View File

@@ -24,6 +24,11 @@
<div id="post-editdate"> <div id="post-editdate">
<i><%= locale.last_modified %>: <%= func.unix_time_to_date_format(post.pubdate) %></i><br/> <i><%= locale.last_modified %>: <%= func.unix_time_to_date_format(post.pubdate) %></i><br/>
</div> </div>
<% if (config.enable_hitcount == true) { %>
<div id='post-hitcount'>
Hitcount: <%- post.hitcount %>
</div>
<% } %>
</div> </div>
<div id="post-commentform"> <div id="post-commentform">