function parseComment(comment) {
let author = data.users[comment[0]]
if (author === undefined) {
author = {}
}
const html = `
`
return html
}
function parsePost(post) {
let author = data.users[post.user_id]
if (author === undefined) {
author = {}
}
const html = `
${post.content.replace(/\n/g,'
')}
${Object.keys(post.likes).map(k => post.likes[k]).filter(v => v !== false).length} Likes
Like
Comment
`
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 id = parseInt(span.parentElement.parentElement.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)
}
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([data.user.user_id, 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 != 201) {
error.innerHTML = response.msg
return;
}
error.innerHTML = '';
data.posts.unshift({
post_id: response.json.post_id,
user_id: data.user.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 = `
${comment[1]}