xssbook/public/js/home.js

164 lines
4.6 KiB
JavaScript
Raw Normal View History

2023-02-12 19:11:50 +00:00
import { div, pfp, p, parse, button, body, a, textarea, span, crawl } from './main.js'
import { loadself, loadpostspage, createpost, loadusers } from './api.js'
import { parsePost, header } from './components.js'
2023-01-26 22:29:16 +00:00
async function post() {
const text = document.getElementById("text").value.trim()
const error = document.getElementsByClassName('error')[0]
const posts_block = document.getElementById("posts")
2023-01-26 22:29:16 +00:00
if (text.length < 1) return;
const response = await createpost(text);
if (response.status != 201) {
error.innerHTML = response.msg
return;
}
error.innerHTML = '';
let post = {
2023-01-27 21:04:04 +00:00
post_id: response.json.post_id,
2023-02-12 19:11:50 +00:00
user_id: data.self.user_id,
2023-01-26 22:29:16 +00:00
date: Date.now(),
content: text,
likes: [],
comments: []
}
data.posts.unshift(post)
2023-02-15 03:16:29 +00:00
let html = parsePost(post, data.users, data.self)
posts_block.insertBefore(
html,
posts_block.firstChild
)
document.getElementById('popup').classList.add('hidden')
}
2023-01-26 22:29:16 +00:00
function render() {
2023-02-12 19:11:50 +00:00
let new_body =
body({},
...header(true, false, data.self.user_id),
div({id: 'create'},
div({class: 'create'},
2023-02-14 03:41:09 +00:00
a({class: 'pfp', href: '/profile'},
2023-02-12 19:11:50 +00:00
pfp(data.self.user_id)
),
button({class: 'pfp'},
p({class: 'gtext', onclick: () => document.getElementById('popup').classList.remove('hidden')},
parse(`What' on your mind, ${data.self.firstname}`)
)
)
)
),
div({id: 'posts'},
...data.posts.map(p => parsePost(p, data.users, data.self))
),
div({id: 'popup', class: 'hidden'},
div({class: 'createpost'},
div({class: 'close', onclick: () => document.getElementById('popup').classList.add('hidden')}),
span({class: 'ltext ctext bold'},
parse('Create post')
),
div({class: 'fullline'}),
div({class: 'postheader'},
2023-02-14 03:41:09 +00:00
a({class: 'pfp', style: 'cursor: auto', href: '/profile'},
2023-02-12 19:11:50 +00:00
pfp(data.self.user_id)
),
div({class: 'postname'},
span({class: 'bold'},
parse(data.self.firstname + ' ' + data.self.lastname)
),
span({class: 'gtext mtext'},
parse('Now')
)
)
),
textarea({type: 'text', name: 'text', id: 'text', placeholder: `What's on your mind, ${data.self.firstname}?`}),
span({class: 'error ctext', style: 'padding-bottom: 15px; margin-top: -30px'}),
button({class: 'primary', onclick: post},
parse('Post')
)
)
),
div({id: 'load'},
2023-02-14 03:41:09 +00:00
span({class: 'blod gtext', onclick: async () => {
2023-02-12 19:11:50 +00:00
const posts = await load()
data.posts.push(... posts)
2023-01-26 22:29:16 +00:00
2023-02-12 19:11:50 +00:00
const el = document.getElementById("posts")
2023-01-26 22:29:16 +00:00
2023-02-12 19:11:50 +00:00
for (const post of posts) {
el.appendChild(
parsePost(post, data.users, data.self)
)
}
}},
parse('Load more posts')
)
)
)
document.body.replaceWith(new_body)
2023-01-26 22:29:16 +00:00
}
var page = 0
const data = {
2023-02-12 19:11:50 +00:00
self: {},
2023-01-26 22:29:16 +00:00
users: {},
posts: []
}
async function load() {
const posts = (await loadpostspage(page)).json
2023-02-12 19:11:50 +00:00
2023-01-26 22:29:16 +00:00
if (posts.length === 0) {
2023-02-12 19:11:50 +00:00
document.getElementById('load').remove()
return []
2023-01-26 22:29:16 +00:00
} else {
page++
}
2023-02-12 19:11:50 +00:00
const batch = Array.from(new Set(crawl('user_id', posts))).filter(id => data.users[id] == undefined)
if (batch.length != 0) {
const users = (await loadusers(batch)).json
for (const user of users) {
data.users[user.user_id] = user
2023-01-26 22:29:16 +00:00
}
}
2023-02-12 19:11:50 +00:00
return posts
2023-01-26 22:29:16 +00:00
}
async function init() {
2023-02-12 19:11:50 +00:00
2023-01-31 05:00:13 +00:00
let request = (await loadself());
2023-02-12 19:11:50 +00:00
2023-01-31 05:00:13 +00:00
if (request.status === 429) {
2023-02-12 19:11:50 +00:00
let new_body =
body({},
...header(true, false)
)
document.body.replaceWith(new_body)
2023-01-31 05:00:13 +00:00
throw new Error("Rate limited");
}
2023-02-12 19:11:50 +00:00
data.self = request.json
2023-02-13 16:59:00 +00:00
if (request.json == undefined) {
location.href = '/login'
return
}
2023-02-12 19:11:50 +00:00
data.users[data.self.user_id] = data.self
const posts = await load()
data.posts.push(... posts)
2023-02-12 19:11:50 +00:00
render()
2023-02-12 19:11:50 +00:00
}
init()