import { createRequire } from "module"; const require = createRequire(import.meta.url); 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','id', postID); if (typeof post.hitcount != 'undefined') { post.hitcount += 1; writedata('posts', post, postID) return 0 } else { 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 if (type.includes('post')) { let list = getdata('posts'); list.forEach((element,index) => { if (typeof element.deleted == 'undefined' || element.deleted == false) { if (element.content.includes(term)) { search_results.posts.push(element) } else if (element.title.includes(term)) { search_results.posts.push(element) } else if (element.tags.toString().includes(term)) { search_results.posts.push(element) }; }; }); } if (type.includes('user')) { let list = getdata('users'); list.forEach((element,index) => { if (typeof element.deleted == 'undefined' || element.deleted == false) { if (element.username.includes(term)) { search_results.users.push(element) } else if (element.prettyname.includes(term)) { search_results.users.push(element) } else if (element.description.includes(term)) { search_results.users.push(element) }; }; }); } return search_results; }; export function getdata(data_type, key=-1, value=-1) { let result = undefined switch (config["data_storage"]) { case 'json': switch (data_type) { case 'users': case 'posts': case 'comments': result = func.require_module(`../data/${data_type}.json`) if (key != -1) { return result[func.find_key_value_pair(result, key, value)] return -1 // This index doesn't exist } return result break; case 'hitcount': result = func.require_module('../data/data.json') // This file is actually called data.json return result["hitcount"] break; default: console.log("Error, invalid requested") return -1 break; } break; // NOT YET WORKING! case 'mysql': 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; }); } }); } } 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 } } }