ai/at
ai/at
1
0
2024-11-12 15:57:43 +09:00

63 lines
1.2 KiB
TypeScript

import "server-only";
import {
atprotoPutRecord,
atprotoCreateRecord,
atprotoDeleteRecord,
atprotoGetRecord,
} from "./record";
import { z } from "zod";
import { DataLayerError } from "../error";
import { DID, getPdsUrl } from "./did";
export const PostCollection = "ai.syui.game";
export const PostRecord = z.object({
account: z.string(),
createdAt: z.string(),
});
export type Post = z.infer<typeof PostRecord>;
type PostInput = {
account: string;
};
export async function putPost({ account }: PostInput) {
const record = { account, createdAt: new Date().toISOString() };
PostRecord.parse(record);
const result = await atprotoPutRecord({
rkey: "self",
record,
collection: PostCollection,
});
return {
rkey: "self",
};
}
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);
}