diff --git a/public/admin.html b/public/admin.html
index cd79337..2d2bcd6 100644
--- a/public/admin.html
+++ b/public/admin.html
@@ -5,17 +5,16 @@
+
-
-
- ${post.content.replace(/\n/g,'
')}
-
-
- ${post.likes.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 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])
-}
+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'
async function post() {
const text = document.getElementById("text").value.trim()
@@ -133,7 +15,7 @@ async function post() {
error.innerHTML = '';
let post = {
post_id: response.json.post_id,
- user_id: data.user.user_id,
+ user_id: data.self.user_id,
date: Date.now(),
content: text,
likes: [],
@@ -145,113 +27,127 @@ async function post() {
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 = `
-
-
About
-
- Name: ${data.user.firstname + ' ' + data.user.lastname}
- Email: ${data.user.email}
- Gender: ${data.user.gender}
- Birthday: ${parseMonth(data.user.month) + ' ' + data.user.day + ', ' + data.user.year}
- User ID: ${data.user.user_id}
-
-
- `
-
- append(about)
-
- const popup = `
-
- `
-
- append(popup)
-
}
-async function logout_button() {
- const response = await logout()
- if (response.status != 200) return;
- location.href = '/login'
-}
-
-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)
- }
-}
-
-var posts = true
var isself = false
var page = 0
-var id
+const data = {
+ self: {},
+ user: {},
+ users: {},
+ posts: []
+}
+
+async function load(id) {
+
+ if (id === undefined) {
+ id = data.user.user_id
+ }
-async function load() {
const posts = (await loadusersposts(id, page)).json
- if (posts.length === 0) {
- page = -1
- remove('load')
+
+ if (posts.length < 1) {
+ document.getElementsByClassName('loadp')[0].remove()
+ return []
} else {
page++
}
- let batch = []
+
+ const batch = Array.from(new Set(crawl('user_id', posts))).filter(id => data.users[id] == undefined)
+
if (!isself) {
batch.push(id)
}
- 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 (batch.length != 0) {
+ const users = await loadusers(batch).json
+ for (const user of users) {
+ data.users[user.user_id] = user
}
- 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
}
@@ -180,21 +224,26 @@ async function load() {
async function init() {
let request = await loadself()
+
if (request.status === 429) {
- header(false, false)
+ let new_body =
+ body({},
+ ...header(false, false)
+ )
+
+ document.body.replaceWith(new_body)
throw new Error("Rate limited");
}
data.self = request.json;
data.users[data.self.user_id] = data.self
-
- header(false, false, data.self.user_id)
var params = {};
for (const [key, value] of new URLSearchParams(location.search)) {
params[key] = value
}
+ let id;
if (params.id !== undefined && !isNaN(params.id)) {
id = parseInt(params.id);
} else {
@@ -203,9 +252,11 @@ async function init() {
isself = id === data.self.user_id
- const posts = await load()
+ const posts = await load(id)
data.posts.push(... posts)
+
data.user = data.users[id]
+
render()
}
diff --git a/public/login.html b/public/login.html
index e0428b9..ce4d0ff 100644
--- a/public/login.html
+++ b/public/login.html
@@ -4,8 +4,7 @@
${comment[1]}
- -