@celljs/ta-relay 4.22.0 → 4.24.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.
Files changed (80) hide show
  1. package/esm/common/index.d.ts +2 -0
  2. package/esm/common/index.js +2 -0
  3. package/esm/common/relay-blobs.d.ts +36 -2
  4. package/esm/common/relay-blobs.js +33 -5
  5. package/esm/common/relay-chat-store.d.ts +69 -0
  6. package/esm/common/relay-chat-store.js +50 -0
  7. package/esm/common/relay-hub-charter.d.ts +2 -0
  8. package/esm/common/relay-hub-charter.js +3 -0
  9. package/esm/common/relay-hub-log.d.ts +65 -0
  10. package/esm/common/relay-hub-log.js +147 -0
  11. package/esm/common/relay-hub.d.ts +3 -0
  12. package/esm/common/relay-hub.js +18 -0
  13. package/esm/common/relay-hubs-admins.d.ts +1 -0
  14. package/esm/common/relay-hubs-admins.js +10 -6
  15. package/esm/common/relay-hubs-chats.d.ts +73 -0
  16. package/esm/common/relay-hubs-chats.js +367 -0
  17. package/esm/common/relay-hubs-connection.d.ts +3 -1
  18. package/esm/common/relay-hubs-connection.js +7 -0
  19. package/esm/common/relay-hubs-protocol.d.ts +16 -0
  20. package/esm/common/relay-hubs.d.ts +2 -2
  21. package/esm/common/relay-hubs.js +14 -11
  22. package/esm/common/relay-info.d.ts +23 -1
  23. package/esm/common/relay-info.js +25 -2
  24. package/esm/common/relay-protocol.d.ts +60 -1
  25. package/esm/common/relay-protocol.js +15 -0
  26. package/esm/common/relay-room-chat.d.ts +126 -4
  27. package/esm/common/relay-room-chat.js +230 -6
  28. package/esm/guest/guest-i18n.d.ts +2 -0
  29. package/esm/guest/guest-i18n.js +8 -0
  30. package/esm/guest/guest.js +8 -5
  31. package/esm/node/index.d.ts +1 -0
  32. package/esm/node/index.js +1 -0
  33. package/esm/node/relay-chat-files.d.ts +52 -0
  34. package/esm/node/relay-chat-files.js +224 -0
  35. package/esm/node/relay-hub-files.d.ts +11 -1
  36. package/esm/node/relay-hub-files.js +8 -1
  37. package/esm/node/relay-server.d.ts +9 -1
  38. package/esm/node/relay-server.js +29 -18
  39. package/lib/common/index.d.ts +2 -0
  40. package/lib/common/index.js +2 -0
  41. package/lib/common/relay-blobs.d.ts +36 -2
  42. package/lib/common/relay-blobs.js +34 -6
  43. package/lib/common/relay-chat-store.d.ts +70 -0
  44. package/lib/common/relay-chat-store.js +54 -0
  45. package/lib/common/relay-hub-charter.d.ts +2 -0
  46. package/lib/common/relay-hub-charter.js +3 -0
  47. package/lib/common/relay-hub-log.d.ts +66 -0
  48. package/lib/common/relay-hub-log.js +151 -0
  49. package/lib/common/relay-hub.d.ts +3 -0
  50. package/lib/common/relay-hub.js +18 -0
  51. package/lib/common/relay-hubs-admins.d.ts +1 -0
  52. package/lib/common/relay-hubs-admins.js +9 -5
  53. package/lib/common/relay-hubs-chats.d.ts +74 -0
  54. package/lib/common/relay-hubs-chats.js +371 -0
  55. package/lib/common/relay-hubs-connection.d.ts +3 -1
  56. package/lib/common/relay-hubs-connection.js +7 -0
  57. package/lib/common/relay-hubs-protocol.d.ts +16 -0
  58. package/lib/common/relay-hubs.d.ts +2 -2
  59. package/lib/common/relay-hubs.js +14 -11
  60. package/lib/common/relay-info.d.ts +23 -1
  61. package/lib/common/relay-info.js +26 -3
  62. package/lib/common/relay-protocol.d.ts +60 -1
  63. package/lib/common/relay-protocol.js +16 -1
  64. package/lib/common/relay-room-chat.d.ts +126 -4
  65. package/lib/common/relay-room-chat.js +231 -7
  66. package/lib/guest/guest-i18n.d.ts +2 -0
  67. package/lib/guest/guest-i18n.js +8 -0
  68. package/lib/guest/guest.js +7 -4
  69. package/lib/node/index.d.ts +1 -0
  70. package/lib/node/index.js +1 -0
  71. package/lib/node/relay-chat-files.d.ts +53 -0
  72. package/lib/node/relay-chat-files.js +228 -0
  73. package/lib/node/relay-hub-files.d.ts +11 -1
  74. package/lib/node/relay-hub-files.js +8 -1
  75. package/lib/node/relay-server.d.ts +9 -1
  76. package/lib/node/relay-server.js +27 -16
  77. package/package.json +3 -3
  78. package/viewer/join.html +4 -4
  79. package/viewer/page.html +10 -10
  80. package/viewer/room.html +15 -15
@@ -0,0 +1,371 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayHubsChats = void 0;
4
+ const relay_chat_store_1 = require("./relay-chat-store");
5
+ const relay_hub_util_1 = require("./relay-hub-util");
6
+ const relay_hubs_admins_1 = require("./relay-hubs-admins");
7
+ const relay_hubs_live_1 = require("./relay-hubs-live");
8
+ const relay_protocol_1 = require("./relay-protocol");
9
+ const relay_room_chat_1 = require("./relay-room-chat");
10
+ /** Ids of lines taken kept to know one again: the relay could serve an old one anew. */
11
+ const IDS_KEPT = 2000;
12
+ /** How soon after the last line taken the relay is told how far this Ta has the log. */
13
+ const ACK_MS = 2000;
14
+ /** The log of a room as this Ta follows it while it runs. */
15
+ class LiveChat {
16
+ constructor() {
17
+ /** A page of the log asked for, not come yet. */
18
+ this.syncing = false;
19
+ this.sending = false;
20
+ this.acked = 0;
21
+ this.lastAt = 0;
22
+ this.ids = new Set();
23
+ /** What changes the state, one after another. */
24
+ this.chain = Promise.resolve();
25
+ }
26
+ run(work) {
27
+ const done = this.chain.then(work);
28
+ this.chain = done.catch(() => undefined);
29
+ return done;
30
+ }
31
+ }
32
+ /**
33
+ * The part of {@link RelayHubs} that keeps what is said in the rooms (docs/design/ta-chat-history.md 4–6): the log of
34
+ * each room read from the relay after the last number taken, kept on this computer (RelayChatStore); lines said go
35
+ * into an outbox first and to the relay once it is reached, the same line again being the same message there, so
36
+ * nothing is lost nor said twice; a number missed is read again; the relay is told how far this Ta has it.
37
+ */
38
+ class RelayHubsChats extends relay_hubs_admins_1.RelayHubsAdmins {
39
+ constructor() {
40
+ var _a, _b;
41
+ var _c, _d;
42
+ super(...arguments);
43
+ this.chats = (_d = (_c = this.options.chats) !== null && _c !== void 0 ? _c : (_b = (_a = this.options.store).chats) === null || _b === void 0 ? void 0 : _b.call(_a)) !== null && _d !== void 0 ? _d : new relay_chat_store_1.RelayChatMemory();
44
+ this.following = new Map();
45
+ }
46
+ /** The latest lines of a room kept here, before the line `before` when given; those not sent yet last. */
47
+ async readChat(roomId, options = {}) {
48
+ var _a;
49
+ var _b;
50
+ const live = this.live.get(roomId);
51
+ if (!live) {
52
+ throw new Error('No such room.');
53
+ }
54
+ const limit = Math.min(Math.max(Math.round(Number(options.limit) || 100), 1), 1000);
55
+ const before = typeof options.before === 'string' ? options.before : undefined;
56
+ const chat = await this.chatOf(live);
57
+ const read = await this.chats.read(roomId, { limit, ...(before ? { before } : {}) });
58
+ return before ? read : { lines: [...read.lines, ...((_b = (_a = chat.state) === null || _a === void 0 ? void 0 : _a.outbox) !== null && _b !== void 0 ? _b : [])], more: read.more };
59
+ }
60
+ /** Says it as this Ta in a room: kept at once, sent once the relay is reached. The line, `pending` till then. */
61
+ async say(roomId, text, extra = {}) {
62
+ var _a, _b, _c, _d;
63
+ var _e;
64
+ const live = this.member(roomId);
65
+ const said = String(text !== null && text !== void 0 ? text : '')
66
+ .trim()
67
+ .slice(0, relay_room_chat_1.RELAY_CHAT_TEXT_MAX);
68
+ const added = relay_room_chat_1.RelayRoomChatUtil.extra(extra);
69
+ if (!said && !((_a = added.images) === null || _a === void 0 ? void 0 : _a.length) && !((_b = added.files) === null || _b === void 0 ? void 0 : _b.length)) {
70
+ return undefined;
71
+ }
72
+ const chat = await this.chatOf(live);
73
+ const identity = await this.identityOf(live);
74
+ const name = (_e = (_d = (_c = live.roster) === null || _c === void 0 ? void 0 : _c.members.find(member => member.member === live.record.member)) === null || _d === void 0 ? void 0 : _d.name) !== null && _e !== void 0 ? _e : identity.name;
75
+ const line = await chat.run(async () => {
76
+ const state = chat.state;
77
+ const at = Math.max(Date.now(), chat.lastAt + 1);
78
+ chat.lastAt = at;
79
+ const made = {
80
+ id: relay_room_chat_1.RelayRoomChatUtil.id(),
81
+ from: live.record.member,
82
+ name,
83
+ text: said,
84
+ at,
85
+ ...added,
86
+ pending: true,
87
+ };
88
+ state.outbox.push(made);
89
+ await this.chats.setState(roomId, state);
90
+ return made;
91
+ });
92
+ this.emit({ type: 'chat', room: roomId, line });
93
+ void this.flush(live, chat);
94
+ return line;
95
+ }
96
+ /** What the hub of a room says of its log; `synced` once connected, to read what came while away. */
97
+ async heardLog(live, event) {
98
+ var _a;
99
+ var _b;
100
+ if (live.record.status !== 'member' || !live.keys) {
101
+ return;
102
+ }
103
+ const chat = await this.chatOf(live);
104
+ switch (event.t) {
105
+ case 'synced':
106
+ chat.syncing = false;
107
+ await chat.run(() => this.migrate(live, chat));
108
+ this.sync(live, chat);
109
+ return;
110
+ case 'log':
111
+ chat.syncing = false;
112
+ await chat.run(() => this.took(live, chat, event.items));
113
+ if (event.more) {
114
+ this.sync(live, chat);
115
+ }
116
+ else {
117
+ this.ack(live, chat);
118
+ void this.flush(live, chat);
119
+ }
120
+ return;
121
+ case 'post': {
122
+ const seq = (_b = (_a = chat.state) === null || _a === void 0 ? void 0 : _a.seq) !== null && _b !== void 0 ? _b : 0;
123
+ if (event.item.s === seq + 1) {
124
+ await chat.run(() => this.took(live, chat, [event.item]));
125
+ clearTimeout(chat.ackTimer);
126
+ chat.ackTimer = setTimeout(() => this.ack(live, chat), ACK_MS);
127
+ }
128
+ else if (event.item.s > seq) {
129
+ // One missed between: read from where this Ta is.
130
+ this.sync(live, chat);
131
+ }
132
+ return;
133
+ }
134
+ }
135
+ }
136
+ /** The bytes kept on this computer of what was said in each room this Ta is in; with `before`, what clearChat would free. */
137
+ async chatUsage(before) {
138
+ const usage = {};
139
+ const until = typeof before === 'number' && Number.isFinite(before) ? before : undefined;
140
+ for (const id of this.live.keys()) {
141
+ usage[id] = await this.chats.usage(id, until).catch(() => 0);
142
+ }
143
+ return usage;
144
+ }
145
+ /**
146
+ * What was said in a room goes from this computer: all of it, or what was said before `before` (a month at a time).
147
+ * The others keep theirs; the pages read the chat again.
148
+ */
149
+ async clearChat(roomId, before) {
150
+ if (!this.live.has(roomId)) {
151
+ throw new Error('No such room.');
152
+ }
153
+ const chat = this.following.get(roomId);
154
+ const clear = () => this.chats.clear(roomId, typeof before === 'number' && Number.isFinite(before) ? before : undefined);
155
+ await (chat ? chat.run(clear) : clear());
156
+ if (chat) {
157
+ chat.ids.clear();
158
+ }
159
+ this.emit({ type: 'rooms' });
160
+ }
161
+ /** The room was left: what was said there goes from this computer. */
162
+ async forgetChat(roomId) {
163
+ const chat = this.following.get(roomId);
164
+ clearTimeout(chat === null || chat === void 0 ? void 0 : chat.ackTimer);
165
+ this.following.delete(roomId);
166
+ await this.chats.forget(roomId).catch(() => undefined);
167
+ }
168
+ chatOf(live) {
169
+ var _a;
170
+ let chat = this.following.get(live.record.id);
171
+ if (!chat) {
172
+ chat = new LiveChat();
173
+ this.following.set(live.record.id, chat);
174
+ }
175
+ const found = chat;
176
+ (_a = found.loading) !== null && _a !== void 0 ? _a : (found.loading = (async () => {
177
+ var _a;
178
+ const state = (_a = (await this.chats.state(live.record.id))) !== null && _a !== void 0 ? _a : { seq: 0, outbox: [] };
179
+ const latest = await this.chats.read(live.record.id, { limit: IDS_KEPT });
180
+ for (const line of latest.lines) {
181
+ found.ids.add(line.id);
182
+ }
183
+ found.lastAt = Math.max(0, ...latest.lines.map(line => line.at), ...state.outbox.map(line => line.at));
184
+ found.acked = state.seq;
185
+ found.state = state;
186
+ return state;
187
+ })());
188
+ return found.loading.then(() => found);
189
+ }
190
+ /**
191
+ * The first time the log of a room is read here: what was said in its state before the log (`chat:` keys), from
192
+ * when this Ta joined, is kept as the start of it (docs/design/ta-chat-history.md 11).
193
+ */
194
+ async migrate(live, chat) {
195
+ var _a, _b;
196
+ const state = chat.state;
197
+ if (state.migrated) {
198
+ return;
199
+ }
200
+ const since = live.record.own ? 0 : ((_a = live.record.joinedAt) !== null && _a !== void 0 ? _a : 0);
201
+ const lines = [];
202
+ for (const [key, value] of live.state) {
203
+ const part = relay_room_chat_1.RelayRoomChatUtil.parse(key);
204
+ if (part && key.startsWith(relay_room_chat_1.RELAY_CHAT_KEY) && value.value !== null) {
205
+ lines.push(...((_b = relay_room_chat_1.RelayRoomChatUtil.lines(part.member, value.value, value.by)) !== null && _b !== void 0 ? _b : []));
206
+ }
207
+ }
208
+ const kept = lines.filter(line => line.at >= since && !chat.ids.has(line.id)).sort((a, b) => a.at - b.at || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
209
+ if (kept.length) {
210
+ await this.chats.append(live.record.id, kept);
211
+ for (const line of kept) {
212
+ chat.ids.add(line.id);
213
+ }
214
+ }
215
+ state.migrated = true;
216
+ await this.chats.setState(live.record.id, state);
217
+ }
218
+ /** Lines of the log come, in order: each opened against the key its member signs with, kept, told. */
219
+ async took(live, chat, items) {
220
+ var _a, _b;
221
+ var _c;
222
+ const state = chat.state;
223
+ const keys = live.keys;
224
+ if (!keys) {
225
+ return;
226
+ }
227
+ const signs = { ...state.signs };
228
+ for (const member of (_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : []) {
229
+ signs[member.member] = member.sign;
230
+ }
231
+ state.signs = signs;
232
+ const taken = [];
233
+ for (const item of items) {
234
+ if (!(item.s > state.seq)) {
235
+ continue;
236
+ }
237
+ state.seq = item.s;
238
+ const signed = await relay_hub_util_1.RelayHubUtil.openIn(keys, item.d, member => signs[member]);
239
+ const line = signed && signed.m === item.m ? relay_room_chat_1.RelayRoomChatUtil.signed(item.m, (_b = signed.b) === null || _b === void 0 ? void 0 : _b.chat, item.s) : undefined;
240
+ if (!line || chat.ids.has(line.id)) {
241
+ continue;
242
+ }
243
+ this.remember(chat, line.id);
244
+ state.outbox = state.outbox.filter(waiting => waiting.id !== line.id);
245
+ taken.push(line);
246
+ }
247
+ if (taken.length) {
248
+ await this.chats.append(live.record.id, taken);
249
+ }
250
+ await this.chats.setState(live.record.id, state);
251
+ for (const line of taken) {
252
+ this.emit({ type: 'chat', room: live.record.id, line });
253
+ }
254
+ }
255
+ /** Sends the lines waiting, one after another, while the room is reached. */
256
+ async flush(live, chat) {
257
+ var _a, _b;
258
+ if (chat.sending || chat.syncing || !(((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === relay_hubs_live_1.OPEN && live.synced)) {
259
+ return;
260
+ }
261
+ chat.sending = true;
262
+ try {
263
+ for (;;) {
264
+ const line = (_b = chat.state) === null || _b === void 0 ? void 0 : _b.outbox.find(waiting => waiting.pending);
265
+ if (!line) {
266
+ return;
267
+ }
268
+ const result = await this.post(live, line);
269
+ if (result.error === 'large' || result.error === 'invalid') {
270
+ await chat.run(() => this.refused(live, chat, line));
271
+ continue;
272
+ }
273
+ if (!result.ok || !(await chat.run(() => this.posted(live, chat, line, result.v)))) {
274
+ return;
275
+ }
276
+ }
277
+ }
278
+ finally {
279
+ chat.sending = false;
280
+ }
281
+ }
282
+ async post(live, line) {
283
+ const identity = await this.identityOf(live);
284
+ const [stored] = relay_room_chat_1.RelayRoomChatUtil.stored([line]);
285
+ const d = await relay_hub_util_1.RelayHubUtil.sealIn(live.keys, identity, live.record.member, { chat: stored });
286
+ const id = this.nextId++;
287
+ return new Promise(resolve => {
288
+ const timer = setTimeout(() => {
289
+ live.done.delete(id);
290
+ resolve({ ok: false, v: 0 });
291
+ }, relay_hubs_live_1.READY_MS);
292
+ live.done.set(id, answer => {
293
+ clearTimeout(timer);
294
+ resolve(answer);
295
+ });
296
+ try {
297
+ this.request(live, { t: 'post', id, mid: line.id, d });
298
+ }
299
+ catch (_a) {
300
+ clearTimeout(timer);
301
+ live.done.delete(id);
302
+ resolve({ ok: false, v: 0 });
303
+ }
304
+ });
305
+ }
306
+ /** The relay took a line of this Ta as number `seq`: kept when it is the next, else read with what came between. Whether to go on. */
307
+ async posted(live, chat, line, seq) {
308
+ var _a;
309
+ const state = chat.state;
310
+ if (seq <= state.seq || !((_a = chat.state) === null || _a === void 0 ? void 0 : _a.outbox.some(waiting => waiting.id === line.id))) {
311
+ state.outbox = state.outbox.filter(waiting => waiting.id !== line.id);
312
+ await this.chats.setState(live.record.id, state);
313
+ return true;
314
+ }
315
+ if (seq !== state.seq + 1) {
316
+ this.sync(live, chat);
317
+ return false;
318
+ }
319
+ const { pending: _pending, ...sent } = line;
320
+ const taken = { ...sent, seq };
321
+ state.seq = seq;
322
+ state.outbox = state.outbox.filter(waiting => waiting.id !== line.id);
323
+ this.remember(chat, line.id);
324
+ await this.chats.append(live.record.id, [taken]);
325
+ await this.chats.setState(live.record.id, state);
326
+ this.emit({ type: 'chat', room: live.record.id, line: taken });
327
+ clearTimeout(chat.ackTimer);
328
+ chat.ackTimer = setTimeout(() => this.ack(live, chat), ACK_MS);
329
+ return true;
330
+ }
331
+ async refused(live, chat, line) {
332
+ const state = chat.state;
333
+ const { pending: _pending, ...rest } = line;
334
+ const failed = { ...rest, failed: true };
335
+ state.outbox = state.outbox.map(waiting => (waiting.id === line.id ? failed : waiting));
336
+ await this.chats.setState(live.record.id, state);
337
+ this.emit({ type: 'chat', room: live.record.id, line: failed });
338
+ }
339
+ sync(live, chat) {
340
+ var _a, _b;
341
+ var _c;
342
+ if (chat.syncing || ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) !== relay_hubs_live_1.OPEN) {
343
+ return;
344
+ }
345
+ chat.syncing = true;
346
+ try {
347
+ this.request(live, { t: 'sync', after: (_c = (_b = chat.state) === null || _b === void 0 ? void 0 : _b.seq) !== null && _c !== void 0 ? _c : 0, limit: relay_protocol_1.RELAY_HUB_SYNC_PAGE });
348
+ }
349
+ catch (_d) {
350
+ chat.syncing = false;
351
+ }
352
+ }
353
+ ack(live, chat) {
354
+ var _a, _b;
355
+ var _c;
356
+ clearTimeout(chat.ackTimer);
357
+ const seq = (_c = (_a = chat.state) === null || _a === void 0 ? void 0 : _a.seq) !== null && _c !== void 0 ? _c : 0;
358
+ if (seq > chat.acked && ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === relay_hubs_live_1.OPEN) {
359
+ chat.acked = seq;
360
+ this.request(live, { t: 'ack', seq });
361
+ }
362
+ }
363
+ remember(chat, id) {
364
+ chat.ids.add(id);
365
+ if (chat.ids.size > IDS_KEPT) {
366
+ chat.ids.delete(chat.ids.values().next().value);
367
+ }
368
+ }
369
+ }
370
+ exports.RelayHubsChats = RelayHubsChats;
371
+ //# sourceMappingURL=relay-hubs-chats.js.map
@@ -3,7 +3,7 @@ import { type RelayHubAskBody, type RelayHubMember } from './relay-hub-util';
3
3
  import { type Live, type MessageBody } from './relay-hubs-live';
4
4
  import type { RelayHubsOptions, RelayRoomEvent, RelayRoomRecord, RelayRoomRequest, RelayRoomStatus } from './relay-hubs-protocol';
5
5
  import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
6
- import { type RelayHubEntry, type RelayHubFailure, type RelayHubRequest, type RelayHubRoster } from './relay-protocol';
6
+ import { type RelayHubEntry, type RelayHubEvent, type RelayHubFailure, type RelayHubRequest, type RelayHubRoster } from './relay-protocol';
7
7
  /**
8
8
  * The half of {@link RelayHubs} that keeps a connection to the hub of each room and reads what comes over it:
9
9
  * connecting again, the welcome, the roster, the state and the messages, each opened and checked against the
@@ -62,6 +62,8 @@ export declare abstract class RelayHubsConnection {
62
62
  protected signOf(live: Live): (member: string) => string | undefined;
63
63
  protected readEntry(live: Live, entry: RelayHubEntry, tell: boolean): Promise<void>;
64
64
  protected readMessage(live: Live, from: string, sealed: string): Promise<void>;
65
+ /** What the hub says of the log of the room, and `synced` (RelayHubsChats). */
66
+ protected heardLog(_live: Live, _event: RelayHubEvent): Promise<void>;
65
67
  /** A member set a key naming another: the owner clears it (RelayHubs). */
66
68
  protected foreignSet(_live: Live, _key: string): void;
67
69
  /** Ids of those the user chose as friends. */
@@ -269,6 +269,11 @@ class RelayHubsConnection {
269
269
  resolve();
270
270
  }
271
271
  this.emit({ type: 'rooms' });
272
+ await this.heardLog(live, event);
273
+ return;
274
+ case 'log':
275
+ case 'post':
276
+ await this.heardLog(live, event);
272
277
  return;
273
278
  case 'state':
274
279
  await this.readEntry(live, event.entry, true);
@@ -524,6 +529,8 @@ class RelayHubsConnection {
524
529
  }
525
530
  this.emit({ type: 'message', room: live.record.id, from, name: (_b = sender === null || sender === void 0 ? void 0 : sender.name) !== null && _b !== void 0 ? _b : '', data: signed.b.d });
526
531
  }
532
+ /** What the hub says of the log of the room, and `synced` (RelayHubsChats). */
533
+ async heardLog(_live, _event) { }
527
534
  /** A member set a key naming another: the owner clears it (RelayHubs). */
528
535
  foreignSet(_live, _key) { }
529
536
  /** Ids of those the user chose as friends. */
@@ -1,4 +1,6 @@
1
+ import type { RelayChatStore } from './relay-chat-store';
1
2
  import type { RelayIdentity, RelayPublicIdentity } from './relay-identity';
3
+ import type { RelayChatLine } from './relay-room-chat';
2
4
  /** `member` in the room; `waiting` for the owner to answer; `refused` by the owner; `gone`: closed, removed, or the invitation no longer works. */
3
5
  export type RelayRoomStatus = 'member' | 'waiting' | 'refused' | 'gone';
4
6
  /** A room this Ta is in or asked to join, in `rooms.json` of the agent directory (docs/design/ta-link.md 3.5). */
@@ -73,6 +75,8 @@ export interface RelayRoomMember {
73
75
  self: boolean;
74
76
  /** In a browser, without Ta: it only chats (docs/design/ta-link.md 3.8). */
75
77
  guest?: boolean;
78
+ /** When it was let in, as the roster says: what was said before is not its to read, unless the room says so. */
79
+ joinedAt?: number;
76
80
  }
77
81
  export interface RelayRoomInfo {
78
82
  id: string;
@@ -89,6 +93,8 @@ export interface RelayRoomInfo {
89
93
  capacity?: number;
90
94
  /** The rules its owner set, for all (RelayHubRules); none when it set none. */
91
95
  rules?: Record<string, boolean>;
96
+ /** Messages from before they joined a newcomer reads, as its owner set; none: none. */
97
+ history?: number;
92
98
  ownerName: string;
93
99
  status: RelayRoomStatus;
94
100
  reason?: RelayRoomRecord['reason'];
@@ -165,6 +171,12 @@ export type RelayRoomEvent = {
165
171
  from: string;
166
172
  name: string;
167
173
  data: unknown;
174
+ }
175
+ /** A line of the chat of a room: said here (`pending`), sent, refused (`failed`), or taken from another member. */
176
+ | {
177
+ type: 'chat';
178
+ room: string;
179
+ line: RelayChatLine;
168
180
  } | {
169
181
  type: 'state';
170
182
  room: string;
@@ -175,6 +187,8 @@ export type RelayRoomEvent = {
175
187
  };
176
188
  /** Where a {@link RelayHubs} keeps what it knows: files of the agent directory for Ta, the storage of a browser for a guest. */
177
189
  export interface RelayHubsStore {
190
+ /** Where what is said in the rooms is kept, when not in memory (RelayHubsOptions.chats says otherwise). */
191
+ chats?(): RelayChatStore | undefined;
178
192
  read<T>(name: RelayHubsStoreName): T | undefined;
179
193
  /** Holds keys: kept where the user alone reads it. */
180
194
  write(name: RelayHubsStoreName, value: unknown): void;
@@ -192,6 +206,8 @@ export interface RelayHubsOptions {
192
206
  guest?: boolean;
193
207
  /** The relay new rooms are made on; `use` the feature a room is for (relays.json), `rooms` when none is said. */
194
208
  relay(use?: string): string;
209
+ /** Where what is said in the rooms is kept (docs/design/ta-chat-history.md 8); in memory without. */
210
+ chats?: RelayChatStore;
195
211
  /** First wait before connecting again; it doubles up to 30 seconds. */
196
212
  reconnectDelayMs?: number;
197
213
  pingMs?: number;
@@ -2,7 +2,7 @@ import { RelayFriendSends, type RelayFriendShare } from './relay-friend-sends';
2
2
  import { RelayFriends } from './relay-friends';
3
3
  import { type RelayGroupInvite, RelayGroupInvites } from './relay-group-invites';
4
4
  import { type RelayHubAskBody } from './relay-hub-util';
5
- import { RelayHubsAdmins } from './relay-hubs-admins';
5
+ import { RelayHubsChats } from './relay-hubs-chats';
6
6
  import { Live } from './relay-hubs-live';
7
7
  import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayRoomEvent, RelayRoomInfo, RelayRoomRecord, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol';
8
8
  import { type RelayPublicIdentity } from './relay-identity';
@@ -12,7 +12,7 @@ import { type RelayPublicIdentity } from './relay-identity';
12
12
  * the others; every message signed by its member and sealed with the key of the room. The friends are a room of
13
13
  * their own (RelayFriends).
14
14
  */
15
- export declare class RelayHubs extends RelayHubsAdmins {
15
+ export declare class RelayHubs extends RelayHubsChats {
16
16
  protected readonly book: RelayFriends;
17
17
  protected readonly groups: RelayGroupInvites;
18
18
  protected readonly sends: RelayFriendSends;
@@ -6,7 +6,7 @@ const relay_friends_1 = require("./relay-friends");
6
6
  const relay_group_invites_1 = require("./relay-group-invites");
7
7
  const relay_hub_charter_1 = require("./relay-hub-charter");
8
8
  const relay_hub_util_1 = require("./relay-hub-util");
9
- const relay_hubs_admins_1 = require("./relay-hubs-admins");
9
+ const relay_hubs_chats_1 = require("./relay-hubs-chats");
10
10
  const relay_hubs_live_1 = require("./relay-hubs-live");
11
11
  const relay_identity_1 = require("./relay-identity");
12
12
  const relay_protocol_1 = require("./relay-protocol");
@@ -17,7 +17,7 @@ const DAY_MS = 24 * 60 * 60 * 1000;
17
17
  * the others; every message signed by its member and sealed with the key of the room. The friends are a room of
18
18
  * their own (RelayFriends).
19
19
  */
20
- class RelayHubs extends relay_hubs_admins_1.RelayHubsAdmins {
20
+ class RelayHubs extends relay_hubs_chats_1.RelayHubsChats {
21
21
  constructor() {
22
22
  super(...arguments);
23
23
  this.book = new relay_friends_1.RelayFriends({
@@ -233,8 +233,8 @@ class RelayHubs extends relay_hubs_admins_1.RelayHubsAdmins {
233
233
  }
234
234
  /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
235
235
  async answer(roomId, askId, accept) {
236
- var _a, _b;
237
- var _c;
236
+ var _a, _b, _c;
237
+ var _d, _e;
238
238
  const live = this.managed(roomId);
239
239
  const asked = live.asks.get(askId);
240
240
  if (!asked) {
@@ -255,7 +255,7 @@ class RelayHubs extends relay_hubs_admins_1.RelayHubsAdmins {
255
255
  ...(live.record.unlisted ? { unlisted: true } : {}),
256
256
  };
257
257
  const ask = asked.body;
258
- if (((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _c !== void 0 ? _c : 0) >= this.capacityOf(live)) {
258
+ if (((_d = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _d !== void 0 ? _d : 0) >= this.capacityOf(live)) {
259
259
  throw new Error('The room is full.');
260
260
  }
261
261
  const head = live.record.owner;
@@ -265,7 +265,7 @@ class RelayHubs extends relay_hubs_admins_1.RelayHubsAdmins {
265
265
  const answer = await relay_hub_util_1.RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite, proof);
266
266
  const member = { ...relay_identity_1.RelayIdentityUtil.publicOf(ask), member: ask.member, ...(ask.guest ? { guest: true } : {}), at: Date.now() };
267
267
  const roster = await this.nextRoster(live, members => [...members.filter(other => other.member !== ask.member), member]);
268
- this.request(live, { t: 'admit', ask: askId, answer, roster });
268
+ this.request(live, { t: 'admit', ask: askId, answer, roster, history: (_e = (_c = live.charter) === null || _c === void 0 ? void 0 : _c.history) !== null && _e !== void 0 ? _e : 0 });
269
269
  }
270
270
  live.asks.delete(askId);
271
271
  this.emit({ type: 'rooms' });
@@ -302,6 +302,7 @@ class RelayHubs extends relay_hubs_admins_1.RelayHubsAdmins {
302
302
  this.disconnect(live);
303
303
  this.live.delete(roomId);
304
304
  this.save();
305
+ await this.forgetChat(roomId);
305
306
  this.emit({ type: 'rooms' });
306
307
  }
307
308
  /** To every other member, or to one (`to`, its member id). */
@@ -501,10 +502,10 @@ class RelayHubs extends relay_hubs_admins_1.RelayHubsAdmins {
501
502
  }
502
503
  }
503
504
  info(live) {
504
- var _a, _b;
505
- var _c, _d;
505
+ var _a, _b, _c;
506
+ var _d, _e;
506
507
  const { record } = live;
507
- const members = ((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : []).map(member => { var _a; return ({
508
+ const members = ((_d = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _d !== void 0 ? _d : []).map(member => { var _a; return ({
508
509
  member: member.member,
509
510
  id: member.id,
510
511
  name: member.name,
@@ -514,6 +515,7 @@ class RelayHubs extends relay_hubs_admins_1.RelayHubsAdmins {
514
515
  online: member.member === record.member ? ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === relay_hubs_live_1.OPEN && live.synced : live.online.has(member.member),
515
516
  self: member.member === record.member,
516
517
  ...(member.guest ? { guest: true } : {}),
518
+ ...(Number.isFinite(member.at) ? { joinedAt: member.at } : {}),
517
519
  }); });
518
520
  return {
519
521
  id: record.id,
@@ -524,12 +526,13 @@ class RelayHubs extends relay_hubs_admins_1.RelayHubsAdmins {
524
526
  relay: record.relay,
525
527
  owner: record.own,
526
528
  ...(!record.own && this.isAdmin(live, record.member) ? { admin: true } : {}),
527
- ...(record.own ? { capacity: (_d = record.capacity) !== null && _d !== void 0 ? _d : relay_protocol_1.RELAY_ROOM_MAX_MEMBERS } : {}),
529
+ ...(record.own ? { capacity: (_e = record.capacity) !== null && _e !== void 0 ? _e : relay_protocol_1.RELAY_ROOM_MAX_MEMBERS } : {}),
528
530
  ...(live.charter && Object.keys(live.charter.rules).length ? { rules: { ...live.charter.rules } } : {}),
531
+ ...(((_b = live.charter) === null || _b === void 0 ? void 0 : _b.history) ? { history: live.charter.history } : {}),
529
532
  ownerName: record.owner.name,
530
533
  status: record.status,
531
534
  ...(record.reason ? { reason: record.reason } : {}),
532
- connected: ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === relay_hubs_live_1.OPEN && (live.synced || record.status === 'waiting'),
535
+ connected: ((_c = live.socket) === null || _c === void 0 ? void 0 : _c.readyState) === relay_hubs_live_1.OPEN && (live.synced || record.status === 'waiting'),
533
536
  members,
534
537
  createdAt: record.createdAt,
535
538
  ...(record.joinedAt ? { joinedAt: record.joinedAt } : {}),
@@ -19,7 +19,24 @@ export interface RelayInfo {
19
19
  limits: RelayLimits;
20
20
  /** Whether rooms and copies stay when the relay restarts. */
21
21
  persistent: boolean;
22
+ /**
23
+ * Sealed copies of the pictures of chats its operator lets it keep (RELAY_MEDIA_PATH), for whoever comes while no
24
+ * member who has one is online: so many days, so many bytes each. None (the default): it keeps none.
25
+ */
26
+ media?: RelayMedia;
22
27
  }
28
+ /** What a relay keeps of the pictures of chats (RelayInfo.media), as its operator set it. */
29
+ export interface RelayMedia {
30
+ days: number;
31
+ bytes: number;
32
+ }
33
+ /** The most an operator may let a relay keep of a picture: as long and as large as a copy of content. */
34
+ export declare const RELAY_MEDIA_MAX: RelayMedia;
35
+ /**
36
+ * Days a relay keeps a picture of a chat unless its operator says otherwise: short, so that what it keeps stays small
37
+ * however many use it, long enough for the others to open a chat the sender left while away.
38
+ */
39
+ export declare const RELAY_MEDIA_DEFAULT_DAYS = 1;
23
40
  /** A relay that answers no RELAY_INFO_PATH, made before it: all it can do, with the limits of then. */
24
41
  export declare const RELAY_LEGACY_INFO: RelayInfo;
25
42
  /** What a probe of a relay found. */
@@ -33,7 +50,12 @@ export interface RelayProbe {
33
50
  error?: string;
34
51
  }
35
52
  export declare namespace RelayInfoUtil {
36
- function of(persistent: boolean): RelayInfo;
53
+ function of(persistent: boolean, media?: RelayMedia): RelayInfo;
54
+ /**
55
+ * What the operator of a relay lets it keep of the pictures of chats, from its settings (`TA_MEDIA_DAYS`,
56
+ * `TA_MEDIA_MAX_MB`): RELAY_MEDIA_DEFAULT_DAYS unless set, none with 0, within RELAY_MEDIA_MAX.
57
+ */
58
+ function media(days: unknown, megabytes?: unknown): RelayMedia | undefined;
37
59
  /** The answer of RELAY_INFO_PATH: JSON, readable by any page (it says nothing secret). */
38
60
  function response(info: RelayInfo): Response;
39
61
  /** The address of a relay as it is kept: http(s), its origin and path, no trailing slash. Throws on what is not one. */