Loading examples/custom-collections/main.ts +19 −25 Original line number Diff line number Diff line import { Create, createFederation, MemoryKvStore, Note } from "@fedify/fedify"; import { print } from "ioredis"; // Mock data - in a real application, this would query your database const POSTS = [ new Create({ new Note({ id: new URL("https://example.com/posts/post-1"), content: "ActivityPub is a decentralized social networking protocol...", tags: [ Loading @@ -10,23 +11,23 @@ const POSTS = [ new URL("https://example.com/tags/Decentralization"), ], }), new Create({ new Note({ id: new URL("https://example.com/posts/post-2"), content: "Fedify makes it easy to build federated applications...", }), new Create({ new Note({ id: new URL("https://example.com/posts/post-3"), content: "WebFinger is a protocol for discovering information...", tags: [new URL("https://example.com/tags/ActivityPub")], }), new Create({ new Note({ id: new URL("https://example.com/posts/post-4"), content: "HTTP Signatures provide authentication for ActivityPub...", }), new Create({ new Note({ id: new URL("https://example.com/posts/post-5"), content: "Understanding ActivityPub's data model is crucial...", }), Loading @@ -37,19 +38,16 @@ function getTagFromUrl(url: string): string { return parts[parts.length - 1]; } function getTaggedPostsByTag(tag: string): Create[] { const results = POSTS.filter((post) => { function getTaggedPostsByTag(tag: string): Note[] { return POSTS .filter((post) => { if (!post.tagIds) { return false; } const postTags = post.tagIds; const matches = postTags.some((tagId) => { return post.tagIds.some((tagId) => { return getTagFromUrl(tagId.toString()) === tag; }); return matches; }); return results; } async function demonstrateCustomCollection() { Loading @@ -69,10 +67,6 @@ async function demonstrateCustomCollection() { throw new Error("Missing userId or tag in values"); } const posts = getTaggedPostsByTag(values.tag); const items = posts.map((post) => (new Note({ id: new URL(`/posts/${post.id}`, _ctx.url), content: post.content, }))); if (cursor != null) { const idx = Number.parseInt(cursor, 10); Loading @@ -80,16 +74,16 @@ async function demonstrateCustomCollection() { throw new Error("Invalid cursor"); } return { items: [items[idx]], nextCursor: idx < items.length - 1 ? (idx + 1).toString() : null, items: idx < posts.length ? [posts[idx]] : [], nextCursor: idx < posts.length - 1 ? (idx + 1).toString() : null, prevCursor: idx > 0 ? (idx - 1).toString() : null, }; } return { items }; return { items: posts, nextCursor: null, prevCursor: null }; }, ).setCounter(async (_ctx, values) => { // Return the total count of tagged posts const count = (await getTaggedPostsByTag(values.tag)).length; const count = getTaggedPostsByTag(values.tag).length; return count; }); Loading Loading
examples/custom-collections/main.ts +19 −25 Original line number Diff line number Diff line import { Create, createFederation, MemoryKvStore, Note } from "@fedify/fedify"; import { print } from "ioredis"; // Mock data - in a real application, this would query your database const POSTS = [ new Create({ new Note({ id: new URL("https://example.com/posts/post-1"), content: "ActivityPub is a decentralized social networking protocol...", tags: [ Loading @@ -10,23 +11,23 @@ const POSTS = [ new URL("https://example.com/tags/Decentralization"), ], }), new Create({ new Note({ id: new URL("https://example.com/posts/post-2"), content: "Fedify makes it easy to build federated applications...", }), new Create({ new Note({ id: new URL("https://example.com/posts/post-3"), content: "WebFinger is a protocol for discovering information...", tags: [new URL("https://example.com/tags/ActivityPub")], }), new Create({ new Note({ id: new URL("https://example.com/posts/post-4"), content: "HTTP Signatures provide authentication for ActivityPub...", }), new Create({ new Note({ id: new URL("https://example.com/posts/post-5"), content: "Understanding ActivityPub's data model is crucial...", }), Loading @@ -37,19 +38,16 @@ function getTagFromUrl(url: string): string { return parts[parts.length - 1]; } function getTaggedPostsByTag(tag: string): Create[] { const results = POSTS.filter((post) => { function getTaggedPostsByTag(tag: string): Note[] { return POSTS .filter((post) => { if (!post.tagIds) { return false; } const postTags = post.tagIds; const matches = postTags.some((tagId) => { return post.tagIds.some((tagId) => { return getTagFromUrl(tagId.toString()) === tag; }); return matches; }); return results; } async function demonstrateCustomCollection() { Loading @@ -69,10 +67,6 @@ async function demonstrateCustomCollection() { throw new Error("Missing userId or tag in values"); } const posts = getTaggedPostsByTag(values.tag); const items = posts.map((post) => (new Note({ id: new URL(`/posts/${post.id}`, _ctx.url), content: post.content, }))); if (cursor != null) { const idx = Number.parseInt(cursor, 10); Loading @@ -80,16 +74,16 @@ async function demonstrateCustomCollection() { throw new Error("Invalid cursor"); } return { items: [items[idx]], nextCursor: idx < items.length - 1 ? (idx + 1).toString() : null, items: idx < posts.length ? [posts[idx]] : [], nextCursor: idx < posts.length - 1 ? (idx + 1).toString() : null, prevCursor: idx > 0 ? (idx - 1).toString() : null, }; } return { items }; return { items: posts, nextCursor: null, prevCursor: null }; }, ).setCounter(async (_ctx, values) => { // Return the total count of tagged posts const count = (await getTaggedPostsByTag(values.tag)).length; const count = getTaggedPostsByTag(values.tag).length; return count; }); Loading