Changing how data is requested in DEV branch
This commit is contained in:
43
src/data.js
43
src/data.js
@@ -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,11 +94,10 @@ export function getdata(table_name, key=-1, value=-1) {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result[func.find_key_value_pair(result, key, value)]
|
let return_list = func.find_key_value_pair(result,key,value)
|
||||||
return -1 // This index doesn't exist
|
return return_list
|
||||||
}
|
}
|
||||||
return result.slice(- config['data_request_limit'])
|
return result
|
||||||
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"]
|
||||||
@@ -109,37 +108,9 @@ export function getdata(table_name, key=-1, value=-1) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
// NOT YET WORKING!
|
default:
|
||||||
case 'mysql':
|
console.log("Error, invalid database management system")
|
||||||
const mysql = require('mysql');
|
return 1
|
||||||
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;
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,8 +57,9 @@ 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)
|
export function render_tags(tags_string)
|
||||||
{
|
{
|
||||||
|
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] == "")
|
||||||
@@ -140,11 +141,15 @@ 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++) {
|
||||||
if (data_array[i][key] == value) {
|
let element = data_array[i][key]
|
||||||
return i
|
if (element == value
|
||||||
|
|| (Array.isArray(element) && element.includes(value))) {
|
||||||
|
return_list.push(data_array[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1
|
return return_list
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ 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()
|
||||||
@@ -19,7 +20,7 @@ router.get("/", (req,res) => {
|
|||||||
{
|
{
|
||||||
config,
|
config,
|
||||||
locale,
|
locale,
|
||||||
posts: data.getdata("posts"),
|
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"),
|
||||||
@@ -33,7 +34,7 @@ router.get("/", (req,res) => {
|
|||||||
// Users
|
// Users
|
||||||
router.get("/user/:username", (req, res) => {
|
router.get("/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 user = data.getdata('users', 'id', userID)[0]
|
||||||
if (userID != -1) {
|
if (userID != -1) {
|
||||||
res.render("pages/user",
|
res.render("pages/user",
|
||||||
{
|
{
|
||||||
@@ -61,7 +62,7 @@ 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)
|
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,
|
||||||
|
|||||||
@@ -18,9 +18,10 @@ router.get("/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", {
|
||||||
config,
|
config,
|
||||||
posts: data.getdata('posts'),
|
posts: data.getdata('posts'),
|
||||||
|
users: data.getdata('users'),
|
||||||
func,
|
func,
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
@@ -28,7 +29,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 userID = func.get_userID(username);
|
const user = data.getdata('users', 'username', 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,
|
||||||
@@ -37,11 +38,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/user_rss", {
|
res.render("syndication/rss", {
|
||||||
config,
|
config,
|
||||||
posts: data.getdata('posts'),
|
posts: data.getdata('posts'),
|
||||||
|
users: user,
|
||||||
func,
|
func,
|
||||||
userID,
|
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -58,6 +59,7 @@ 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,
|
||||||
})
|
})
|
||||||
@@ -78,6 +80,7 @@ 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,
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
<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]}); %>
|
||||||
<% } %>
|
<% } %>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="<%= config.charset %>" ?>
|
<?xml version="1.0" encoding="<%= config.charset %>" ?>
|
||||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||||
<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>
|
||||||
<updated><%= func.unix_time_to_atom_date(getUnixTime(new Date())) %></updated>
|
<updated><%= func.unix_time_to_atom_date(getUnixTime(new Date())) %></updated>
|
||||||
<id><%= config.site_url %></id>
|
<id><%= config.site_url %></id>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<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) { %>
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
<description><![CDATA[<%- func.render_md(posts[postID]["content"]) %>]]></description>
|
<description><![CDATA[<%- func.render_md(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_rss_date(posts[postID]['pubdate']) %></pubDate>
|
||||||
|
<author><%= users[posts[postID]['userID']]['prettyname'] %></author>
|
||||||
<% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %>
|
<% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %>
|
||||||
<category><![CDATA[<%= posts[postID]['tags'][tag_index] %>]]></category>
|
<category><![CDATA[<%= posts[postID]['tags'][tag_index] %>]]></category>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
<description><![CDATA[<%- func.render_md(posts[postID]["content"]) %>]]></description>
|
<description><![CDATA[<%- func.render_md(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_rss_date(posts[postID]['pubdate']) %></pubDate>
|
||||||
|
<author><%= users[posts[postID]['userID']]['prettyname'] %></author>
|
||||||
<% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %>
|
<% for (let tag_index = 0; tag_index < posts[postID]['tags'].length; tag_index++) { %>
|
||||||
<category><![CDATA[<%= posts[postID]['tags'][tag_index] %>]]></category>
|
<category><![CDATA[<%= posts[postID]['tags'][tag_index] %>]]></category>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|||||||
Reference in New Issue
Block a user