-
Notifications
You must be signed in to change notification settings - Fork 10
/
post.ts
68 lines (56 loc) · 1.55 KB
/
post.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import "server-only";
import {
atprotoCreateRecord,
atprotoDeleteRecord,
atprotoGetRecord,
} from "./record";
import { z } from "zod";
import { DataLayerError } from "../error";
import { DID, getPdsUrl } from "./did";
import { MAX_POST_TITLE_LENGTH, MAX_POST_URL_LENGTH } from "../db/constants";
export const PostCollection = "fyi.unravel.frontpage.post";
export const PostRecord = z.object({
title: z.string().max(MAX_POST_TITLE_LENGTH),
url: z.string().url().max(MAX_POST_URL_LENGTH),
createdAt: z.string(),
});
export type Post = z.infer<typeof PostRecord>;
type PostInput = {
title: string;
url: string;
};
export async function createPost({ title, url }: PostInput) {
const record = { title, url, createdAt: new Date().toISOString() };
const parseResult = PostRecord.safeParse(record);
if (!parseResult.success) {
throw new DataLayerError("Invalid post record", {
cause: parseResult.error,
});
}
const result = await atprotoCreateRecord({
record,
collection: PostCollection,
});
return {
rkey: result.uri.rkey,
};
}
export async function deletePost(rkey: string) {
await atprotoDeleteRecord({
rkey,
collection: PostCollection,
});
}
export async function getPost({ rkey, repo }: { rkey: string; repo: DID }) {
const service = await getPdsUrl(repo);
if (!service) {
throw new DataLayerError("Failed to get service url");
}
const { value } = await atprotoGetRecord({
serviceEndpoint: service,
repo,
collection: PostCollection,
rkey,
});
return PostRecord.parse(value);
}