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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
import type Urbit from "urbit-api";
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 });
}
private async scry(path: string) {
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) => {
console.log(data, "nostrill subscription kicked");
this.subs.delete(path);
};
const res = await this.airlock.subscribe({
app: "nostrill",
path,
event: handler,
err,
quit,
});
this.subs.set(path, res);
console.log(res, "subscribed to nostrill agent");
}
async unsub(sub: number) {
return await this.airlock.unsubscribe(sub);
}
// subs
async subscribeStore(handler: Handler) {
const res = await this.sub("/ui", handler);
return res;
}
// scries
async scryFeed(start: Cursor, end: Cursor, desc = true) {
const order = desc ? 1 : 0;
const term = "feed";
const path = `/j/feed/${term}/${start}/${end}/${FeedPostCount}/${order}`;
return await this.scry(path);
}
async scryPost(
host: Ship,
id: PostID,
start: Cursor,
end: Cursor,
desc = true,
) {
const order = desc ? 1 : 0;
const path = `/j/post/${host}/${id}/${start}/${end}/${FeedPostCount}/${order}`;
return await this.scry(path);
}
// pokes
async pokeAlive() {
return await this.poke({ alive: true });
}
async addPost(content: string) {
const json = { add: { content } };
return this.poke({ post: json });
}
// async addPost(post: SentPoast, gossip: boolean) {
// const json = {
// "new-post": {
// "sent-post": post,
// gossip,
// },
// };
// return this.poke(json);
// }
async deletePost(id: string) {
const host = `~${this.airlock.ship}`;
const json = {
"del-post": {
ship: host,
id: id,
},
};
return this.poke(json);
}
async addReact(ship: Ship, id: PostID, reaction: string) {
const json = {
"new-react": {
react: reaction,
pid: {
id: id,
ship: ship,
},
},
};
return this.poke(json);
}
// follows
async follow(ship: Ship) {
const json = { add: ship };
return this.poke({ fols: json });
}
async unfollow(ship: Ship) {
const json = { del: ship };
return await this.poke({ fols: json });
}
// profiles
async createProfile(profile: UserProfile) {
const json = { add: profile };
return await this.poke({ prof: json });
}
async deleteProfile() {
const json = { del: null };
return await this.poke({ prof: 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
// mark as read
|