a tool for shared writing and social publishing
1"use server";
2
3import { AppBskyActorDefs, Agent as BskyAgent } from "@atproto/api";
4import { getIdentityData } from "actions/getIdentityData";
5import {
6 restoreOAuthSession,
7 OAuthSessionError,
8} from "src/atproto-oauth";
9const leafletFeedURI =
10 "at://did:plc:btxrwcaeyodrap5mnjw2fvmz/app.bsky.feed.generator/subscribedPublications";
11
12export async function addFeed(): Promise<
13 { success: true } | { success: false; error: OAuthSessionError }
14> {
15 let identity = await getIdentityData();
16 if (!identity || !identity.atp_did) {
17 return {
18 success: false,
19 error: {
20 type: "oauth_session_expired",
21 message: "Not authenticated",
22 did: "",
23 },
24 };
25 }
26
27 const sessionResult = await restoreOAuthSession(identity.atp_did);
28 if (!sessionResult.ok) {
29 return { success: false, error: sessionResult.error };
30 }
31 let credentialSession = sessionResult.value;
32 let bsky = new BskyAgent(credentialSession);
33 let prefs = await bsky.app.bsky.actor.getPreferences();
34 let savedFeeds = prefs.data.preferences.find(
35 (pref) => pref.$type === "app.bsky.actor.defs#savedFeedsPrefV2",
36 ) as AppBskyActorDefs.SavedFeedsPrefV2;
37
38 let hasFeed = !!savedFeeds.items.find(
39 (feed) => feed.value === leafletFeedURI,
40 );
41 if (hasFeed) return { success: true };
42
43 await bsky.addSavedFeeds([
44 {
45 value: leafletFeedURI,
46 pinned: true,
47 type: "feed",
48 },
49 ]);
50 return { success: true };
51}