@celljs/ta-relay 4.11.0 → 4.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +26 -1
- package/README.md +26 -1
- package/esm/common/index.d.ts +5 -0
- package/esm/common/index.js +5 -0
- package/esm/common/relay-faces.d.ts +32 -0
- package/esm/common/relay-faces.js +208 -0
- package/esm/common/relay-friends.d.ts +154 -0
- package/esm/common/relay-friends.js +482 -0
- package/esm/common/relay-hub-util.d.ts +9 -0
- package/esm/common/relay-hub-util.js +19 -2
- package/esm/common/relay-hubs-connection.d.ts +110 -0
- package/esm/common/relay-hubs-connection.js +508 -0
- package/esm/common/relay-hubs-protocol.d.ts +174 -0
- package/esm/common/relay-hubs-protocol.js +2 -0
- package/esm/common/relay-hubs.d.ts +68 -216
- package/esm/common/relay-hubs.js +303 -507
- package/esm/common/relay-identity.d.ts +2 -0
- package/esm/common/relay-info.d.ts +44 -0
- package/esm/common/relay-info.js +94 -0
- package/esm/common/relay-protocol.d.ts +7 -3
- package/esm/common/relay-protocol.js +7 -3
- package/esm/common/relay-room-chat.d.ts +36 -2
- package/esm/common/relay-room-chat.js +32 -7
- package/esm/common/relay-spot.d.ts +122 -0
- package/esm/common/relay-spot.js +179 -0
- package/esm/guest/guest-i18n.d.ts +5 -0
- package/esm/guest/guest-i18n.js +10 -0
- package/esm/guest/guest-style.d.ts +1 -1
- package/esm/guest/guest-style.js +11 -4
- package/esm/guest/guest.js +39 -18
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/node/relay-file-storage.d.ts +32 -0
- package/esm/node/relay-file-storage.js +101 -0
- package/esm/node/relay-host.d.ts +63 -28
- package/esm/node/relay-host.js +145 -52
- package/esm/node/relay-hub-files.d.ts +1 -1
- package/esm/node/relay-server.d.ts +37 -3
- package/esm/node/relay-server.js +193 -37
- package/esm/node/relay-store.d.ts +34 -6
- package/esm/node/relay-store.js +54 -22
- package/esm/node/relays-file.d.ts +139 -0
- package/esm/node/relays-file.js +373 -0
- package/lib/common/index.d.ts +5 -0
- package/lib/common/index.js +5 -0
- package/lib/common/relay-faces.d.ts +33 -0
- package/lib/common/relay-faces.js +211 -0
- package/lib/common/relay-friends.d.ts +155 -0
- package/lib/common/relay-friends.js +486 -0
- package/lib/common/relay-hub-util.d.ts +9 -0
- package/lib/common/relay-hub-util.js +19 -2
- package/lib/common/relay-hubs-connection.d.ts +111 -0
- package/lib/common/relay-hubs-connection.js +513 -0
- package/lib/common/relay-hubs-protocol.d.ts +175 -0
- package/lib/common/relay-hubs-protocol.js +3 -0
- package/lib/common/relay-hubs.d.ts +68 -216
- package/lib/common/relay-hubs.js +311 -515
- package/lib/common/relay-identity.d.ts +2 -0
- package/lib/common/relay-info.d.ts +45 -0
- package/lib/common/relay-info.js +97 -0
- package/lib/common/relay-protocol.d.ts +7 -3
- package/lib/common/relay-protocol.js +8 -4
- package/lib/common/relay-room-chat.d.ts +36 -2
- package/lib/common/relay-room-chat.js +32 -7
- package/lib/common/relay-spot.d.ts +123 -0
- package/lib/common/relay-spot.js +183 -0
- package/lib/guest/guest-i18n.d.ts +5 -0
- package/lib/guest/guest-i18n.js +10 -0
- package/lib/guest/guest-style.d.ts +1 -1
- package/lib/guest/guest-style.js +11 -4
- package/lib/guest/guest.js +39 -18
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/relay-file-storage.d.ts +33 -0
- package/lib/node/relay-file-storage.js +105 -0
- package/lib/node/relay-host.d.ts +63 -28
- package/lib/node/relay-host.js +146 -53
- package/lib/node/relay-hub-files.d.ts +1 -1
- package/lib/node/relay-server.d.ts +37 -3
- package/lib/node/relay-server.js +193 -37
- package/lib/node/relay-store.d.ts +34 -6
- package/lib/node/relay-store.js +54 -22
- package/lib/node/relays-file.d.ts +140 -0
- package/lib/node/relays-file.js +378 -0
- package/package.json +2 -2
- package/viewer/room.html +21 -14
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayFriendsUtil = exports.RelayFriends = exports.RELAY_FRIENDS_KIND = void 0;
|
|
4
|
+
const relay_hub_util_1 = require("./relay-hub-util");
|
|
5
|
+
const relay_identity_1 = require("./relay-identity");
|
|
6
|
+
const relay_protocol_1 = require("./relay-protocol");
|
|
7
|
+
/** The kind of the room of a Ta's friends (docs/design/ta-link.md 3.1): unlisted, one a Ta, its friends its members. */
|
|
8
|
+
exports.RELAY_FRIENDS_KIND = 'ta-friends';
|
|
9
|
+
/** What the room of friends was before friends were Ta's own: Moments kept it. */
|
|
10
|
+
const LEGACY_KINDS = new Set(['ta-moments']);
|
|
11
|
+
/** The link of the user's room a friend writes in the room of the other, for the other to join back. */
|
|
12
|
+
const BACK = 'back:';
|
|
13
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
14
|
+
/** A friend asked of in a room, and never answered, is no longer asked after so long. */
|
|
15
|
+
const OFFER_DAYS = 14;
|
|
16
|
+
const LINK_DAYS = 30;
|
|
17
|
+
const SETTLE_MS = 300;
|
|
18
|
+
/**
|
|
19
|
+
* The friends of a Ta (docs/design/ta-link.md 3.1): one kind of friend, each on the word of both. A Ta has one
|
|
20
|
+
* unlisted room of friends; two are friends when each is a member of the other's. One asks by the link of the other's
|
|
21
|
+
* room (a code, a link sent), or in a room the two share (the other is sent the link of this one's room); the other
|
|
22
|
+
* answers. Then each Ta joins the other's room on its own: the user chose the other (`friends`), and the other wrote
|
|
23
|
+
* the link of its room in this one's (`back:`). Removing a friend ends it on both sides. Other features of Ta keep
|
|
24
|
+
* what they give friends in the room of friends (Moments its posts), and share with friends by their keys.
|
|
25
|
+
*/
|
|
26
|
+
class RelayFriends {
|
|
27
|
+
constructor(rooms, store, emit) {
|
|
28
|
+
this.rooms = rooms;
|
|
29
|
+
this.store = store;
|
|
30
|
+
this.emit = emit;
|
|
31
|
+
this.queue = Promise.resolve();
|
|
32
|
+
/** Links of `back:` joined while this runs: one that failed is not tried again till it changes. */
|
|
33
|
+
this.tried = new Set();
|
|
34
|
+
/** Asks the user was told of while this runs. */
|
|
35
|
+
this.told = new Set();
|
|
36
|
+
this.stopped = true;
|
|
37
|
+
}
|
|
38
|
+
start() {
|
|
39
|
+
this.stopped = false;
|
|
40
|
+
}
|
|
41
|
+
stop() {
|
|
42
|
+
this.stopped = true;
|
|
43
|
+
clearTimeout(this.settle);
|
|
44
|
+
}
|
|
45
|
+
/** The user's room of friends, made the first time. */
|
|
46
|
+
async room() {
|
|
47
|
+
var _a;
|
|
48
|
+
return (_a = this.mine()) !== null && _a !== void 0 ? _a : (await this.rooms.create({ kind: exports.RELAY_FRIENDS_KIND, title: 'Friends', unlisted: true }));
|
|
49
|
+
}
|
|
50
|
+
/** The link (and code) that asks to be the user's friend. */
|
|
51
|
+
async link(renew = false) {
|
|
52
|
+
const room = await this.room();
|
|
53
|
+
const invite = await this.rooms.invite(room.id, { days: LINK_DAYS, renew });
|
|
54
|
+
return { link: relay_hub_util_1.RelayHubUtil.withKind(invite.link, exports.RELAY_FRIENDS_KIND), expiresAt: invite.expiresAt };
|
|
55
|
+
}
|
|
56
|
+
/** Asks the one of a link to be friends, on the user's word. */
|
|
57
|
+
add(link) {
|
|
58
|
+
return this.serial(async () => {
|
|
59
|
+
const parsed = relay_hub_util_1.RelayHubUtil.parse(link);
|
|
60
|
+
if (!RelayFriendsUtil.isKind(parsed.kind)) {
|
|
61
|
+
throw new Error('Not a link that adds a friend.');
|
|
62
|
+
}
|
|
63
|
+
if (parsed.owner === (await this.rooms.identity()).id) {
|
|
64
|
+
throw new Error('This is your own link.');
|
|
65
|
+
}
|
|
66
|
+
await this.room();
|
|
67
|
+
this.choose({ id: parsed.owner, name: parsed.by });
|
|
68
|
+
await this.rooms.join(link);
|
|
69
|
+
this.updated();
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/** Asks a member of a room the user is in to be friends: it is told, and answers in its Ta. */
|
|
73
|
+
offer(room, member) {
|
|
74
|
+
return this.serial(async () => {
|
|
75
|
+
const info = this.rooms.list().find(item => item.id === room && item.status === 'member');
|
|
76
|
+
const other = info === null || info === void 0 ? void 0 : info.members.find(item => item.member === member);
|
|
77
|
+
const identity = this.rooms.memberOf(room, member);
|
|
78
|
+
if (!info || !other || other.self || other.guest || !identity) {
|
|
79
|
+
throw new Error('Not one to ask in that room.');
|
|
80
|
+
}
|
|
81
|
+
const { link } = await this.link();
|
|
82
|
+
this.choose(identity);
|
|
83
|
+
await this.rooms.offer(room, member, link);
|
|
84
|
+
this.updated();
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/** A member of a shared room asked the user to be friends: joined back at once if the user asked it, else asked. */
|
|
88
|
+
offered(from, link, title) {
|
|
89
|
+
let parsed;
|
|
90
|
+
try {
|
|
91
|
+
parsed = relay_hub_util_1.RelayHubUtil.parse(link);
|
|
92
|
+
}
|
|
93
|
+
catch (_a) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
// The link must be of its own room of friends.
|
|
97
|
+
if (parsed.owner !== from.id || !RelayFriendsUtil.isKind(parsed.kind)) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
void this.serial(async () => {
|
|
101
|
+
const book = this.read();
|
|
102
|
+
if (book.friends.some(friend => friend.id === from.id)) {
|
|
103
|
+
await this.rooms.join(link).catch(() => undefined);
|
|
104
|
+
this.changed();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const offer = { ...relay_identity_1.RelayIdentityUtil.publicOf(from), link, room: title.slice(0, 80), at: Date.now() };
|
|
108
|
+
book.offers = [...book.offers.filter(each => each.id !== from.id), offer];
|
|
109
|
+
this.write(book);
|
|
110
|
+
this.emit({ type: 'friend', request: RelayFriendsUtil.requestOf(offer) });
|
|
111
|
+
this.emit({ type: 'rooms' });
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/** Those asking the user to be friends: by the link of the user's room, or in a room the two share. */
|
|
115
|
+
requests() {
|
|
116
|
+
const book = this.read();
|
|
117
|
+
const chosen = new Set(book.friends.map(friend => friend.id));
|
|
118
|
+
const mine = this.mine();
|
|
119
|
+
const asks = mine
|
|
120
|
+
? this.rooms
|
|
121
|
+
.asksOf(mine.id)
|
|
122
|
+
.filter(ask => !ask.guest && !chosen.has(ask.id))
|
|
123
|
+
.map(ask => ({ id: ask.id, name: ask.name, at: ask.at, via: 'link' }))
|
|
124
|
+
: [];
|
|
125
|
+
const offers = book.offers.filter(offer => !chosen.has(offer.id) && !asks.some(ask => ask.id === offer.id)).map(RelayFriendsUtil.requestOf);
|
|
126
|
+
return [...asks, ...offers].sort((a, b) => a.at - b.at);
|
|
127
|
+
}
|
|
128
|
+
/** One asking to join the room of friends, heard: told the user, unless the user chose it (let in as it settles). */
|
|
129
|
+
asked(ask) {
|
|
130
|
+
if (!ask.guest && !this.read().friends.some(friend => friend.id === ask.id) && !this.told.has(ask.member)) {
|
|
131
|
+
this.told.add(ask.member);
|
|
132
|
+
this.emit({ type: 'friend', request: { id: ask.id, name: ask.name, at: ask.at, via: 'link' } });
|
|
133
|
+
this.emit({ type: 'rooms' });
|
|
134
|
+
}
|
|
135
|
+
this.changed();
|
|
136
|
+
}
|
|
137
|
+
/** The user's answer to one asking to be friends. Only ever on the user's word, never on an agent's. */
|
|
138
|
+
answer(id, accept) {
|
|
139
|
+
return this.serial(async () => {
|
|
140
|
+
const book = this.read();
|
|
141
|
+
const mine = this.mine();
|
|
142
|
+
const ask = mine ? this.rooms.asksOf(mine.id).find(each => each.id === id && !each.guest) : undefined;
|
|
143
|
+
const offer = book.offers.find(each => each.id === id);
|
|
144
|
+
if (!ask && !offer) {
|
|
145
|
+
throw new Error('Nobody asks that now.');
|
|
146
|
+
}
|
|
147
|
+
book.offers = book.offers.filter(each => each.id !== id);
|
|
148
|
+
this.write(book);
|
|
149
|
+
if (accept) {
|
|
150
|
+
this.choose(ask !== null && ask !== void 0 ? ask : offer);
|
|
151
|
+
}
|
|
152
|
+
if (mine && ask) {
|
|
153
|
+
await this.rooms.answer(mine.id, ask.member, accept);
|
|
154
|
+
}
|
|
155
|
+
if (accept && offer && !ask) {
|
|
156
|
+
await this.rooms.join(offer.link);
|
|
157
|
+
}
|
|
158
|
+
this.updated();
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/** No longer friends, on both sides: out of the user's room, and the user out of theirs. */
|
|
162
|
+
remove(id) {
|
|
163
|
+
return this.serial(async () => {
|
|
164
|
+
const book = this.read();
|
|
165
|
+
const had = book.friends.some(friend => friend.id === id) || book.offers.some(offer => offer.id === id);
|
|
166
|
+
book.friends = book.friends.filter(friend => friend.id !== id);
|
|
167
|
+
book.offers = book.offers.filter(offer => offer.id !== id);
|
|
168
|
+
this.write(book);
|
|
169
|
+
await this.unfollow(id);
|
|
170
|
+
this.updated();
|
|
171
|
+
return had;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/** The user's friends, and those being added: whether each is a friend yet, and online. */
|
|
175
|
+
list() {
|
|
176
|
+
const rooms = this.rooms.list();
|
|
177
|
+
const mine = rooms.find(room => room.owner && room.kind === exports.RELAY_FRIENDS_KIND);
|
|
178
|
+
return this.read()
|
|
179
|
+
.friends.map(friend => {
|
|
180
|
+
const member = mine === null || mine === void 0 ? void 0 : mine.members.find(item => item.id === friend.id && !item.owner);
|
|
181
|
+
const theirs = this.theirs(rooms, friend.id);
|
|
182
|
+
const owner = theirs === null || theirs === void 0 ? void 0 : theirs.members.find(item => item.owner);
|
|
183
|
+
return {
|
|
184
|
+
id: friend.id,
|
|
185
|
+
name: (member === null || member === void 0 ? void 0 : member.name) || (owner === null || owner === void 0 ? void 0 : owner.name) || friend.name,
|
|
186
|
+
status: member && (theirs === null || theirs === void 0 ? void 0 : theirs.status) === 'member' ? 'friend' : 'adding',
|
|
187
|
+
online: !!(member === null || member === void 0 ? void 0 : member.online) || !!(owner === null || owner === void 0 ? void 0 : owner.online),
|
|
188
|
+
metAt: friend.metAt,
|
|
189
|
+
seenAt: friend.seenAt,
|
|
190
|
+
};
|
|
191
|
+
})
|
|
192
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
193
|
+
}
|
|
194
|
+
/** The friends, with their keys: those both chose, and in each other's room (shares to friends use their keys). */
|
|
195
|
+
friends() {
|
|
196
|
+
const friends = new Set(this.list()
|
|
197
|
+
.filter(friend => friend.status === 'friend')
|
|
198
|
+
.map(friend => friend.id));
|
|
199
|
+
return this.read().friends.filter((friend) => friends.has(friend.id) && !!friend.sign && !!friend.box);
|
|
200
|
+
}
|
|
201
|
+
/** Ids of those the user chose. */
|
|
202
|
+
ids() {
|
|
203
|
+
return new Set(this.read().friends.map(friend => friend.id));
|
|
204
|
+
}
|
|
205
|
+
/** Those of a roster: the names and keys of friends kept up to date. */
|
|
206
|
+
heard(members) {
|
|
207
|
+
const book = this.read();
|
|
208
|
+
let changed = false;
|
|
209
|
+
const now = Date.now();
|
|
210
|
+
for (const member of members) {
|
|
211
|
+
const known = book.friends.find(friend => friend.id === member.id);
|
|
212
|
+
if (known && (known.name !== member.name || known.sign !== member.sign || known.box !== member.box || now - known.seenAt > DAY_MS)) {
|
|
213
|
+
Object.assign(known, relay_identity_1.RelayIdentityUtil.publicOf(member), { seenAt: now });
|
|
214
|
+
changed = true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (changed) {
|
|
218
|
+
this.write(book);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/** Friends of the user's other computer, with those of this Ta (a move, sync). */
|
|
222
|
+
merge(friends) {
|
|
223
|
+
const book = this.read();
|
|
224
|
+
let changed = 0;
|
|
225
|
+
for (const friend of friends) {
|
|
226
|
+
if (!RelayFriendsUtil.isFriend(friend)) {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const index = book.friends.findIndex(each => each.id === friend.id);
|
|
230
|
+
const merged = index < 0 ? RelayFriendsUtil.clean(friend) : RelayFriendsUtil.mergeFriend(book.friends[index], friend);
|
|
231
|
+
if (index < 0 || JSON.stringify(merged) !== JSON.stringify(book.friends[index])) {
|
|
232
|
+
changed++;
|
|
233
|
+
book.friends[index < 0 ? book.friends.length : index] = merged;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (changed) {
|
|
237
|
+
this.write(book);
|
|
238
|
+
this.updated();
|
|
239
|
+
}
|
|
240
|
+
return changed;
|
|
241
|
+
}
|
|
242
|
+
/** Those the user chose, for the user's other computers. */
|
|
243
|
+
chosen() {
|
|
244
|
+
return this.read().friends.sort((a, b) => b.seenAt - a.seenAt);
|
|
245
|
+
}
|
|
246
|
+
/** Something changed in the rooms: friends are made both ways, let in and let go of, once it settles. */
|
|
247
|
+
changed() {
|
|
248
|
+
var _a, _b;
|
|
249
|
+
if (this.stopped) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
clearTimeout(this.settle);
|
|
253
|
+
this.settle = setTimeout(() => void this.serial(() => this.reconcile()).catch(() => undefined), SETTLE_MS);
|
|
254
|
+
(_b = (_a = this.settle).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
255
|
+
}
|
|
256
|
+
/** The user changed the friends: pages read them again, and the rooms follow. */
|
|
257
|
+
updated() {
|
|
258
|
+
this.emit({ type: 'rooms' });
|
|
259
|
+
this.changed();
|
|
260
|
+
}
|
|
261
|
+
async reconcile() {
|
|
262
|
+
var _a, _b;
|
|
263
|
+
if (this.stopped) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const book = this.read();
|
|
267
|
+
const chosen = new Set(book.friends.map(friend => friend.id));
|
|
268
|
+
const now = Date.now();
|
|
269
|
+
let mine = this.mine();
|
|
270
|
+
for (const room of this.rooms.list().filter(item => !item.owner && item.kind === exports.RELAY_FRIENDS_KIND)) {
|
|
271
|
+
const owner = this.rooms.ownerOf(room.id);
|
|
272
|
+
if (!owner) {
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (room.status === 'gone' || room.status === 'refused') {
|
|
276
|
+
// Let go of or refused: no longer friends; the link no longer working: asked no more.
|
|
277
|
+
chosen.delete(owner);
|
|
278
|
+
await this.unfollow(owner);
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
if (!chosen.has(owner)) {
|
|
282
|
+
// No longer chosen (removed on the user's other computer): let go of.
|
|
283
|
+
await this.unfollow(owner);
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (room.status !== 'member') {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
mine !== null && mine !== void 0 ? mine : (mine = await this.room());
|
|
290
|
+
if (mine.members.some(member => member.id === owner)) {
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
// Gives the owner the link of the user's room, while they are not in it yet.
|
|
294
|
+
const self = room.members.find(member => member.self);
|
|
295
|
+
if (!self) {
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
const key = `${BACK}${self.member}`;
|
|
299
|
+
const back = (_a = (await this.rooms.getState(room.id, key))[key]) === null || _a === void 0 ? void 0 : _a.value;
|
|
300
|
+
if (typeof (back === null || back === void 0 ? void 0 : back.until) !== 'number' || back.until < now + DAY_MS) {
|
|
301
|
+
const invite = await this.link();
|
|
302
|
+
await this.rooms.setState(room.id, key, { link: invite.link, until: invite.expiresAt });
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (mine) {
|
|
306
|
+
const backs = await this.rooms.getState(mine.id, BACK).catch(() => ({}));
|
|
307
|
+
for (const [key, value] of Object.entries(backs)) {
|
|
308
|
+
const member = key.slice(BACK.length);
|
|
309
|
+
const who = mine.members.find(item => item.member === member);
|
|
310
|
+
const link = (_b = value.value) === null || _b === void 0 ? void 0 : _b.link;
|
|
311
|
+
if (value.by !== member || !who || !chosen.has(who.id) || typeof link !== 'string' || this.tried.has(link)) {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
const theirs = this.theirs(this.rooms.list(), who.id);
|
|
315
|
+
if (theirs && (theirs.status === 'member' || theirs.status === 'waiting')) {
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
this.tried.add(link);
|
|
319
|
+
await this.rooms.join(link).catch(() => undefined);
|
|
320
|
+
}
|
|
321
|
+
for (const ask of this.rooms.asksOf(mine.id)) {
|
|
322
|
+
if (ask.guest) {
|
|
323
|
+
await this.rooms.answer(mine.id, ask.member, false).catch(() => undefined);
|
|
324
|
+
}
|
|
325
|
+
else if (chosen.has(ask.id) && mine.members.length <= relay_protocol_1.RELAY_FRIENDS_MAX) {
|
|
326
|
+
await this.rooms.answer(mine.id, ask.member, true).catch(() => undefined);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const after = this.read();
|
|
331
|
+
const expired = after.offers.filter(offer => now - offer.at > OFFER_DAYS * DAY_MS);
|
|
332
|
+
if (after.friends.some(friend => !chosen.has(friend.id)) || expired.length) {
|
|
333
|
+
after.friends = after.friends.filter(friend => chosen.has(friend.id));
|
|
334
|
+
after.offers = after.offers.filter(offer => !expired.includes(offer));
|
|
335
|
+
this.write(after);
|
|
336
|
+
this.emit({ type: 'rooms' });
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
/** No longer friends with `id`: out of the user's room, and the user out of theirs (forgotten when gone). */
|
|
340
|
+
async unfollow(id) {
|
|
341
|
+
const mine = this.mine();
|
|
342
|
+
const member = mine === null || mine === void 0 ? void 0 : mine.members.find(item => item.id === id && !item.owner);
|
|
343
|
+
if (mine && member) {
|
|
344
|
+
await this.rooms.remove(mine.id, member.member).catch(() => undefined);
|
|
345
|
+
}
|
|
346
|
+
for (const room of this.rooms.list().filter(item => !item.owner && item.kind === exports.RELAY_FRIENDS_KIND && this.rooms.ownerOf(item.id) === id)) {
|
|
347
|
+
await this.rooms.leave(room.id).catch(() => undefined);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
mine() {
|
|
351
|
+
return this.rooms.list().find(room => room.owner && room.kind === exports.RELAY_FRIENDS_KIND && room.status === 'member');
|
|
352
|
+
}
|
|
353
|
+
theirs(rooms, id) {
|
|
354
|
+
return rooms.find(room => !room.owner && room.kind === exports.RELAY_FRIENDS_KIND && this.rooms.ownerOf(room.id) === id);
|
|
355
|
+
}
|
|
356
|
+
/** The user chose this one: kept, with its keys when known. */
|
|
357
|
+
choose(person) {
|
|
358
|
+
var _a, _b;
|
|
359
|
+
const book = this.read();
|
|
360
|
+
const now = Date.now();
|
|
361
|
+
const known = book.friends.find(friend => friend.id === person.id);
|
|
362
|
+
if (known) {
|
|
363
|
+
known.name = person.name || known.name;
|
|
364
|
+
known.sign = (_a = person.sign) !== null && _a !== void 0 ? _a : known.sign;
|
|
365
|
+
known.box = (_b = person.box) !== null && _b !== void 0 ? _b : known.box;
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
book.friends.push(RelayFriendsUtil.clean({ ...person, metAt: now, seenAt: now }));
|
|
369
|
+
}
|
|
370
|
+
book.offers = book.offers.filter(offer => offer.id !== person.id);
|
|
371
|
+
this.write(book);
|
|
372
|
+
}
|
|
373
|
+
read() {
|
|
374
|
+
return RelayFriendsUtil.book(this.store.read('friends'));
|
|
375
|
+
}
|
|
376
|
+
write(book) {
|
|
377
|
+
this.store.write('friends', book);
|
|
378
|
+
}
|
|
379
|
+
/** One after another: each reads what the one before wrote. */
|
|
380
|
+
serial(run) {
|
|
381
|
+
const next = this.queue.then(run, run);
|
|
382
|
+
this.queue = next.catch(() => undefined);
|
|
383
|
+
return next;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
exports.RelayFriends = RelayFriends;
|
|
387
|
+
var RelayFriendsUtil;
|
|
388
|
+
(function (RelayFriendsUtil) {
|
|
389
|
+
/** The kind of a room or link of friends, as it is now or was. */
|
|
390
|
+
function isKind(kind) {
|
|
391
|
+
return kind === exports.RELAY_FRIENDS_KIND || (!!kind && LEGACY_KINDS.has(kind));
|
|
392
|
+
}
|
|
393
|
+
RelayFriendsUtil.isKind = isKind;
|
|
394
|
+
/** The kind a room of friends goes by, whatever it was made as. */
|
|
395
|
+
function kindOf(kind) {
|
|
396
|
+
return LEGACY_KINDS.has(kind) ? exports.RELAY_FRIENDS_KIND : kind;
|
|
397
|
+
}
|
|
398
|
+
RelayFriendsUtil.kindOf = kindOf;
|
|
399
|
+
function isFriend(friend) {
|
|
400
|
+
const value = friend;
|
|
401
|
+
return !!value && typeof value.id === 'string' && typeof value.name === 'string' && typeof value.metAt === 'number' && typeof value.seenAt === 'number';
|
|
402
|
+
}
|
|
403
|
+
RelayFriendsUtil.isFriend = isFriend;
|
|
404
|
+
/** What is kept of a friend: its identity and when, nothing else a bundle of before held. */
|
|
405
|
+
function clean(friend) {
|
|
406
|
+
var _a;
|
|
407
|
+
return {
|
|
408
|
+
id: friend.id,
|
|
409
|
+
name: String((_a = friend.name) !== null && _a !== void 0 ? _a : '').slice(0, 40),
|
|
410
|
+
...(typeof friend.sign === 'string' ? { sign: friend.sign } : {}),
|
|
411
|
+
...(typeof friend.box === 'string' ? { box: friend.box } : {}),
|
|
412
|
+
metAt: friend.metAt,
|
|
413
|
+
seenAt: friend.seenAt,
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
RelayFriendsUtil.clean = clean;
|
|
417
|
+
function mergeFriend(local, other) {
|
|
418
|
+
var _a, _b;
|
|
419
|
+
const later = other.seenAt > local.seenAt ? other : local;
|
|
420
|
+
const earlier = later === local ? other : local;
|
|
421
|
+
return clean({
|
|
422
|
+
...earlier,
|
|
423
|
+
...later,
|
|
424
|
+
sign: (_a = later.sign) !== null && _a !== void 0 ? _a : earlier.sign,
|
|
425
|
+
box: (_b = later.box) !== null && _b !== void 0 ? _b : earlier.box,
|
|
426
|
+
metAt: Math.min(local.metAt, other.metAt),
|
|
427
|
+
seenAt: Math.max(local.seenAt, other.seenAt),
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
RelayFriendsUtil.mergeFriend = mergeFriend;
|
|
431
|
+
function requestOf(offer) {
|
|
432
|
+
return { id: offer.id, name: offer.name, at: offer.at, via: 'room', room: offer.room };
|
|
433
|
+
}
|
|
434
|
+
RelayFriendsUtil.requestOf = requestOf;
|
|
435
|
+
/** `friends.json` as it is now: those of before kept only if they were Moments friends (see migrate). */
|
|
436
|
+
function book(value) {
|
|
437
|
+
const read = (value !== null && value !== void 0 ? value : {});
|
|
438
|
+
if (read.v !== 2) {
|
|
439
|
+
return { v: 2, friends: [], offers: [] };
|
|
440
|
+
}
|
|
441
|
+
return {
|
|
442
|
+
v: 2,
|
|
443
|
+
friends: Array.isArray(read.friends) ? read.friends.filter(isFriend) : [],
|
|
444
|
+
offers: Array.isArray(read.offers) ? read.offers.filter(offer => !!offer && typeof offer.id === 'string' && typeof offer.link === 'string') : [],
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
RelayFriendsUtil.book = book;
|
|
448
|
+
/**
|
|
449
|
+
* Friends as they were before (2026-11-16): everyone met in a room was a "Ta friend", and Moments kept friends of
|
|
450
|
+
* its own in rooms of kind `ta-moments`. Now the rooms of Moments are the rooms of friends, and the friends are
|
|
451
|
+
* those whose room the user is in (both chose each other); the others met in rooms are no longer friends. Undefined
|
|
452
|
+
* when there is nothing to do.
|
|
453
|
+
*/
|
|
454
|
+
function migrate(records, stored) {
|
|
455
|
+
const read = (stored !== null && stored !== void 0 ? stored : {});
|
|
456
|
+
const legacy = records.some(record => LEGACY_KINDS.has(record.kind));
|
|
457
|
+
if (read.v === 2 && !legacy) {
|
|
458
|
+
return undefined;
|
|
459
|
+
}
|
|
460
|
+
const next = records.map(record => (LEGACY_KINDS.has(record.kind) ? { ...record, kind: exports.RELAY_FRIENDS_KIND } : record));
|
|
461
|
+
if (read.v === 2) {
|
|
462
|
+
return { records: next, book: book(stored) };
|
|
463
|
+
}
|
|
464
|
+
const before = Array.isArray(read.friends) ? read.friends : [];
|
|
465
|
+
const now = Date.now();
|
|
466
|
+
const friends = [];
|
|
467
|
+
for (const record of next) {
|
|
468
|
+
if (record.own || record.kind !== exports.RELAY_FRIENDS_KIND || (record.status !== 'member' && record.status !== 'waiting')) {
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
const known = before.find(friend => (friend === null || friend === void 0 ? void 0 : friend.id) === record.owner.id);
|
|
472
|
+
if (!friends.some(friend => friend.id === record.owner.id)) {
|
|
473
|
+
friends.push(clean({
|
|
474
|
+
...(known !== null && known !== void 0 ? known : {}),
|
|
475
|
+
...record.owner,
|
|
476
|
+
name: record.owner.name || (known === null || known === void 0 ? void 0 : known.name) || '',
|
|
477
|
+
metAt: typeof (known === null || known === void 0 ? void 0 : known.metAt) === 'number' ? known.metAt : now,
|
|
478
|
+
seenAt: typeof (known === null || known === void 0 ? void 0 : known.seenAt) === 'number' ? known.seenAt : now,
|
|
479
|
+
}));
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return { records: next, book: { v: 2, friends, offers: [] } };
|
|
483
|
+
}
|
|
484
|
+
RelayFriendsUtil.migrate = migrate;
|
|
485
|
+
})(RelayFriendsUtil || (exports.RelayFriendsUtil = RelayFriendsUtil = {}));
|
|
486
|
+
//# sourceMappingURL=relay-friends.js.map
|
|
@@ -10,6 +10,11 @@ export interface RelayHubLink {
|
|
|
10
10
|
title: string;
|
|
11
11
|
/** The owner's name, as they gave it. */
|
|
12
12
|
by: string;
|
|
13
|
+
/**
|
|
14
|
+
* What the room is for, `c` of the link, when a package keeps such rooms for itself (`ta-moments`): its Ta opens
|
|
15
|
+
* the link, not the Rooms page, and a browser says to open it in Ta.
|
|
16
|
+
*/
|
|
17
|
+
kind?: string;
|
|
13
18
|
}
|
|
14
19
|
/** One of a room, as its owner lists them in the roster. */
|
|
15
20
|
export interface RelayHubMember extends RelayPublicIdentity {
|
|
@@ -37,6 +42,8 @@ export interface RelayHubAnswerBody {
|
|
|
37
42
|
kind: string;
|
|
38
43
|
title: string;
|
|
39
44
|
view?: string;
|
|
45
|
+
/** Not on the Rooms page: a room of an extension's own (the user's devices). */
|
|
46
|
+
unlisted?: true;
|
|
40
47
|
}
|
|
41
48
|
export interface RelayHubSigned<T> {
|
|
42
49
|
/** The member who signed it. */
|
|
@@ -53,6 +60,8 @@ export interface RelayHubKeys {
|
|
|
53
60
|
export declare namespace RelayHubUtil {
|
|
54
61
|
/** `https://<relay>/r#v=1&r=<room>&i=<invite>&o=<owner>&n=<title>&by=<name>`. */
|
|
55
62
|
function link(link: RelayHubLink): string;
|
|
63
|
+
/** The link of a room as a package keeps it for itself (RelayHubLink.kind). */
|
|
64
|
+
function withKind(text: string, kind: string): string;
|
|
56
65
|
function is(text: string): boolean;
|
|
57
66
|
/** Throws on what is not a link of {@link link}. */
|
|
58
67
|
function parse(text: string): RelayHubLink;
|
|
@@ -10,9 +10,17 @@ var RelayHubUtil;
|
|
|
10
10
|
/** `https://<relay>/r#v=1&r=<room>&i=<invite>&o=<owner>&n=<title>&by=<name>`. */
|
|
11
11
|
function link(link) {
|
|
12
12
|
const fragment = new URLSearchParams({ v: '1', r: link.room, i: link.invite, o: link.owner, n: link.title, by: link.by });
|
|
13
|
+
if (link.kind) {
|
|
14
|
+
fragment.set('c', link.kind);
|
|
15
|
+
}
|
|
13
16
|
return `${link.relay}${relay_protocol_1.RELAY_ROOM_PATH}#${fragment}`;
|
|
14
17
|
}
|
|
15
18
|
RelayHubUtil.link = link;
|
|
19
|
+
/** The link of a room as a package keeps it for itself (RelayHubLink.kind). */
|
|
20
|
+
function withKind(text, kind) {
|
|
21
|
+
return link({ ...parse(text), kind });
|
|
22
|
+
}
|
|
23
|
+
RelayHubUtil.withKind = withKind;
|
|
16
24
|
function is(text) {
|
|
17
25
|
try {
|
|
18
26
|
parse(text);
|
|
@@ -25,7 +33,7 @@ var RelayHubUtil;
|
|
|
25
33
|
RelayHubUtil.is = is;
|
|
26
34
|
/** Throws on what is not a link of {@link link}. */
|
|
27
35
|
function parse(text) {
|
|
28
|
-
var _a, _b;
|
|
36
|
+
var _a, _b, _c;
|
|
29
37
|
const url = new URL(text.trim());
|
|
30
38
|
const fragment = new URLSearchParams(url.hash.slice(1));
|
|
31
39
|
const [room, invite, owner] = ['r', 'i', 'o'].map(name => { var _a; return (_a = fragment.get(name)) !== null && _a !== void 0 ? _a : ''; });
|
|
@@ -37,7 +45,16 @@ var RelayHubUtil;
|
|
|
37
45
|
!/^[A-Za-z0-9_-]{22}$/.test(owner)) {
|
|
38
46
|
throw new Error('Not a link inviting into a room of Ta.');
|
|
39
47
|
}
|
|
40
|
-
|
|
48
|
+
const kind = (_a = fragment.get('c')) !== null && _a !== void 0 ? _a : '';
|
|
49
|
+
return {
|
|
50
|
+
relay: url.origin,
|
|
51
|
+
room,
|
|
52
|
+
invite,
|
|
53
|
+
owner,
|
|
54
|
+
title: ((_b = fragment.get('n')) !== null && _b !== void 0 ? _b : '').slice(0, 200),
|
|
55
|
+
by: ((_c = fragment.get('by')) !== null && _c !== void 0 ? _c : '').slice(0, 100),
|
|
56
|
+
...(/^[a-z0-9-]{1,40}$/.test(kind) ? { kind } : {}),
|
|
57
|
+
};
|
|
41
58
|
}
|
|
42
59
|
RelayHubUtil.parse = parse;
|
|
43
60
|
function randomId() {
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { type RelayHubAskBody, type RelayHubKeys, type RelayHubMember, type RelayHubRosterBody } from './relay-hub-util';
|
|
2
|
+
import type { RelayHubsOptions, RelayRoomEvent, RelayRoomRecord, RelayRoomRequest, RelayRoomStatus, RelayRoomValue } from './relay-hubs-protocol';
|
|
3
|
+
import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
|
|
4
|
+
import { type RelayHubEntry, type RelayHubFailure, type RelayHubRequest } from './relay-protocol';
|
|
5
|
+
/** What a member sends in a message, signed and sealed: data of an extension, or a word of Ta itself. */
|
|
6
|
+
export interface MessageBody {
|
|
7
|
+
d?: unknown;
|
|
8
|
+
/** A member's new name, for the owner to put in the roster. */
|
|
9
|
+
name?: string;
|
|
10
|
+
/** Asks the one it is sent to to be friends: the link of the sender's room of friends (RelayFriends.offer). */
|
|
11
|
+
friend?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface StateBody {
|
|
14
|
+
key: string;
|
|
15
|
+
value: unknown;
|
|
16
|
+
}
|
|
17
|
+
/** How long a call waits for the room to be connected, or for the relay to answer. */
|
|
18
|
+
export declare const READY_MS = 15000;
|
|
19
|
+
export declare const OPEN = 1;
|
|
20
|
+
/** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
|
|
21
|
+
export declare class Live {
|
|
22
|
+
record: RelayRoomRecord;
|
|
23
|
+
socket?: WebSocket;
|
|
24
|
+
keys?: RelayHubKeys;
|
|
25
|
+
synced: boolean;
|
|
26
|
+
online: Set<string>;
|
|
27
|
+
roster?: RelayHubRosterBody;
|
|
28
|
+
readonly state: Map<string, RelayRoomValue>;
|
|
29
|
+
/** Asks to join, with the invitation each came with. */
|
|
30
|
+
readonly asks: Map<string, {
|
|
31
|
+
body: RelayHubAskBody;
|
|
32
|
+
invite: string;
|
|
33
|
+
}>;
|
|
34
|
+
readonly done: Map<number, (result: {
|
|
35
|
+
ok: boolean;
|
|
36
|
+
v: number;
|
|
37
|
+
error?: RelayHubFailure;
|
|
38
|
+
}) => void>;
|
|
39
|
+
readonly ready: Array<() => void>;
|
|
40
|
+
attempts: number;
|
|
41
|
+
/** Connect again at once, whatever the close said. */
|
|
42
|
+
retry: boolean;
|
|
43
|
+
timer?: ReturnType<typeof setTimeout>;
|
|
44
|
+
pingTimer?: ReturnType<typeof setInterval>;
|
|
45
|
+
awaitingPong: boolean;
|
|
46
|
+
/** Events are handled one after another: opening them is async. */
|
|
47
|
+
chain: Promise<void>;
|
|
48
|
+
constructor(record: RelayRoomRecord);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The half of {@link RelayHubs} that keeps a connection to the hub of each room and reads what comes over it:
|
|
52
|
+
* connecting again, the welcome, the roster, the state and the messages, each opened and checked against the
|
|
53
|
+
* signatures of the roster; what is kept of the rooms.
|
|
54
|
+
*/
|
|
55
|
+
export declare abstract class RelayHubsConnection {
|
|
56
|
+
protected readonly options: RelayHubsOptions;
|
|
57
|
+
protected readonly live: Map<string, Live>;
|
|
58
|
+
protected readonly listeners: Set<(event: RelayRoomEvent) => void>;
|
|
59
|
+
protected own?: RelayIdentity;
|
|
60
|
+
/** Asks the user was told of while this runs: those the owner finds waiting when it connects are told once. */
|
|
61
|
+
protected readonly told: Set<string>;
|
|
62
|
+
protected stopped: boolean;
|
|
63
|
+
protected nextId: number;
|
|
64
|
+
constructor(options: RelayHubsOptions);
|
|
65
|
+
/** This Ta's identity, made the first time. */
|
|
66
|
+
identity(): Promise<RelayIdentity>;
|
|
67
|
+
/** Who this Ta is in a room: the identity it joined with when it took on another since (RelayRoomRecord.identity). */
|
|
68
|
+
protected identityOf(live: Live): Promise<RelayIdentity>;
|
|
69
|
+
protected requestOf(live: Live, ask: RelayHubAskBody, friends: Set<string>): RelayRoomRequest;
|
|
70
|
+
/** Resolves once the room is connected and has its state; rejects after a while. */
|
|
71
|
+
protected ready(live: Live): Promise<void>;
|
|
72
|
+
protected request(live: Live, request: RelayHubRequest): void;
|
|
73
|
+
protected sendBody(live: Live, body: MessageBody, to?: string): Promise<void>;
|
|
74
|
+
/** The roster with `change` made, its version one more, signed by the owner. */
|
|
75
|
+
protected nextRoster(live: Live, change: (members: RelayHubMember[]) => RelayHubMember[]): Promise<{
|
|
76
|
+
v: number;
|
|
77
|
+
d: string;
|
|
78
|
+
}>;
|
|
79
|
+
protected writeRoster(live: Live, change: (members: RelayHubMember[]) => RelayHubMember[]): Promise<void>;
|
|
80
|
+
protected connect(live: Live): void;
|
|
81
|
+
protected hello(live: Live, socket: WebSocket): Promise<void>;
|
|
82
|
+
protected closed(live: Live, code: number): void;
|
|
83
|
+
protected disconnect(live: Live): void;
|
|
84
|
+
protected receive(live: Live, text: string): Promise<void>;
|
|
85
|
+
protected failed(live: Live, code: RelayHubFailure): void;
|
|
86
|
+
protected setStatus(live: Live, status: RelayRoomStatus, reason?: RelayRoomRecord['reason']): void;
|
|
87
|
+
protected admitted(live: Live, answer: string): Promise<void>;
|
|
88
|
+
/** An ask to join: told as a request the first time this Ta hears it; `fresh` when it came now, not with the welcome. */
|
|
89
|
+
protected asked(live: Live, sealed: string, fresh: boolean): Promise<void>;
|
|
90
|
+
/** One asks to join a room of this Ta, heard the first time. */
|
|
91
|
+
protected heardAsk(live: Live, ask: RelayHubAskBody): void;
|
|
92
|
+
/** The members of a roster, read: those of them the user chose as friends are kept up to date. */
|
|
93
|
+
protected heardMembers(_members: RelayPublicIdentity[]): void;
|
|
94
|
+
/** A member asks the user to be friends, by the link of its room of friends. */
|
|
95
|
+
protected offered(_live: Live, _from: RelayPublicIdentity, _link: string): void;
|
|
96
|
+
protected readRoster(live: Live, sealed: string): Promise<void>;
|
|
97
|
+
protected guestOf(live: Live, member: string): boolean;
|
|
98
|
+
protected signOf(live: Live): (member: string) => string | undefined;
|
|
99
|
+
protected readEntry(live: Live, entry: RelayHubEntry, tell: boolean): Promise<void>;
|
|
100
|
+
protected readMessage(live: Live, from: string, sealed: string): Promise<void>;
|
|
101
|
+
/** Ids of those the user chose as friends. */
|
|
102
|
+
protected friendIds(): Set<string>;
|
|
103
|
+
protected readRooms(): RelayRoomRecord[];
|
|
104
|
+
protected save(): void;
|
|
105
|
+
/** To the listeners, then what follows from it (RelayHubs: the friends settle). */
|
|
106
|
+
protected emit(event: RelayRoomEvent): void;
|
|
107
|
+
protected after(_event: RelayRoomEvent): void;
|
|
108
|
+
/** To the listeners alone. */
|
|
109
|
+
protected notify(event: RelayRoomEvent): void;
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=relay-hubs-connection.d.ts.map
|