@celljs/ta-relay 4.20.0 → 4.22.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 (91) hide show
  1. package/esm/common/index.d.ts +2 -0
  2. package/esm/common/index.js +2 -0
  3. package/esm/common/relay-account-client.d.ts +4 -0
  4. package/esm/common/relay-account-client.js +13 -3
  5. package/esm/common/relay-account.d.ts +17 -2
  6. package/esm/common/relay-account.js +15 -6
  7. package/esm/common/relay-accounts.d.ts +2 -0
  8. package/esm/common/relay-accounts.js +16 -2
  9. package/esm/common/relay-group-invites.d.ts +6 -2
  10. package/esm/common/relay-group-invites.js +6 -5
  11. package/esm/common/relay-hub-charter.d.ts +78 -0
  12. package/esm/common/relay-hub-charter.js +152 -0
  13. package/esm/common/relay-hub-epochs.d.ts +43 -0
  14. package/esm/common/relay-hub-epochs.js +92 -0
  15. package/esm/common/relay-hub-util.d.ts +39 -7
  16. package/esm/common/relay-hub-util.js +46 -15
  17. package/esm/common/relay-hub.d.ts +13 -0
  18. package/esm/common/relay-hub.js +81 -18
  19. package/esm/common/relay-hubs-admins.d.ts +27 -0
  20. package/esm/common/relay-hubs-admins.js +91 -0
  21. package/esm/common/relay-hubs-connection.d.ts +19 -63
  22. package/esm/common/relay-hubs-connection.js +100 -87
  23. package/esm/common/relay-hubs-identity.d.ts +7 -1
  24. package/esm/common/relay-hubs-identity.js +28 -1
  25. package/esm/common/relay-hubs-live.d.ts +58 -0
  26. package/esm/common/relay-hubs-live.js +27 -0
  27. package/esm/common/relay-hubs-protocol.d.ts +10 -0
  28. package/esm/common/relay-hubs-util.d.ts +9 -0
  29. package/esm/common/relay-hubs-util.js +23 -1
  30. package/esm/common/relay-hubs.d.ts +15 -15
  31. package/esm/common/relay-hubs.js +76 -57
  32. package/esm/common/relay-protocol.d.ts +14 -3
  33. package/esm/common/relay-protocol.js +1 -1
  34. package/esm/common/relay-room-app.d.ts +106 -0
  35. package/esm/common/relay-room-app.js +171 -0
  36. package/esm/common/relay-room-chat.d.ts +6 -1
  37. package/esm/common/relay-room-chat.js +1 -0
  38. package/esm/guest/guest-i18n.d.ts +3 -0
  39. package/esm/guest/guest-i18n.js +12 -0
  40. package/esm/guest/guest-style.d.ts +1 -1
  41. package/esm/guest/guest-style.js +11 -0
  42. package/esm/guest/guest.js +76 -6
  43. package/esm/node/relay-server-accounts.d.ts +1 -1
  44. package/esm/node/relay-server-accounts.js +1 -1
  45. package/lib/common/index.d.ts +2 -0
  46. package/lib/common/index.js +2 -0
  47. package/lib/common/relay-account-client.d.ts +4 -0
  48. package/lib/common/relay-account-client.js +13 -3
  49. package/lib/common/relay-account.d.ts +17 -2
  50. package/lib/common/relay-account.js +15 -6
  51. package/lib/common/relay-accounts.d.ts +2 -0
  52. package/lib/common/relay-accounts.js +16 -2
  53. package/lib/common/relay-group-invites.d.ts +6 -2
  54. package/lib/common/relay-group-invites.js +6 -5
  55. package/lib/common/relay-hub-charter.d.ts +79 -0
  56. package/lib/common/relay-hub-charter.js +155 -0
  57. package/lib/common/relay-hub-epochs.d.ts +44 -0
  58. package/lib/common/relay-hub-epochs.js +95 -0
  59. package/lib/common/relay-hub-util.d.ts +39 -7
  60. package/lib/common/relay-hub-util.js +46 -15
  61. package/lib/common/relay-hub.d.ts +13 -0
  62. package/lib/common/relay-hub.js +81 -18
  63. package/lib/common/relay-hubs-admins.d.ts +28 -0
  64. package/lib/common/relay-hubs-admins.js +95 -0
  65. package/lib/common/relay-hubs-connection.d.ts +19 -63
  66. package/lib/common/relay-hubs-connection.js +106 -94
  67. package/lib/common/relay-hubs-identity.d.ts +7 -1
  68. package/lib/common/relay-hubs-identity.js +28 -1
  69. package/lib/common/relay-hubs-live.d.ts +59 -0
  70. package/lib/common/relay-hubs-live.js +31 -0
  71. package/lib/common/relay-hubs-protocol.d.ts +10 -0
  72. package/lib/common/relay-hubs-util.d.ts +9 -0
  73. package/lib/common/relay-hubs-util.js +23 -1
  74. package/lib/common/relay-hubs.d.ts +15 -15
  75. package/lib/common/relay-hubs.js +82 -63
  76. package/lib/common/relay-protocol.d.ts +14 -3
  77. package/lib/common/relay-protocol.js +1 -1
  78. package/lib/common/relay-room-app.d.ts +107 -0
  79. package/lib/common/relay-room-app.js +176 -0
  80. package/lib/common/relay-room-chat.d.ts +6 -1
  81. package/lib/common/relay-room-chat.js +1 -0
  82. package/lib/guest/guest-i18n.d.ts +3 -0
  83. package/lib/guest/guest-i18n.js +12 -0
  84. package/lib/guest/guest-style.d.ts +1 -1
  85. package/lib/guest/guest-style.js +11 -0
  86. package/lib/guest/guest.js +76 -6
  87. package/lib/node/relay-server-accounts.d.ts +1 -1
  88. package/lib/node/relay-server-accounts.js +1 -1
  89. package/package.json +3 -3
  90. package/viewer/join.html +17 -6
  91. package/viewer/room.html +26 -15
@@ -1,3 +1,4 @@
1
+ import { RelayHubEpochs } from './relay-hub-epochs.js';
1
2
  /** How far a membership has come: one computer heard it end, it ended for both. */
2
3
  const STATUS_RANK = { waiting: 0, member: 1, refused: 2, gone: 3 };
3
4
  export var RelayHubsUtil;
@@ -34,7 +35,7 @@ export var RelayHubsUtil;
34
35
  * key and the owner's keys once either has them, the invitations of both, made once either made it.
35
36
  */
36
37
  function merge(local, other) {
37
- var _a, _b, _c, _d, _e, _f;
38
+ var _a, _b, _c, _d, _e, _f, _g, _h;
38
39
  const ahead = STATUS_RANK[other.status] > STATUS_RANK[local.status] ? other : local;
39
40
  const behind = ahead === local ? other : local;
40
41
  const invites = [...((_a = local.invites) !== null && _a !== void 0 ? _a : []), ...((_b = other.invites) !== null && _b !== void 0 ? _b : [])]
@@ -51,7 +52,12 @@ export var RelayHubsUtil;
51
52
  const key = (_c = ahead.key) !== null && _c !== void 0 ? _c : behind.key;
52
53
  const joinedAt = Math.max((_d = local.joinedAt) !== null && _d !== void 0 ? _d : 0, (_e = other.joinedAt) !== null && _e !== void 0 ? _e : 0);
53
54
  const identity = (_f = local.identity) !== null && _f !== void 0 ? _f : other.identity;
55
+ // The keys of the epochs either has (docs/design/ta-link.md 8.7), the latest kept.
56
+ const epochs = RelayHubEpochs.kept({ ...behind.epochs, ...ahead.epochs, ...local.epochs, ...other.epochs });
57
+ const rosterV = Math.max((_g = local.rosterV) !== null && _g !== void 0 ? _g : 0, (_h = other.rosterV) !== null && _h !== void 0 ? _h : 0);
54
58
  delete merged.key;
59
+ delete merged.epochs;
60
+ delete merged.rosterV;
55
61
  delete merged.invites;
56
62
  delete merged.joinedAt;
57
63
  delete merged.identity;
@@ -63,6 +69,8 @@ export var RelayHubsUtil;
63
69
  return {
64
70
  ...merged,
65
71
  ...(key && (merged.status === 'member' || merged.status === 'waiting') ? { key } : {}),
72
+ ...(key && merged.status === 'member' && Object.keys(epochs).length ? { epochs } : {}),
73
+ ...(rosterV ? { rosterV } : {}),
66
74
  ...(invites.length ? { invites } : {}),
67
75
  ...(local.made || other.made ? { made: true } : {}),
68
76
  ...(joinedAt ? { joinedAt } : {}),
@@ -70,5 +78,19 @@ export var RelayHubsUtil;
70
78
  };
71
79
  }
72
80
  RelayHubsUtil.merge = merge;
81
+ /**
82
+ * Whether a key of the state names another member than the one who set it (docs/design/ta-link.md 8.8): a key naming
83
+ * a member (`chat:<member>:<n>`, `agents:<member>`, a like of a post) is theirs alone; the owner of the room may only
84
+ * clear it. Keys naming nobody (a game's) are anyone's.
85
+ */
86
+ function foreign(members, key, by, value) {
87
+ const parts = new Set(key.split(':'));
88
+ const named = members.find(member => parts.has(member.member));
89
+ if (!named || named.member === by) {
90
+ return false;
91
+ }
92
+ return !(value === null && members.some(member => member.member === by && member.owner));
93
+ }
94
+ RelayHubsUtil.foreign = foreign;
73
95
  })(RelayHubsUtil || (RelayHubsUtil = {}));
74
96
  //# sourceMappingURL=relay-hubs-util.js.map
@@ -2,9 +2,9 @@ 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 { Live } from './relay-hubs-connection.js';
6
- import { RelayHubsIdentity } from './relay-hubs-identity.js';
7
- import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayRoomEvent, RelayRoomInfo, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol.js';
5
+ import { RelayHubsAdmins } from './relay-hubs-admins.js';
6
+ import { Live } from './relay-hubs-live.js';
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';
9
9
  /**
10
10
  * The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
@@ -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 RelayHubsIdentity {
15
+ export declare class RelayHubs extends RelayHubsAdmins {
16
16
  protected readonly book: RelayFriends;
17
17
  protected readonly groups: RelayGroupInvites;
18
18
  protected readonly sends: RelayFriendSends;
@@ -49,14 +49,7 @@ export declare class RelayHubs extends RelayHubsIdentity {
49
49
  requests(): Promise<RelayRoomRequest[]>;
50
50
  /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
51
51
  answer(roomId: string, askId: string, accept: boolean): Promise<void>;
52
- /**
53
- * The owner's choices for a room: how many members it lets in at most (RELAY_ROOM_MAX_MEMBERS by default, 2 to
54
- * RELAY_ROOM_CAPACITY_MAX), for one many come to (feedback from a browser). Those in already stay.
55
- */
56
- configure(roomId: string, options: {
57
- members?: number;
58
- }): RelayRoomInfo;
59
- /** The owner takes a member out: the relay closes it at once. */
52
+ /** The owner or an admin takes a member out: the relay closes it at once. An admin takes out neither the owner nor an admin. */
60
53
  remove(roomId: string, member: string): Promise<void>;
61
54
  /** Leaves the room; the owner closes it, for everyone. A room asked to join is forgotten. */
62
55
  leave(roomId: string): Promise<void>;
@@ -72,6 +65,12 @@ export declare class RelayHubs extends RelayHubsIdentity {
72
65
  ok: boolean;
73
66
  version: number;
74
67
  }>;
68
+ /**
69
+ * Whether a key of the state is this Ta's own, which no other member may replace or clear at the relay: one that
70
+ * names its member id (`chat:<member>:<n>`, `agents:<member>`, a like of a post), and every key of a room of
71
+ * friends it owns (its posts, what it sends its friends). Any other is shared, as a game's.
72
+ */
73
+ static owns(record: Pick<RelayRoomRecord, 'member' | 'kind' | 'own'>, name: string): boolean;
75
74
  /** Friends of the user's other computer, with those of this Ta. */
76
75
  mergeFriends(friends: RelayFriend[]): number;
77
76
  /** The friends, with their keys: each in the room of the other (shares to friends are sealed for them). */
@@ -109,14 +108,15 @@ export declare class RelayHubs extends RelayHubsIdentity {
109
108
  friendShares(): Promise<RelayFriendShare[]>;
110
109
  /** The user opened one (`keep`) or let it go. */
111
110
  answerFriendShare(id: string, keep: boolean): void;
112
- protected isFriends(live: Live): boolean;
113
111
  protected friendIds(): Set<string>;
114
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;
116
+ protected foreignSet(live: Live, key: string): void;
115
117
  protected heardMembers(members: RelayPublicIdentity[]): void;
116
118
  protected offered(live: Live, from: RelayPublicIdentity, link: string): void;
117
119
  /** What changes the friends: rooms coming and going, members, the links friends leave to join back. */
118
120
  protected after(event: RelayRoomEvent): void;
119
- protected owned(roomId: string): Live;
120
- protected member(roomId: string): Live;
121
121
  protected info(live: Live): RelayRoomInfo;
122
122
  }
@@ -1,11 +1,12 @@
1
1
  import { RelayFriendSends } from './relay-friend-sends.js';
2
2
  import { RELAY_FRIENDS_KIND, RelayFriends, RelayFriendsUtil } from './relay-friends.js';
3
3
  import { RelayGroupInvites } from './relay-group-invites.js';
4
+ import { RELAY_GUESTS_PER_HOUR, RELAY_GUESTS_RULE, RelayHubCharterUtil } from './relay-hub-charter.js';
4
5
  import { RelayHubUtil } from './relay-hub-util.js';
5
- import { Live, OPEN, READY_MS } from './relay-hubs-connection.js';
6
- import { RelayHubsIdentity } from './relay-hubs-identity.js';
6
+ import { RelayHubsAdmins } from './relay-hubs-admins.js';
7
+ import { Live, OPEN, READY_MS } from './relay-hubs-live.js';
7
8
  import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
8
- import { RELAY_FRIENDS_MAX, RELAY_INVITE_MAX_DAYS, RELAY_ROOM_CAPACITY_MAX, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
9
+ import { RELAY_INVITE_MAX_DAYS, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
9
10
  const DAY_MS = 24 * 60 * 60 * 1000;
10
11
  /**
11
12
  * The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
@@ -13,7 +14,7 @@ const DAY_MS = 24 * 60 * 60 * 1000;
13
14
  * the others; every message signed by its member and sealed with the key of the room. The friends are a room of
14
15
  * their own (RelayFriends).
15
16
  */
16
- export class RelayHubs extends RelayHubsIdentity {
17
+ export class RelayHubs extends RelayHubsAdmins {
17
18
  constructor() {
18
19
  super(...arguments);
19
20
  this.book = new RelayFriends({
@@ -40,6 +41,10 @@ export class RelayHubs extends RelayHubsIdentity {
40
41
  identity: () => this.identity(),
41
42
  list: () => this.list(),
42
43
  record: room => { var _a; return (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.record; },
44
+ manages: room => {
45
+ const live = this.live.get(room);
46
+ return !!live && this.manages(live);
47
+ },
43
48
  save: () => this.save(),
44
49
  friends: () => this.friends(),
45
50
  friendRoom: () => this.friendRoom(),
@@ -57,6 +62,7 @@ export class RelayHubs extends RelayHubsIdentity {
57
62
  getState: (room, prefix) => this.getState(room, prefix),
58
63
  setState: (room, key, value) => this.setState(room, key, value),
59
64
  }, this.options.store);
65
+ this.guestsIn = new Map();
60
66
  }
61
67
  /** Connects to the rooms of rooms.json, and keeps connected. */
62
68
  start() {
@@ -142,14 +148,15 @@ export class RelayHubs extends RelayHubsIdentity {
142
148
  async invite(roomId, options = {}) {
143
149
  var _a;
144
150
  var _b, _c;
145
- const live = this.owned(roomId);
151
+ const live = this.managed(roomId);
146
152
  const identity = await this.identityOf(live);
147
153
  const linkOf = (invite) => ({
148
154
  link: RelayHubUtil.link({
149
155
  relay: live.record.relay,
150
156
  room: live.record.id,
151
157
  invite: invite.secret,
152
- owner: identity.id,
158
+ // The owner's, whoever invites: the one who asks checks the answer against it (an admin's by the charter).
159
+ owner: live.record.owner.id,
153
160
  title: live.record.title,
154
161
  by: identity.name,
155
162
  }),
@@ -223,9 +230,9 @@ export class RelayHubs extends RelayHubsIdentity {
223
230
  }
224
231
  /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
225
232
  async answer(roomId, askId, accept) {
226
- var _a;
227
- var _b, _c;
228
- const live = this.owned(roomId);
233
+ var _a, _b;
234
+ var _c;
235
+ const live = this.managed(roomId);
229
236
  const asked = live.asks.get(askId);
230
237
  if (!asked) {
231
238
  throw new Error('Nobody asks that now.');
@@ -238,17 +245,21 @@ export class RelayHubs extends RelayHubsIdentity {
238
245
  const identity = await this.identityOf(live);
239
246
  const body = {
240
247
  key: live.record.key,
248
+ ...(live.record.epochs && Object.keys(live.record.epochs).length ? { epochs: live.record.epochs } : {}),
241
249
  kind: live.record.kind,
242
250
  title: live.record.title,
243
251
  ...(live.record.view ? { view: live.record.view } : {}),
244
252
  ...(live.record.unlisted ? { unlisted: true } : {}),
245
253
  };
246
254
  const ask = asked.body;
247
- const most = this.isFriends(live) ? RELAY_FRIENDS_MAX + 1 : ((_b = live.record.capacity) !== null && _b !== void 0 ? _b : RELAY_ROOM_MAX_MEMBERS);
248
- if (((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _c !== void 0 ? _c : 0) >= most) {
255
+ if (((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _c !== void 0 ? _c : 0) >= this.capacityOf(live)) {
249
256
  throw new Error('The room is full.');
250
257
  }
251
- const answer = await RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite);
258
+ const head = live.record.owner;
259
+ const proof = !live.record.own && ((_b = live.roster) === null || _b === void 0 ? void 0 : _b.charter) && head.sign && head.box
260
+ ? RelayHubCharterUtil.answerProof({ ...head, sign: head.sign, box: head.box }, live.roster.charter)
261
+ : undefined;
262
+ const answer = await RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite, proof);
252
263
  const member = { ...RelayIdentityUtil.publicOf(ask), member: ask.member, ...(ask.guest ? { guest: true } : {}), at: Date.now() };
253
264
  const roster = await this.nextRoster(live, members => [...members.filter(other => other.member !== ask.member), member]);
254
265
  this.request(live, { t: 'admit', ask: askId, answer, roster });
@@ -256,35 +267,20 @@ export class RelayHubs extends RelayHubsIdentity {
256
267
  live.asks.delete(askId);
257
268
  this.emit({ type: 'rooms' });
258
269
  }
259
- /**
260
- * The owner's choices for a room: how many members it lets in at most (RELAY_ROOM_MAX_MEMBERS by default, 2 to
261
- * RELAY_ROOM_CAPACITY_MAX), for one many come to (feedback from a browser). Those in already stay.
262
- */
263
- configure(roomId, options) {
264
- const live = this.owned(roomId);
265
- if (options.members !== undefined) {
266
- const members = Math.round(Number(options.members));
267
- if (!(members >= 2 && members <= RELAY_ROOM_CAPACITY_MAX)) {
268
- throw new Error(`A room takes 2 to ${RELAY_ROOM_CAPACITY_MAX} members.`);
269
- }
270
- if (members === RELAY_ROOM_MAX_MEMBERS) {
271
- delete live.record.capacity;
272
- }
273
- else {
274
- live.record.capacity = members;
275
- }
276
- this.save();
277
- }
278
- return this.info(live);
279
- }
280
- /** The owner takes a member out: the relay closes it at once. */
270
+ /** The owner or an admin takes a member out: the relay closes it at once. An admin takes out neither the owner nor an admin. */
281
271
  async remove(roomId, member) {
282
- const live = this.owned(roomId);
272
+ var _a, _b;
273
+ const live = this.managed(roomId);
283
274
  if (member === live.record.member) {
284
275
  throw new Error('The owner leaves by closing the room.');
285
276
  }
277
+ const owner = (_b = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.find(other => other.owner)) === null || _b === void 0 ? void 0 : _b.member;
278
+ if (!live.record.own && (member === owner || this.isAdmin(live, member))) {
279
+ throw new Error('Only the owner of the room does that.');
280
+ }
286
281
  await this.ready(live);
287
- const roster = await this.nextRoster(live, members => members.filter(other => other.member !== member));
282
+ // A new key for those left: the one taken out holds those of before alone (docs/design/ta-link.md 8.7).
283
+ const roster = await this.nextRoster(live, members => members.filter(other => other.member !== member), undefined, true);
288
284
  this.request(live, { t: 'remove', member, roster });
289
285
  }
290
286
  /** Leaves the room; the owner closes it, for everyone. A room asked to join is forgotten. */
@@ -329,7 +325,7 @@ export class RelayHubs extends RelayHubsIdentity {
329
325
  await this.ready(live);
330
326
  const identity = await this.identityOf(live);
331
327
  const keys = live.keys;
332
- const d = await RelayHubUtil.seal(keys.seal, identity, live.record.member, { key: name, value: value !== null && value !== void 0 ? value : null });
328
+ const d = await RelayHubUtil.sealIn(keys, identity, live.record.member, { key: name, value: value !== null && value !== void 0 ? value : null });
333
329
  const k = await RelayHubUtil.keyId(keys, name);
334
330
  const id = this.nextId++;
335
331
  const result = await new Promise((resolve, reject) => {
@@ -342,7 +338,14 @@ export class RelayHubs extends RelayHubsIdentity {
342
338
  resolve(answer);
343
339
  });
344
340
  try {
345
- this.request(live, { t: 'set', id, k, d, ...(version === undefined ? {} : { v: version }) });
341
+ this.request(live, {
342
+ t: 'set',
343
+ id,
344
+ k,
345
+ d,
346
+ ...(version === undefined ? {} : { v: version }),
347
+ ...(RelayHubs.owns(live.record, name) ? { own: true } : {}),
348
+ });
346
349
  }
347
350
  catch (error) {
348
351
  clearTimeout(timer);
@@ -355,7 +358,9 @@ export class RelayHubs extends RelayHubsIdentity {
355
358
  ? 'The value is too large.'
356
359
  : result.error === 'limit'
357
360
  ? 'The shared state of the room is full.'
358
- : 'The relay refused it.');
361
+ : result.error === 'owned'
362
+ ? 'Another member keeps this key: only they or the owner of the room change it.'
363
+ : 'The relay refused it.');
359
364
  }
360
365
  if (result.ok) {
361
366
  live.state.set(name, { value: value !== null && value !== void 0 ? value : null, version: result.v, by: live.record.member });
@@ -363,6 +368,14 @@ export class RelayHubs extends RelayHubsIdentity {
363
368
  }
364
369
  return { ok: result.ok, version: result.v };
365
370
  }
371
+ /**
372
+ * Whether a key of the state is this Ta's own, which no other member may replace or clear at the relay: one that
373
+ * names its member id (`chat:<member>:<n>`, `agents:<member>`, a like of a post), and every key of a room of
374
+ * friends it owns (its posts, what it sends its friends). Any other is shared, as a game's.
375
+ */
376
+ static owns(record, name) {
377
+ return name.split(':').includes(record.member) || (record.own && RelayFriendsUtil.isKind(record.kind));
378
+ }
366
379
  /** Friends of the user's other computer, with those of this Ta. */
367
380
  mergeFriends(friends) {
368
381
  return this.book.merge(friends);
@@ -427,24 +440,41 @@ export class RelayHubs extends RelayHubsIdentity {
427
440
  this.sends.answer(id, keep);
428
441
  this.emit({ type: 'rooms' });
429
442
  }
430
- isFriends(live) {
431
- return live.record.own && live.record.kind === RELAY_FRIENDS_KIND;
432
- }
433
443
  friendIds() {
434
444
  return this.book.ids();
435
445
  }
436
446
  heardAsk(live, ask) {
447
+ var _a, _b;
437
448
  if (this.isFriends(live)) {
438
449
  this.book.asked(ask);
439
450
  }
440
- else if (this.groups.admits(live.record, ask)) {
451
+ else if (!((_a = live.charter) === null || _a === void 0 ? void 0 : _a.rules.approve) && this.groups.admits(live.record, ask)) {
441
452
  // A friend the user invited: in at once, as the user chose it; asked as any other should that fail.
442
453
  void this.answer(live.record.id, ask.member, true).then(() => this.groups.admitted(live.record, ask.id), () => super.heardAsk(live, ask));
443
454
  }
455
+ else if (ask.guest && ((_b = live.charter) === null || _b === void 0 ? void 0 : _b.rules[RELAY_GUESTS_RULE]) && this.manages(live) && this.guestsLetIn(live.record.id)) {
456
+ // The owner lets those of a browser in without asking (they only chat), so many an hour; asked past it, or should that fail.
457
+ void this.answer(live.record.id, ask.member, true).catch(() => super.heardAsk(live, ask));
458
+ }
444
459
  else {
445
460
  super.heardAsk(live, ask);
446
461
  }
447
462
  }
463
+ /** One more guest let in without asking fits in the hour of a room (RELAY_GUESTS_PER_HOUR): counted. */
464
+ guestsLetIn(room) {
465
+ var _a;
466
+ const now = Date.now();
467
+ const times = ((_a = this.guestsIn.get(room)) !== null && _a !== void 0 ? _a : []).filter(at => now - at < 60 * 60 * 1000);
468
+ if (times.length >= RELAY_GUESTS_PER_HOUR) {
469
+ return false;
470
+ }
471
+ this.guestsIn.set(room, [...times, now]);
472
+ return true;
473
+ }
474
+ foreignSet(live, key) {
475
+ // Not awaited: the owner clears what one set under another's name, as soon as it is in the room.
476
+ void this.setState(live.record.id, key, null).catch(() => undefined);
477
+ }
448
478
  heardMembers(members) {
449
479
  if (!this.options.guest) {
450
480
  this.book.heard(members);
@@ -467,20 +497,6 @@ export class RelayHubs extends RelayHubsIdentity {
467
497
  this.emit({ type: 'rooms' });
468
498
  }
469
499
  }
470
- owned(roomId) {
471
- const live = this.member(roomId);
472
- if (!live.record.own) {
473
- throw new Error('Only the owner of the room does that.');
474
- }
475
- return live;
476
- }
477
- member(roomId) {
478
- const live = this.live.get(roomId);
479
- if ((live === null || live === void 0 ? void 0 : live.record.status) !== 'member') {
480
- throw new Error('Not in that room.');
481
- }
482
- return live;
483
- }
484
500
  info(live) {
485
501
  var _a, _b;
486
502
  var _c, _d;
@@ -491,6 +507,7 @@ export class RelayHubs extends RelayHubsIdentity {
491
507
  name: member.name,
492
508
  ...(RelayIdentityUtil.isLook(member.look) ? { look: member.look } : {}),
493
509
  owner: !!member.owner,
510
+ ...(this.isAdmin(live, member.member) ? { admin: true } : {}),
494
511
  online: member.member === record.member ? ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced : live.online.has(member.member),
495
512
  self: member.member === record.member,
496
513
  ...(member.guest ? { guest: true } : {}),
@@ -503,7 +520,9 @@ export class RelayHubs extends RelayHubsIdentity {
503
520
  ...(record.unlisted ? { unlisted: true } : {}),
504
521
  relay: record.relay,
505
522
  owner: record.own,
523
+ ...(!record.own && this.isAdmin(live, record.member) ? { admin: true } : {}),
506
524
  ...(record.own ? { capacity: (_d = record.capacity) !== null && _d !== void 0 ? _d : RELAY_ROOM_MAX_MEMBERS } : {}),
525
+ ...(live.charter && Object.keys(live.charter.rules).length ? { rules: { ...live.charter.rules } } : {}),
507
526
  ownerName: record.owner.name,
508
527
  status: record.status,
509
528
  ...(record.reason ? { reason: record.reason } : {}),
@@ -186,7 +186,7 @@ export declare const RELAY_HUB_MAX_VALUE: number;
186
186
  export declare const RELAY_HUB_MAX_KEYS = 4096;
187
187
  /** Characters of all of the state, sealed. */
188
188
  export declare const RELAY_HUB_MAX_STATE: number;
189
- /** Asks to join waiting for the owner, and for how long. */
189
+ /** Asks to join waiting for the owner or an admin, and for how long. */
190
190
  export declare const RELAY_HUB_MAX_ASKS = 20;
191
191
  /**
192
192
  * The key of the state of a room a member keeps its picture under (RelayIdentity.picture), its member id after it: each
@@ -200,10 +200,15 @@ export declare const RELAY_HUB_ASK_DAYS = 7;
200
200
  export declare const RELAY_HUB_IDLE_DAYS = 90;
201
201
  /** Messages of a connection in a second. */
202
202
  export declare const RELAY_HUB_RATE = 40;
203
- /** The sealed list of the members of a room, signed by its owner; `v` grows with each change. */
203
+ /**
204
+ * The sealed list of the members of a room, signed by its owner (or an admin); `v` grows with each change. After a
205
+ * member was taken out or left, `k` holds the keys of the room's epochs sealed for each member left
206
+ * (docs/design/ta-link.md 8.7), by member id.
207
+ */
204
208
  export interface RelayHubRoster {
205
209
  v: number;
206
210
  d: string;
211
+ k?: Record<string, string>;
207
212
  }
208
213
  /** A key of the shared state: `k` the HMAC of its name, `v` its version (from 1), `d` sealed, `by` the member who wrote it last. */
209
214
  export interface RelayHubEntry {
@@ -211,6 +216,8 @@ export interface RelayHubEntry {
211
216
  v: number;
212
217
  d: string;
213
218
  by: string;
219
+ /** Its member's own: only they, or the owner of the room, set it again or clear it while they are a member. */
220
+ own?: true;
214
221
  }
215
222
  /** An ask to join, for the owner: `d` sealed with the key of the invitation. */
216
223
  export interface RelayHubAsk {
@@ -251,10 +258,14 @@ export type RelayHubRequest = {
251
258
  k: string;
252
259
  d: string;
253
260
  v?: number;
261
+ own?: true;
254
262
  } | {
255
263
  t: 'invite';
256
264
  hash: string;
257
265
  expiresAt: number;
266
+ } | {
267
+ t: 'admins';
268
+ admins: string[];
258
269
  } | {
259
270
  t: 'admit';
260
271
  ask: string;
@@ -276,7 +287,7 @@ export type RelayHubRequest = {
276
287
  t: 'close';
277
288
  };
278
289
  /** Why the hub refused: `auth` not a member, `gone` no such room, `invite` the invitation is wrong or ran out. */
279
- export type RelayHubFailure = 'auth' | 'gone' | 'invite' | 'full' | 'large' | 'limit' | 'rate' | 'owner' | 'exists' | 'invalid';
290
+ export type RelayHubFailure = 'auth' | 'gone' | 'invite' | 'full' | 'large' | 'limit' | 'rate' | 'owner' | 'exists' | 'invalid' | 'owned';
280
291
  /**
281
292
  * What a hub sends. A member is first `welcome`d (who is online, the roster, asks for the owner), then given the
282
293
  * state in `entries` and `synced`.
@@ -119,7 +119,7 @@ export const RELAY_HUB_MAX_VALUE = 96 * 1024;
119
119
  export const RELAY_HUB_MAX_KEYS = 4096;
120
120
  /** Characters of all of the state, sealed. */
121
121
  export const RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
122
- /** Asks to join waiting for the owner, and for how long. */
122
+ /** Asks to join waiting for the owner or an admin, and for how long. */
123
123
  export const RELAY_HUB_MAX_ASKS = 20;
124
124
  /**
125
125
  * The key of the state of a room a member keeps its picture under (RelayIdentity.picture), its member id after it: each
@@ -0,0 +1,106 @@
1
+ import type { RelayChatLine, RelayChatLineExtra } from './relay-room-chat.js';
2
+ /**
3
+ * What every app of a group chat that listens and speaks there needs (docs/design/ta-feedback.md 1): what others said
4
+ * since it last read, paced; who speaks for the user (their double or an agent of theirs in the room); what others wrote
5
+ * kept as theirs when a model reads it; and a board of how things go that every member, and those in a browser, see.
6
+ * Feedback is one such app; a vote, a rota or a help desk would be others.
7
+ */
8
+ /** Under `agents:<member>` of the state of a room, each member lists the agents of theirs they invited to it. */
9
+ export declare const ROOM_AGENTS_KEY = "agents:";
10
+ /** A line older than this when an app reads it is left: Ta was off for days. */
11
+ export declare const ROOM_APP_STALE_MS: number;
12
+ export declare namespace RoomAppUtil {
13
+ /** What others wrote, which a model reads inside tags: no tags of its own. */
14
+ function quote(text: string): string;
15
+ /**
16
+ * What others said to the room since `since`: not by the user, not too long ago, not to someone in particular (@ a
17
+ * person, a double or an agent: theirs to answer), not said by a double or an agent.
18
+ */
19
+ function fresh(lines: readonly RelayChatLine[], self: string, since: number, now: number, staleMs?: number): RelayChatLine[];
20
+ /** Whether one more fits in the hour before `now`. */
21
+ function within(times: readonly number[], now: number, most: number): boolean;
22
+ /** People named with @, each once. */
23
+ function mentions(names: readonly string[]): string;
24
+ }
25
+ export interface RoomSpeaker {
26
+ name: string;
27
+ /** What the line it says carries (RelayRoomChat.say): the agent, or that it is the double. */
28
+ extra: RelayChatLineExtra;
29
+ }
30
+ /**
31
+ * Who speaks for the user in a room: the agent of theirs they chose, while it is in the room (as `agents:<member>` of
32
+ * its state lists it, written by them), else their double, named `doubleName`.
33
+ */
34
+ export declare function roomSpeaker(rooms: {
35
+ getState(room: string, prefix?: string): Promise<Record<string, {
36
+ value: unknown;
37
+ by: string;
38
+ }>>;
39
+ }, room: string, self: string, agent: string | undefined, doubleName: string): Promise<RoomSpeaker>;
40
+ /**
41
+ * Paces the work of an app on what is said in rooms: once the room is quiet for `settleMs` (a few lines read as one), one
42
+ * run at a time per room, and no more runs that did something than `hourly` in an hour (past it, again after `laterMs`).
43
+ * `run` says whether it did something (a turn of a model, a line said).
44
+ */
45
+ export declare class RoomPacer {
46
+ protected readonly run: (room: string) => Promise<boolean>;
47
+ protected readonly options: {
48
+ settleMs: number;
49
+ hourly: number;
50
+ laterMs: number;
51
+ now(): number;
52
+ failed(error: unknown): void;
53
+ };
54
+ protected readonly timers: Map<string, NodeJS.Timeout>;
55
+ protected readonly queues: Map<string, Promise<void>>;
56
+ protected readonly runs: Map<string, number[]>;
57
+ protected disposed: boolean;
58
+ constructor(run: (room: string) => Promise<boolean>, options: {
59
+ settleMs: number;
60
+ hourly: number;
61
+ laterMs: number;
62
+ now(): number;
63
+ failed(error: unknown): void;
64
+ });
65
+ /** Something was said in a room: it is read once it is quiet (`delay`, settleMs by default). */
66
+ heard(room: string, delay?: number): void;
67
+ /** What runs in a room now, done. */
68
+ idle(room: string): Promise<void>;
69
+ dispose(): void;
70
+ protected turn(room: string): Promise<void>;
71
+ }
72
+ /** Under `board:<app>:<member>` of the state of a room, its owner says how the things of an app go, for all in it. */
73
+ export declare const RELAY_BOARD_KEY = "board:";
74
+ /** Items of a board at most, the latest. */
75
+ export declare const RELAY_BOARD_MAX = 30;
76
+ /**
77
+ * How an item goes, for anyone to draw without knowing the app: being done, waiting (for the owner, or for more from
78
+ * those who said it), done, or closed without it.
79
+ */
80
+ export type RelayBoardState = 'doing' | 'waiting' | 'done' | 'closed';
81
+ export interface RelayBoardItem {
82
+ /** Its number in the room, as people name it (#n). */
83
+ n: number;
84
+ title: string;
85
+ state: RelayBoardState;
86
+ /** How it goes in words of the owner's language: `Being fixed`, `Waiting for the owner`. */
87
+ label: string;
88
+ /** Who it came from, by name. */
89
+ people: string[];
90
+ at: number;
91
+ }
92
+ /** A board of an app in a room: its name (`Feedback`), and its items, the latest last. */
93
+ export interface RelayRoomBoard {
94
+ title: string;
95
+ items: RelayBoardItem[];
96
+ }
97
+ export declare namespace RelayBoardUtil {
98
+ function key(app: string, member: string): string;
99
+ /** The board under a key of the state, if the owner of the room wrote it under their own name and it is well formed. */
100
+ function read(key: string, value: unknown, by: string, owner: string | undefined): RelayRoomBoard | undefined;
101
+ /** How many are being done or wait, and how many are done: what a line over the chat says. */
102
+ function counts(board: RelayRoomBoard): {
103
+ open: number;
104
+ done: number;
105
+ };
106
+ }