diff options
Diffstat (limited to 'front/src/logic/requests')
-rw-r--r-- | front/src/logic/requests/nostrill.ts | 63 |
1 files changed, 53 insertions, 10 deletions
diff --git a/front/src/logic/requests/nostrill.ts b/front/src/logic/requests/nostrill.ts index 6334c34..74fcb87 100644 --- a/front/src/logic/requests/nostrill.ts +++ b/front/src/logic/requests/nostrill.ts @@ -1,16 +1,26 @@ import type Urbit from "urbit-api"; -import type { Cursor, PostID } from "@/types/trill"; +import type { Cursor, FC, PostID } from "@/types/trill"; import type { Ship } from "@/types/urbit"; import { FeedPostCount } from "../constants"; import type { UserProfile } from "@/types/nostrill"; +import type { AsyncRes } from "@/types/ui"; // Subscribe type Handler = (date: any) => void; export default class IO { airlock; + subs: Map<string, number> = new Map(); constructor(airlock: Urbit) { this.airlock = airlock; } + private async thread(threadName: string, json: any) { + return this.airlock.thread({ + body: json, + inputMark: "json", + outputMark: "json", + threadName, + }); + } private async poke(json: any) { return this.airlock.poke({ app: "nostrill", mark: "json", json }); } @@ -18,10 +28,15 @@ export default class IO { return this.airlock.scry({ app: "nostrill", path }); } private async sub(path: string, handler: Handler) { + const has = this.subs.get(path); + if (has) return; + const err = (err: any, _id: string) => console.log(err, "error on nostrill subscription"); - const quit = (data: any) => + const quit = (data: any) => { console.log(data, "nostrill subscription kicked"); + this.subs.delete(path); + }; const res = await this.airlock.subscribe({ app: "nostrill", path, @@ -29,6 +44,7 @@ export default class IO { err, quit, }); + this.subs.set(path, res); console.log(res, "subscribed to nostrill agent"); } async unsub(sub: number) { @@ -115,23 +131,50 @@ export default class IO { return await this.poke({ fols: json }); } // profiles - async createProfile(pubkey: string, profile: UserProfile) { - const json = { add: { pubkey, profile } }; + async createProfile(profile: UserProfile) { + const json = { add: profile }; return await this.poke({ prof: json }); } - async createKey() { - const json = { add: null }; - return await this.poke({ keys: json }); + async deleteProfile() { + const json = { del: null }; + return await this.poke({ prof: json }); } - async removeKey(pubkey: string) { - const json = { del: pubkey }; - return await this.poke({ keys: json }); + async cycleKeys() { + return await this.poke({ keys: null }); } // relaying + async addRelay(url: string) { + const json = { add: url }; + return await this.poke({ rela: json }); + } + async deleteRelay(url: string) { + const json = { del: url }; + return await this.poke({ rela: json }); + } + async syncRelays() { + // TODO make it choosable? + const json = { sync: null }; + return await this.poke({ rela: json }); + } async relayPost(host: string, id: string, relays: string[]) { const json = { send: { host, id, relays } }; return await this.poke({ rela: json }); } + // threads + // + async peekFeed(host: string): AsyncRes<FC> { + try { + const json = { begs: { feed: host } }; + const res: any = await this.thread("beg", json); + console.log("peeking feed", res); + if (!("begs" in res)) return { error: "wrong request" }; + if ("ng" in res.begs) return { error: res.begs.ng }; + if (!("feed" in res.begs.ok)) return { error: "wrong request" }; + else return { ok: res.begs.ok.feed }; + } catch (e) { + return { error: `${e}` }; + } + } } // notifications |