Skip to content

Commit 188aa41

Browse files
committed
Let blog example use object dispatcher
1 parent 088024b commit 188aa41

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

examples/blog/federation/mod.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
getFollowers,
2626
removeFollower,
2727
} from "../models/follower.ts";
28-
import { countPosts, getPosts, toArticle } from "../models/post.ts";
28+
import { countPosts, getPost, getPosts, toArticle } from "../models/post.ts";
2929
import { openKv } from "../models/kv.ts";
3030
import { getLogger } from "@logtape/logtape";
3131

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

94+
// Registers the object dispatcher, which is responsible for creating an
95+
// `Article` object for a given post UUID:
96+
federation.setObjectDispatcher(
97+
Article,
98+
"/posts/{uuid}",
99+
async (ctx, { uuid }) => {
100+
const blog = await getBlog();
101+
if (blog == null) return null;
102+
const post = await getPost(uuid);
103+
if (post == null) return null;
104+
const comments = await getComments(post.uuid);
105+
return toArticle(ctx, blog, post, comments);
106+
},
107+
);
108+
94109
// Registers the outbox dispatcher, which is responsible for listing
95110
// activities in the outbox:
96111
federation.setOutboxDispatcher(

examples/blog/routes/posts/[uuid].tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,12 @@ export interface PostPageData {
2424
followers: bigint;
2525
}
2626

27-
export const handler: Handler<PostPageData> = async (req, ctx) => {
27+
export const handler: Handler<PostPageData> = async (_req, ctx) => {
2828
const blog = await getBlog();
2929
if (blog == null) return await ctx.renderNotFound();
3030
const post = await getPost(ctx.params.uuid);
3131
if (post == null) return await ctx.renderNotFound();
3232
const comments = await getComments(post.uuid);
33-
const fedCtx = federation.createContext(req);
34-
const article = toArticle(fedCtx, blog, post, comments);
35-
const response = await respondWithObjectIfAcceptable(article, req, fedCtx);
36-
if (response != null) return response;
37-
3833
const followers = await countFollowers();
3934
const data: PostPageData = {
4035
blog,

0 commit comments

Comments
 (0)