@celljs/ta-relay 4.16.0 → 4.18.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 (106) hide show
  1. package/esm/common/index.d.ts +2 -0
  2. package/esm/common/index.js +2 -0
  3. package/esm/common/relay-content.d.ts +13 -0
  4. package/esm/common/relay-content.js +42 -0
  5. package/esm/common/relay-conversation.d.ts +14 -0
  6. package/esm/common/relay-faces.d.ts +1 -1
  7. package/esm/common/relay-faces.js +56 -1
  8. package/esm/common/relay-files.d.ts +14 -3
  9. package/esm/common/relay-files.js +41 -16
  10. package/esm/common/relay-friend-sends.d.ts +64 -0
  11. package/esm/common/relay-friend-sends.js +126 -0
  12. package/esm/common/relay-group-invites.d.ts +76 -0
  13. package/esm/common/relay-group-invites.js +163 -0
  14. package/esm/common/relay-hub.js +2 -2
  15. package/esm/common/relay-hubs-identity.d.ts +36 -0
  16. package/esm/common/relay-hubs-identity.js +156 -0
  17. package/esm/common/relay-hubs-protocol.d.ts +7 -1
  18. package/esm/common/relay-hubs-util.d.ts +11 -0
  19. package/esm/common/relay-hubs-util.js +74 -0
  20. package/esm/common/relay-hubs.d.ts +36 -44
  21. package/esm/common/relay-hubs.js +95 -235
  22. package/esm/common/relay-open-in-ta.d.ts +11 -0
  23. package/esm/common/relay-open-in-ta.js +34 -0
  24. package/esm/common/relay-protocol.d.ts +11 -2
  25. package/esm/common/relay-protocol.js +18 -2
  26. package/esm/common/relay-spot.d.ts +15 -3
  27. package/esm/common/relay-spot.js +17 -5
  28. package/esm/guest/guest-i18n.d.ts +1 -0
  29. package/esm/guest/guest-i18n.js +45 -41
  30. package/esm/guest/guest-style.d.ts +1 -1
  31. package/esm/guest/guest-style.js +2 -0
  32. package/esm/guest/guest.js +7 -2
  33. package/esm/join/join-i18n.d.ts +1 -0
  34. package/esm/join/join-i18n.js +4 -0
  35. package/esm/join/join.js +4 -1
  36. package/esm/node/index.d.ts +1 -0
  37. package/esm/node/index.js +1 -0
  38. package/esm/node/relay-files-host.d.ts +2 -0
  39. package/esm/node/relay-files-host.js +24 -5
  40. package/esm/node/relay-memory-storage.d.ts +26 -0
  41. package/esm/node/relay-memory-storage.js +60 -0
  42. package/esm/node/relay-server.d.ts +14 -26
  43. package/esm/node/relay-server.js +33 -60
  44. package/esm/node/relay-store.js +4 -4
  45. package/esm/node/relays-file.d.ts +27 -0
  46. package/esm/node/relays-file.js +59 -4
  47. package/esm/viewer/viewer-i18n.d.ts +1 -0
  48. package/esm/viewer/viewer-i18n.js +4 -0
  49. package/esm/viewer/viewer-style.d.ts +1 -1
  50. package/esm/viewer/viewer-style.js +2 -0
  51. package/esm/viewer/viewer.js +36 -2
  52. package/lib/common/index.d.ts +2 -0
  53. package/lib/common/index.js +2 -0
  54. package/lib/common/relay-content.d.ts +13 -0
  55. package/lib/common/relay-content.js +43 -1
  56. package/lib/common/relay-conversation.d.ts +14 -0
  57. package/lib/common/relay-faces.d.ts +1 -1
  58. package/lib/common/relay-faces.js +56 -1
  59. package/lib/common/relay-files.d.ts +14 -3
  60. package/lib/common/relay-files.js +42 -17
  61. package/lib/common/relay-friend-sends.d.ts +65 -0
  62. package/lib/common/relay-friend-sends.js +130 -0
  63. package/lib/common/relay-group-invites.d.ts +77 -0
  64. package/lib/common/relay-group-invites.js +167 -0
  65. package/lib/common/relay-hub.js +1 -1
  66. package/lib/common/relay-hubs-identity.d.ts +37 -0
  67. package/lib/common/relay-hubs-identity.js +160 -0
  68. package/lib/common/relay-hubs-protocol.d.ts +7 -1
  69. package/lib/common/relay-hubs-util.d.ts +12 -0
  70. package/lib/common/relay-hubs-util.js +77 -0
  71. package/lib/common/relay-hubs.d.ts +36 -44
  72. package/lib/common/relay-hubs.js +94 -234
  73. package/lib/common/relay-open-in-ta.d.ts +12 -0
  74. package/lib/common/relay-open-in-ta.js +37 -0
  75. package/lib/common/relay-protocol.d.ts +11 -2
  76. package/lib/common/relay-protocol.js +20 -3
  77. package/lib/common/relay-spot.d.ts +15 -3
  78. package/lib/common/relay-spot.js +17 -5
  79. package/lib/guest/guest-i18n.d.ts +1 -0
  80. package/lib/guest/guest-i18n.js +45 -41
  81. package/lib/guest/guest-style.d.ts +1 -1
  82. package/lib/guest/guest-style.js +2 -0
  83. package/lib/guest/guest.js +7 -2
  84. package/lib/join/join-i18n.d.ts +1 -0
  85. package/lib/join/join-i18n.js +4 -0
  86. package/lib/join/join.js +4 -1
  87. package/lib/node/index.d.ts +1 -0
  88. package/lib/node/index.js +1 -0
  89. package/lib/node/relay-files-host.d.ts +2 -0
  90. package/lib/node/relay-files-host.js +23 -4
  91. package/lib/node/relay-memory-storage.d.ts +27 -0
  92. package/lib/node/relay-memory-storage.js +65 -0
  93. package/lib/node/relay-server.d.ts +14 -26
  94. package/lib/node/relay-server.js +34 -61
  95. package/lib/node/relay-store.js +3 -3
  96. package/lib/node/relays-file.d.ts +27 -0
  97. package/lib/node/relays-file.js +58 -3
  98. package/lib/viewer/viewer-i18n.d.ts +1 -0
  99. package/lib/viewer/viewer-i18n.js +4 -0
  100. package/lib/viewer/viewer-style.d.ts +1 -1
  101. package/lib/viewer/viewer-style.js +2 -0
  102. package/lib/viewer/viewer.js +35 -1
  103. package/package.json +3 -2
  104. package/viewer/join.html +12 -3
  105. package/viewer/page.html +18 -9
  106. package/viewer/room.html +21 -10
@@ -27,6 +27,15 @@ export type RelayConversationMessage = {
27
27
  role: 'user';
28
28
  content: string | RelayConversationPart[];
29
29
  timestamp?: number;
30
+ }
31
+ /** What an extension showed in the conversation (pi's custom message with `display`): its card, drawn by its renderer. */
32
+ | {
33
+ role: 'custom';
34
+ customType: string;
35
+ content: string | RelayConversationPart[];
36
+ display: true;
37
+ details?: unknown;
38
+ timestamp?: number;
30
39
  } | {
31
40
  role: 'assistant';
32
41
  content: RelayConversationPart[];
@@ -49,6 +58,11 @@ export interface RelayConversation {
49
58
  /** The name of its directory, for a line under the title. */
50
59
  place?: string;
51
60
  messages: RelayConversationMessage[];
61
+ /**
62
+ * The pictures of the workspace its replies show (`![](shots/a.png)`), as data URLs by the path written there:
63
+ * the link shows them as the conversation did.
64
+ */
65
+ files?: Record<string, string>;
52
66
  /** What was left out when it was shared: the outputs of its tools, and how many images. */
53
67
  omitted?: {
54
68
  outputs?: boolean;
@@ -1,5 +1,5 @@
1
1
  /** How many faces there are. */
2
- export declare const FACE_COUNT = 16;
2
+ export declare const FACE_COUNT = 25;
3
3
  /** An element of SVG: its tag, its attributes as written in SVG (`stroke-width`, `class`), what is in it. */
4
4
  export interface FaceNode {
5
5
  tag: string;
@@ -8,7 +8,7 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.FaceUtil = exports.FACE_COUNT = void 0;
10
10
  /** How many faces there are. */
11
- exports.FACE_COUNT = 16;
11
+ exports.FACE_COUNT = 25;
12
12
  const BLUE = '#4285F4';
13
13
  const RED = '#EA4335';
14
14
  const YELLOW = '#FBBC05';
@@ -48,6 +48,15 @@ const LOOKS = [
48
48
  { color: PINK, eye: '#4A1428', cheek: 'rgba(160, 20, 70, 0.3)', eyeY: 24, gap: 7.4 }, // drop
49
49
  { color: LILAC, eye: '#2A1A5E', cheek: CHEEK, eyeY: 25, gap: 8 }, // rabbit
50
50
  { color: YELLOW, eye: INK, cheek: CHEEK_WARM, eyeY: 25, gap: 7.4 }, // triangle
51
+ { color: BLUE, eye: WHITE, cheek: CHEEK, eyeY: 19.5, gap: 8 }, // shield
52
+ { color: CREAM, eye: '#4A3420', cheek: 'rgba(240, 120, 120, 0.5)', eyeY: 23.5, gap: 8 }, // house
53
+ { color: GREEN, eye: WHITE, cheek: CHEEK, eyeY: 21, gap: 8 }, // trapezoid
54
+ { color: PINK, eye: '#4A1428', cheek: 'rgba(160, 20, 70, 0.3)', eyeY: 19.5, gap: 8.4 }, // pill
55
+ { color: RED, eye: WHITE, cheek: CHEEK_DEEP, accent: '#B3261E', eyeY: 20, gap: 8.4 }, // lantern
56
+ { color: SKY, eye: WHITE, cheek: CHEEK, eyeY: 23.5, gap: 8 }, // cloud
57
+ { color: VIOLET, eye: WHITE, cheek: CHEEK, accent: '#6B3FD4', eyeY: 22, gap: 8 }, // jar
58
+ { color: TEAL, eye: WHITE, cheek: CHEEK, accent: '#1B8A84', eyeY: 25, gap: 7.6 }, // gourd
59
+ { color: MINT, eye: '#1D3B27', cheek: CHEEK, accent: '#4AAE6C', eyeY: 22.5, gap: 9 }, // frog
51
60
  ];
52
61
  /** The corners of a polygon of `count` sides around (cx, cy), the first straight up; `inner` makes it a star. */
53
62
  function corners(count, radius, cx, cy, inner) {
@@ -141,6 +150,52 @@ function shape(face, look) {
141
150
  ]);
142
151
  case 15:
143
152
  return rounded('20,7 35,33 5,33', color, 6);
153
+ case 16:
154
+ return node('path', {
155
+ d: 'M20 6.5l12 4.2v8.4c0 7.4-4.9 12.6-12 15.9-7.1-3.3-12-8.5-12-15.9v-8.4Z',
156
+ fill: color,
157
+ stroke: color,
158
+ 'stroke-width': 3,
159
+ 'stroke-linejoin': 'round',
160
+ });
161
+ case 17:
162
+ return rounded('20,7 32.5,17.5 32.5,33 7.5,33 7.5,17.5', color, 4.5);
163
+ case 18:
164
+ return rounded('14,9 26,9 32.5,32 7.5,32', color, 6);
165
+ case 19:
166
+ return node('rect', { x: 4.5, y: 10.5, width: 31, height: 19.5, rx: 9.75, fill: color });
167
+ case 20:
168
+ return node('g', {}, [
169
+ node('rect', { x: 14, y: 5.5, width: 12, height: 4.5, rx: 1.6, fill: accent }),
170
+ node('rect', { x: 14, y: 30.5, width: 12, height: 4.5, rx: 1.6, fill: accent }),
171
+ node('ellipse', { cx: 20, cy: 20.25, rx: 15, ry: 12.25, fill: color }),
172
+ ]);
173
+ case 21:
174
+ return node('g', {}, [
175
+ node('circle', { cx: 13.5, cy: 21.5, r: 7, fill: color }),
176
+ node('circle', { cx: 21.5, cy: 16.5, r: 9, fill: color }),
177
+ node('circle', { cx: 28.5, cy: 22.5, r: 6.5, fill: color }),
178
+ node('rect', { x: 5.5, y: 21, width: 29, height: 12, rx: 6, fill: color }),
179
+ ]);
180
+ case 22:
181
+ return node('g', {}, [
182
+ node('rect', { x: 12.5, y: 5.5, width: 15, height: 5.5, rx: 2, fill: accent }),
183
+ node('rect', { x: 7.5, y: 9.5, width: 25, height: 25.5, rx: 6.5, fill: color }),
184
+ ]);
185
+ case 23:
186
+ return node('g', {}, [
187
+ node('rect', { x: 18.8, y: 3.5, width: 2.4, height: 6, rx: 1.2, fill: accent }),
188
+ node('circle', { cx: 20, cy: 13, r: 6.5, fill: color }),
189
+ node('circle', { cx: 20, cy: 25, r: 11.5, fill: color }),
190
+ ]);
191
+ case 24:
192
+ return node('g', {}, [
193
+ node('circle', { cx: 12, cy: 13, r: 5.5, fill: color }),
194
+ node('circle', { cx: 28, cy: 13, r: 5.5, fill: color }),
195
+ node('circle', { cx: 12, cy: 13, r: 2.2, fill: accent }),
196
+ node('circle', { cx: 28, cy: 13, r: 2.2, fill: accent }),
197
+ node('ellipse', { cx: 20, cy: 23.5, rx: 15.5, ry: 11.5, fill: color }),
198
+ ]);
144
199
  default:
145
200
  return node('circle', { cx: 20, cy: 21, r: 14.5, fill: color });
146
201
  }
@@ -47,9 +47,11 @@ export declare const RELAY_FILES_PREFIX = "/api/files/";
47
47
  export declare const RELAY_FILE_CHUNK_BYTES: number;
48
48
  /** The largest file a device reads this way: a video of a workspace is previewed by its first bytes, not all of it. */
49
49
  export declare const RELAY_FILE_MAX_BYTES: number;
50
- /** Files read at once on one channel; a page that loads its pictures asks for several. */
50
+ /** Files read at once on one channel; a page that loads its pictures asks for more, which wait their turn. */
51
51
  export declare const RELAY_FILES_MAX_PARALLEL = 4;
52
- /** For one file, all of it. */
52
+ /** Files waiting their turn on one channel, at most: past them a page asks for more than it can show. */
53
+ export declare const RELAY_FILES_MAX_WAITING = 256;
54
+ /** For one file, all of it, from when it is read, not while it waits its turn. */
53
55
  export declare const RELAY_FILES_TIMEOUT_MS = 30000;
54
56
  /** A file as the page gets it: its type as ta serve answers it, and the bytes. */
55
57
  export interface RelayFileBytes {
@@ -65,6 +67,7 @@ export declare namespace RelayFilesUtil {
65
67
  }
66
68
  /** One file asked for, and what came of it so far. */
67
69
  interface Pending {
70
+ readonly url: string;
68
71
  readonly chunks: Uint8Array[];
69
72
  received: number;
70
73
  head?: {
@@ -73,7 +76,8 @@ interface Pending {
73
76
  };
74
77
  readonly resolve: (bytes: RelayFileBytes) => void;
75
78
  readonly reject: (error: Error) => void;
76
- readonly timer: ReturnType<typeof setTimeout>;
79
+ /** Set once it is asked of the host: before, it waits its turn. */
80
+ timer?: ReturnType<typeof setTimeout>;
77
81
  }
78
82
  export interface RelayFilesOptions {
79
83
  /** For one file. Default RELAY_FILES_TIMEOUT_MS. */
@@ -90,9 +94,14 @@ export declare class RelayFiles {
90
94
  protected opening?: Promise<void>;
91
95
  protected called: number;
92
96
  protected readonly pending: Map<number, Pending>;
97
+ /** Asked for but not yet of the host: no more than {@link RELAY_FILES_MAX_PARALLEL} are read at once. */
98
+ protected readonly waiting: number[];
99
+ protected asked: number;
93
100
  constructor(pairing: RelayPairing, options?: RelayFilesOptions);
94
101
  /** The bytes of a URL of {@link RELAY_FILES_PREFIX}, as ta serve answers them. */
95
102
  get(url: string): Promise<RelayFileBytes>;
103
+ /** Asks the host for the files waiting, as many as it reads at once: more would be turned away. */
104
+ protected ask(): void;
96
105
  /** Whether the channel is open: the page may keep one for as long as it shows files. */
97
106
  get open(): boolean;
98
107
  close(): void;
@@ -102,6 +111,8 @@ export declare class RelayFiles {
102
111
  protected receive(text: string): void;
103
112
  protected settle(id: number, error: Error): void;
104
113
  protected finish(id: number, bytes: RelayFileBytes): void;
114
+ /** A file came or will not: the next one waiting is asked for. */
115
+ protected done(id: number): Pending | undefined;
105
116
  /** The channel went: what was asked for is not coming. */
106
117
  protected fail(error: Error): void;
107
118
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RelayFiles = exports.RelayFilesUtil = exports.RELAY_FILES_TIMEOUT_MS = exports.RELAY_FILES_MAX_PARALLEL = exports.RELAY_FILE_MAX_BYTES = exports.RELAY_FILE_CHUNK_BYTES = exports.RELAY_FILES_PREFIX = void 0;
3
+ exports.RelayFiles = exports.RelayFilesUtil = exports.RELAY_FILES_TIMEOUT_MS = exports.RELAY_FILES_MAX_WAITING = exports.RELAY_FILES_MAX_PARALLEL = exports.RELAY_FILE_MAX_BYTES = exports.RELAY_FILE_CHUNK_BYTES = exports.RELAY_FILES_PREFIX = void 0;
4
4
  const relay_bytes_1 = require("./relay-bytes");
5
5
  const relay_socket_1 = require("./relay-socket");
6
6
  /** The only paths of ta serve a device may ask for: the ones answered without its token. */
@@ -9,9 +9,11 @@ exports.RELAY_FILES_PREFIX = '/api/files/';
9
9
  exports.RELAY_FILE_CHUNK_BYTES = 192 * 1024;
10
10
  /** The largest file a device reads this way: a video of a workspace is previewed by its first bytes, not all of it. */
11
11
  exports.RELAY_FILE_MAX_BYTES = 128 * 1024 * 1024;
12
- /** Files read at once on one channel; a page that loads its pictures asks for several. */
12
+ /** Files read at once on one channel; a page that loads its pictures asks for more, which wait their turn. */
13
13
  exports.RELAY_FILES_MAX_PARALLEL = 4;
14
- /** For one file, all of it. */
14
+ /** Files waiting their turn on one channel, at most: past them a page asks for more than it can show. */
15
+ exports.RELAY_FILES_MAX_WAITING = 256;
16
+ /** For one file, all of it, from when it is read, not while it waits its turn. */
15
17
  exports.RELAY_FILES_TIMEOUT_MS = 30000;
16
18
  var RelayFilesUtil;
17
19
  (function (RelayFilesUtil) {
@@ -42,6 +44,9 @@ class RelayFiles {
42
44
  this.options = options;
43
45
  this.called = 1;
44
46
  this.pending = new Map();
47
+ /** Asked for but not yet of the host: no more than {@link RELAY_FILES_MAX_PARALLEL} are read at once. */
48
+ this.waiting = [];
49
+ this.asked = 0;
45
50
  }
46
51
  /** The bytes of a URL of {@link RELAY_FILES_PREFIX}, as ta serve answers them. */
47
52
  async get(url) {
@@ -51,13 +56,26 @@ class RelayFiles {
51
56
  await this.connect();
52
57
  const id = this.called++;
53
58
  return new Promise((resolve, reject) => {
54
- var _a;
55
- const timeout = (_a = this.options.timeoutMs) !== null && _a !== void 0 ? _a : exports.RELAY_FILES_TIMEOUT_MS;
56
- const timer = setTimeout(() => this.settle(id, new Error('The file took too long to come.')), timeout);
57
- this.pending.set(id, { chunks: [], received: 0, resolve, reject, timer });
58
- this.send({ t: 'get', id, url });
59
+ this.pending.set(id, { url, chunks: [], received: 0, resolve, reject });
60
+ this.waiting.push(id);
61
+ this.ask();
59
62
  });
60
63
  }
64
+ /** Asks the host for the files waiting, as many as it reads at once: more would be turned away. */
65
+ ask() {
66
+ var _a;
67
+ while (this.asked < exports.RELAY_FILES_MAX_PARALLEL && this.waiting.length > 0) {
68
+ const id = this.waiting.shift();
69
+ const pending = this.pending.get(id);
70
+ if (!pending) {
71
+ continue;
72
+ }
73
+ this.asked++;
74
+ const timeout = (_a = this.options.timeoutMs) !== null && _a !== void 0 ? _a : exports.RELAY_FILES_TIMEOUT_MS;
75
+ pending.timer = setTimeout(() => this.settle(id, new Error('The file took too long to come.')), timeout);
76
+ this.send({ t: 'get', id, url: pending.url });
77
+ }
78
+ }
61
79
  /** Whether the channel is open: the page may keep one for as long as it shows files. */
62
80
  get open() {
63
81
  var _a;
@@ -148,23 +166,30 @@ class RelayFiles {
148
166
  }
149
167
  }
150
168
  settle(id, error) {
151
- const pending = this.pending.get(id);
152
- if (pending) {
153
- this.pending.delete(id);
154
- clearTimeout(pending.timer);
155
- pending.reject(error);
156
- }
169
+ var _a;
170
+ (_a = this.done(id)) === null || _a === void 0 ? void 0 : _a.reject(error);
157
171
  }
158
172
  finish(id, bytes) {
173
+ var _a;
174
+ (_a = this.done(id)) === null || _a === void 0 ? void 0 : _a.resolve(bytes);
175
+ }
176
+ /** A file came or will not: the next one waiting is asked for. */
177
+ done(id) {
159
178
  const pending = this.pending.get(id);
160
- if (pending) {
161
- this.pending.delete(id);
179
+ if (!pending) {
180
+ return undefined;
181
+ }
182
+ this.pending.delete(id);
183
+ if (pending.timer !== undefined) {
162
184
  clearTimeout(pending.timer);
163
- pending.resolve(bytes);
185
+ this.asked--;
164
186
  }
187
+ this.ask();
188
+ return pending;
165
189
  }
166
190
  /** The channel went: what was asked for is not coming. */
167
191
  fail(error) {
192
+ this.waiting.length = 0;
168
193
  for (const [id] of [...this.pending]) {
169
194
  this.settle(id, error);
170
195
  }
@@ -0,0 +1,65 @@
1
+ import type { RelayFriend, RelayHubsStore, RelayRoomInfo, RelayRoomRecord, RelayRoomValue } from './relay-hubs-protocol';
2
+ import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
3
+ /** The keys of the room of friends of the one who sends that hold what it sent, one a friend and thing. */
4
+ export declare const RELAY_FRIEND_SEND = "send:";
5
+ /** What a friend sent the user: a link of Ta (a card, a conversation, an invitation), opened as every link is. */
6
+ export interface RelayFriendShare {
7
+ id: string;
8
+ link: string;
9
+ title: string;
10
+ by: {
11
+ id: string;
12
+ name: string;
13
+ look?: string;
14
+ };
15
+ at: number;
16
+ /** The user opened it once. */
17
+ seen: boolean;
18
+ }
19
+ /** What it reads and does of the rooms (RelayHubs). */
20
+ export interface RelayFriendSendRooms {
21
+ identity(): Promise<RelayIdentity>;
22
+ list(): RelayRoomInfo[];
23
+ record(room: string): RelayRoomRecord | undefined;
24
+ friends(): (RelayFriend & RelayPublicIdentity)[];
25
+ friendRoom(): Promise<RelayRoomInfo>;
26
+ getState(room: string, prefix?: string): Promise<Record<string, RelayRoomValue>>;
27
+ setState(room: string, key: string, value: unknown): Promise<{
28
+ ok: boolean;
29
+ version: number;
30
+ }>;
31
+ }
32
+ /** What it keeps (`sends.json`): what the user opened and what they let go, by id, till it would be gone anyway. */
33
+ interface SendsBook {
34
+ seen: Record<string, number>;
35
+ gone: Record<string, number>;
36
+ }
37
+ /**
38
+ * Links sent to friends from the share sheet (docs/design/ta-links.md 4), as invitations into group chats are: sealed
39
+ * for each friend (ECDH of the two, the friend's id the salt) and left in the sender's room of friends, where the
40
+ * friend's Ta reads it even after being away. The relay reads none of it; a friend opens it as any link of Ta, asked
41
+ * first wherever it would do something (join, add, import).
42
+ */
43
+ export declare class RelayFriendSends {
44
+ protected readonly rooms: RelayFriendSendRooms;
45
+ protected readonly store: RelayHubsStore;
46
+ constructor(rooms: RelayFriendSendRooms, store: RelayHubsStore);
47
+ /** Sends a link to friends: how many it went to. */
48
+ send(friendIds: readonly string[], item: {
49
+ link: string;
50
+ title: string;
51
+ }): Promise<number>;
52
+ /** What friends sent the user, not let go, the latest first. */
53
+ list(): Promise<RelayFriendShare[]>;
54
+ /** The user opened one (`keep`), or let it go: no longer listed. */
55
+ answer(id: string, keep: boolean): void;
56
+ /** Whether a change of the state is of something a friend may have sent the user: the pages read again. */
57
+ static heard(record: RelayRoomRecord | undefined, key: string): boolean;
58
+ /** The key of a thing sent: neither it nor the friend can be read from it by the other friends. */
59
+ static keyOf(id: string, friend: string): Promise<string>;
60
+ protected book(): SendsBook;
61
+ /** What the user sent that no longer waits, taken out of the room of friends. */
62
+ protected prune(room: string, now: number): Promise<void>;
63
+ }
64
+ export {};
65
+ //# sourceMappingURL=relay-friend-sends.d.ts.map
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayFriendSends = exports.RELAY_FRIEND_SEND = void 0;
4
+ const relay_bytes_1 = require("./relay-bytes");
5
+ const relay_friends_1 = require("./relay-friends");
6
+ const relay_identity_1 = require("./relay-identity");
7
+ /** The keys of the room of friends of the one who sends that hold what it sent, one a friend and thing. */
8
+ exports.RELAY_FRIEND_SEND = 'send:';
9
+ /** How long a thing sent waits for the friend. */
10
+ const DAYS = 30;
11
+ const INFO = 'ta-friends/1 send';
12
+ const MAX_LINK = 2048;
13
+ const MAX_TITLE = 80;
14
+ /**
15
+ * Links sent to friends from the share sheet (docs/design/ta-links.md 4), as invitations into group chats are: sealed
16
+ * for each friend (ECDH of the two, the friend's id the salt) and left in the sender's room of friends, where the
17
+ * friend's Ta reads it even after being away. The relay reads none of it; a friend opens it as any link of Ta, asked
18
+ * first wherever it would do something (join, add, import).
19
+ */
20
+ class RelayFriendSends {
21
+ constructor(rooms, store) {
22
+ this.rooms = rooms;
23
+ this.store = store;
24
+ }
25
+ /** Sends a link to friends: how many it went to. */
26
+ async send(friendIds, item) {
27
+ var _a, _b;
28
+ const link = String((_a = item.link) !== null && _a !== void 0 ? _a : '').trim();
29
+ if (!/^(https?|ta):\/\/\S+$/i.test(link) || link.length > MAX_LINK) {
30
+ throw new Error('Only a link of Ta goes to friends.');
31
+ }
32
+ const ids = new Set(friendIds);
33
+ const friends = this.rooms.friends().filter(friend => ids.has(friend.id));
34
+ if (friends.length === 0) {
35
+ throw new Error('None of them is a friend.');
36
+ }
37
+ const identity = await this.rooms.identity();
38
+ const mine = await this.rooms.friendRoom();
39
+ const now = Date.now();
40
+ await this.prune(mine.id, now);
41
+ const id = relay_bytes_1.RelayBytes.toBase64(relay_bytes_1.RelayBytes.random(12));
42
+ const opened = { id, link, title: String((_b = item.title) !== null && _b !== void 0 ? _b : '').slice(0, MAX_TITLE), at: now };
43
+ const expiresAt = now + DAYS * 86400000;
44
+ for (const friend of friends) {
45
+ const key = await relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayIdentityUtil.agree(identity, friend.box, friend.id, INFO));
46
+ const sealed = { d: await relay_identity_1.RelayKeys.seal(key, JSON.stringify(opened)), expiresAt };
47
+ await this.rooms.setState(mine.id, await RelayFriendSends.keyOf(id, friend.id), sealed);
48
+ }
49
+ return friends.length;
50
+ }
51
+ /** What friends sent the user, not let go, the latest first. */
52
+ async list() {
53
+ var _a;
54
+ const identity = await this.rooms.identity();
55
+ const { seen, gone } = this.book();
56
+ const now = Date.now();
57
+ const found = [];
58
+ for (const room of this.rooms.list()) {
59
+ const record = this.rooms.record(room.id);
60
+ if (!record || record.own || record.kind !== relay_friends_1.RELAY_FRIENDS_KIND || room.status !== 'member' || !record.owner.box) {
61
+ continue;
62
+ }
63
+ const owner = room.members.find(member => member.owner);
64
+ if (!owner || owner.id !== record.owner.id) {
65
+ continue;
66
+ }
67
+ const key = await relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayIdentityUtil.agree(identity, record.owner.box, identity.id, INFO));
68
+ for (const entry of Object.values(await this.rooms.getState(room.id, exports.RELAY_FRIEND_SEND).catch(() => ({})))) {
69
+ const value = entry.value;
70
+ if (entry.by !== owner.member || !value || typeof value.d !== 'string' || Number(value.expiresAt) <= now) {
71
+ continue;
72
+ }
73
+ const opened = await relay_identity_1.RelayKeys.open(key, value.d).then(text => JSON.parse(text), () => undefined);
74
+ if (!opened || typeof opened.id !== 'string' || typeof opened.link !== 'string' || gone[opened.id] !== undefined) {
75
+ continue;
76
+ }
77
+ if (!/^(https?|ta):\/\/\S+$/i.test(opened.link) || opened.link.length > MAX_LINK) {
78
+ continue;
79
+ }
80
+ found.push({
81
+ id: opened.id,
82
+ link: opened.link,
83
+ title: String((_a = opened.title) !== null && _a !== void 0 ? _a : '').slice(0, MAX_TITLE),
84
+ by: { id: owner.id, name: owner.name, ...(owner.look ? { look: owner.look } : {}) },
85
+ at: Number(opened.at) || now,
86
+ seen: seen[opened.id] !== undefined,
87
+ });
88
+ }
89
+ }
90
+ return found.sort((a, b) => b.at - a.at);
91
+ }
92
+ /** The user opened one (`keep`), or let it go: no longer listed. */
93
+ answer(id, keep) {
94
+ const book = this.book();
95
+ const now = Date.now();
96
+ const until = now + DAYS * 86400000;
97
+ const fresh = (map) => Object.fromEntries(Object.entries(map).filter(([, at]) => at > now));
98
+ book.seen = { ...fresh(book.seen), [id]: until };
99
+ book.gone = keep ? fresh(book.gone) : { ...fresh(book.gone), [id]: until };
100
+ this.store.write('sends', book);
101
+ }
102
+ /** Whether a change of the state is of something a friend may have sent the user: the pages read again. */
103
+ static heard(record, key) {
104
+ return !!record && !record.own && record.kind === relay_friends_1.RELAY_FRIENDS_KIND && key.startsWith(exports.RELAY_FRIEND_SEND);
105
+ }
106
+ /** The key of a thing sent: neither it nor the friend can be read from it by the other friends. */
107
+ static async keyOf(id, friend) {
108
+ return exports.RELAY_FRIEND_SEND + (await relay_identity_1.RelayKeys.hash(`${id}\n${friend}`)).slice(0, 22);
109
+ }
110
+ book() {
111
+ const book = this.store.read('sends');
112
+ const map = (value) => (value && typeof value === 'object' ? value : {});
113
+ return { seen: map(book === null || book === void 0 ? void 0 : book.seen), gone: map(book === null || book === void 0 ? void 0 : book.gone) };
114
+ }
115
+ /** What the user sent that no longer waits, taken out of the room of friends. */
116
+ async prune(room, now) {
117
+ var _a, _b;
118
+ const self = (_b = (_a = this.rooms
119
+ .list()
120
+ .find(info => info.id === room)) === null || _a === void 0 ? void 0 : _a.members.find(member => member.self)) === null || _b === void 0 ? void 0 : _b.member;
121
+ for (const [key, entry] of Object.entries(await this.rooms.getState(room, exports.RELAY_FRIEND_SEND))) {
122
+ const value = entry.value;
123
+ if (entry.by === self && value && Number(value.expiresAt) <= now) {
124
+ await this.rooms.setState(room, key, null).catch(() => undefined);
125
+ }
126
+ }
127
+ }
128
+ }
129
+ exports.RelayFriendSends = RelayFriendSends;
130
+ //# sourceMappingURL=relay-friend-sends.js.map
@@ -0,0 +1,77 @@
1
+ import type { RelayFriend, RelayHubsStore, RelayRoomInfo, RelayRoomRecord, RelayRoomValue } from './relay-hubs-protocol';
2
+ import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
3
+ /** The keys of the room of friends of the one who invites that hold its invitations to group chats, one a friend and chat. */
4
+ export declare const RELAY_GROUP_INVITE = "invite:";
5
+ /** One a friend invited the user into: a group chat of its own, the user's to join or not. */
6
+ export interface RelayGroupInvite {
7
+ /** The id of the room. */
8
+ room: string;
9
+ title: string;
10
+ by: {
11
+ id: string;
12
+ name: string;
13
+ look?: string;
14
+ };
15
+ at: number;
16
+ expiresAt: number;
17
+ }
18
+ /** What it reads and does of the rooms (RelayHubs). */
19
+ export interface RelayGroupRooms {
20
+ identity(): Promise<RelayIdentity>;
21
+ list(): RelayRoomInfo[];
22
+ record(room: string): RelayRoomRecord | undefined;
23
+ save(): void;
24
+ friends(): (RelayFriend & RelayPublicIdentity)[];
25
+ friendRoom(): Promise<RelayRoomInfo>;
26
+ invite(room: string, options: {
27
+ days?: number;
28
+ }): Promise<{
29
+ link: string;
30
+ expiresAt: number;
31
+ }>;
32
+ join(link: string): Promise<RelayRoomInfo>;
33
+ getState(room: string, prefix?: string): Promise<Record<string, RelayRoomValue>>;
34
+ setState(room: string, key: string, value: unknown): Promise<{
35
+ ok: boolean;
36
+ version: number;
37
+ }>;
38
+ }
39
+ /** What it keeps (`groups.json`): the invitations the user turned down, by room, till they would no longer work. */
40
+ interface GroupsBook {
41
+ declined: Record<string, number>;
42
+ }
43
+ /**
44
+ * Friends invited into a group chat (docs/design/ta-partners.md): the owner seals the invitation's link for each friend
45
+ * (ECDH of the two, the friend's id the salt) and leaves it in its room of friends, where the friend's Ta reads it even
46
+ * after being away; the friend joins on the user's word, and the owner's Ta lets in at once those it invited, by the
47
+ * identity their ask is signed with, and no one else: another who has the link only asks, as by any link.
48
+ */
49
+ export declare class RelayGroupInvites {
50
+ protected readonly rooms: RelayGroupRooms;
51
+ protected readonly store: RelayHubsStore;
52
+ constructor(rooms: RelayGroupRooms, store: RelayHubsStore);
53
+ /** Invites friends into a group chat of the user's own: how many were. */
54
+ invite(roomId: string, friendIds: readonly string[]): Promise<number>;
55
+ /** Whether an ask to a room of the user's is of a friend it invited: let in at once. */
56
+ admits(record: RelayRoomRecord, ask: RelayPublicIdentity & {
57
+ guest?: boolean;
58
+ }): boolean;
59
+ /** One invited is in: its invitation goes. */
60
+ admitted(record: RelayRoomRecord, id: string): Promise<void>;
61
+ /** The group chats friends invited the user into, not joined nor turned down, the latest first. */
62
+ list(): Promise<RelayGroupInvite[]>;
63
+ /** The user's answer to an invitation: joined, or no longer listed. */
64
+ answer(roomId: string, accept: boolean): Promise<void>;
65
+ /** Whether a change of the state is of an invitation the user may have: the page reads them again. */
66
+ static heard(record: RelayRoomRecord | undefined, key: string): boolean;
67
+ /** The key of an invitation: neither the room nor the friend can be read from it by the other friends. */
68
+ static keyOf(room: string, friend: string): Promise<string>;
69
+ protected book(): GroupsBook;
70
+ /** The invitations of the user's own that no longer work, taken out of the room of friends. */
71
+ protected prune(room: string, now: number): Promise<void>;
72
+ protected read(): Promise<(RelayGroupInvite & {
73
+ link: string;
74
+ })[]>;
75
+ }
76
+ export {};
77
+ //# sourceMappingURL=relay-group-invites.d.ts.map