diff --git a/src/data.js b/src/data.js index e72640e..8eedbd4 100644 --- a/src/data.js +++ b/src/data.js @@ -5,6 +5,26 @@ const func = require('./functions.js') const config = require("../config.json") 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 let search_results = {"posts": [], "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 + } + } +} diff --git a/src/functions.js b/src/functions.js index 7fa8b70..e06f979 100644 --- a/src/functions.js +++ b/src/functions.js @@ -124,12 +124,3 @@ export function render_md(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'); - } -}; diff --git a/src/routes/standard_pages.js b/src/routes/standard_pages.js index dadeb0c..f67a3dc 100644 --- a/src/routes/standard_pages.js +++ b/src/routes/standard_pages.js @@ -12,7 +12,7 @@ const router = express.Router(); router.get("/", (req,res) => { // Increment the hitcount if (config.enable_hitcount) { - func.increment_hitcount() + data.increment_hitcount() } res.render("pages/timeline", @@ -62,6 +62,9 @@ router.get("/user/:username", (req, res) => { router.get("/post/:post_index", (req, res) => { const postID = parseInt(req.params.post_index) 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 res.render("partials/message", { message: locale.post_doesnt_exist, diff --git a/views/headers/site_wide.ejs b/views/headers/site_wide.ejs index b6abf11..c363065 100644 --- a/views/headers/site_wide.ejs +++ b/views/headers/site_wide.ejs @@ -1,6 +1,6 @@ <%= locale.home_page %> <%= locale.site_index %> <%= locale.new_post %> -
+

<%- config.seperator %> diff --git a/views/posts/post.ejs b/views/posts/post.ejs index 8ae3d41..efc45fc 100644 --- a/views/posts/post.ejs +++ b/views/posts/post.ejs @@ -24,6 +24,11 @@
<%= locale.last_modified %>: <%= func.unix_time_to_date_format(post.pubdate) %>
+ <% if (config.enable_hitcount == true) { %> +
+ Hitcount: <%- post.hitcount %> +
+ <% } %>