@celljs/ta-relay 4.12.0 → 4.13.1

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.
Files changed (74) hide show
  1. package/README.en.md +26 -1
  2. package/README.md +26 -1
  3. package/esm/common/index.d.ts +3 -0
  4. package/esm/common/index.js +3 -0
  5. package/esm/common/relay-friends.d.ts +154 -0
  6. package/esm/common/relay-friends.js +482 -0
  7. package/esm/common/relay-hub-util.d.ts +9 -0
  8. package/esm/common/relay-hub-util.js +19 -2
  9. package/esm/common/relay-hubs-connection.d.ts +17 -5
  10. package/esm/common/relay-hubs-connection.js +40 -37
  11. package/esm/common/relay-hubs-protocol.d.ts +51 -7
  12. package/esm/common/relay-hubs.d.ts +67 -5
  13. package/esm/common/relay-hubs.js +305 -16
  14. package/esm/common/relay-identity.d.ts +2 -0
  15. package/esm/common/relay-info.d.ts +44 -0
  16. package/esm/common/relay-info.js +94 -0
  17. package/esm/common/relay-protocol.d.ts +7 -3
  18. package/esm/common/relay-protocol.js +7 -3
  19. package/esm/common/relay-room-chat.d.ts +6 -1
  20. package/esm/common/relay-room-chat.js +1 -0
  21. package/esm/common/relay-spot.d.ts +122 -0
  22. package/esm/common/relay-spot.js +179 -0
  23. package/esm/guest/guest-i18n.d.ts +2 -0
  24. package/esm/guest/guest-i18n.js +4 -0
  25. package/esm/guest/guest.js +9 -1
  26. package/esm/node/index.d.ts +2 -0
  27. package/esm/node/index.js +2 -0
  28. package/esm/node/relay-file-storage.d.ts +32 -0
  29. package/esm/node/relay-file-storage.js +101 -0
  30. package/esm/node/relay-host.d.ts +63 -28
  31. package/esm/node/relay-host.js +145 -52
  32. package/esm/node/relay-server.d.ts +37 -3
  33. package/esm/node/relay-server.js +193 -37
  34. package/esm/node/relay-store.d.ts +34 -6
  35. package/esm/node/relay-store.js +54 -22
  36. package/esm/node/relays-file.d.ts +139 -0
  37. package/esm/node/relays-file.js +373 -0
  38. package/lib/common/index.d.ts +3 -0
  39. package/lib/common/index.js +3 -0
  40. package/lib/common/relay-friends.d.ts +155 -0
  41. package/lib/common/relay-friends.js +486 -0
  42. package/lib/common/relay-hub-util.d.ts +9 -0
  43. package/lib/common/relay-hub-util.js +19 -2
  44. package/lib/common/relay-hubs-connection.d.ts +17 -5
  45. package/lib/common/relay-hubs-connection.js +40 -37
  46. package/lib/common/relay-hubs-protocol.d.ts +51 -7
  47. package/lib/common/relay-hubs.d.ts +67 -5
  48. package/lib/common/relay-hubs.js +306 -17
  49. package/lib/common/relay-identity.d.ts +2 -0
  50. package/lib/common/relay-info.d.ts +45 -0
  51. package/lib/common/relay-info.js +97 -0
  52. package/lib/common/relay-protocol.d.ts +7 -3
  53. package/lib/common/relay-protocol.js +8 -4
  54. package/lib/common/relay-room-chat.d.ts +6 -1
  55. package/lib/common/relay-room-chat.js +1 -0
  56. package/lib/common/relay-spot.d.ts +123 -0
  57. package/lib/common/relay-spot.js +183 -0
  58. package/lib/guest/guest-i18n.d.ts +2 -0
  59. package/lib/guest/guest-i18n.js +4 -0
  60. package/lib/guest/guest.js +9 -1
  61. package/lib/node/index.d.ts +2 -0
  62. package/lib/node/index.js +2 -0
  63. package/lib/node/relay-file-storage.d.ts +33 -0
  64. package/lib/node/relay-file-storage.js +105 -0
  65. package/lib/node/relay-host.d.ts +63 -28
  66. package/lib/node/relay-host.js +146 -53
  67. package/lib/node/relay-server.d.ts +37 -3
  68. package/lib/node/relay-server.js +193 -37
  69. package/lib/node/relay-store.d.ts +34 -6
  70. package/lib/node/relay-store.js +54 -22
  71. package/lib/node/relays-file.d.ts +140 -0
  72. package/lib/node/relays-file.js +378 -0
  73. package/package.json +2 -2
  74. package/viewer/room.html +10 -10
@@ -0,0 +1,155 @@
1
+ import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayHubsStore, RelayRoomEvent, RelayRoomInfo, RelayRoomRecord, RelayRoomValue } from './relay-hubs-protocol';
2
+ import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
3
+ /** 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. */
4
+ export declare const RELAY_FRIENDS_KIND = "ta-friends";
5
+ /** What the friends of a Ta need of its rooms (RelayHubs). */
6
+ export interface RelayFriendRooms {
7
+ identity(): Promise<RelayIdentity>;
8
+ list(): RelayRoomInfo[];
9
+ /** The identity id of a room's owner, known from the link while waiting. */
10
+ ownerOf(room: string): string | undefined;
11
+ /** The public identity of a member of a room, as its roster has it. */
12
+ memberOf(room: string, member: string): RelayPublicIdentity | undefined;
13
+ /** Those asking to join a room of this Ta, as they ask. */
14
+ asksOf(room: string): (RelayPublicIdentity & {
15
+ member: string;
16
+ at: number;
17
+ guest?: boolean;
18
+ })[];
19
+ create(input: {
20
+ kind: string;
21
+ title: string;
22
+ unlisted: boolean;
23
+ }): Promise<RelayRoomInfo>;
24
+ invite(room: string, options: {
25
+ days?: number;
26
+ renew?: boolean;
27
+ }): Promise<{
28
+ link: string;
29
+ expiresAt: number;
30
+ }>;
31
+ join(link: string): Promise<RelayRoomInfo>;
32
+ answer(room: string, ask: string, accept: boolean): Promise<void>;
33
+ remove(room: string, member: string): Promise<void>;
34
+ leave(room: string): Promise<void>;
35
+ getState(room: string, prefix?: string): Promise<Record<string, RelayRoomValue>>;
36
+ setState(room: string, key: string, value: unknown): Promise<{
37
+ ok: boolean;
38
+ version: number;
39
+ }>;
40
+ /** Asks a member of a shared room to be friends: the link of this Ta's room of friends, to it alone. */
41
+ offer(room: string, member: string, link: string): Promise<void>;
42
+ }
43
+ /** Asked to be friends in a room the two share: the link of the other's room of friends. */
44
+ interface Offer extends RelayPublicIdentity {
45
+ link: string;
46
+ /** The title of the room it was asked in. */
47
+ room: string;
48
+ at: number;
49
+ }
50
+ /** `friends.json`: those the user chose as friends, and who asked the user in a shared room. */
51
+ interface Book {
52
+ v: 2;
53
+ friends: RelayFriend[];
54
+ offers: Offer[];
55
+ }
56
+ /**
57
+ * 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
58
+ * 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
59
+ * 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
60
+ * answers. Then each Ta joins the other's room on its own: the user chose the other (`friends`), and the other wrote
61
+ * the link of its room in this one's (`back:`). Removing a friend ends it on both sides. Other features of Ta keep
62
+ * what they give friends in the room of friends (Moments its posts), and share with friends by their keys.
63
+ */
64
+ export declare class RelayFriends {
65
+ protected readonly rooms: RelayFriendRooms;
66
+ protected readonly store: RelayHubsStore;
67
+ protected readonly emit: (event: RelayRoomEvent) => void;
68
+ protected settle?: ReturnType<typeof setTimeout>;
69
+ protected queue: Promise<unknown>;
70
+ /** Links of `back:` joined while this runs: one that failed is not tried again till it changes. */
71
+ protected readonly tried: Set<string>;
72
+ /** Asks the user was told of while this runs. */
73
+ protected readonly told: Set<string>;
74
+ protected stopped: boolean;
75
+ constructor(rooms: RelayFriendRooms, store: RelayHubsStore, emit: (event: RelayRoomEvent) => void);
76
+ start(): void;
77
+ stop(): void;
78
+ /** The user's room of friends, made the first time. */
79
+ room(): Promise<RelayRoomInfo>;
80
+ /** The link (and code) that asks to be the user's friend. */
81
+ link(renew?: boolean): Promise<{
82
+ link: string;
83
+ expiresAt: number;
84
+ }>;
85
+ /** Asks the one of a link to be friends, on the user's word. */
86
+ add(link: string): Promise<void>;
87
+ /** Asks a member of a room the user is in to be friends: it is told, and answers in its Ta. */
88
+ offer(room: string, member: string): Promise<void>;
89
+ /** A member of a shared room asked the user to be friends: joined back at once if the user asked it, else asked. */
90
+ offered(from: RelayPublicIdentity, link: string, title: string): void;
91
+ /** Those asking the user to be friends: by the link of the user's room, or in a room the two share. */
92
+ requests(): RelayFriendRequest[];
93
+ /** One asking to join the room of friends, heard: told the user, unless the user chose it (let in as it settles). */
94
+ asked(ask: RelayPublicIdentity & {
95
+ member: string;
96
+ at: number;
97
+ guest?: boolean;
98
+ }): void;
99
+ /** The user's answer to one asking to be friends. Only ever on the user's word, never on an agent's. */
100
+ answer(id: string, accept: boolean): Promise<void>;
101
+ /** No longer friends, on both sides: out of the user's room, and the user out of theirs. */
102
+ remove(id: string): Promise<boolean>;
103
+ /** The user's friends, and those being added: whether each is a friend yet, and online. */
104
+ list(): RelayFriendInfo[];
105
+ /** The friends, with their keys: those both chose, and in each other's room (shares to friends use their keys). */
106
+ friends(): (RelayFriend & RelayPublicIdentity)[];
107
+ /** Ids of those the user chose. */
108
+ ids(): Set<string>;
109
+ /** Those of a roster: the names and keys of friends kept up to date. */
110
+ heard(members: RelayPublicIdentity[]): void;
111
+ /** Friends of the user's other computer, with those of this Ta (a move, sync). */
112
+ merge(friends: RelayFriend[]): number;
113
+ /** Those the user chose, for the user's other computers. */
114
+ chosen(): RelayFriend[];
115
+ /** Something changed in the rooms: friends are made both ways, let in and let go of, once it settles. */
116
+ changed(): void;
117
+ /** The user changed the friends: pages read them again, and the rooms follow. */
118
+ protected updated(): void;
119
+ protected reconcile(): Promise<void>;
120
+ /** No longer friends with `id`: out of the user's room, and the user out of theirs (forgotten when gone). */
121
+ protected unfollow(id: string): Promise<void>;
122
+ protected mine(): RelayRoomInfo | undefined;
123
+ protected theirs(rooms: RelayRoomInfo[], id: string): RelayRoomInfo | undefined;
124
+ /** The user chose this one: kept, with its keys when known. */
125
+ protected choose(person: Pick<RelayPublicIdentity, 'id' | 'name'> & Partial<RelayPublicIdentity>): void;
126
+ protected read(): Book;
127
+ protected write(book: Book): void;
128
+ /** One after another: each reads what the one before wrote. */
129
+ protected serial<T>(run: () => Promise<T>): Promise<T>;
130
+ }
131
+ export declare namespace RelayFriendsUtil {
132
+ /** The kind of a room or link of friends, as it is now or was. */
133
+ function isKind(kind: string | undefined): boolean;
134
+ /** The kind a room of friends goes by, whatever it was made as. */
135
+ function kindOf(kind: string): string;
136
+ function isFriend(friend: unknown): friend is RelayFriend;
137
+ /** What is kept of a friend: its identity and when, nothing else a bundle of before held. */
138
+ function clean(friend: RelayFriend): RelayFriend;
139
+ function mergeFriend(local: RelayFriend, other: RelayFriend): RelayFriend;
140
+ function requestOf(offer: Offer): RelayFriendRequest;
141
+ /** `friends.json` as it is now: those of before kept only if they were Moments friends (see migrate). */
142
+ function book(value: unknown): Book;
143
+ /**
144
+ * Friends as they were before (2026-11-16): everyone met in a room was a "Ta friend", and Moments kept friends of
145
+ * its own in rooms of kind `ta-moments`. Now the rooms of Moments are the rooms of friends, and the friends are
146
+ * those whose room the user is in (both chose each other); the others met in rooms are no longer friends. Undefined
147
+ * when there is nothing to do.
148
+ */
149
+ function migrate(records: RelayRoomRecord[], stored: unknown): {
150
+ records: RelayRoomRecord[];
151
+ book: Book;
152
+ } | undefined;
153
+ }
154
+ export {};
155
+ //# sourceMappingURL=relay-friends.d.ts.map
@@ -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;