function parseComment(comment) { let author = data.users[comment[0]] if (author === undefined) { author = {} } const html = `
${author.firstname + ' ' + author.lastname}

${comment[1]}

` return html } function parsePost(post) { let author = data.users[post.user_id] if (author === undefined) { author = {} } const html = `
${author.firstname + ' ' + author.lastname} ${parseDate(new Date(post.date))}

${post.content.replace(/\n/g,'
')}

${post.likes.length} Likes
Like Comment
${post.comments.map(parseComment).join('')}
` return html } function getPost(post_id) { for (let i = 0; i < data.posts.length; i++) { if (data.posts[i].post_id === post_id) { return i } } return -1 } async function like(span) { const container = span.parentElement.parentElement; const id = parseInt(container.getAttribute('postid')) const post = data.posts[getPost(id)] const index = post.likes.indexOf(data.user.user_id) const current = index !== -1 const response = await postlike(id, !current) if (response.status != 200) return; if (current) { post.likes.splice(index, 1) } else { post.likes.push(data.user.user_id) } const buttons = container .getElementsByClassName("postbuttons")[0] .getElementsByClassName("likeclicky")[0] .getElementsByClassName("liketoggle") if (current) { buttons[0].classList.remove("blue") buttons[1].classList.remove("bltext") } else { buttons[0].classList.add("blue") buttons[1].classList.add("bltext") } container.getElementsByClassName("likes")[0].innerHTML = post.likes.length + " Likes" } async function comment(event) { event.preventDefault(); const text = event.target.elements.text.value.trim(); if (text.length < 1) return; const container = event.target.parentElement.parentElement.parentElement; const post_id = parseInt(container.getAttribute('postid')) var index = getPost(post_id); if (index === -1) return; const response = await postcomment(post_id, text) if (response.status != 200) return; event.target.elements.text.value = ''; let new_comment = [data.user.user_id, text] data.posts[index].comments.push(new_comment) let comments = container.getElementsByClassName("comments")[0] prepend(parseComment(new_comment), comments, comments.getElementsByClassName("commentsubmit")[0]) } async function post() { const text = document.getElementById("text").value.trim() const error = document.getElementsByClassName('error')[0] const posts_block = document.getElementById("posts") if (text.length < 1) return; const response = await createpost(text); if (response.status != 201) { error.innerHTML = response.msg return; } error.innerHTML = ''; let post = { post_id: response.json.post_id, user_id: data.user.user_id, date: Date.now(), content: text, likes: [], comments: [] } data.posts.unshift(post) let html = parsePost(post) prepend(html, posts_block) document.getElementById('popup').classList.add('hidden') } async function loadMore() { const posts = await load() data.posts.push(... posts) const posts_block = document.getElementById("posts") for (p of posts) { append(parsePost(p), posts_block) } } function render() { const html = `
${data.posts.map(p => parsePost(p)).join('')}
` append(html) const popup = ` ` append(popup) const load = `
Load more posts
` append(load) } var page = 0 const data = { user: {}, users: {}, posts: [] } async function load() { const posts = (await loadpostspage(page)).json if (posts.length === 0) { page = -1 remove('load') return [] } else { page++ } const batch = [] for (const post of posts) { for(const comment of post.comments) { if (data.users[comment[0]] !== undefined) continue if (batch.includes(comment[0])) continue batch.push(comment[0]) } if (data.users[post.user_id] !== undefined) continue if (batch.includes(post.user_id)) continue batch.push(post.user_id) } const users = batch.length == 0 ? [] : (await loadusers(batch)).json for (const user of users) { data.users[user.user_id] = user } return posts } async function init() { header(true, false) data.user = (await loadself()).json data.users[data.user.user_id] = data.user const posts = await load() data.posts.push(... posts) render() }