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
derzombiiie 2021-08-06 02:06:41 +02:00
parent 25fc801fd3
commit a5f385971f
11 changed files with 259 additions and 13 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Mico logs:
log.txt
# Mby someday the WS-access logs
# Mby storage

14
TODO.md Normal file
View File

@ -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

View File

@ -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 {

View File

@ -1,2 +0,0 @@
2021/08/05 00:07:43 Micro started
2021/08/05 00:08:40 Cleared cursors: 1

View File

@ -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
}

BIN
log Normal file

Binary file not shown.

View File

@ -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 )

View File

@ -47,4 +47,7 @@ this.registerdcmds["eval"] = () => {
this.registerdcmds["exit"] = () => {
process.exit(1)
}
this.registerdcmds["clear"] = () => {
console.log('\033[2J')
}

82
node/posts.js Normal file
View File

@ -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
}

37
sort.js Normal file
View File

@ -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)

View File

@ -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
}
}