Skip to content

Commit c4853a9

Browse files
committed
Migrate frontpage post to be lexicon first
1 parent ce0f68d commit c4853a9

16 files changed

Lines changed: 81 additions & 106 deletions

File tree

packages/frontpage/app/(app)/_components/post-card.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { getVoteForPost } from "@/lib/data/db/vote";
44
import { ensureUser, getUser } from "@/lib/data/user";
55
import { TimeAgo } from "@/lib/components/time-ago";
66
import { VoteButton } from "./vote-button";
7-
import { PostCollection, deletePost } from "@/lib/data/atproto/post";
87
import { getVerifiedHandle } from "@/lib/data/atproto/identity";
98
import { UserHoverCard } from "@/lib/components/user-hover-card";
109
import type { DID } from "@/lib/data/atproto/did";
@@ -15,6 +14,7 @@ import { revalidatePath } from "next/cache";
1514
import { ReportDialogDropdownButton } from "./report-dialog";
1615
import { DeleteButton } from "./delete-button";
1716
import { ShareDropdownButton } from "./share-button";
17+
import { atprotoClient, nsids } from "@/lib/data/atproto/repo";
1818

1919
type PostProps = {
2020
id: number;
@@ -59,7 +59,7 @@ export async function PostCard({
5959
subjectAuthorDid: author,
6060
subjectCid: cid,
6161
subjectRkey: rkey,
62-
subjectCollection: PostCollection,
62+
subjectCollection: nsids.FyiUnravelFrontpagePost,
6363
});
6464
}}
6565
unvoteAction={async () => {
@@ -147,7 +147,10 @@ export async function PostCard({
147147
export async function deletePostAction(rkey: string) {
148148
"use server";
149149
await ensureUser();
150-
await deletePost(rkey);
150+
const atproto = atprotoClient();
151+
await atproto.fyi.unravel.frontpage.post.delete({
152+
rkey,
153+
});
151154

152155
revalidatePath("/");
153156
}
@@ -170,9 +173,9 @@ export async function reportPostAction(
170173

171174
await createReport({
172175
...formResult.data,
173-
subjectUri: `at://${input.author}/${PostCollection}/${input.rkey}`,
176+
subjectUri: `at://${input.author}/${nsids.FyiUnravelFrontpagePost}/${input.rkey}`,
174177
subjectDid: input.author,
175-
subjectCollection: PostCollection,
178+
subjectCollection: nsids.FyiUnravelFrontpagePost,
176179
subjectRkey: input.rkey,
177180
subjectCid: input.cid,
178181
});

packages/frontpage/app/(app)/moderation/_components/report-card.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ import { UserHandle } from "./user-handle";
1212
import Link from "next/link";
1313
import { cn } from "@/lib/utils";
1414
import { CommentCollection } from "@/lib/data/atproto/comment";
15-
import { PostCollection } from "@/lib/data/atproto/post";
1615
import { getPostFromComment } from "@/lib/data/db/post";
1716
import { getCommentLink, getPostLink } from "@/lib/navigation";
17+
import { nsids } from "@/lib/data/atproto/repo";
1818

1919
const createLink = async (
2020
collection?: string | null,
2121
author?: DID | null,
2222
rkey?: string | null,
2323
) => {
2424
switch (collection) {
25-
case PostCollection:
25+
case nsids.FyiUnravelFrontpagePost:
2626
return getPostLink({ handleOrDid: author!, rkey: rkey! });
2727

2828
case CommentCollection:

packages/frontpage/app/(app)/moderation/page.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
ModerationEventDTO,
1818
createModerationEvent,
1919
} from "@/lib/data/db/moderation";
20-
import { PostCollection } from "@/lib/data/atproto/post";
2120
import { CommentCollection } from "@/lib/data/atproto/comment";
2221
import { revalidatePath } from "next/cache";
2322
import Link from "next/link";
@@ -26,6 +25,7 @@ import { moderatePost } from "@/lib/data/db/post";
2625
import { DID } from "@/lib/data/atproto/did";
2726
import { moderateComment } from "@/lib/data/db/comment";
2827
import { moderateUser } from "@/lib/data/db/user";
28+
import { nsids } from "@/lib/data/atproto/repo";
2929

3030
export async function performModerationAction(
3131
input: { reportId: number; status: "accepted" | "rejected" },
@@ -49,8 +49,8 @@ export async function performModerationAction(
4949
};
5050

5151
if (report.subjectCollection) {
52-
if (report.subjectCollection === PostCollection) {
53-
newModEvent.subjectCollection = PostCollection;
52+
if (report.subjectCollection === nsids.FyiUnravelFrontpagePost) {
53+
newModEvent.subjectCollection = nsids.FyiUnravelFrontpagePost;
5454
} else if (report.subjectCollection === CommentCollection) {
5555
newModEvent.subjectCollection = CommentCollection;
5656
}
@@ -61,7 +61,7 @@ export async function performModerationAction(
6161

6262
const modAction = async () => {
6363
switch (report.subjectCollection) {
64-
case PostCollection:
64+
case nsids.FyiUnravelFrontpagePost:
6565
return await moderatePost({
6666
rkey: report.subjectRkey!,
6767
authorDid: report.subjectDid! as DID,

packages/frontpage/app/(app)/post/[postAuthor]/[postRkey]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Metadata } from "next";
55
import { getVerifiedHandle } from "@/lib/data/atproto/identity";
66
import { PostPageParams, getPostPageData } from "./_lib/page-data";
77
import { LinkAlternateAtUri } from "@/lib/components/link-alternate-at";
8-
import { PostCollection } from "@/lib/data/atproto/post";
8+
import { nsids } from "@/lib/data/atproto/repo";
99

1010
export async function generateMetadata(props: {
1111
params: Promise<PostPageParams>;
@@ -47,7 +47,7 @@ export default async function Post(props: { params: Promise<PostPageParams> }) {
4747
<>
4848
<LinkAlternateAtUri
4949
authority={authorDid}
50-
collection={PostCollection}
50+
collection={nsids.FyiUnravelFrontpagePost}
5151
rkey={post.rkey}
5252
/>
5353
{post.status === "live" ? (

packages/frontpage/app/(app)/post/new/_action.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import { DID } from "@/lib/data/atproto/did";
44
import { getVerifiedHandle } from "@/lib/data/atproto/identity";
5-
import { createPost } from "@/lib/data/atproto/post";
5+
import { atprotoClient } from "@/lib/data/atproto/repo";
66
import { uncached_doesPostExist } from "@/lib/data/db/post";
77
import { DataLayerError } from "@/lib/data/error";
88
import { ensureUser } from "@/lib/data/user";
9+
import { AtUri } from "@atproto/syntax";
910
import { redirect } from "next/navigation";
1011

1112
export async function newPostAction(_prevState: unknown, formData: FormData) {
@@ -26,13 +27,24 @@ export async function newPostAction(_prevState: unknown, formData: FormData) {
2627
return { error: "Invalid URL" };
2728
}
2829

30+
const atproto = atprotoClient();
31+
2932
try {
30-
const { rkey } = await createPost({ title, url });
33+
const record = await atproto.fyi.unravel.frontpage.post.create(
34+
{},
35+
{
36+
title,
37+
url,
38+
createdAt: new Date().toISOString(),
39+
},
40+
);
41+
const uri = new AtUri(record.uri);
42+
3143
const [handle] = await Promise.all([
3244
getVerifiedHandle(user.did),
33-
waitForPost(user.did, rkey),
45+
waitForPost(user.did, uri.rkey),
3446
]);
35-
redirect(`/post/${handle}/${rkey}`);
47+
redirect(`/post/${handle}/${uri.rkey}`);
3648
} catch (error) {
3749
if (!(error instanceof DataLayerError)) throw error;
3850
return { error: "Failed to create post" };

packages/frontpage/app/api/receive_hook/route.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { db } from "@/lib/db";
22
import * as schema from "@/lib/schema";
33
import { atprotoGetRecord } from "@/lib/data/atproto/record";
44
import { Commit } from "@/lib/data/atproto/event";
5-
import * as atprotoPost from "@/lib/data/atproto/post";
65
import * as dbPost from "@/lib/data/db/post";
76
import * as atprotoComment from "@/lib/data/atproto/comment";
87
import { VoteRecord } from "@/lib/data/atproto/vote";
@@ -17,6 +16,7 @@ import {
1716
unauthed_createCommentVote,
1817
} from "@/lib/data/db/vote";
1918
import { unauthed_createNotification } from "@/lib/data/db/notification";
19+
import { atprotoClient, nsids } from "@/lib/data/atproto/repo";
2020

2121
export async function POST(request: Request) {
2222
const auth = request.headers.get("Authorization");
@@ -36,24 +36,23 @@ export async function POST(request: Request) {
3636
throw new Error("No AtprotoPersonalDataServer service found");
3737
}
3838

39+
const atproto = atprotoClient(service);
40+
3941
const promises = ops.map(async (op) => {
4042
const { collection, rkey } = op.path;
4143
console.log("Processing", collection, rkey, op.action);
4244

43-
if (collection === atprotoPost.PostCollection) {
45+
if (collection === nsids.FyiUnravelFrontpagePost) {
4446
if (op.action === "create") {
45-
const record = await atprotoGetRecord({
46-
serviceEndpoint: service,
47+
const postRecord = await atproto.fyi.unravel.frontpage.post.get({
4748
repo,
48-
collection,
4949
rkey,
5050
});
51-
const postRecord = atprotoPost.PostRecord.parse(record.value);
5251
await dbPost.unauthed_createPost({
53-
post: postRecord,
52+
post: postRecord.value,
5453
rkey,
5554
authorDid: repo,
56-
cid: record.cid,
55+
cid: postRecord.cid,
5756
offset: seq,
5857
});
5958
} else if (op.action === "delete") {
@@ -108,7 +107,7 @@ export async function POST(request: Request) {
108107

109108
if (
110109
hydratedVoteRecordValue.subject.uri.collection ===
111-
atprotoPost.PostCollection
110+
nsids.FyiUnravelFrontpagePost
112111
) {
113112
await unauthed_createPostVote({
114113
repo,

packages/frontpage/lib/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ export async function importDpopJwks({
448448
}
449449

450450
export async function fetchAuthenticatedAtproto(
451-
input: RequestInfo,
451+
input: string | Request | URL,
452452
init?: RequestInit,
453453
) {
454454
const session = await getSession();

packages/frontpage/lib/data/atproto/comment.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
import { createAtUriParser } from "./uri";
88
import { DataLayerError } from "../error";
99
import { z } from "zod";
10-
import { PostCollection } from "./post";
1110
import { DID, getPdsUrl } from "./did";
1211
import { MAX_COMMENT_LENGTH } from "../db/constants";
12+
import { nsids } from "./repo";
1313

1414
export const CommentCollection = "fyi.unravel.frontpage.comment";
1515

@@ -23,7 +23,7 @@ export const CommentRecord = z.object({
2323
.optional(),
2424
post: z.object({
2525
cid: z.string(),
26-
uri: createAtUriParser(z.literal(PostCollection)),
26+
uri: createAtUriParser(z.literal(nsids.FyiUnravelFrontpagePost)),
2727
}),
2828
createdAt: z.string(),
2929
});
@@ -49,7 +49,7 @@ export async function createComment({ parent, post, content }: CommentInput) {
4949
: undefined,
5050
post: {
5151
cid: post.cid,
52-
uri: `at://${post.authorDid}/${PostCollection}/${post.rkey}`,
52+
uri: `at://${post.authorDid}/${nsids.FyiUnravelFrontpagePost}/${post.rkey}`,
5353
},
5454
createdAt: new Date().toISOString(),
5555
};

packages/frontpage/lib/data/atproto/event.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import "server-only";
22
import { z } from "zod";
33
import { CommentCollection } from "./comment";
4-
import { PostCollection } from "./post";
54
import { isDid } from "./did";
5+
import { nsids } from "./repo";
66

77
// This module refers to the event emitted by the Firehose
88

99
export const Collection = z.union([
10-
z.literal(PostCollection),
10+
z.literal(nsids.FyiUnravelFrontpagePost),
1111
z.literal(CommentCollection),
1212
z.literal("fyi.unravel.frontpage.vote"),
1313
]);

packages/frontpage/lib/data/atproto/post.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)