diff options
author | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-02 11:29:37 -0500 |
---|---|---|
committer | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-02 11:29:37 -0500 |
commit | 18a27f62befb0c0d2e3b175f08f6088f10b25063 (patch) | |
tree | 1d8c0d00685f9374fdb3705062c08265d7e1fdb0 /public | |
parent | add caching header (diff) | |
download | xssbook-18a27f62befb0c0d2e3b175f08f6088f10b25063.tar.gz xssbook-18a27f62befb0c0d2e3b175f08f6088f10b25063.tar.bz2 xssbook-18a27f62befb0c0d2e3b175f08f6088f10b25063.zip |
loading by offset on profile
Diffstat (limited to 'public')
-rw-r--r-- | public/js/api.js | 4 | ||||
-rw-r--r-- | public/js/profile.js | 78 |
2 files changed, 52 insertions, 30 deletions
diff --git a/public/js/api.js b/public/js/api.js index b2ea597..886a017 100644 --- a/public/js/api.js +++ b/public/js/api.js @@ -59,8 +59,8 @@ const loadpostspage = async (page) => { return await request('/posts/page', {page}) } -const loadusersposts = async (user_id) => { - return await request('/posts/user', {user_id}) +const loadusersposts = async (user_id, page) => { + return await request('/posts/user', {user_id, page}) } const loadusers = async (ids) => { diff --git a/public/js/profile.js b/public/js/profile.js index 083464f..40e6354 100644 --- a/public/js/profile.js +++ b/public/js/profile.js @@ -88,8 +88,12 @@ function render() { <div id="posts" class="${posts ? '' : 'hidden'}"> ${data.posts.map(p => parsePost(p)).join('')} </div> + <div id="load"> + <a class="bold gtext" onclick="loadMore()">Load more posts</a> + </div> ` + append(postsh) const about = ` @@ -129,18 +133,52 @@ async function logout_button() { 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 async function load() { - - var params = {}; - for (const [key, value] of new URLSearchParams(location.search)) { - params[key] = value + const posts = (await loadusersposts(id, page)).json + if (posts.length === 0) { + page = -1 + remove('load') + } else { + page++ + } + let batch = [] + 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 (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 +} - let request = await loadself() +async function init() { + let request = await loadself() if (request.status === 429) { header(false, false) throw new Error("Rate limited"); @@ -148,12 +186,15 @@ async function load() { data.self = request.json; data.users[data.self.user_id] = data.self - let id; header(false, false, data.self.user_id) - if (params.id !== undefined && !isNaN(params.id)) { + var params = {}; + for (const [key, value] of new URLSearchParams(location.search)) { + params[key] = value + } + if (params.id !== undefined && !isNaN(params.id)) { id = parseInt(params.id); } else { id = data.self.user_id @@ -161,29 +202,10 @@ async function load() { isself = id === data.self.user_id - const posts = (await loadusersposts(id)).json + const posts = await load() data.posts.push(... posts) - const batch = [] - - 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 (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 - } data.user = data.users[id] render() } -load()
\ No newline at end of file +init()
\ No newline at end of file |