@celljs/ta-relay 4.17.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.
- package/esm/common/index.d.ts +1 -0
- package/esm/common/index.js +1 -0
- package/esm/common/relay-faces.d.ts +1 -1
- package/esm/common/relay-faces.js +56 -1
- package/esm/common/relay-files.d.ts +14 -3
- package/esm/common/relay-files.js +41 -16
- package/esm/common/relay-friend-sends.d.ts +64 -0
- package/esm/common/relay-friend-sends.js +126 -0
- package/esm/common/relay-group-invites.d.ts +76 -0
- package/esm/common/relay-group-invites.js +163 -0
- package/esm/common/relay-hubs-protocol.d.ts +3 -1
- package/esm/common/relay-hubs.d.ts +19 -0
- package/esm/common/relay-hubs.js +58 -0
- package/esm/common/relay-open-in-ta.d.ts +11 -0
- package/esm/common/relay-open-in-ta.js +34 -0
- package/esm/common/relay-protocol.d.ts +2 -2
- package/esm/common/relay-protocol.js +5 -2
- package/esm/guest/guest-i18n.d.ts +1 -0
- package/esm/guest/guest-i18n.js +45 -41
- package/esm/guest/guest-style.d.ts +1 -1
- package/esm/guest/guest-style.js +2 -0
- package/esm/guest/guest.js +7 -2
- package/esm/join/join-i18n.d.ts +1 -0
- package/esm/join/join-i18n.js +4 -0
- package/esm/join/join.js +4 -1
- package/esm/node/relay-files-host.d.ts +2 -0
- package/esm/node/relay-files-host.js +24 -5
- package/esm/viewer/viewer-i18n.d.ts +1 -0
- package/esm/viewer/viewer-i18n.js +4 -0
- package/esm/viewer/viewer-style.d.ts +1 -1
- package/esm/viewer/viewer-style.js +2 -0
- package/esm/viewer/viewer.js +3 -1
- package/lib/common/index.d.ts +1 -0
- package/lib/common/index.js +1 -0
- package/lib/common/relay-faces.d.ts +1 -1
- package/lib/common/relay-faces.js +56 -1
- package/lib/common/relay-files.d.ts +14 -3
- package/lib/common/relay-files.js +42 -17
- package/lib/common/relay-friend-sends.d.ts +65 -0
- package/lib/common/relay-friend-sends.js +130 -0
- package/lib/common/relay-group-invites.d.ts +77 -0
- package/lib/common/relay-group-invites.js +167 -0
- package/lib/common/relay-hubs-protocol.d.ts +3 -1
- package/lib/common/relay-hubs.d.ts +19 -0
- package/lib/common/relay-hubs.js +58 -0
- package/lib/common/relay-open-in-ta.d.ts +12 -0
- package/lib/common/relay-open-in-ta.js +37 -0
- package/lib/common/relay-protocol.d.ts +2 -2
- package/lib/common/relay-protocol.js +5 -2
- package/lib/guest/guest-i18n.d.ts +1 -0
- package/lib/guest/guest-i18n.js +45 -41
- package/lib/guest/guest-style.d.ts +1 -1
- package/lib/guest/guest-style.js +2 -0
- package/lib/guest/guest.js +7 -2
- package/lib/join/join-i18n.d.ts +1 -0
- package/lib/join/join-i18n.js +4 -0
- package/lib/join/join.js +4 -1
- package/lib/node/relay-files-host.d.ts +2 -0
- package/lib/node/relay-files-host.js +23 -4
- package/lib/viewer/viewer-i18n.d.ts +1 -0
- package/lib/viewer/viewer-i18n.js +4 -0
- package/lib/viewer/viewer-style.d.ts +1 -1
- package/lib/viewer/viewer-style.js +2 -0
- package/lib/viewer/viewer.js +3 -1
- package/package.json +3 -3
- package/viewer/join.html +12 -3
- package/viewer/page.html +18 -9
- package/viewer/room.html +19 -8
|
@@ -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
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayGroupInvites = exports.RELAY_GROUP_INVITE = void 0;
|
|
4
|
+
const relay_friends_1 = require("./relay-friends");
|
|
5
|
+
const relay_hub_util_1 = require("./relay-hub-util");
|
|
6
|
+
const relay_identity_1 = require("./relay-identity");
|
|
7
|
+
/** The keys of the room of friends of the one who invites that hold its invitations to group chats, one a friend and chat. */
|
|
8
|
+
exports.RELAY_GROUP_INVITE = 'invite:';
|
|
9
|
+
/** How long an invitation to a group chat works. */
|
|
10
|
+
const DAYS = 7;
|
|
11
|
+
const INFO = 'ta-group/1 invite';
|
|
12
|
+
/**
|
|
13
|
+
* Friends invited into a group chat (docs/design/ta-partners.md): the owner seals the invitation's link for each friend
|
|
14
|
+
* (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
|
|
15
|
+
* 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
|
|
16
|
+
* identity their ask is signed with, and no one else: another who has the link only asks, as by any link.
|
|
17
|
+
*/
|
|
18
|
+
class RelayGroupInvites {
|
|
19
|
+
constructor(rooms, store) {
|
|
20
|
+
this.rooms = rooms;
|
|
21
|
+
this.store = store;
|
|
22
|
+
}
|
|
23
|
+
/** Invites friends into a group chat of the user's own: how many were. */
|
|
24
|
+
async invite(roomId, friendIds) {
|
|
25
|
+
var _a;
|
|
26
|
+
const record = this.rooms.record(roomId);
|
|
27
|
+
if (!(record === null || record === void 0 ? void 0 : record.own) || record.status !== 'member' || record.unlisted || record.kind === relay_friends_1.RELAY_FRIENDS_KIND) {
|
|
28
|
+
throw new Error('Only a group chat of your own takes friends.');
|
|
29
|
+
}
|
|
30
|
+
const ids = new Set(friendIds);
|
|
31
|
+
const friends = this.rooms.friends().filter(friend => ids.has(friend.id));
|
|
32
|
+
if (friends.length === 0) {
|
|
33
|
+
throw new Error('None of them is a friend.');
|
|
34
|
+
}
|
|
35
|
+
const { link, expiresAt } = await this.rooms.invite(roomId, { days: DAYS });
|
|
36
|
+
const identity = await this.rooms.identity();
|
|
37
|
+
const mine = await this.rooms.friendRoom();
|
|
38
|
+
const now = Date.now();
|
|
39
|
+
record.invited = {
|
|
40
|
+
...Object.fromEntries(Object.entries((_a = record.invited) !== null && _a !== void 0 ? _a : {}).filter(([, until]) => until > now)),
|
|
41
|
+
...Object.fromEntries(friends.map(friend => [friend.id, expiresAt])),
|
|
42
|
+
};
|
|
43
|
+
this.rooms.save();
|
|
44
|
+
await this.prune(mine.id, now);
|
|
45
|
+
for (const friend of friends) {
|
|
46
|
+
const key = await relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayIdentityUtil.agree(identity, friend.box, friend.id, INFO));
|
|
47
|
+
const opened = { link, title: record.title, at: now, expiresAt };
|
|
48
|
+
const sealed = { d: await relay_identity_1.RelayKeys.seal(key, JSON.stringify(opened)), expiresAt };
|
|
49
|
+
await this.rooms.setState(mine.id, await RelayGroupInvites.keyOf(roomId, friend.id), sealed);
|
|
50
|
+
}
|
|
51
|
+
return friends.length;
|
|
52
|
+
}
|
|
53
|
+
/** Whether an ask to a room of the user's is of a friend it invited: let in at once. */
|
|
54
|
+
admits(record, ask) {
|
|
55
|
+
var _a;
|
|
56
|
+
var _b;
|
|
57
|
+
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();
|
|
58
|
+
}
|
|
59
|
+
/** One invited is in: its invitation goes. */
|
|
60
|
+
async admitted(record, id) {
|
|
61
|
+
var _a;
|
|
62
|
+
if (((_a = record.invited) === null || _a === void 0 ? void 0 : _a[id]) !== undefined) {
|
|
63
|
+
const { [id]: _gone, ...rest } = record.invited;
|
|
64
|
+
record.invited = rest;
|
|
65
|
+
this.rooms.save();
|
|
66
|
+
}
|
|
67
|
+
const mine = await this.rooms.friendRoom().catch(() => undefined);
|
|
68
|
+
if (mine) {
|
|
69
|
+
await this.rooms.setState(mine.id, await RelayGroupInvites.keyOf(record.id, id), null).catch(() => undefined);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** The group chats friends invited the user into, not joined nor turned down, the latest first. */
|
|
73
|
+
async list() {
|
|
74
|
+
return (await this.read()).map(({ link: _link, ...invite }) => invite);
|
|
75
|
+
}
|
|
76
|
+
/** The user's answer to an invitation: joined, or no longer listed. */
|
|
77
|
+
async answer(roomId, accept) {
|
|
78
|
+
const invite = (await this.read()).find(each => each.room === roomId);
|
|
79
|
+
if (!invite) {
|
|
80
|
+
throw new Error('No friend invites you there now.');
|
|
81
|
+
}
|
|
82
|
+
if (accept) {
|
|
83
|
+
await this.rooms.join(invite.link);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const book = this.book();
|
|
87
|
+
const now = Date.now();
|
|
88
|
+
book.declined = Object.fromEntries([...Object.entries(book.declined).filter(([, until]) => until > now), [roomId, invite.expiresAt]]);
|
|
89
|
+
this.store.write('groups', book);
|
|
90
|
+
}
|
|
91
|
+
/** Whether a change of the state is of an invitation the user may have: the page reads them again. */
|
|
92
|
+
static heard(record, key) {
|
|
93
|
+
return !!record && !record.own && record.kind === relay_friends_1.RELAY_FRIENDS_KIND && key.startsWith(exports.RELAY_GROUP_INVITE);
|
|
94
|
+
}
|
|
95
|
+
/** The key of an invitation: neither the room nor the friend can be read from it by the other friends. */
|
|
96
|
+
static async keyOf(room, friend) {
|
|
97
|
+
return exports.RELAY_GROUP_INVITE + (await relay_identity_1.RelayKeys.hash(`${room}\n${friend}`)).slice(0, 22);
|
|
98
|
+
}
|
|
99
|
+
book() {
|
|
100
|
+
const book = this.store.read('groups');
|
|
101
|
+
return { declined: (book === null || book === void 0 ? void 0 : book.declined) && typeof book.declined === 'object' ? book.declined : {} };
|
|
102
|
+
}
|
|
103
|
+
/** The invitations of the user's own that no longer work, taken out of the room of friends. */
|
|
104
|
+
async prune(room, now) {
|
|
105
|
+
var _a, _b;
|
|
106
|
+
const self = (_b = (_a = this.rooms
|
|
107
|
+
.list()
|
|
108
|
+
.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;
|
|
109
|
+
for (const [key, entry] of Object.entries(await this.rooms.getState(room, exports.RELAY_GROUP_INVITE))) {
|
|
110
|
+
const value = entry.value;
|
|
111
|
+
if (entry.by === self && value && Number(value.expiresAt) <= now) {
|
|
112
|
+
await this.rooms.setState(room, key, null).catch(() => undefined);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async read() {
|
|
117
|
+
const identity = await this.rooms.identity();
|
|
118
|
+
const all = this.rooms.list();
|
|
119
|
+
const inside = new Set(all.filter(room => room.status === 'member' || room.status === 'waiting').map(room => room.id));
|
|
120
|
+
const { declined } = this.book();
|
|
121
|
+
const now = Date.now();
|
|
122
|
+
const found = [];
|
|
123
|
+
for (const room of all) {
|
|
124
|
+
const record = this.rooms.record(room.id);
|
|
125
|
+
if (!record || record.own || record.kind !== relay_friends_1.RELAY_FRIENDS_KIND || room.status !== 'member' || !record.owner.box) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const owner = room.members.find(member => member.owner);
|
|
129
|
+
if (!owner || owner.id !== record.owner.id) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const key = await relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayIdentityUtil.agree(identity, record.owner.box, identity.id, INFO));
|
|
133
|
+
for (const entry of Object.values(await this.rooms.getState(room.id, exports.RELAY_GROUP_INVITE).catch(() => ({})))) {
|
|
134
|
+
const value = entry.value;
|
|
135
|
+
if (entry.by !== owner.member || !value || typeof value.d !== 'string' || Number(value.expiresAt) <= now) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const opened = await relay_identity_1.RelayKeys.open(key, value.d).then(text => JSON.parse(text), () => undefined);
|
|
139
|
+
if (!opened || typeof opened.link !== 'string') {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
let link;
|
|
143
|
+
try {
|
|
144
|
+
link = relay_hub_util_1.RelayHubUtil.parse(opened.link);
|
|
145
|
+
}
|
|
146
|
+
catch (_a) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
// Only a room of the friend's own, by a link it made.
|
|
150
|
+
if (link.owner !== owner.id || link.kind || inside.has(link.room) || declined[link.room] !== undefined || opened.expiresAt <= now) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
found.push({
|
|
154
|
+
room: link.room,
|
|
155
|
+
title: String(opened.title || link.title).slice(0, 80),
|
|
156
|
+
by: { id: owner.id, name: owner.name, ...(owner.look ? { look: owner.look } : {}) },
|
|
157
|
+
at: Number(opened.at) || now,
|
|
158
|
+
expiresAt: opened.expiresAt,
|
|
159
|
+
link: opened.link,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return found.sort((a, b) => b.at - a.at);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
exports.RelayGroupInvites = RelayGroupInvites;
|
|
167
|
+
//# sourceMappingURL=relay-group-invites.js.map
|
|
@@ -32,6 +32,8 @@ export interface RelayRoomRecord {
|
|
|
32
32
|
capacity?: number;
|
|
33
33
|
/** Of one asking: the invitation, till the relay has the ask. */
|
|
34
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>;
|
|
35
37
|
asked?: boolean;
|
|
36
38
|
createdAt: number;
|
|
37
39
|
joinedAt?: number;
|
|
@@ -168,7 +170,7 @@ export interface RelayHubsStore {
|
|
|
168
170
|
write(name: RelayHubsStoreName, value: unknown): void;
|
|
169
171
|
}
|
|
170
172
|
/** The identity, the rooms (with their keys) and the friends. */
|
|
171
|
-
export type RelayHubsStoreName = 'identity' | 'rooms' | 'friends';
|
|
173
|
+
export type RelayHubsStoreName = 'identity' | 'rooms' | 'friends' | 'groups' | 'sends';
|
|
172
174
|
export interface RelayHubsOptions {
|
|
173
175
|
store: RelayHubsStore;
|
|
174
176
|
/** The name of a new identity: that of the user on the system. */
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { RelayFriendSends, type RelayFriendShare } from './relay-friend-sends';
|
|
1
2
|
import { RelayFriends } from './relay-friends';
|
|
3
|
+
import { type RelayGroupInvite, RelayGroupInvites } from './relay-group-invites';
|
|
2
4
|
import { type RelayHubAskBody } from './relay-hub-util';
|
|
3
5
|
import { Live } from './relay-hubs-connection';
|
|
4
6
|
import { RelayHubsIdentity } from './relay-hubs-identity';
|
|
@@ -12,6 +14,8 @@ import { type RelayPublicIdentity } from './relay-identity';
|
|
|
12
14
|
*/
|
|
13
15
|
export declare class RelayHubs extends RelayHubsIdentity {
|
|
14
16
|
protected readonly book: RelayFriends;
|
|
17
|
+
protected readonly groups: RelayGroupInvites;
|
|
18
|
+
protected readonly sends: RelayFriendSends;
|
|
15
19
|
/** Connects to the rooms of rooms.json, and keeps connected. */
|
|
16
20
|
start(): void;
|
|
17
21
|
stop(): void;
|
|
@@ -90,6 +94,21 @@ export declare class RelayHubs extends RelayHubsIdentity {
|
|
|
90
94
|
answerFriend(id: string, accept: boolean): Promise<void>;
|
|
91
95
|
/** No longer friends, on both sides. */
|
|
92
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;
|
|
93
112
|
protected isFriends(live: Live): boolean;
|
|
94
113
|
protected friendIds(): Set<string>;
|
|
95
114
|
protected heardAsk(live: Live, ask: RelayHubAskBody): void;
|
package/lib/common/relay-hubs.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RelayHubs = void 0;
|
|
4
|
+
const relay_friend_sends_1 = require("./relay-friend-sends");
|
|
4
5
|
const relay_friends_1 = require("./relay-friends");
|
|
6
|
+
const relay_group_invites_1 = require("./relay-group-invites");
|
|
5
7
|
const relay_hub_util_1 = require("./relay-hub-util");
|
|
6
8
|
const relay_hubs_connection_1 = require("./relay-hubs-connection");
|
|
7
9
|
const relay_hubs_identity_1 = require("./relay-hubs-identity");
|
|
@@ -37,6 +39,27 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
|
|
|
37
39
|
setState: (room, key, value) => this.setState(room, key, value),
|
|
38
40
|
offer: (room, member, link) => this.sendBody(this.member(room), { friend: link }, member),
|
|
39
41
|
}, this.options.store, event => this.notify(event));
|
|
42
|
+
this.groups = new relay_group_invites_1.RelayGroupInvites({
|
|
43
|
+
identity: () => this.identity(),
|
|
44
|
+
list: () => this.list(),
|
|
45
|
+
record: room => { var _a; return (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.record; },
|
|
46
|
+
save: () => this.save(),
|
|
47
|
+
friends: () => this.friends(),
|
|
48
|
+
friendRoom: () => this.friendRoom(),
|
|
49
|
+
invite: (room, options) => this.invite(room, options),
|
|
50
|
+
join: link => this.join(link),
|
|
51
|
+
getState: (room, prefix) => this.getState(room, prefix),
|
|
52
|
+
setState: (room, key, value) => this.setState(room, key, value),
|
|
53
|
+
}, this.options.store);
|
|
54
|
+
this.sends = new relay_friend_sends_1.RelayFriendSends({
|
|
55
|
+
identity: () => this.identity(),
|
|
56
|
+
list: () => this.list(),
|
|
57
|
+
record: room => { var _a; return (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.record; },
|
|
58
|
+
friends: () => this.friends(),
|
|
59
|
+
friendRoom: () => this.friendRoom(),
|
|
60
|
+
getState: (room, prefix) => this.getState(room, prefix),
|
|
61
|
+
setState: (room, key, value) => this.setState(room, key, value),
|
|
62
|
+
}, this.options.store);
|
|
40
63
|
}
|
|
41
64
|
/** Connects to the rooms of rooms.json, and keeps connected. */
|
|
42
65
|
start() {
|
|
@@ -382,6 +405,31 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
|
|
|
382
405
|
removeFriend(id) {
|
|
383
406
|
return this.book.remove(id);
|
|
384
407
|
}
|
|
408
|
+
/** Invites friends into a group chat of the user's own: they join on their word, let in at once. How many were. */
|
|
409
|
+
inviteFriends(roomId, friends) {
|
|
410
|
+
return this.groups.invite(roomId, friends);
|
|
411
|
+
}
|
|
412
|
+
/** The group chats friends invited the user into. */
|
|
413
|
+
groupInvites() {
|
|
414
|
+
return this.groups.list();
|
|
415
|
+
}
|
|
416
|
+
/** The user's answer to an invitation into a group chat. */
|
|
417
|
+
answerGroupInvite(roomId, accept) {
|
|
418
|
+
return this.groups.answer(roomId, accept);
|
|
419
|
+
}
|
|
420
|
+
/** Sends a link to friends from the share sheet: how many it went to. */
|
|
421
|
+
sendToFriends(friends, item) {
|
|
422
|
+
return this.sends.send(friends, item);
|
|
423
|
+
}
|
|
424
|
+
/** What friends sent the user. */
|
|
425
|
+
friendShares() {
|
|
426
|
+
return this.sends.list();
|
|
427
|
+
}
|
|
428
|
+
/** The user opened one (`keep`) or let it go. */
|
|
429
|
+
answerFriendShare(id, keep) {
|
|
430
|
+
this.sends.answer(id, keep);
|
|
431
|
+
this.emit({ type: 'rooms' });
|
|
432
|
+
}
|
|
385
433
|
isFriends(live) {
|
|
386
434
|
return live.record.own && live.record.kind === relay_friends_1.RELAY_FRIENDS_KIND;
|
|
387
435
|
}
|
|
@@ -392,6 +440,10 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
|
|
|
392
440
|
if (this.isFriends(live)) {
|
|
393
441
|
this.book.asked(ask);
|
|
394
442
|
}
|
|
443
|
+
else if (this.groups.admits(live.record, ask)) {
|
|
444
|
+
// A friend the user invited: in at once, as the user chose it; asked as any other should that fail.
|
|
445
|
+
void this.answer(live.record.id, ask.member, true).then(() => this.groups.admitted(live.record, ask.id), () => super.heardAsk(live, ask));
|
|
446
|
+
}
|
|
395
447
|
else {
|
|
396
448
|
super.heardAsk(live, ask);
|
|
397
449
|
}
|
|
@@ -408,9 +460,15 @@ class RelayHubs extends relay_hubs_identity_1.RelayHubsIdentity {
|
|
|
408
460
|
}
|
|
409
461
|
/** What changes the friends: rooms coming and going, members, the links friends leave to join back. */
|
|
410
462
|
after(event) {
|
|
463
|
+
var _a;
|
|
411
464
|
if (!this.options.guest && (event.type === 'rooms' || (event.type === 'state' && event.key.startsWith('back:')))) {
|
|
412
465
|
this.book.changed();
|
|
413
466
|
}
|
|
467
|
+
// A friend invited the user into a group chat, or took it back: the pages read the rooms again.
|
|
468
|
+
const record = (_a = this.live.get(event.type === 'state' ? event.room : '')) === null || _a === void 0 ? void 0 : _a.record;
|
|
469
|
+
if (!this.options.guest && event.type === 'state' && (relay_group_invites_1.RelayGroupInvites.heard(record, event.key) || relay_friend_sends_1.RelayFriendSends.heard(record, event.key))) {
|
|
470
|
+
this.emit({ type: 'rooms' });
|
|
471
|
+
}
|
|
414
472
|
}
|
|
415
473
|
owned(roomId) {
|
|
416
474
|
const live = this.member(roomId);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A link of Ta handed to the Ta app by the system (docs/design/ta-links.md 5): `ta://open?link=<the link>`, which the
|
|
3
|
+
* pages of the relay offer as "Open in Ta" and the app takes as its own (a scheme of its own on the computer and the
|
|
4
|
+
* phone). The link inside opens as any link of Ta, asking first wherever it would do something. Pure, for the tests.
|
|
5
|
+
*/
|
|
6
|
+
export declare namespace RelayOpenInTa {
|
|
7
|
+
const SCHEME = "ta:";
|
|
8
|
+
function link(href: string): string;
|
|
9
|
+
/** The link of Ta inside, or undefined for what is not one (another `ta://`, a page of the web, one too long). */
|
|
10
|
+
function inner(text: string): string | undefined;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=relay-open-in-ta.d.ts.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayOpenInTa = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A link of Ta handed to the Ta app by the system (docs/design/ta-links.md 5): `ta://open?link=<the link>`, which the
|
|
6
|
+
* pages of the relay offer as "Open in Ta" and the app takes as its own (a scheme of its own on the computer and the
|
|
7
|
+
* phone). The link inside opens as any link of Ta, asking first wherever it would do something. Pure, for the tests.
|
|
8
|
+
*/
|
|
9
|
+
var RelayOpenInTa;
|
|
10
|
+
(function (RelayOpenInTa) {
|
|
11
|
+
RelayOpenInTa.SCHEME = 'ta:';
|
|
12
|
+
const MAX = 4096;
|
|
13
|
+
function link(href) {
|
|
14
|
+
return `ta://open?link=${encodeURIComponent(href)}`;
|
|
15
|
+
}
|
|
16
|
+
RelayOpenInTa.link = link;
|
|
17
|
+
/** The link of Ta inside, or undefined for what is not one (another `ta://`, a page of the web, one too long). */
|
|
18
|
+
function inner(text) {
|
|
19
|
+
const trimmed = text.trim();
|
|
20
|
+
if (trimmed.length > MAX * 3 || !/^ta:\/\/open\?/i.test(trimmed)) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
const found = /[?&]link=([^&#]+)/.exec(trimmed);
|
|
24
|
+
if (!found) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
const link = decodeURIComponent(found[1]).trim();
|
|
29
|
+
return /^https?:\/\/\S+$/i.test(link) && link.length <= MAX ? link : undefined;
|
|
30
|
+
}
|
|
31
|
+
catch (_a) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
RelayOpenInTa.inner = inner;
|
|
36
|
+
})(RelayOpenInTa || (exports.RelayOpenInTa = RelayOpenInTa = {}));
|
|
37
|
+
//# sourceMappingURL=relay-open-in-ta.js.map
|