-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
+page.server.ts
150 lines (138 loc) · 4.61 KB
/
+page.server.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { and, eq } from "drizzle-orm";
import { db } from "$lib/server/db";
import * as schema from "$lib/schema";
import { atclient } from "$lib/server/client";
import { isValidHandle } from "@atproto/syntax";
import { Agent, AtpBaseClient, RichText } from "@atproto/api";
import { error, fail, redirect, type Actions } from "@sveltejs/kit";
export const actions: Actions = {
"login": async ({ cookies, request }) => {
const formData = await request.formData();
const handle = formData.get("handle") as string;
if (!isValidHandle(handle)) { error(400, { message: "invalid handle" }); }
const stayLoggedIn = (formData.get("stay_logged_in") as string) === "on";
cookies.set("stayLoggedIn", stayLoggedIn.toString(), { path: "/", maxAge: 60 });
const url = await atclient.authorize(handle, { scope: "atproto transition:generic" });
if (!url) {
error(500);
}
redirect(301, url.toString());
},
"logout": async ({ cookies }) => {
cookies.delete("sid", { path: "/" });
cookies.delete("stayLoggedIn", { path: "/" });
redirect(301, "/");
},
"createPost": async ({ request, locals }) => {
const formData = await request.formData();
const content = formData.get("content") as string;
const draftId = formData.get("draft_id") as string;
if (locals.agent instanceof Agent) {
const rt = new RichText({
text: content
});
await rt.detectFacets(locals.agent);
locals.agent.post({
$type: "app.bsky.feed.post", // lexicon
text: rt.text,
facets: rt.facets,
createdAt: new Date().toISOString()
});
}
if (draftId) {
await db.delete(schema.DraftPost).where(eq(schema.DraftPost.id, draftId));
}
},
"saveDraft": async ({ url, request, locals }) => {
const formData = await request.formData();
const content = formData.get("content") as string;
try {
await db.insert(schema.DraftPost)
.values({
id: crypto.randomUUID(),
authorDid: locals.user.did,
content
})
.onConflictDoUpdate({
target: schema.DraftPost.id,
set: { content }
});
}
catch (e) {
return fail(500);
}
},
"deleteDraft": async ({ url, request }) => {
const formData = await request.formData();
const id = formData.get("id") as string;
try {
await db.delete(schema.DraftPost).where(eq(schema.DraftPost.id, id));
}
catch (e) {
return fail(500);
}
},
"toggleLikePost": async ({ request, locals }) => {
const formData = await request.formData();
const likeUri = formData.get("like_uri") as string;
const cid = formData.get("post_cid") as string;
const uri = formData.get("post_uri") as string;
const agent = locals.agent;
if (agent instanceof Agent) {
if (!likeUri) {
const { uri: newLikeUri } = await agent.like(uri, cid);
return { message: "liked", uri, likeUri: newLikeUri }
}
else {
await agent.deleteLike(likeUri);
return { message: "unliked", uri, likeUri: "" }
}
}
},
"toggleRepostPost": async ({ request, locals }) => {
const formData = await request.formData();
const repostUri = formData.get("repost_uri") as string;
const cid = formData.get("post_cid") as string;
const uri = formData.get("post_uri") as string;
const agent = locals.agent;
if (agent instanceof Agent) {
if (!repostUri) {
const { uri: newRepostUri } = await agent.repost(uri, cid);
return { message: "reposted", uri, repostUri: newRepostUri }
}
else {
await agent.deleteRepost(repostUri);
return { message: "unrepost", uri, repostUri: "" }
}
}
},
"bookmarkPost": async ({ request, locals }) => {
if (!locals.user) {
return fail(401);
}
const formData = await request.formData();
const uri = formData.get("post_uri") as string;
const isBookmarked = formData.get("is_bookmarked") as string;
if (isBookmarked === "false") {
const bookmark = await db.insert(schema.Bookmark)
.values({
id: crypto.randomUUID(),
uri,
authorDid: locals.user.did,
})
.returning();
locals.bookmarks.add(bookmark[0].uri ?? "");
return { message: "bookmarked", uri };
}
else {
const removed = await db.delete(schema.Bookmark)
.where(and(
eq(schema.Bookmark.authorDid, locals.user.did),
eq(schema.Bookmark.uri, uri)
))
.returning();
locals.bookmarks.delete(removed[0].uri ?? "");
return { message: "unbookmarked", uri };
}
}
};