Unfollow tool for Bluesky

resolve handle

Changed files
+33 -14
src
+33 -14
src/App.tsx
···
import { BskyAgent } from "@atproto/api";
type Form = {
-
serviceURL: string;
handle: string;
password: string;
blockedby: boolean;
···
setNotices(tmp);
}
+
const resolveHandle = async (handle: string) => {
+
const agent = new BskyAgent({
+
service: "https://public.api.bsky.app",
+
});
+
+
const res = await agent.com.atproto.identity.resolveHandle({
+
handle: handle,
+
});
+
+
return res.data.did;
+
};
+
+
const fetchServiceEndpoint = async (handle: string) => {
+
const did = await resolveHandle(handle);
+
+
const res = await fetch(
+
did.startsWith("did:web")
+
? "https://" + did.split(":")[2] + "/.well-known/did.json"
+
: "https://plc.directory/" + did,
+
);
+
+
return res.json().then((doc) => {
+
for (const service of doc.service) {
+
if (service.id.includes("#atproto_pds")) {
+
return service.serviceEndpoint;
+
}
+
}
+
});
+
};
+
const unfollowBsky = async (form: Form, preview: boolean) => {
setNotices([]);
+
+
const serviceURL = await fetchServiceEndpoint(form.handle);
const agent = new BskyAgent({
-
service: form.serviceURL,
+
service: serviceURL,
});
try {
···
const UnfollowForm: Component = () => {
const [formStore, setFormStore] = createStore<Form>({
-
serviceURL: "https://bsky.social",
handle: "",
password: "",
blockedby: true,
···
return (
<div>
-
<div>
-
<input
-
type="text"
-
placeholder="https://bsky.social (optional)"
-
onInput={(e) => {
-
if (e.currentTarget.value)
-
setFormStore("serviceURL", e.currentTarget.value);
-
else setFormStore("serviceURL", "https://bsky.social");
-
}}
-
/>
-
</div>
<div>
<input
type="text"