@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
@@ -0,0 +1,163 @@
1
+ import { RELAY_FRIENDS_KIND } from './relay-friends.js';
2
+ import { RelayHubUtil } from './relay-hub-util.js';
3
+ import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
4
+ /** The keys of the room of friends of the one who invites that hold its invitations to group chats, one a friend and chat. */
5
+ export const RELAY_GROUP_INVITE = 'invite:';
6
+ /** How long an invitation to a group chat works. */
7
+ const DAYS = 7;
8
+ const INFO = 'ta-group/1 invite';
9
+ /**
10
+ * Friends invited into a group chat (docs/design/ta-partners.md): the owner seals the invitation's link for each friend
11
+ * (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
12
+ * 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
13
+ * identity their ask is signed with, and no one else: another who has the link only asks, as by any link.
14
+ */
15
+ export class RelayGroupInvites {
16
+ constructor(rooms, store) {
17
+ this.rooms = rooms;
18
+ this.store = store;
19
+ }
20
+ /** Invites friends into a group chat of the user's own: how many were. */
21
+ async invite(roomId, friendIds) {
22
+ var _a;
23
+ const record = this.rooms.record(roomId);
24
+ if (!(record === null || record === void 0 ? void 0 : record.own) || record.status !== 'member' || record.unlisted || record.kind === RELAY_FRIENDS_KIND) {
25
+ throw new Error('Only a group chat of your own takes friends.');
26
+ }
27
+ const ids = new Set(friendIds);
28
+ const friends = this.rooms.friends().filter(friend => ids.has(friend.id));
29
+ if (friends.length === 0) {
30
+ throw new Error('None of them is a friend.');
31
+ }
32
+ const { link, expiresAt } = await this.rooms.invite(roomId, { days: DAYS });
33
+ const identity = await this.rooms.identity();
34
+ const mine = await this.rooms.friendRoom();
35
+ const now = Date.now();
36
+ record.invited = {
37
+ ...Object.fromEntries(Object.entries((_a = record.invited) !== null && _a !== void 0 ? _a : {}).filter(([, until]) => until > now)),
38
+ ...Object.fromEntries(friends.map(friend => [friend.id, expiresAt])),
39
+ };
40
+ this.rooms.save();
41
+ await this.prune(mine.id, now);
42
+ for (const friend of friends) {
43
+ const key = await RelayKeys.aesKey(await RelayIdentityUtil.agree(identity, friend.box, friend.id, INFO));
44
+ const opened = { link, title: record.title, at: now, expiresAt };
45
+ const sealed = { d: await RelayKeys.seal(key, JSON.stringify(opened)), expiresAt };
46
+ await this.rooms.setState(mine.id, await RelayGroupInvites.keyOf(roomId, friend.id), sealed);
47
+ }
48
+ return friends.length;
49
+ }
50
+ /** Whether an ask to a room of the user's is of a friend it invited: let in at once. */
51
+ admits(record, ask) {
52
+ var _a;
53
+ var _b;
54
+ return record.own && !ask.guest && ((_b = (_a = record.invited) === null || _a === void 0 ? void 0 : _a[ask.id]) !== null && _b !== void 0 ? _b : 0) > Date.now();
55
+ }
56
+ /** One invited is in: its invitation goes. */
57
+ async admitted(record, id) {
58
+ var _a;
59
+ if (((_a = record.invited) === null || _a === void 0 ? void 0 : _a[id]) !== undefined) {
60
+ const { [id]: _gone, ...rest } = record.invited;
61
+ record.invited = rest;
62
+ this.rooms.save();
63
+ }
64
+ const mine = await this.rooms.friendRoom().catch(() => undefined);
65
+ if (mine) {
66
+ await this.rooms.setState(mine.id, await RelayGroupInvites.keyOf(record.id, id), null).catch(() => undefined);
67
+ }
68
+ }
69
+ /** The group chats friends invited the user into, not joined nor turned down, the latest first. */
70
+ async list() {
71
+ return (await this.read()).map(({ link: _link, ...invite }) => invite);
72
+ }
73
+ /** The user's answer to an invitation: joined, or no longer listed. */
74
+ async answer(roomId, accept) {
75
+ const invite = (await this.read()).find(each => each.room === roomId);
76
+ if (!invite) {
77
+ throw new Error('No friend invites you there now.');
78
+ }
79
+ if (accept) {
80
+ await this.rooms.join(invite.link);
81
+ return;
82
+ }
83
+ const book = this.book();
84
+ const now = Date.now();
85
+ book.declined = Object.fromEntries([...Object.entries(book.declined).filter(([, until]) => until > now), [roomId, invite.expiresAt]]);
86
+ this.store.write('groups', book);
87
+ }
88
+ /** Whether a change of the state is of an invitation the user may have: the page reads them again. */
89
+ static heard(record, key) {
90
+ return !!record && !record.own && record.kind === RELAY_FRIENDS_KIND && key.startsWith(RELAY_GROUP_INVITE);
91
+ }
92
+ /** The key of an invitation: neither the room nor the friend can be read from it by the other friends. */
93
+ static async keyOf(room, friend) {
94
+ return RELAY_GROUP_INVITE + (await RelayKeys.hash(`${room}\n${friend}`)).slice(0, 22);
95
+ }
96
+ book() {
97
+ const book = this.store.read('groups');
98
+ return { declined: (book === null || book === void 0 ? void 0 : book.declined) && typeof book.declined === 'object' ? book.declined : {} };
99
+ }
100
+ /** The invitations of the user's own that no longer work, taken out of the room of friends. */
101
+ async prune(room, now) {
102
+ var _a, _b;
103
+ const self = (_b = (_a = this.rooms
104
+ .list()
105
+ .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;
106
+ for (const [key, entry] of Object.entries(await this.rooms.getState(room, RELAY_GROUP_INVITE))) {
107
+ const value = entry.value;
108
+ if (entry.by === self && value && Number(value.expiresAt) <= now) {
109
+ await this.rooms.setState(room, key, null).catch(() => undefined);
110
+ }
111
+ }
112
+ }
113
+ async read() {
114
+ const identity = await this.rooms.identity();
115
+ const all = this.rooms.list();
116
+ const inside = new Set(all.filter(room => room.status === 'member' || room.status === 'waiting').map(room => room.id));
117
+ const { declined } = this.book();
118
+ const now = Date.now();
119
+ const found = [];
120
+ for (const room of all) {
121
+ const record = this.rooms.record(room.id);
122
+ if (!record || record.own || record.kind !== RELAY_FRIENDS_KIND || room.status !== 'member' || !record.owner.box) {
123
+ continue;
124
+ }
125
+ const owner = room.members.find(member => member.owner);
126
+ if (!owner || owner.id !== record.owner.id) {
127
+ continue;
128
+ }
129
+ const key = await RelayKeys.aesKey(await RelayIdentityUtil.agree(identity, record.owner.box, identity.id, INFO));
130
+ for (const entry of Object.values(await this.rooms.getState(room.id, RELAY_GROUP_INVITE).catch(() => ({})))) {
131
+ const value = entry.value;
132
+ if (entry.by !== owner.member || !value || typeof value.d !== 'string' || Number(value.expiresAt) <= now) {
133
+ continue;
134
+ }
135
+ const opened = await RelayKeys.open(key, value.d).then(text => JSON.parse(text), () => undefined);
136
+ if (!opened || typeof opened.link !== 'string') {
137
+ continue;
138
+ }
139
+ let link;
140
+ try {
141
+ link = RelayHubUtil.parse(opened.link);
142
+ }
143
+ catch (_a) {
144
+ continue;
145
+ }
146
+ // Only a room of the friend's own, by a link it made.
147
+ if (link.owner !== owner.id || link.kind || inside.has(link.room) || declined[link.room] !== undefined || opened.expiresAt <= now) {
148
+ continue;
149
+ }
150
+ found.push({
151
+ room: link.room,
152
+ title: String(opened.title || link.title).slice(0, 80),
153
+ by: { id: owner.id, name: owner.name, ...(owner.look ? { look: owner.look } : {}) },
154
+ at: Number(opened.at) || now,
155
+ expiresAt: opened.expiresAt,
156
+ link: opened.link,
157
+ });
158
+ }
159
+ }
160
+ return found.sort((a, b) => b.at - a.at);
161
+ }
162
+ }
163
+ //# sourceMappingURL=relay-group-invites.js.map
@@ -1,5 +1,5 @@
1
1
  import { RelayKeys } from './relay-identity.js';
2
- import { RELAY_HUB_ASK_DAYS, RELAY_HUB_IDLE_DAYS, RELAY_HUB_MAX_ASKS, RELAY_HUB_MAX_KEYS, RELAY_HUB_MAX_MEMBERS, RELAY_HUB_MAX_STATE, RELAY_HUB_MAX_VALUE, RELAY_HUB_RATE, RELAY_ID, RELAY_PING, RELAY_PONG, } from './relay-protocol.js';
2
+ import { RELAY_HUB_ASK_DAYS, RELAY_HUB_IDLE_DAYS, RELAY_HUB_MAX_ASKS, RELAY_HUB_MAX_KEYS, RELAY_HUB_MAX_MEMBERS, RELAY_HUB_MAX_STATE, RELAY_HUB_MAX_VALUE, RELAY_HUB_RATE, RELAY_ID, RELAY_INVITE_MAX_DAYS, RELAY_PING, RELAY_PONG, } from './relay-protocol.js';
3
3
  /** Close codes of a hub. */
4
4
  export var RelayHubCloseCode;
5
5
  (function (RelayHubCloseCode) {
@@ -251,7 +251,7 @@ export class RelayHub {
251
251
  this.send(socket, { t: 'error', code: 'invalid', message: 'An invitation needs a hash and a time.' });
252
252
  return;
253
253
  }
254
- meta.invite = { hash: request.hash, expiresAt: Math.min(request.expiresAt, this.now() + 30 * DAY_MS) };
254
+ meta.invite = { hash: request.hash, expiresAt: Math.min(request.expiresAt, this.now() + RELAY_INVITE_MAX_DAYS * DAY_MS) };
255
255
  await this.storage.put('meta', meta);
256
256
  return;
257
257
  case 'admit':
@@ -0,0 +1,36 @@
1
+ import type { RelayFriends } from './relay-friends.js';
2
+ import { RelayHubsConnection } from './relay-hubs-connection.js';
3
+ import type { RelayIdentityBundle, RelayIdentityImport, RelayRoomRecord } from './relay-hubs-protocol.js';
4
+ import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity.js';
5
+ export declare abstract class RelayHubsIdentity extends RelayHubsConnection {
6
+ protected abstract readonly book: RelayFriends;
7
+ /** The name others see; the owners of rooms this Ta is in hear it. */
8
+ setName(name: string): Promise<RelayPublicIdentity>;
9
+ /**
10
+ * The kind of dinosaur the double of this Ta is (RelayPublicIdentity.look), which others draw it by; none for a
11
+ * look that is none. The owners of rooms this Ta is in hear it, as a name.
12
+ */
13
+ setLook(look: string | undefined): Promise<RelayPublicIdentity>;
14
+ /** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
15
+ protected changeSelf(identity: RelayIdentity): Promise<RelayPublicIdentity>;
16
+ /** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
17
+ exportIdentity(): Promise<RelayIdentityBundle>;
18
+ /**
19
+ * Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
20
+ * sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
21
+ * (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
22
+ */
23
+ importIdentity(bundle: RelayIdentityBundle, options?: {
24
+ shared?: boolean;
25
+ }): Promise<RelayIdentityImport>;
26
+ /** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
27
+ setShared(shared: boolean): Promise<void>;
28
+ /**
29
+ * A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
30
+ * (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
31
+ * as another member.
32
+ */
33
+ upsert(record: RelayRoomRecord): boolean;
34
+ /** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
35
+ forget(roomId: string, member: string): boolean;
36
+ }
@@ -0,0 +1,156 @@
1
+ import { Live, RelayHubsConnection } from './relay-hubs-connection.js';
2
+ import { RelayHubsUtil } from './relay-hubs-util.js';
3
+ import { RelayIdentityUtil } from './relay-identity.js';
4
+ export class RelayHubsIdentity extends RelayHubsConnection {
5
+ /** The name others see; the owners of rooms this Ta is in hear it. */
6
+ async setName(name) {
7
+ const trimmed = String(name).trim().slice(0, 40);
8
+ if (!trimmed) {
9
+ throw new Error('A name is needed.');
10
+ }
11
+ return this.changeSelf({ ...(await this.identity()), name: trimmed });
12
+ }
13
+ /**
14
+ * The kind of dinosaur the double of this Ta is (RelayPublicIdentity.look), which others draw it by; none for a
15
+ * look that is none. The owners of rooms this Ta is in hear it, as a name.
16
+ */
17
+ async setLook(look) {
18
+ const identity = await this.identity();
19
+ const next = RelayIdentityUtil.withLook(identity, look);
20
+ return next.look === identity.look ? RelayIdentityUtil.publicOf(identity) : this.changeSelf(next);
21
+ }
22
+ /** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
23
+ async changeSelf(identity) {
24
+ var _a;
25
+ this.options.store.write('identity', identity);
26
+ this.own = identity;
27
+ for (const live of this.live.values()) {
28
+ // A room joined with another identity keeps that one's name.
29
+ if (live.record.status !== 'member' || live.record.identity) {
30
+ continue;
31
+ }
32
+ if (live.record.own) {
33
+ await this.writeRoster(live, members => members.map(member => (member.id === identity.id ? RelayIdentityUtil.withLook({ ...member, name: identity.name }, identity.look) : member))).catch(() => undefined);
34
+ }
35
+ else if (live.roster) {
36
+ const owner = live.roster.members.find(member => member.owner);
37
+ if (owner) {
38
+ const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
39
+ await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
40
+ live.toldLook = look;
41
+ }, () => undefined);
42
+ }
43
+ }
44
+ }
45
+ return RelayIdentityUtil.publicOf(identity);
46
+ }
47
+ /** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
48
+ async exportIdentity() {
49
+ return { identity: await this.identity(), rooms: [...this.live.values()].map(live => live.record), friends: this.book.chosen() };
50
+ }
51
+ /**
52
+ * Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
53
+ * sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
54
+ * (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
55
+ */
56
+ async importIdentity(bundle, options = {}) {
57
+ const incoming = bundle === null || bundle === void 0 ? void 0 : bundle.identity;
58
+ if (!RelayHubsUtil.isPrivate(incoming) || !(await RelayIdentityUtil.check(incoming))) {
59
+ throw new Error('Not an identity of Ta.');
60
+ }
61
+ const own = await this.identity();
62
+ const switched = incoming.id !== own.id;
63
+ if (switched) {
64
+ for (const live of this.live.values()) {
65
+ if (!live.record.identity) {
66
+ live.record = { ...live.record, identity: own };
67
+ }
68
+ }
69
+ }
70
+ const shared = options.shared || own.shared;
71
+ const next = { ...(switched ? incoming : own), ...(shared ? { shared: true } : {}) };
72
+ if (!shared) {
73
+ delete next.shared;
74
+ }
75
+ this.own = next;
76
+ this.options.store.write('identity', next);
77
+ let rooms = 0;
78
+ for (const record of Array.isArray(bundle.rooms) ? bundle.rooms : []) {
79
+ if (this.upsert(record)) {
80
+ rooms++;
81
+ }
82
+ }
83
+ const friends = this.book.merge(Array.isArray(bundle.friends) ? bundle.friends : []);
84
+ this.save();
85
+ this.emit({ type: 'rooms' });
86
+ return { switched, rooms, friends };
87
+ }
88
+ /** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
89
+ async setShared(shared) {
90
+ const identity = { ...(await this.identity()) };
91
+ if (!!identity.shared === shared) {
92
+ return;
93
+ }
94
+ if (shared) {
95
+ identity.shared = true;
96
+ }
97
+ else {
98
+ delete identity.shared;
99
+ }
100
+ this.own = identity;
101
+ this.options.store.write('identity', identity);
102
+ }
103
+ /**
104
+ * A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
105
+ * (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
106
+ * as another member.
107
+ */
108
+ upsert(record) {
109
+ if (!RelayHubsUtil.isRecord(record)) {
110
+ return false;
111
+ }
112
+ const live = this.live.get(record.id);
113
+ if (!live) {
114
+ const added = new Live({ ...record });
115
+ this.live.set(record.id, added);
116
+ if (record.status === 'member' || record.status === 'waiting') {
117
+ this.connect(added);
118
+ }
119
+ this.save();
120
+ this.emit({ type: 'rooms' });
121
+ return true;
122
+ }
123
+ if (live.record.member !== record.member) {
124
+ return false;
125
+ }
126
+ const merged = RelayHubsUtil.merge(live.record, record);
127
+ if (JSON.stringify(merged) === JSON.stringify(live.record)) {
128
+ return false;
129
+ }
130
+ const was = live.record.status;
131
+ live.record = merged;
132
+ if (merged.status !== 'member' && merged.status !== 'waiting') {
133
+ this.disconnect(live);
134
+ }
135
+ else if (was !== merged.status || !live.socket) {
136
+ this.disconnect(live);
137
+ this.connect(live);
138
+ }
139
+ this.save();
140
+ this.emit({ type: 'rooms' });
141
+ return true;
142
+ }
143
+ /** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
144
+ forget(roomId, member) {
145
+ const live = this.live.get(roomId);
146
+ if (!live || live.record.member !== member) {
147
+ return false;
148
+ }
149
+ this.disconnect(live);
150
+ this.live.delete(roomId);
151
+ this.save();
152
+ this.emit({ type: 'rooms' });
153
+ return true;
154
+ }
155
+ }
156
+ //# sourceMappingURL=relay-hubs-identity.js.map
@@ -28,8 +28,12 @@ export interface RelayRoomRecord {
28
28
  }[];
29
29
  /** Of the owner: the relay has the room. */
30
30
  made?: boolean;
31
+ /** Of the owner: the members it lets in at most, when not RELAY_ROOM_MAX_MEMBERS (RelayHubs.configure). */
32
+ capacity?: number;
31
33
  /** Of one asking: the invitation, till the relay has the ask. */
32
34
  invite?: string;
35
+ /** Of the owner: the friends invited into it (RelayGroupInvites), by identity id, till when: let in at once as they ask. */
36
+ invited?: Record<string, number>;
33
37
  asked?: boolean;
34
38
  createdAt: number;
35
39
  joinedAt?: number;
@@ -73,6 +77,8 @@ export interface RelayRoomInfo {
73
77
  /** The relay it is on. */
74
78
  relay: string;
75
79
  owner: boolean;
80
+ /** Of a room this Ta made: the members it lets in at most. */
81
+ capacity?: number;
76
82
  ownerName: string;
77
83
  status: RelayRoomStatus;
78
84
  reason?: RelayRoomRecord['reason'];
@@ -164,7 +170,7 @@ export interface RelayHubsStore {
164
170
  write(name: RelayHubsStoreName, value: unknown): void;
165
171
  }
166
172
  /** The identity, the rooms (with their keys) and the friends. */
167
- export type RelayHubsStoreName = 'identity' | 'rooms' | 'friends';
173
+ export type RelayHubsStoreName = 'identity' | 'rooms' | 'friends' | 'groups' | 'sends';
168
174
  export interface RelayHubsOptions {
169
175
  store: RelayHubsStore;
170
176
  /** The name of a new identity: that of the user on the system. */
@@ -0,0 +1,11 @@
1
+ import type { RelayRoomRecord } from './relay-hubs-protocol.js';
2
+ import type { RelayIdentity } from './relay-identity.js';
3
+ export declare namespace RelayHubsUtil {
4
+ function isPrivate(identity: unknown): identity is RelayIdentity;
5
+ function isRecord(record: unknown): record is RelayRoomRecord;
6
+ /**
7
+ * Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
8
+ * key and the owner's keys once either has them, the invitations of both, made once either made it.
9
+ */
10
+ function merge(local: RelayRoomRecord, other: RelayRoomRecord): RelayRoomRecord;
11
+ }
@@ -0,0 +1,74 @@
1
+ /** How far a membership has come: one computer heard it end, it ended for both. */
2
+ const STATUS_RANK = { waiting: 0, member: 1, refused: 2, gone: 3 };
3
+ export var RelayHubsUtil;
4
+ (function (RelayHubsUtil) {
5
+ function isPrivate(identity) {
6
+ const value = identity;
7
+ return (!!value &&
8
+ typeof value.id === 'string' &&
9
+ typeof value.name === 'string' &&
10
+ typeof value.sign === 'string' &&
11
+ typeof value.box === 'string' &&
12
+ !!value.signKey &&
13
+ typeof value.signKey === 'object' &&
14
+ !!value.boxKey &&
15
+ typeof value.boxKey === 'object');
16
+ }
17
+ RelayHubsUtil.isPrivate = isPrivate;
18
+ function isRecord(record) {
19
+ const value = record;
20
+ return (!!value &&
21
+ typeof value.id === 'string' &&
22
+ typeof value.relay === 'string' &&
23
+ typeof value.member === 'string' &&
24
+ typeof value.token === 'string' &&
25
+ typeof value.status === 'string' &&
26
+ value.status in STATUS_RANK &&
27
+ !!value.owner &&
28
+ typeof value.owner.id === 'string' &&
29
+ (value.identity === undefined || isPrivate(value.identity)));
30
+ }
31
+ RelayHubsUtil.isRecord = isRecord;
32
+ /**
33
+ * Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
34
+ * key and the owner's keys once either has them, the invitations of both, made once either made it.
35
+ */
36
+ function merge(local, other) {
37
+ var _a, _b, _c, _d, _e, _f;
38
+ const ahead = STATUS_RANK[other.status] > STATUS_RANK[local.status] ? other : local;
39
+ const behind = ahead === local ? other : local;
40
+ const invites = [...((_a = local.invites) !== null && _a !== void 0 ? _a : []), ...((_b = other.invites) !== null && _b !== void 0 ? _b : [])]
41
+ .filter((invite, index, all) => all.findIndex(each => each.secret === invite.secret) === index)
42
+ .sort((a, b) => a.expiresAt - b.expiresAt)
43
+ .slice(-3);
44
+ const merged = {
45
+ ...behind,
46
+ ...ahead,
47
+ kind: ahead.kind || behind.kind,
48
+ owner: ahead.owner.sign ? ahead.owner : behind.owner.sign ? behind.owner : ahead.owner,
49
+ createdAt: Math.min(local.createdAt, other.createdAt),
50
+ };
51
+ const key = (_c = ahead.key) !== null && _c !== void 0 ? _c : behind.key;
52
+ const joinedAt = Math.max((_d = local.joinedAt) !== null && _d !== void 0 ? _d : 0, (_e = other.joinedAt) !== null && _e !== void 0 ? _e : 0);
53
+ const identity = (_f = local.identity) !== null && _f !== void 0 ? _f : other.identity;
54
+ delete merged.key;
55
+ delete merged.invites;
56
+ delete merged.joinedAt;
57
+ delete merged.identity;
58
+ delete merged.made;
59
+ if (merged.status !== 'waiting') {
60
+ delete merged.invite;
61
+ delete merged.asked;
62
+ }
63
+ return {
64
+ ...merged,
65
+ ...(key && (merged.status === 'member' || merged.status === 'waiting') ? { key } : {}),
66
+ ...(invites.length ? { invites } : {}),
67
+ ...(local.made || other.made ? { made: true } : {}),
68
+ ...(joinedAt ? { joinedAt } : {}),
69
+ ...(identity ? { identity } : {}),
70
+ };
71
+ }
72
+ RelayHubsUtil.merge = merge;
73
+ })(RelayHubsUtil || (RelayHubsUtil = {}));
74
+ //# sourceMappingURL=relay-hubs-util.js.map
@@ -1,29 +1,25 @@
1
+ import { RelayFriendSends, type RelayFriendShare } from './relay-friend-sends.js';
1
2
  import { RelayFriends } from './relay-friends.js';
3
+ import { type RelayGroupInvite, RelayGroupInvites } from './relay-group-invites.js';
2
4
  import { type RelayHubAskBody } from './relay-hub-util.js';
3
- import { Live, RelayHubsConnection } from './relay-hubs-connection.js';
4
- import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayIdentityBundle, RelayIdentityImport, RelayRoomEvent, RelayRoomInfo, RelayRoomRecord, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol.js';
5
- import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity.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';
8
+ import { type RelayPublicIdentity } from './relay-identity.js';
6
9
  /**
7
10
  * The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
8
11
  * and a connection to the hub of each while it runs, through which it sends, sets the shared state and hears
9
12
  * the others; every message signed by its member and sealed with the key of the room. The friends are a room of
10
13
  * their own (RelayFriends).
11
14
  */
12
- export declare class RelayHubs extends RelayHubsConnection {
15
+ export declare class RelayHubs extends RelayHubsIdentity {
13
16
  protected readonly book: RelayFriends;
17
+ protected readonly groups: RelayGroupInvites;
18
+ protected readonly sends: RelayFriendSends;
14
19
  /** Connects to the rooms of rooms.json, and keeps connected. */
15
20
  start(): void;
16
21
  stop(): void;
17
22
  onEvent(listener: (event: RelayRoomEvent) => void): () => void;
18
- /** The name others see; the owners of rooms this Ta is in hear it. */
19
- setName(name: string): Promise<RelayPublicIdentity>;
20
- /**
21
- * The kind of dinosaur the double of this Ta is (RelayPublicIdentity.look), which others draw it by; none for a
22
- * look that is none. The owners of rooms this Ta is in hear it, as a name.
23
- */
24
- setLook(look: string | undefined): Promise<RelayPublicIdentity>;
25
- /** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
26
- protected changeSelf(identity: RelayIdentity): Promise<RelayPublicIdentity>;
27
23
  list(): RelayRoomInfo[];
28
24
  room(id: string): RelayRoomInfo | undefined;
29
25
  create(input: {
@@ -35,7 +31,7 @@ export declare class RelayHubs extends RelayHubsConnection {
35
31
  }): Promise<RelayRoomInfo>;
36
32
  /**
37
33
  * The link that lets others ask to join: the one of before while it has a day left, else (or with `renew`) a new
38
- * one for `days` (7 by default, 30 at most), the links of before no longer working.
34
+ * one for `days` (7 by default, RELAY_INVITE_MAX_DAYS at most), the links of before no longer working.
39
35
  */
40
36
  invite(roomId: string, options?: {
41
37
  days?: number;
@@ -44,12 +40,22 @@ export declare class RelayHubs extends RelayHubsConnection {
44
40
  link: string;
45
41
  expiresAt: number;
46
42
  }>;
47
- /** Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it. */
43
+ /**
44
+ * Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it.
45
+ * A room the identity owns on the user's other computer (an identity shared or moved) is joined as any other.
46
+ */
48
47
  join(text: string): Promise<RelayRoomInfo>;
49
48
  /** Those asking to join the rooms of this Ta, the earliest first. */
50
49
  requests(): Promise<RelayRoomRequest[]>;
51
50
  /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
52
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;
53
59
  /** The owner takes a member out: the relay closes it at once. */
54
60
  remove(roomId: string, member: string): Promise<void>;
55
61
  /** Leaves the room; the owner closes it, for everyone. A room asked to join is forgotten. */
@@ -66,26 +72,6 @@ export declare class RelayHubs extends RelayHubsConnection {
66
72
  ok: boolean;
67
73
  version: number;
68
74
  }>;
69
- /** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
70
- exportIdentity(): Promise<RelayIdentityBundle>;
71
- /**
72
- * Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
73
- * sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
74
- * (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
75
- */
76
- importIdentity(bundle: RelayIdentityBundle, options?: {
77
- shared?: boolean;
78
- }): Promise<RelayIdentityImport>;
79
- /** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
80
- setShared(shared: boolean): Promise<void>;
81
- /**
82
- * A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
83
- * (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
84
- * as another member.
85
- */
86
- upsert(record: RelayRoomRecord): boolean;
87
- /** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
88
- forget(roomId: string, member: string): boolean;
89
75
  /** Friends of the user's other computer, with those of this Ta. */
90
76
  mergeFriends(friends: RelayFriend[]): number;
91
77
  /** The friends, with their keys: each in the room of the other (shares to friends are sealed for them). */
@@ -108,6 +94,21 @@ export declare class RelayHubs extends RelayHubsConnection {
108
94
  answerFriend(id: string, accept: boolean): Promise<void>;
109
95
  /** No longer friends, on both sides. */
110
96
  removeFriend(id: string): Promise<boolean>;
97
+ /** Invites friends into a group chat of the user's own: they join on their word, let in at once. How many were. */
98
+ inviteFriends(roomId: string, friends: readonly string[]): Promise<number>;
99
+ /** The group chats friends invited the user into. */
100
+ groupInvites(): Promise<RelayGroupInvite[]>;
101
+ /** The user's answer to an invitation into a group chat. */
102
+ answerGroupInvite(roomId: string, accept: boolean): Promise<void>;
103
+ /** Sends a link to friends from the share sheet: how many it went to. */
104
+ sendToFriends(friends: readonly string[], item: {
105
+ link: string;
106
+ title: string;
107
+ }): Promise<number>;
108
+ /** What friends sent the user. */
109
+ friendShares(): Promise<RelayFriendShare[]>;
110
+ /** The user opened one (`keep`) or let it go. */
111
+ answerFriendShare(id: string, keep: boolean): void;
111
112
  protected isFriends(live: Live): boolean;
112
113
  protected friendIds(): Set<string>;
113
114
  protected heardAsk(live: Live, ask: RelayHubAskBody): void;
@@ -119,12 +120,3 @@ export declare class RelayHubs extends RelayHubsConnection {
119
120
  protected member(roomId: string): Live;
120
121
  protected info(live: Live): RelayRoomInfo;
121
122
  }
122
- export declare namespace RelayHubsUtil {
123
- function isPrivate(identity: unknown): identity is RelayIdentity;
124
- function isRecord(record: unknown): record is RelayRoomRecord;
125
- /**
126
- * Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
127
- * key and the owner's keys once either has them, the invitations of both, made once either made it.
128
- */
129
- function merge(local: RelayRoomRecord, other: RelayRoomRecord): RelayRoomRecord;
130
- }