From a5f385971fb2f6b6c2af63591513bdb17490c6d0 Mon Sep 17 00:00:00 2001 From: derzombiiie Date: Fri, 6 Aug 2021 02:06:41 +0200 Subject: [PATCH] Ranking for posts in Newness + voted shell thing now kinda ok "post" command implemented appeval implemented (eval in app.js context "global" or sth) made posts be served 2 api /posts?api&featured&len=NUM is implemented (only call) thats it noone is going to read this anyway lol sos if you acctually read this: 1. lol y? 2. pls send me email derz@elidragon.com i want 2 know if anyone acctually read this! thx! --- .gitignore | 5 +++ TODO.md | 14 ++++++++ html/js/index.js | 3 +- html/log.txt | 2 -- html/posts/index.html | 17 ++++++++- log | Bin 0 -> 88 bytes node/app.js | 72 ++++++++++++++++++++++++++++++++----- node/console.js | 3 ++ node/posts.js | 82 ++++++++++++++++++++++++++++++++++++++++++ sort.js | 37 +++++++++++++++++++ storage/posts.json | 37 +++++++++++++++++++ 11 files changed, 259 insertions(+), 13 deletions(-) create mode 100644 .gitignore create mode 100644 TODO.md delete mode 100644 html/log.txt create mode 100644 log create mode 100644 node/posts.js create mode 100644 sort.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..66a7658 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Mico logs: +log.txt + +# Mby someday the WS-access logs +# Mby storage diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..540412c --- /dev/null +++ b/TODO.md @@ -0,0 +1,14 @@ +# TODO + +- Actual post view site thingy + - markdown interpreter thing + - idk comments + - voting + +- Admin interface + - create posts in MD + - mby with upload (pics + text) + + +- Put up! +- random blog articles diff --git a/html/js/index.js b/html/js/index.js index 2813487..3ddf713 100644 --- a/html/js/index.js +++ b/html/js/index.js @@ -9,8 +9,7 @@ console.log( new themes( "light" ) // get newest 10 blog articles from api -$.get("/posts?api&featured&len=10", d => { - let json = JSON.parse(d) +$.get("/posts?api&featured&len=10", json => { if ( json.type != "s" ) return false else { diff --git a/html/log.txt b/html/log.txt deleted file mode 100644 index d836500..0000000 --- a/html/log.txt +++ /dev/null @@ -1,2 +0,0 @@ -2021/08/05 00:07:43 Micro started -2021/08/05 00:08:40 Cleared cursors: 1 diff --git a/html/posts/index.html b/html/posts/index.html index 305a418..73a6807 100644 --- a/html/posts/index.html +++ b/html/posts/index.html @@ -1 +1,16 @@ -{"type":"s","content":[{"title":"My second Post","author":"derzombiiie","desc":"Lorem ipsum dolor sit.","href":"#","tags":["testing","$$$"],"id":1}]} \ No newline at end of file +{ + "title":"My second Post", + "author":"derzombiiie", + "desc":"Lorem ipsum dolor sit.", + "href":"#", + "tags":[ + "testing", + "$$$" + ], + "rating":{ + "+":10000, + "-":-10 + } + "create":1628198909950 + "id":1 +} \ No newline at end of file diff --git a/log b/log new file mode 100644 index 0000000000000000000000000000000000000000..0c31e20d05fa45cb0be97354d0843e3a5d3ef93f GIT binary patch literal 88 zcmW-YO$vY@7)0MX@DAS%YU9Rhbb<)UKr~7^em2E}`Ge`tOyuPL)yV8zG_|5s*f6Tx XNLm{Qfl*|wD~miSeUE?_lW+{YaE}nX literal 0 HcmV?d00001 diff --git a/node/app.js b/node/app.js index 1135560..8871444 100644 --- a/node/app.js +++ b/node/app.js @@ -3,6 +3,7 @@ const express = require("express") const filestuff = require("./filestuff") var con = require("./console") const JSONdb = require("simple-json-db") +var posts = require("./posts") const app = express() const port = 5500 @@ -14,6 +15,60 @@ app.use("/static", express.static("html")) // console commands: con.registercmd( "stop", () => shutdown() ) +con.registercmd( "getpost", (arg) => console.log(postsDB.get(arg[0])) ) +con.registercmd( "getpostranking", (arg) => console.log(posts.ranking[arg[0]])) +con.registercmd( "appeval", (arg) => {try {console.log(eval(arg.join(" ")))} catch {console.log("Couldn't execute!")}}) + +con.registercmd( "post", (arg => { + let t + sw: + switch (arg[0]) { + case "get": + if (!arg[1]) { + console.log("Nothing to get!") + } else { + console.log(postsDB.get(arg[1])) + } + break + + case "ranking": + if (arg[1]) { + console.log(posts.ranking[arg[1]]) + break sw + } else { + let keys = Object.keys(posts.ranking) + for ( let i = 0 ; i < keys.length ; i++ ) { + console.log(keys[i] + ":") + console.log(posts.ranking[keys[i]]) + } + break sw + } + + case "rank": + console.log("Ranking...") + t = posts.rank() + if (t) console.log(t) + console.log("DONE!") + break + + case "sync": + console.log("syncing...") + t = postsDB.sync() + if (t) console.log(t) + console.log("DONE!") + break + + default: + console.log("Sub-command not found or not supplied!") + + + case "?": + case "h": + case "help": + console.log("Availible cmds: get, ranking, rank") + break + } +})) // shutdown: function shutdown() { @@ -21,19 +76,20 @@ function shutdown() { } // readout jsonDB: -const posts = new JSONdb("storage/posts.json") +const postsDB = new JSONdb("storage/posts.json") +posts.init(postsDB) +posts.rank() // posts: app.get("/posts", (req, res) => { if( typeof( req.query.api ) != "undefined" ) { res.type( "application/json" ) - if( typeof(req.query.featured) != "undefined") { - if( ! ( req.query.len < 50 ) ) { - res.status( 400 ) - res.end( JSON.stringify({"type":"err","text":"no len or to high specified"}) ) - } else { - - } + if( ! ( req.query.len < 50 ) ) { + res.status( 400 ) + res.end( JSON.stringify({"type":"err","text":"no len or to high specified"}) ) + } else { + res.status( 200 ) + res.send(`{"type":"s","content":${JSON.stringify(posts.read(10, "featured"))}}`) } } else { res.status( 400 ) diff --git a/node/console.js b/node/console.js index 2c4978e..dd25b94 100644 --- a/node/console.js +++ b/node/console.js @@ -47,4 +47,7 @@ this.registerdcmds["eval"] = () => { this.registerdcmds["exit"] = () => { process.exit(1) } +this.registerdcmds["clear"] = () => { + console.log('\033[2J') +} diff --git a/node/posts.js b/node/posts.js new file mode 100644 index 0000000..e5ab45b --- /dev/null +++ b/node/posts.js @@ -0,0 +1,82 @@ +this.init = (db) => { + this.db = db + return this +} + +this.ranking = { "hot":[-1], "new":[-1] } +// generating "hot" articles etc. (should run 1 / 0:30h and at startup + on new article) +this.rank = (c) => { + if (!c) { + // read * from db to work on + this.postBUFF = [] + let readall = false + let i = 0 + while ( !readall ) { + if ( this.db.get(i) ) { + this.postBUFF[i] = this.db.get(i); + } else { + readall = true + } + i++ + } + Object.keys(this.ranking).forEach((elem, index) => { + this.rank(elem) + }); + } else { + let filterMap = {} + let result = []; + switch (c) { + case "hot": + filterMap = {} + this.postBUFF.forEach((item) => { + if (!filterMap[item.id] || ( filterMap[item.id].rating["+"] - filterMap[item.id].rating["-"] ) < ( item.rating["+"] - item.rating["-"] ) ) { + filterMap[item.id] = item; + + } + }) + result = [] + + for (let id in filterMap) { + result.push(filterMap[id]); + } + result.sort((a , b) => { + return ( b.rating["+"] - b.rating["-"] ) - ( a.rating["+"] - a.rating["-"] ); + }); + this.ranking.hot = [] + result.forEach((elem, i) => { + this.ranking.hot.push(elem.id) + }) + break + + case "new": + filterMap = {} + this.postBUFF.forEach((item) => { + if (!filterMap[item.id] || filterMap[item.id].create < item.create ) { + filterMap[item.id] = item; + } + }) + result = [] + + for (let id in filterMap) { + result.push(filterMap[id]); + } + result.sort((a , b) => { + return b.create - a.create; + }); + this.ranking.new = [] + result.forEach((elem, i) => { + this.ranking.new.push(elem.id) + }) + break + } + } +} + +this.read = (count, sort) => { + let ret = [] + for ( let i = 0 ; i < count ; i++ ) { + if ( this.db.get( String(i) ) ) + ret.push( this.db.get( String( i ) ) ) + } + return ret +} \ No newline at end of file diff --git a/sort.js b/sort.js new file mode 100644 index 0000000..faebb55 --- /dev/null +++ b/sort.js @@ -0,0 +1,37 @@ +posts = [ + + {"id":4, + "rating":{"+":50,"-":30}}, + + {"id":3, + "rating":{"+":50,"-":990}}, + {"id":1, + "rating":{"+":50,"-":10}}, + {"id":2, + "rating":{"+":50,"-":20}}, +] + +let filterMap = {}; +posts.forEach((item, index) => { + if (!filterMap[item.id] || filterMap[item.id].rating < ( posts.rating["+"] - posts.rating["-"]) ) { + // calc item rating: + let newitem = item + newitem.rating = item.rating["+"] - item.rating["-"] + filterMap[item.id] = newitem; + } +}) + +let result = []; + +for (let id in filterMap) { + result.push(filterMap[id]); +} +result.sort((a , b) => { + return b.rating - a.rating; +}); +let sorted = [] +result.forEach((elem, i) => { + sorted.push(elem.id) +}) + +console.log(sorted) \ No newline at end of file diff --git a/storage/posts.json b/storage/posts.json index e69de29..b905cee 100644 --- a/storage/posts.json +++ b/storage/posts.json @@ -0,0 +1,37 @@ +{ + "0": { + "title": "My 1 Post", + "author": "derzombiiie", + "desc": "Lorem ipsum dolor sit.", + "href": "#", + "tags": [ + "testing", + "$$$" + ], + "rating": { + "+": 99, + "-": 0 + }, + "create": 1, + "id": 0 + }, + "1": { + "title": "My 2 Post", + "author": "derzombiiie", + "desc": "Lorem ipsum dolor sit.", + "href": "#", + "tags": [ + "testing", + "$$$" + ], + "dummy": { + "1": 2 + }, + "rating": { + "+": 999, + "-": 0 + }, + "create": 2, + "id": 1 + } +} \ No newline at end of file