From f00e063861dc9118ee71cb19c90a9ea43bb4d1db Mon Sep 17 00:00:00 2001 From: ari Date: Mon, 21 Apr 2025 20:41:01 -0400 Subject: [PATCH] Frontend works --- deno.lock | 5 +++++ package.json | 3 ++- src/App.svelte | 52 +++++++++++++++++++++++++++++++-------------- src/lib/pdsfetch.ts | 49 +++++++++++++++++++++++++----------------- 4 files changed, 72 insertions(+), 37 deletions(-) diff --git a/deno.lock b/deno.lock index 0616852..724a5c0 100644 --- a/deno.lock +++ b/deno.lock @@ -8,6 +8,7 @@ "npm:@tsconfig/svelte@^5.0.4": "5.0.4", "npm:moment@^2.30.1": "2.30.1", "npm:svelte-check@^4.1.5": "4.1.6_svelte@5.28.1__acorn@8.14.1_typescript@5.7.3", + "npm:svelte-infinite-loading@^1.4.0": "1.4.0", "npm:svelte@^5.23.1": "5.28.1_acorn@8.14.1", "npm:typescript@~5.7.2": "5.7.3", "npm:vite@^6.3.1": "6.3.2_picomatch@4.0.2" @@ -415,6 +416,9 @@ "typescript" ] }, + "svelte-infinite-loading@1.4.0": { + "integrity": "sha512-Jo+f/yr/HmZQuIiiKKzAHVFXdAUWHW2RBbrcQTil8JVk1sCm/riy7KTJVzjBgQvHasrFQYKF84zvtc9/Y4lFYg==" + }, "svelte@5.28.1_acorn@8.14.1": { "integrity": "sha512-iOa9WmfNG95lSOSJdMhdjJ4Afok7IRAQYXpbnxhd5EINnXseG0GVa9j6WPght4eX78XfFez45Fi+uRglGKPV/Q==", "dependencies": [ @@ -476,6 +480,7 @@ "npm:@tsconfig/svelte@^5.0.4", "npm:moment@^2.30.1", "npm:svelte-check@^4.1.5", + "npm:svelte-infinite-loading@^1.4.0", "npm:svelte@^5.23.1", "npm:typescript@~5.7.2", "npm:vite@^6.3.1" diff --git a/package.json b/package.json index 9f84465..1db6461 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "@atcute/bluesky": "^2.0.2", "@atcute/client": "^3.0.1", "@atcute/identity-resolver": "^0.1.2", - "moment": "^2.30.1" + "moment": "^2.30.1", + "svelte-infinite-loading": "^1.4.0" }, "devDependencies": { "@sveltejs/vite-plugin-svelte": "^5.0.3", diff --git a/src/App.svelte b/src/App.svelte index eb4ed82..e87c087 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -1,10 +1,31 @@
@@ -12,7 +33,6 @@ {#await accountsPromise}

Loading...

{:then accountsData} -

ATProto PDS

Home to {accountsData.length} accounts

@@ -27,20 +47,20 @@

Error: {error.message}

{/await} - {#await postsPromise} -

Loading...

- {:then postsData} - -
-
- {#each postsData as postObject} - - {/each} -
-
- {/await} +
+
+ {#each posts as postObject} + + {/each} + +
+
diff --git a/src/lib/pdsfetch.ts b/src/lib/pdsfetch.ts index d60718e..adfee48 100644 --- a/src/lib/pdsfetch.ts +++ b/src/lib/pdsfetch.ts @@ -26,9 +26,7 @@ interface AccountMetadata { } let accountsMetadata: AccountMetadata[] = []; -// a chronologically sorted list of posts for all users, that will be shown by svelte -// getNextPosts will populate this list with additional posts as needed -let posts: Post[] = []; + interface atUriObject { repo: string; collection: string; @@ -259,7 +257,7 @@ const filterPostsByDate = (posts: PostsAcc[], cutoffDate: Date) => { return postDate >= cutoffDate; }); if (filtered.length > 0) { - postAcc.account.currentCursor = filtered[filtered.length - 1].cid; + postAcc.account.currentCursor = processAtUri(filtered[filtered.length - 1].uri).rkey; } return { posts: filtered, @@ -298,7 +296,16 @@ const getNextPosts = async () => { const cutoffDate = getCutoffDate(recordsFiltered); const recordsCutoff = filterPostsByDate(recordsFiltered, cutoffDate); // update the accountMetadata with the new cursor - accountsMetadata = recordsCutoff.map((postAcc) => postAcc.account); + accountsMetadata = accountsMetadata.map((account) => { + const postAcc = recordsCutoff.find( + (postAcc) => postAcc.account.did == account.did, + ); + if (postAcc) { + account.currentCursor = postAcc.account.currentCursor; + } + return account; + } + ); // throw the records in a big single array let records = recordsCutoff.flatMap((postAcc) => postAcc.posts); // sort the records by timestamp @@ -310,8 +317,7 @@ const getNextPosts = async () => { (b.value as AppBskyFeedPost.Record).createdAt, ).getTime(); return bDate - aDate; - } - ); + }); // filter out posts that are in the future if (!Config.SHOW_FUTURE_POSTS) { const now = Date.now(); @@ -323,23 +329,26 @@ const getNextPosts = async () => { }); } // append the new posts to the existing posts - posts = posts.concat( - records.map((record) => { - const account = accountsMetadata.find( - (account) => account.did == processAtUri(record.uri).repo, + + const newPosts = records.map((record) => { + const account = accountsMetadata.find( + (account) => account.did == processAtUri(record.uri).repo, + ); + if (!account) { + throw new Error( + `Account with DID ${processAtUri(record.uri).repo} not found`, ); - if (!account) { - throw new Error(`Account with DID ${processAtUri(record.uri).repo} not found`); - } - return new Post(record, account); - }), - ); - console.log("Fetched posts:", posts); - return posts; + } + return new Post(record, account); + }); + console.log("Fetched posts:", newPosts); + console.log("Metadata:", accountsMetadata); + return newPosts; }; const fetchPostsForUser = async (did: At.Did, cursor: string | null) => { try { + console.log("Fetching posts for user:", did, "with Cursor: ", cursor); const { data } = await rpc.get("com.atproto.repo.listRecords", { params: { repo: did as At.Identifier, @@ -388,5 +397,5 @@ const fetchPostsForUser = async (did: At.Did, cursor: string | null) => { // return posts.slice(0, Config.MAX_POSTS); // }; -export { getAllMetadataFromPds, getNextPosts, Post, posts }; +export { getAllMetadataFromPds, getNextPosts, Post }; export type { AccountMetadata };