@celljs/ta-relay 4.21.0 → 4.23.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/esm/common/index.d.ts +3 -0
- package/esm/common/index.js +3 -0
- package/esm/common/relay-blobs.d.ts +36 -2
- package/esm/common/relay-blobs.js +33 -5
- package/esm/common/relay-chat-store.d.ts +59 -0
- package/esm/common/relay-chat-store.js +40 -0
- package/esm/common/relay-hub-charter.d.ts +7 -1
- package/esm/common/relay-hub-charter.js +6 -0
- package/esm/common/relay-hub-log.d.ts +65 -0
- package/esm/common/relay-hub-log.js +147 -0
- package/esm/common/relay-hub.d.ts +3 -0
- package/esm/common/relay-hub.js +18 -0
- package/esm/common/relay-hubs-admins.d.ts +1 -0
- package/esm/common/relay-hubs-admins.js +10 -6
- package/esm/common/relay-hubs-chats.d.ts +66 -0
- package/esm/common/relay-hubs-chats.js +342 -0
- package/esm/common/relay-hubs-connection.d.ts +3 -1
- package/esm/common/relay-hubs-connection.js +7 -0
- package/esm/common/relay-hubs-protocol.d.ts +14 -0
- package/esm/common/relay-hubs.d.ts +5 -2
- package/esm/common/relay-hubs.js +31 -13
- package/esm/common/relay-info.d.ts +23 -1
- package/esm/common/relay-info.js +25 -2
- package/esm/common/relay-protocol.d.ts +60 -1
- package/esm/common/relay-protocol.js +15 -0
- package/esm/common/relay-room-app.d.ts +106 -0
- package/esm/common/relay-room-app.js +171 -0
- package/esm/common/relay-room-chat.d.ts +126 -4
- package/esm/common/relay-room-chat.js +230 -6
- package/esm/guest/guest-i18n.d.ts +5 -0
- package/esm/guest/guest-i18n.js +20 -0
- package/esm/guest/guest-style.d.ts +1 -1
- package/esm/guest/guest-style.js +11 -0
- package/esm/guest/guest.js +84 -11
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- package/esm/node/relay-chat-files.d.ts +46 -0
- package/esm/node/relay-chat-files.js +199 -0
- package/esm/node/relay-hub-files.d.ts +11 -1
- package/esm/node/relay-hub-files.js +8 -1
- package/esm/node/relay-server.d.ts +7 -1
- package/esm/node/relay-server.js +23 -13
- package/lib/common/index.d.ts +3 -0
- package/lib/common/index.js +3 -0
- package/lib/common/relay-blobs.d.ts +36 -2
- package/lib/common/relay-blobs.js +34 -6
- package/lib/common/relay-chat-store.d.ts +60 -0
- package/lib/common/relay-chat-store.js +44 -0
- package/lib/common/relay-hub-charter.d.ts +7 -1
- package/lib/common/relay-hub-charter.js +7 -1
- package/lib/common/relay-hub-log.d.ts +66 -0
- package/lib/common/relay-hub-log.js +151 -0
- package/lib/common/relay-hub.d.ts +3 -0
- package/lib/common/relay-hub.js +18 -0
- package/lib/common/relay-hubs-admins.d.ts +1 -0
- package/lib/common/relay-hubs-admins.js +9 -5
- package/lib/common/relay-hubs-chats.d.ts +67 -0
- package/lib/common/relay-hubs-chats.js +346 -0
- package/lib/common/relay-hubs-connection.d.ts +3 -1
- package/lib/common/relay-hubs-connection.js +7 -0
- package/lib/common/relay-hubs-protocol.d.ts +14 -0
- package/lib/common/relay-hubs.d.ts +5 -2
- package/lib/common/relay-hubs.js +30 -12
- package/lib/common/relay-info.d.ts +23 -1
- package/lib/common/relay-info.js +26 -3
- package/lib/common/relay-protocol.d.ts +60 -1
- package/lib/common/relay-protocol.js +16 -1
- package/lib/common/relay-room-app.d.ts +107 -0
- package/lib/common/relay-room-app.js +176 -0
- package/lib/common/relay-room-chat.d.ts +126 -4
- package/lib/common/relay-room-chat.js +231 -7
- package/lib/guest/guest-i18n.d.ts +5 -0
- package/lib/guest/guest-i18n.js +20 -0
- package/lib/guest/guest-style.d.ts +1 -1
- package/lib/guest/guest-style.js +11 -0
- package/lib/guest/guest.js +83 -10
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.js +1 -0
- package/lib/node/relay-chat-files.d.ts +47 -0
- package/lib/node/relay-chat-files.js +203 -0
- package/lib/node/relay-hub-files.d.ts +11 -1
- package/lib/node/relay-hub-files.js +8 -1
- package/lib/node/relay-server.d.ts +7 -1
- package/lib/node/relay-server.js +21 -11
- package/package.json +3 -3
- package/viewer/join.html +17 -6
- package/viewer/page.html +10 -10
- package/viewer/room.html +26 -15
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RELAY_FRIENDS_KIND } from './relay-friends.js';
|
|
2
2
|
import { RELAY_HUB_MAX_ADMINS, RelayHubCharterUtil } from './relay-hub-charter.js';
|
|
3
3
|
import { RelayHubsIdentity } from './relay-hubs-identity.js';
|
|
4
|
-
import { RELAY_FRIENDS_MAX, RELAY_ROOM_CAPACITY_MAX, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
|
|
4
|
+
import { RELAY_FRIENDS_MAX, RELAY_HUB_HISTORY_MAX, RELAY_ROOM_CAPACITY_MAX, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
|
|
5
5
|
/**
|
|
6
6
|
* The part of {@link RelayHubs} that says who may do what in a room (docs/design/ta-link.md 8): the owner's choices,
|
|
7
7
|
* signed in the charter of the roster (its admins, its rules, how many it lets in), and the checks of the calls.
|
|
@@ -13,8 +13,8 @@ export class RelayHubsAdmins extends RelayHubsIdentity {
|
|
|
13
13
|
* Ta of the room, not guests) and its rules, for all; the last two signed in the charter of the roster.
|
|
14
14
|
*/
|
|
15
15
|
async configure(roomId, options) {
|
|
16
|
-
var _a;
|
|
17
|
-
var
|
|
16
|
+
var _a, _b;
|
|
17
|
+
var _c, _d;
|
|
18
18
|
const live = this.owned(roomId);
|
|
19
19
|
let capacity = live.record.capacity;
|
|
20
20
|
if (options.members !== undefined) {
|
|
@@ -29,11 +29,14 @@ export class RelayHubsAdmins extends RelayHubsIdentity {
|
|
|
29
29
|
}
|
|
30
30
|
this.save();
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
const history = options.history === undefined
|
|
33
|
+
? (_a = live.charter) === null || _a === void 0 ? void 0 : _a.history
|
|
34
|
+
: Math.min(Math.max(Math.round(Number(options.history) || 0), 0), RELAY_HUB_HISTORY_MAX) || undefined;
|
|
35
|
+
if (options.admins === undefined && options.rules === undefined && options.history === undefined && (options.members === undefined || !live.charter)) {
|
|
33
36
|
return this.info(live);
|
|
34
37
|
}
|
|
35
|
-
const before = (
|
|
36
|
-
const roster = (
|
|
38
|
+
const before = (_c = live.charter) !== null && _c !== void 0 ? _c : { room: live.record.id, v: 0, admins: [], rules: {} };
|
|
39
|
+
const roster = (_d = (_b = live.roster) === null || _b === void 0 ? void 0 : _b.members) !== null && _d !== void 0 ? _d : [];
|
|
37
40
|
const admins = options.admins === undefined
|
|
38
41
|
? before.admins
|
|
39
42
|
: [...new Set(options.admins.map(String))]
|
|
@@ -47,6 +50,7 @@ export class RelayHubsAdmins extends RelayHubsIdentity {
|
|
|
47
50
|
admins,
|
|
48
51
|
rules: options.rules === undefined ? before.rules : RelayHubCharterUtil.rules(options.rules),
|
|
49
52
|
...(capacity ? { capacity } : {}),
|
|
53
|
+
...(history ? { history } : {}),
|
|
50
54
|
};
|
|
51
55
|
await this.ready(live);
|
|
52
56
|
await this.writeRoster(live, same => same, charter);
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { type RelayChatState, type RelayChatStore } from './relay-chat-store.js';
|
|
2
|
+
import { RelayHubsAdmins } from './relay-hubs-admins.js';
|
|
3
|
+
import { type Live } from './relay-hubs-live.js';
|
|
4
|
+
import { type RelayHubEvent, type RelayHubFailure, type RelayHubLogItem } from './relay-protocol.js';
|
|
5
|
+
import { type RelayChatLine, type RelayChatLineExtra } from './relay-room-chat.js';
|
|
6
|
+
/** The log of a room as this Ta follows it while it runs. */
|
|
7
|
+
declare class LiveChat {
|
|
8
|
+
state?: RelayChatState;
|
|
9
|
+
loading?: Promise<RelayChatState>;
|
|
10
|
+
/** A page of the log asked for, not come yet. */
|
|
11
|
+
syncing: boolean;
|
|
12
|
+
sending: boolean;
|
|
13
|
+
acked: number;
|
|
14
|
+
ackTimer?: ReturnType<typeof setTimeout>;
|
|
15
|
+
lastAt: number;
|
|
16
|
+
readonly ids: Set<string>;
|
|
17
|
+
/** What changes the state, one after another. */
|
|
18
|
+
protected chain: Promise<unknown>;
|
|
19
|
+
run<T>(work: () => Promise<T>): Promise<T>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The part of {@link RelayHubs} that keeps what is said in the rooms (docs/design/ta-chat-history.md 4–6): the log of
|
|
23
|
+
* each room read from the relay after the last number taken, kept on this computer (RelayChatStore); lines said go
|
|
24
|
+
* into an outbox first and to the relay once it is reached, the same line again being the same message there, so
|
|
25
|
+
* nothing is lost nor said twice; a number missed is read again; the relay is told how far this Ta has it.
|
|
26
|
+
*/
|
|
27
|
+
export declare abstract class RelayHubsChats extends RelayHubsAdmins {
|
|
28
|
+
protected readonly chats: RelayChatStore;
|
|
29
|
+
protected readonly following: Map<string, LiveChat>;
|
|
30
|
+
/** The latest lines of a room kept here, before the line `before` when given; those not sent yet last. */
|
|
31
|
+
readChat(roomId: string, options?: {
|
|
32
|
+
before?: string;
|
|
33
|
+
limit?: number;
|
|
34
|
+
}): Promise<{
|
|
35
|
+
lines: RelayChatLine[];
|
|
36
|
+
more: boolean;
|
|
37
|
+
}>;
|
|
38
|
+
/** Says it as this Ta in a room: kept at once, sent once the relay is reached. The line, `pending` till then. */
|
|
39
|
+
say(roomId: string, text: string, extra?: RelayChatLineExtra): Promise<RelayChatLine | undefined>;
|
|
40
|
+
/** What the hub of a room says of its log; `synced` once connected, to read what came while away. */
|
|
41
|
+
protected heardLog(live: Live, event: RelayHubEvent): Promise<void>;
|
|
42
|
+
/** The room was left: what was said there goes from this computer. */
|
|
43
|
+
protected forgetChat(roomId: string): Promise<void>;
|
|
44
|
+
protected chatOf(live: Live): Promise<LiveChat>;
|
|
45
|
+
/**
|
|
46
|
+
* The first time the log of a room is read here: what was said in its state before the log (`chat:` keys), from
|
|
47
|
+
* when this Ta joined, is kept as the start of it (docs/design/ta-chat-history.md 11).
|
|
48
|
+
*/
|
|
49
|
+
protected migrate(live: Live, chat: LiveChat): Promise<void>;
|
|
50
|
+
/** Lines of the log come, in order: each opened against the key its member signs with, kept, told. */
|
|
51
|
+
protected took(live: Live, chat: LiveChat, items: readonly RelayHubLogItem[]): Promise<void>;
|
|
52
|
+
/** Sends the lines waiting, one after another, while the room is reached. */
|
|
53
|
+
protected flush(live: Live, chat: LiveChat): Promise<void>;
|
|
54
|
+
protected post(live: Live, line: RelayChatLine): Promise<{
|
|
55
|
+
ok: boolean;
|
|
56
|
+
v: number;
|
|
57
|
+
error?: RelayHubFailure;
|
|
58
|
+
}>;
|
|
59
|
+
/** 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. */
|
|
60
|
+
protected posted(live: Live, chat: LiveChat, line: RelayChatLine, seq: number): Promise<boolean>;
|
|
61
|
+
protected refused(live: Live, chat: LiveChat, line: RelayChatLine): Promise<void>;
|
|
62
|
+
protected sync(live: Live, chat: LiveChat): void;
|
|
63
|
+
protected ack(live: Live, chat: LiveChat): void;
|
|
64
|
+
protected remember(chat: LiveChat, id: string): void;
|
|
65
|
+
}
|
|
66
|
+
export {};
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { RelayChatMemory } from './relay-chat-store.js';
|
|
2
|
+
import { RelayHubUtil } from './relay-hub-util.js';
|
|
3
|
+
import { RelayHubsAdmins } from './relay-hubs-admins.js';
|
|
4
|
+
import { OPEN, READY_MS } from './relay-hubs-live.js';
|
|
5
|
+
import { RELAY_HUB_SYNC_PAGE } from './relay-protocol.js';
|
|
6
|
+
import { RELAY_CHAT_KEY, RELAY_CHAT_TEXT_MAX, RelayRoomChatUtil } from './relay-room-chat.js';
|
|
7
|
+
/** Ids of lines taken kept to know one again: the relay could serve an old one anew. */
|
|
8
|
+
const IDS_KEPT = 2000;
|
|
9
|
+
/** How soon after the last line taken the relay is told how far this Ta has the log. */
|
|
10
|
+
const ACK_MS = 2000;
|
|
11
|
+
/** The log of a room as this Ta follows it while it runs. */
|
|
12
|
+
class LiveChat {
|
|
13
|
+
constructor() {
|
|
14
|
+
/** A page of the log asked for, not come yet. */
|
|
15
|
+
this.syncing = false;
|
|
16
|
+
this.sending = false;
|
|
17
|
+
this.acked = 0;
|
|
18
|
+
this.lastAt = 0;
|
|
19
|
+
this.ids = new Set();
|
|
20
|
+
/** What changes the state, one after another. */
|
|
21
|
+
this.chain = Promise.resolve();
|
|
22
|
+
}
|
|
23
|
+
run(work) {
|
|
24
|
+
const done = this.chain.then(work);
|
|
25
|
+
this.chain = done.catch(() => undefined);
|
|
26
|
+
return done;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The part of {@link RelayHubs} that keeps what is said in the rooms (docs/design/ta-chat-history.md 4–6): the log of
|
|
31
|
+
* each room read from the relay after the last number taken, kept on this computer (RelayChatStore); lines said go
|
|
32
|
+
* into an outbox first and to the relay once it is reached, the same line again being the same message there, so
|
|
33
|
+
* nothing is lost nor said twice; a number missed is read again; the relay is told how far this Ta has it.
|
|
34
|
+
*/
|
|
35
|
+
export class RelayHubsChats extends RelayHubsAdmins {
|
|
36
|
+
constructor() {
|
|
37
|
+
var _a, _b;
|
|
38
|
+
var _c, _d;
|
|
39
|
+
super(...arguments);
|
|
40
|
+
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 RelayChatMemory();
|
|
41
|
+
this.following = new Map();
|
|
42
|
+
}
|
|
43
|
+
/** The latest lines of a room kept here, before the line `before` when given; those not sent yet last. */
|
|
44
|
+
async readChat(roomId, options = {}) {
|
|
45
|
+
var _a;
|
|
46
|
+
var _b;
|
|
47
|
+
const live = this.live.get(roomId);
|
|
48
|
+
if (!live) {
|
|
49
|
+
throw new Error('No such room.');
|
|
50
|
+
}
|
|
51
|
+
const limit = Math.min(Math.max(Math.round(Number(options.limit) || 100), 1), 1000);
|
|
52
|
+
const before = typeof options.before === 'string' ? options.before : undefined;
|
|
53
|
+
const chat = await this.chatOf(live);
|
|
54
|
+
const read = await this.chats.read(roomId, { limit, ...(before ? { before } : {}) });
|
|
55
|
+
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 };
|
|
56
|
+
}
|
|
57
|
+
/** Says it as this Ta in a room: kept at once, sent once the relay is reached. The line, `pending` till then. */
|
|
58
|
+
async say(roomId, text, extra = {}) {
|
|
59
|
+
var _a, _b, _c, _d;
|
|
60
|
+
var _e;
|
|
61
|
+
const live = this.member(roomId);
|
|
62
|
+
const said = String(text !== null && text !== void 0 ? text : '')
|
|
63
|
+
.trim()
|
|
64
|
+
.slice(0, RELAY_CHAT_TEXT_MAX);
|
|
65
|
+
const added = RelayRoomChatUtil.extra(extra);
|
|
66
|
+
if (!said && !((_a = added.images) === null || _a === void 0 ? void 0 : _a.length) && !((_b = added.files) === null || _b === void 0 ? void 0 : _b.length)) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
const chat = await this.chatOf(live);
|
|
70
|
+
const identity = await this.identityOf(live);
|
|
71
|
+
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;
|
|
72
|
+
const line = await chat.run(async () => {
|
|
73
|
+
const state = chat.state;
|
|
74
|
+
const at = Math.max(Date.now(), chat.lastAt + 1);
|
|
75
|
+
chat.lastAt = at;
|
|
76
|
+
const made = {
|
|
77
|
+
id: RelayRoomChatUtil.id(),
|
|
78
|
+
from: live.record.member,
|
|
79
|
+
name,
|
|
80
|
+
text: said,
|
|
81
|
+
at,
|
|
82
|
+
...added,
|
|
83
|
+
pending: true,
|
|
84
|
+
};
|
|
85
|
+
state.outbox.push(made);
|
|
86
|
+
await this.chats.setState(roomId, state);
|
|
87
|
+
return made;
|
|
88
|
+
});
|
|
89
|
+
this.emit({ type: 'chat', room: roomId, line });
|
|
90
|
+
void this.flush(live, chat);
|
|
91
|
+
return line;
|
|
92
|
+
}
|
|
93
|
+
/** What the hub of a room says of its log; `synced` once connected, to read what came while away. */
|
|
94
|
+
async heardLog(live, event) {
|
|
95
|
+
var _a;
|
|
96
|
+
var _b;
|
|
97
|
+
if (live.record.status !== 'member' || !live.keys) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const chat = await this.chatOf(live);
|
|
101
|
+
switch (event.t) {
|
|
102
|
+
case 'synced':
|
|
103
|
+
chat.syncing = false;
|
|
104
|
+
await chat.run(() => this.migrate(live, chat));
|
|
105
|
+
this.sync(live, chat);
|
|
106
|
+
return;
|
|
107
|
+
case 'log':
|
|
108
|
+
chat.syncing = false;
|
|
109
|
+
await chat.run(() => this.took(live, chat, event.items));
|
|
110
|
+
if (event.more) {
|
|
111
|
+
this.sync(live, chat);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
this.ack(live, chat);
|
|
115
|
+
void this.flush(live, chat);
|
|
116
|
+
}
|
|
117
|
+
return;
|
|
118
|
+
case 'post': {
|
|
119
|
+
const seq = (_b = (_a = chat.state) === null || _a === void 0 ? void 0 : _a.seq) !== null && _b !== void 0 ? _b : 0;
|
|
120
|
+
if (event.item.s === seq + 1) {
|
|
121
|
+
await chat.run(() => this.took(live, chat, [event.item]));
|
|
122
|
+
clearTimeout(chat.ackTimer);
|
|
123
|
+
chat.ackTimer = setTimeout(() => this.ack(live, chat), ACK_MS);
|
|
124
|
+
}
|
|
125
|
+
else if (event.item.s > seq) {
|
|
126
|
+
// One missed between: read from where this Ta is.
|
|
127
|
+
this.sync(live, chat);
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** The room was left: what was said there goes from this computer. */
|
|
134
|
+
async forgetChat(roomId) {
|
|
135
|
+
const chat = this.following.get(roomId);
|
|
136
|
+
clearTimeout(chat === null || chat === void 0 ? void 0 : chat.ackTimer);
|
|
137
|
+
this.following.delete(roomId);
|
|
138
|
+
await this.chats.forget(roomId).catch(() => undefined);
|
|
139
|
+
}
|
|
140
|
+
chatOf(live) {
|
|
141
|
+
var _a;
|
|
142
|
+
let chat = this.following.get(live.record.id);
|
|
143
|
+
if (!chat) {
|
|
144
|
+
chat = new LiveChat();
|
|
145
|
+
this.following.set(live.record.id, chat);
|
|
146
|
+
}
|
|
147
|
+
const found = chat;
|
|
148
|
+
(_a = found.loading) !== null && _a !== void 0 ? _a : (found.loading = (async () => {
|
|
149
|
+
var _a;
|
|
150
|
+
const state = (_a = (await this.chats.state(live.record.id))) !== null && _a !== void 0 ? _a : { seq: 0, outbox: [] };
|
|
151
|
+
const latest = await this.chats.read(live.record.id, { limit: IDS_KEPT });
|
|
152
|
+
for (const line of latest.lines) {
|
|
153
|
+
found.ids.add(line.id);
|
|
154
|
+
}
|
|
155
|
+
found.lastAt = Math.max(0, ...latest.lines.map(line => line.at), ...state.outbox.map(line => line.at));
|
|
156
|
+
found.acked = state.seq;
|
|
157
|
+
found.state = state;
|
|
158
|
+
return state;
|
|
159
|
+
})());
|
|
160
|
+
return found.loading.then(() => found);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The first time the log of a room is read here: what was said in its state before the log (`chat:` keys), from
|
|
164
|
+
* when this Ta joined, is kept as the start of it (docs/design/ta-chat-history.md 11).
|
|
165
|
+
*/
|
|
166
|
+
async migrate(live, chat) {
|
|
167
|
+
var _a, _b;
|
|
168
|
+
const state = chat.state;
|
|
169
|
+
if (state.migrated) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const since = live.record.own ? 0 : ((_a = live.record.joinedAt) !== null && _a !== void 0 ? _a : 0);
|
|
173
|
+
const lines = [];
|
|
174
|
+
for (const [key, value] of live.state) {
|
|
175
|
+
const part = RelayRoomChatUtil.parse(key);
|
|
176
|
+
if (part && key.startsWith(RELAY_CHAT_KEY) && value.value !== null) {
|
|
177
|
+
lines.push(...((_b = RelayRoomChatUtil.lines(part.member, value.value, value.by)) !== null && _b !== void 0 ? _b : []));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
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));
|
|
181
|
+
if (kept.length) {
|
|
182
|
+
await this.chats.append(live.record.id, kept);
|
|
183
|
+
for (const line of kept) {
|
|
184
|
+
chat.ids.add(line.id);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
state.migrated = true;
|
|
188
|
+
await this.chats.setState(live.record.id, state);
|
|
189
|
+
}
|
|
190
|
+
/** Lines of the log come, in order: each opened against the key its member signs with, kept, told. */
|
|
191
|
+
async took(live, chat, items) {
|
|
192
|
+
var _a, _b;
|
|
193
|
+
var _c;
|
|
194
|
+
const state = chat.state;
|
|
195
|
+
const keys = live.keys;
|
|
196
|
+
if (!keys) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const signs = { ...state.signs };
|
|
200
|
+
for (const member of (_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : []) {
|
|
201
|
+
signs[member.member] = member.sign;
|
|
202
|
+
}
|
|
203
|
+
state.signs = signs;
|
|
204
|
+
const taken = [];
|
|
205
|
+
for (const item of items) {
|
|
206
|
+
if (!(item.s > state.seq)) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
state.seq = item.s;
|
|
210
|
+
const signed = await RelayHubUtil.openIn(keys, item.d, member => signs[member]);
|
|
211
|
+
const line = signed && signed.m === item.m ? RelayRoomChatUtil.signed(item.m, (_b = signed.b) === null || _b === void 0 ? void 0 : _b.chat, item.s) : undefined;
|
|
212
|
+
if (!line || chat.ids.has(line.id)) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
this.remember(chat, line.id);
|
|
216
|
+
state.outbox = state.outbox.filter(waiting => waiting.id !== line.id);
|
|
217
|
+
taken.push(line);
|
|
218
|
+
}
|
|
219
|
+
if (taken.length) {
|
|
220
|
+
await this.chats.append(live.record.id, taken);
|
|
221
|
+
}
|
|
222
|
+
await this.chats.setState(live.record.id, state);
|
|
223
|
+
for (const line of taken) {
|
|
224
|
+
this.emit({ type: 'chat', room: live.record.id, line });
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/** Sends the lines waiting, one after another, while the room is reached. */
|
|
228
|
+
async flush(live, chat) {
|
|
229
|
+
var _a, _b;
|
|
230
|
+
if (chat.sending || chat.syncing || !(((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced)) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
chat.sending = true;
|
|
234
|
+
try {
|
|
235
|
+
for (;;) {
|
|
236
|
+
const line = (_b = chat.state) === null || _b === void 0 ? void 0 : _b.outbox.find(waiting => waiting.pending);
|
|
237
|
+
if (!line) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const result = await this.post(live, line);
|
|
241
|
+
if (result.error === 'large' || result.error === 'invalid') {
|
|
242
|
+
await chat.run(() => this.refused(live, chat, line));
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (!result.ok || !(await chat.run(() => this.posted(live, chat, line, result.v)))) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
finally {
|
|
251
|
+
chat.sending = false;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
async post(live, line) {
|
|
255
|
+
const identity = await this.identityOf(live);
|
|
256
|
+
const [stored] = RelayRoomChatUtil.stored([line]);
|
|
257
|
+
const d = await RelayHubUtil.sealIn(live.keys, identity, live.record.member, { chat: stored });
|
|
258
|
+
const id = this.nextId++;
|
|
259
|
+
return new Promise(resolve => {
|
|
260
|
+
const timer = setTimeout(() => {
|
|
261
|
+
live.done.delete(id);
|
|
262
|
+
resolve({ ok: false, v: 0 });
|
|
263
|
+
}, READY_MS);
|
|
264
|
+
live.done.set(id, answer => {
|
|
265
|
+
clearTimeout(timer);
|
|
266
|
+
resolve(answer);
|
|
267
|
+
});
|
|
268
|
+
try {
|
|
269
|
+
this.request(live, { t: 'post', id, mid: line.id, d });
|
|
270
|
+
}
|
|
271
|
+
catch (_a) {
|
|
272
|
+
clearTimeout(timer);
|
|
273
|
+
live.done.delete(id);
|
|
274
|
+
resolve({ ok: false, v: 0 });
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
/** 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. */
|
|
279
|
+
async posted(live, chat, line, seq) {
|
|
280
|
+
var _a;
|
|
281
|
+
const state = chat.state;
|
|
282
|
+
if (seq <= state.seq || !((_a = chat.state) === null || _a === void 0 ? void 0 : _a.outbox.some(waiting => waiting.id === line.id))) {
|
|
283
|
+
state.outbox = state.outbox.filter(waiting => waiting.id !== line.id);
|
|
284
|
+
await this.chats.setState(live.record.id, state);
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
if (seq !== state.seq + 1) {
|
|
288
|
+
this.sync(live, chat);
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
const { pending: _pending, ...sent } = line;
|
|
292
|
+
const taken = { ...sent, seq };
|
|
293
|
+
state.seq = seq;
|
|
294
|
+
state.outbox = state.outbox.filter(waiting => waiting.id !== line.id);
|
|
295
|
+
this.remember(chat, line.id);
|
|
296
|
+
await this.chats.append(live.record.id, [taken]);
|
|
297
|
+
await this.chats.setState(live.record.id, state);
|
|
298
|
+
this.emit({ type: 'chat', room: live.record.id, line: taken });
|
|
299
|
+
clearTimeout(chat.ackTimer);
|
|
300
|
+
chat.ackTimer = setTimeout(() => this.ack(live, chat), ACK_MS);
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
async refused(live, chat, line) {
|
|
304
|
+
const state = chat.state;
|
|
305
|
+
const { pending: _pending, ...rest } = line;
|
|
306
|
+
const failed = { ...rest, failed: true };
|
|
307
|
+
state.outbox = state.outbox.map(waiting => (waiting.id === line.id ? failed : waiting));
|
|
308
|
+
await this.chats.setState(live.record.id, state);
|
|
309
|
+
this.emit({ type: 'chat', room: live.record.id, line: failed });
|
|
310
|
+
}
|
|
311
|
+
sync(live, chat) {
|
|
312
|
+
var _a, _b;
|
|
313
|
+
var _c;
|
|
314
|
+
if (chat.syncing || ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) !== OPEN) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
chat.syncing = true;
|
|
318
|
+
try {
|
|
319
|
+
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_HUB_SYNC_PAGE });
|
|
320
|
+
}
|
|
321
|
+
catch (_d) {
|
|
322
|
+
chat.syncing = false;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
ack(live, chat) {
|
|
326
|
+
var _a, _b;
|
|
327
|
+
var _c;
|
|
328
|
+
clearTimeout(chat.ackTimer);
|
|
329
|
+
const seq = (_c = (_a = chat.state) === null || _a === void 0 ? void 0 : _a.seq) !== null && _c !== void 0 ? _c : 0;
|
|
330
|
+
if (seq > chat.acked && ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === OPEN) {
|
|
331
|
+
chat.acked = seq;
|
|
332
|
+
this.request(live, { t: 'ack', seq });
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
remember(chat, id) {
|
|
336
|
+
chat.ids.add(id);
|
|
337
|
+
if (chat.ids.size > IDS_KEPT) {
|
|
338
|
+
chat.ids.delete(chat.ids.values().next().value);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
//# sourceMappingURL=relay-hubs-chats.js.map
|
|
@@ -3,7 +3,7 @@ import { type RelayHubAskBody, type RelayHubMember } from './relay-hub-util.js';
|
|
|
3
3
|
import { type Live, type MessageBody } from './relay-hubs-live.js';
|
|
4
4
|
import type { RelayHubsOptions, RelayRoomEvent, RelayRoomRecord, RelayRoomRequest, RelayRoomStatus } from './relay-hubs-protocol.js';
|
|
5
5
|
import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity.js';
|
|
6
|
-
import { type RelayHubEntry, type RelayHubFailure, type RelayHubRequest, type RelayHubRoster } from './relay-protocol.js';
|
|
6
|
+
import { type RelayHubEntry, type RelayHubEvent, type RelayHubFailure, type RelayHubRequest, type RelayHubRoster } from './relay-protocol.js';
|
|
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. */
|
|
@@ -266,6 +266,11 @@ export class RelayHubsConnection {
|
|
|
266
266
|
resolve();
|
|
267
267
|
}
|
|
268
268
|
this.emit({ type: 'rooms' });
|
|
269
|
+
await this.heardLog(live, event);
|
|
270
|
+
return;
|
|
271
|
+
case 'log':
|
|
272
|
+
case 'post':
|
|
273
|
+
await this.heardLog(live, event);
|
|
269
274
|
return;
|
|
270
275
|
case 'state':
|
|
271
276
|
await this.readEntry(live, event.entry, true);
|
|
@@ -521,6 +526,8 @@ export class RelayHubsConnection {
|
|
|
521
526
|
}
|
|
522
527
|
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 });
|
|
523
528
|
}
|
|
529
|
+
/** What the hub says of the log of the room, and `synced` (RelayHubsChats). */
|
|
530
|
+
async heardLog(_live, _event) { }
|
|
524
531
|
/** A member set a key naming another: the owner clears it (RelayHubs). */
|
|
525
532
|
foreignSet(_live, _key) { }
|
|
526
533
|
/** Ids of those the user chose as friends. */
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { RelayChatStore } from './relay-chat-store.js';
|
|
1
2
|
import type { RelayIdentity, RelayPublicIdentity } from './relay-identity.js';
|
|
3
|
+
import type { RelayChatLine } from './relay-room-chat.js';
|
|
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). */
|
|
@@ -89,6 +91,8 @@ export interface RelayRoomInfo {
|
|
|
89
91
|
capacity?: number;
|
|
90
92
|
/** The rules its owner set, for all (RelayHubRules); none when it set none. */
|
|
91
93
|
rules?: Record<string, boolean>;
|
|
94
|
+
/** Messages from before they joined a newcomer reads, as its owner set; none: none. */
|
|
95
|
+
history?: number;
|
|
92
96
|
ownerName: string;
|
|
93
97
|
status: RelayRoomStatus;
|
|
94
98
|
reason?: RelayRoomRecord['reason'];
|
|
@@ -165,6 +169,12 @@ export type RelayRoomEvent = {
|
|
|
165
169
|
from: string;
|
|
166
170
|
name: string;
|
|
167
171
|
data: unknown;
|
|
172
|
+
}
|
|
173
|
+
/** A line of the chat of a room: said here (`pending`), sent, refused (`failed`), or taken from another member. */
|
|
174
|
+
| {
|
|
175
|
+
type: 'chat';
|
|
176
|
+
room: string;
|
|
177
|
+
line: RelayChatLine;
|
|
168
178
|
} | {
|
|
169
179
|
type: 'state';
|
|
170
180
|
room: string;
|
|
@@ -175,6 +185,8 @@ export type RelayRoomEvent = {
|
|
|
175
185
|
};
|
|
176
186
|
/** Where a {@link RelayHubs} keeps what it knows: files of the agent directory for Ta, the storage of a browser for a guest. */
|
|
177
187
|
export interface RelayHubsStore {
|
|
188
|
+
/** Where what is said in the rooms is kept, when not in memory (RelayHubsOptions.chats says otherwise). */
|
|
189
|
+
chats?(): RelayChatStore | undefined;
|
|
178
190
|
read<T>(name: RelayHubsStoreName): T | undefined;
|
|
179
191
|
/** Holds keys: kept where the user alone reads it. */
|
|
180
192
|
write(name: RelayHubsStoreName, value: unknown): void;
|
|
@@ -192,6 +204,8 @@ export interface RelayHubsOptions {
|
|
|
192
204
|
guest?: boolean;
|
|
193
205
|
/** The relay new rooms are made on; `use` the feature a room is for (relays.json), `rooms` when none is said. */
|
|
194
206
|
relay(use?: string): string;
|
|
207
|
+
/** Where what is said in the rooms is kept (docs/design/ta-chat-history.md 8); in memory without. */
|
|
208
|
+
chats?: RelayChatStore;
|
|
195
209
|
/** First wait before connecting again; it doubles up to 30 seconds. */
|
|
196
210
|
reconnectDelayMs?: number;
|
|
197
211
|
pingMs?: number;
|
|
@@ -2,7 +2,7 @@ import { RelayFriendSends, type RelayFriendShare } from './relay-friend-sends.js
|
|
|
2
2
|
import { RelayFriends } from './relay-friends.js';
|
|
3
3
|
import { type RelayGroupInvite, RelayGroupInvites } from './relay-group-invites.js';
|
|
4
4
|
import { type RelayHubAskBody } from './relay-hub-util.js';
|
|
5
|
-
import {
|
|
5
|
+
import { RelayHubsChats } from './relay-hubs-chats.js';
|
|
6
6
|
import { Live } from './relay-hubs-live.js';
|
|
7
7
|
import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayRoomEvent, RelayRoomInfo, RelayRoomRecord, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol.js';
|
|
8
8
|
import { type RelayPublicIdentity } from './relay-identity.js';
|
|
@@ -12,7 +12,7 @@ import { type RelayPublicIdentity } from './relay-identity.js';
|
|
|
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
|
|
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;
|
|
@@ -110,6 +110,9 @@ export declare class RelayHubs extends RelayHubsAdmins {
|
|
|
110
110
|
answerFriendShare(id: string, keep: boolean): void;
|
|
111
111
|
protected friendIds(): Set<string>;
|
|
112
112
|
protected heardAsk(live: Live, ask: RelayHubAskBody): void;
|
|
113
|
+
protected readonly guestsIn: Map<string, number[]>;
|
|
114
|
+
/** One more guest let in without asking fits in the hour of a room (RELAY_GUESTS_PER_HOUR): counted. */
|
|
115
|
+
protected guestsLetIn(room: string): boolean;
|
|
113
116
|
protected foreignSet(live: Live, key: string): void;
|
|
114
117
|
protected heardMembers(members: RelayPublicIdentity[]): void;
|
|
115
118
|
protected offered(live: Live, from: RelayPublicIdentity, link: string): void;
|