Skip to content

Commit

Permalink
Let blog example use object dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Apr 23, 2024
1 parent 088024b commit 188aa41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
17 changes: 16 additions & 1 deletion examples/blog/federation/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
getFollowers,
removeFollower,
} from "../models/follower.ts";
import { countPosts, getPosts, toArticle } from "../models/post.ts";
import { countPosts, getPost, getPosts, toArticle } from "../models/post.ts";
import { openKv } from "../models/kv.ts";
import { getLogger } from "@logtape/logtape";

Expand Down Expand Up @@ -91,6 +91,21 @@ federation.setActorDispatcher("/users/{handle}", async (ctx, handle, key) => {
};
});

// Registers the object dispatcher, which is responsible for creating an
// `Article` object for a given post UUID:
federation.setObjectDispatcher(
Article,
"/posts/{uuid}",
async (ctx, { uuid }) => {
const blog = await getBlog();
if (blog == null) return null;
const post = await getPost(uuid);
if (post == null) return null;
const comments = await getComments(post.uuid);
return toArticle(ctx, blog, post, comments);
},
);

// Registers the outbox dispatcher, which is responsible for listing
// activities in the outbox:
federation.setOutboxDispatcher(
Expand Down
7 changes: 1 addition & 6 deletions examples/blog/routes/posts/[uuid].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@ export interface PostPageData {
followers: bigint;
}

export const handler: Handler<PostPageData> = async (req, ctx) => {
export const handler: Handler<PostPageData> = async (_req, ctx) => {
const blog = await getBlog();
if (blog == null) return await ctx.renderNotFound();
const post = await getPost(ctx.params.uuid);
if (post == null) return await ctx.renderNotFound();
const comments = await getComments(post.uuid);
const fedCtx = federation.createContext(req);
const article = toArticle(fedCtx, blog, post, comments);
const response = await respondWithObjectIfAcceptable(article, req, fedCtx);
if (response != null) return response;

const followers = await countFollowers();
const data: PostPageData = {
blog,
Expand Down

0 comments on commit 188aa41

Please sign in to comment.