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!master
parent
25fc801fd3
commit
a5f385971f
|
@ -0,0 +1,5 @@
|
||||||
|
# Mico logs:
|
||||||
|
log.txt
|
||||||
|
|
||||||
|
# Mby someday the WS-access logs
|
||||||
|
# Mby storage
|
|
@ -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
|
|
@ -9,8 +9,7 @@ console.log(
|
||||||
new themes( "light" )
|
new themes( "light" )
|
||||||
|
|
||||||
// get newest 10 blog articles from api
|
// get newest 10 blog articles from api
|
||||||
$.get("/posts?api&featured&len=10", d => {
|
$.get("/posts?api&featured&len=10", json => {
|
||||||
let json = JSON.parse(d)
|
|
||||||
if ( json.type != "s" )
|
if ( json.type != "s" )
|
||||||
return false
|
return false
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
2021/08/05 00:07:43 Micro started
|
|
||||||
2021/08/05 00:08:40 Cleared cursors: 1
|
|
|
@ -1 +1,16 @@
|
||||||
{"type":"s","content":[{"title":"My second Post","author":"derzombiiie","desc":"Lorem ipsum dolor sit.","href":"#","tags":["testing","$$$"],"id":1}]}
|
{
|
||||||
|
"title":"My second Post",
|
||||||
|
"author":"derzombiiie",
|
||||||
|
"desc":"Lorem ipsum dolor sit.",
|
||||||
|
"href":"#",
|
||||||
|
"tags":[
|
||||||
|
"testing",
|
||||||
|
"$$$"
|
||||||
|
],
|
||||||
|
"rating":{
|
||||||
|
"+":10000,
|
||||||
|
"-":-10
|
||||||
|
}
|
||||||
|
"create":1628198909950
|
||||||
|
"id":1
|
||||||
|
}
|
72
node/app.js
72
node/app.js
|
@ -3,6 +3,7 @@ const express = require("express")
|
||||||
const filestuff = require("./filestuff")
|
const filestuff = require("./filestuff")
|
||||||
var con = require("./console")
|
var con = require("./console")
|
||||||
const JSONdb = require("simple-json-db")
|
const JSONdb = require("simple-json-db")
|
||||||
|
var posts = require("./posts")
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
const port = 5500
|
const port = 5500
|
||||||
|
@ -14,6 +15,60 @@ app.use("/static", express.static("html"))
|
||||||
|
|
||||||
// console commands:
|
// console commands:
|
||||||
con.registercmd( "stop", () => shutdown() )
|
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:
|
// shutdown:
|
||||||
function shutdown() {
|
function shutdown() {
|
||||||
|
@ -21,19 +76,20 @@ function shutdown() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// readout jsonDB:
|
// readout jsonDB:
|
||||||
const posts = new JSONdb("storage/posts.json")
|
const postsDB = new JSONdb("storage/posts.json")
|
||||||
|
posts.init(postsDB)
|
||||||
|
posts.rank()
|
||||||
|
|
||||||
// posts:
|
// posts:
|
||||||
app.get("/posts", (req, res) => {
|
app.get("/posts", (req, res) => {
|
||||||
if( typeof( req.query.api ) != "undefined" ) {
|
if( typeof( req.query.api ) != "undefined" ) {
|
||||||
res.type( "application/json" )
|
res.type( "application/json" )
|
||||||
if( typeof(req.query.featured) != "undefined") {
|
if( ! ( req.query.len < 50 ) ) {
|
||||||
if( ! ( req.query.len < 50 ) ) {
|
res.status( 400 )
|
||||||
res.status( 400 )
|
res.end( JSON.stringify({"type":"err","text":"no len or to high specified"}) )
|
||||||
res.end( JSON.stringify({"type":"err","text":"no len or to high specified"}) )
|
} else {
|
||||||
} else {
|
res.status( 200 )
|
||||||
|
res.send(`{"type":"s","content":${JSON.stringify(posts.read(10, "featured"))}}`)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res.status( 400 )
|
res.status( 400 )
|
||||||
|
|
|
@ -47,4 +47,7 @@ this.registerdcmds["eval"] = () => {
|
||||||
this.registerdcmds["exit"] = () => {
|
this.registerdcmds["exit"] = () => {
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
this.registerdcmds["clear"] = () => {
|
||||||
|
console.log('\033[2J')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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)
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue