function parseDate(date) { var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear() + ' ' + date.toLocaleTimeString(); } function parseComment(comment) { const author = data.users[comment.user] if (author === undefined) { author = {} } const html = `
${author.first + ' ' + author.last}

${comment.content}

` return html } function parsePost(post) { const author = data.users[post.user] if (author === undefined) { author = {} } const html = `
${author.first + ' ' + author.last} ${parseDate(new Date(post.date))}

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

${Object.keys(post.likes).map(k => post.likes[k]).filter(v => v !== false).length} Likes
Like Comment
${post.comments.map(parseComment).join('')}
` return html } function getPost(id) { for (let i = 0; i < data.posts.length; i++) { if (data.posts[i].id === id) { return id } } return -1 } async function like(span) { const id = parseInt(span.parentElement.parentElement.getAttribute('postid')) const post = data.posts[getPost(id)] const current = post.likes[data.user.id] const response = await postlike(id, !current) if (response.status != 200) return; post.likes[data.user.id] = !currentg render() } async function comment(event) { event.preventDefault(); const text = event.target.elements.text.value.trim(); if (text.length < 1) return; const id = parseInt(event.target.parentElement.parentElement.parentElement.getAttribute('postid')) var index = getPost(id); if (index === -1) return; const response = await postcomment(id, text) if (response.status != 200) return; event.target.elements.text.value = ''; data.posts[index].comments.push({ user: data.user.id, content: text }) render() } async function post() { const text = document.getElementById("text").value.trim() const error = document.getElementsByClassName('error')[0] if (text.length < 1) return; const response = await createpost(text); if (response.status != 200) { error.innerHTML = response.msg return; } error.innerHTML = ''; data.posts.unshift({ id: response.msg, user: data.user.id, date: Date.now(), content: text, likes: [], comments: [] }) render() } function render() { const html = `
${data.posts.map(p => parsePost(p)).join('')}
` add(html, 'posts') const popup = ` ` add(popup, 'popup') const load = `
Load more posts
` if (page !== -1) { add(load, 'load') } else { remove('load') } } var page = 0 const data = { user: {}, users: {}, posts: [] } async function load() { const posts = (await loadposts(page)).json if (posts.length === 0) { page = -1 } else { page++ } data.posts.push(... posts) const batch = [] for (const post of posts) { for(const comment of post.comments) { if (data.users[comment.user] !== undefined) continue if (batch.includes(comment.user)) continue batch.push(comment.user) } if (data.users[post.user] !== undefined) continue if (batch.includes(post.user)) continue batch.push(post.user) } const users = (await loadusers(batch)).json for (const id in users) { data.users[id] = users[id] } render() } async function init() { header(true, false) data.user = (await loadself()).json data.users[data.user.id] = data.user load() }