@wtfalch/chat 0.5.1 → 0.7.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.
@@ -14,10 +14,10 @@ export type NewDirectMessageProps = {
14
14
  * (`apps/agora/DESIGN.md`'s `Board`): an enamel card under an ink name-plate.
15
15
  * Nothing floats, so the Flat Map Rule holds.
16
16
  *
17
- * Who is offered comes from `client.people()`, which is the company space's
18
- * own membership. A guest is never in that space, so a guest can never be
19
- * on this list -- the Synapse gate would refuse the invitation anyway
20
- * (chat plan section 5), and an offer that ends in a refusal is not an
21
- * offer.
17
+ * Who is offered comes from `client.people()`: the company space's own
18
+ * membership, plus any guest sharing a room with the signed-in person
19
+ * (docs/guests.md). A guest outside a shared room is never offered -- the
20
+ * Synapse gate would refuse the invitation anyway (chat plan section 5),
21
+ * and an offer that ends in a refusal is not an offer.
22
22
  */
23
23
  export declare function NewDirectMessage({ client, onStarted, onCancel }: NewDirectMessageProps): import("react").JSX.Element;
@@ -9,11 +9,11 @@ import { useCallback, useEffect, useState } from 'react';
9
9
  * (`apps/agora/DESIGN.md`'s `Board`): an enamel card under an ink name-plate.
10
10
  * Nothing floats, so the Flat Map Rule holds.
11
11
  *
12
- * Who is offered comes from `client.people()`, which is the company space's
13
- * own membership. A guest is never in that space, so a guest can never be
14
- * on this list -- the Synapse gate would refuse the invitation anyway
15
- * (chat plan section 5), and an offer that ends in a refusal is not an
16
- * offer.
12
+ * Who is offered comes from `client.people()`: the company space's own
13
+ * membership, plus any guest sharing a room with the signed-in person
14
+ * (docs/guests.md). A guest outside a shared room is never offered -- the
15
+ * Synapse gate would refuse the invitation anyway (chat plan section 5),
16
+ * and an offer that ends in a refusal is not an offer.
17
17
  */
18
18
  export function NewDirectMessage({ client, onStarted, onCancel }) {
19
19
  const [people, setPeople] = useState(() => client.people());
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { Button, Icon } from '@wtfalch/design';
3
+ import { useEffect, useRef, useState } from 'react';
3
4
  import { Composer } from "./Composer.js";
4
5
  import { Timeline } from "./Timeline.js";
5
6
  /**
@@ -13,5 +14,47 @@ import { Timeline } from "./Timeline.js";
13
14
  * panel. One word, one action, no second control that does the same thing.
14
15
  */
15
16
  export function ThreadPanel({ client, roomId, threadId, onClose, onGoToRoot, now, sendOnEnter, }) {
16
- return (_jsxs("aside", { className: "chat-thread", "aria-label": "Thread", children: [_jsxs("header", { className: "chat-thread-bar", children: [_jsxs(Button, { kind: "ghost", size: "sm", onPress: onClose, children: [_jsx(Icon, { name: "back", size: 16 }), "Back"] }), _jsx("span", { className: "chat-thread-heading", children: "Thread" }), _jsx(Button, { kind: "ghost", size: "sm", onPress: onGoToRoot, children: "Go to message" })] }), _jsx(Timeline, { client: client, roomId: roomId, threadId: threadId, now: now }, threadId), _jsx(Composer, { client: client, roomId: roomId, threadId: threadId, sendOnEnter: sendOnEnter }, `composer-${threadId}`)] }));
17
+ // `null` until the first read answers, and again after a read fails.
18
+ const [following, setFollowing] = useState(null);
19
+ // Bumped by every click and every follow the person's own reply makes:
20
+ // a read that started before one of them answers stale, and is dropped.
21
+ const version = useRef(0);
22
+ const read = () => {
23
+ const started = version.current;
24
+ client
25
+ .threadFollowing(roomId, threadId)
26
+ .then((value) => {
27
+ if (version.current === started)
28
+ setFollowing(value);
29
+ })
30
+ .catch(() => { });
31
+ };
32
+ // Read on open, and listen for the follow the person's own reply makes.
33
+ // Nothing else re-reads: someone else's reply never changes it.
34
+ // biome-ignore lint/correctness/useExhaustiveDependencies: `client` is stable for a session.
35
+ useEffect(() => {
36
+ version.current += 1;
37
+ setFollowing(null);
38
+ read();
39
+ const stop = client.onThreadFollowChanged(roomId, threadId, (value) => {
40
+ version.current += 1;
41
+ setFollowing(value);
42
+ });
43
+ return () => {
44
+ version.current += 1;
45
+ stop();
46
+ };
47
+ }, [roomId, threadId]);
48
+ const toggleFollow = () => {
49
+ // The state is not known yet, or the read failed: read it again.
50
+ if (following === null) {
51
+ read();
52
+ return;
53
+ }
54
+ version.current += 1;
55
+ const next = !following;
56
+ setFollowing(next);
57
+ (next ? client.followThread(roomId, threadId) : client.unfollowThread(roomId, threadId)).catch(() => setFollowing(!next));
58
+ };
59
+ return (_jsxs("aside", { className: "chat-thread", "aria-label": "Thread", children: [_jsxs("header", { className: "chat-thread-bar", children: [_jsxs(Button, { kind: "ghost", size: "sm", onPress: onClose, children: [_jsx(Icon, { name: "back", size: 16 }), "Back"] }), _jsx("span", { className: "chat-thread-heading", children: "Thread" }), _jsx(Button, { kind: "ghost", size: "sm", onPress: toggleFollow, children: following ? 'Unfollow' : 'Follow' }), _jsx(Button, { kind: "ghost", size: "sm", onPress: onGoToRoot, children: "Go to message" })] }), _jsx(Timeline, { client: client, roomId: roomId, threadId: threadId, now: now }, threadId), _jsx(Composer, { client: client, roomId: roomId, threadId: threadId, sendOnEnter: sendOnEnter }, `composer-${threadId}`)] }));
17
60
  }
@@ -1,16 +1,54 @@
1
1
  import { IndexedDBStore } from 'matrix-js-sdk';
2
- /** The `dbName` this package hands `IndexedDBStore` for a given
3
- * `storeName`. Its own function so a test can assert on it directly,
2
+ /** The `dbName` this package hands `IndexedDBStore` for a workspace,
3
+ * authenticated homeserver and user. Its own function so a test can assert on it directly,
4
4
  * without starting a client. matrix-js-sdk prefixes this again with
5
5
  * `matrix-js-sdk:` for the real IndexedDB database name
6
6
  * (`indexeddb-local-backend.ts`) -- an internal of its own, not this
7
7
  * package's to name. */
8
- export declare function storeDbName(storeName: string): string;
8
+ export declare function storeDbName(storeName: string, homeserver: string, userId: string): string;
9
+ /** The `cryptoDatabasePrefix` this package hands `initRustCrypto()`, bound
10
+ * to the exchanged identity the same tuple `storeDbName` is -- its own
11
+ * function for the same reason: a test can assert on it without starting
12
+ * a client. Without this, matrix-js-sdk's own default
13
+ * (`RUST_SDK_STORE_PREFIX`, a fixed constant) names the same IndexedDB
14
+ * database for every client in the browser, defeating the one thing
15
+ * `storeName` exists for -- two companies, or two accounts under one
16
+ * `storeName`, open at once would either fail to start (the Rust store
17
+ * refusing a user/device id it does not recognise) or, for the very first
18
+ * account on a fresh store, succeed and then reject the next account that
19
+ * tries (chat#71 review). Binding to `userId` as well as `storeName`, not
20
+ * just the latter, is what turns a stale account's crypto store into an
21
+ * unrelated empty one rather than a rejected mismatch. matrix-js-sdk
22
+ * suffixes this again with `::matrix-sdk-crypto` and
23
+ * `::matrix-sdk-crypto-meta` for the real database names -- internals of
24
+ * its own, not this package's to name, same as `storeDbName`'s `matrix-
25
+ * js-sdk:` prefix.
26
+ *
27
+ * The user and device ids are part of the name because the Rust store
28
+ * binds to one account: the same `storeName` with a new device (a fresh
29
+ * sign-in) would make `initRustCrypto` fail with "the account in the store
30
+ * doesn't match the account in the constructor". Not `homeserver`, unlike
31
+ * `storeDbName`: `OlmMachine.initFromStore` validates `userId`/`deviceId`
32
+ * alone, never a homeserver, and a Matrix user id already carries its
33
+ * homeserver as the part after the colon. Stale crypto stores left behind
34
+ * by a device or account change are not purged the way
35
+ * `purgeStaleChatStores` purges the sync store -- a known gap, not a
36
+ * correctness bug: a mismatch now opens a fresh, empty crypto store rather
37
+ * than being rejected. */
38
+ export declare function cryptoStoreDbPrefix(storeName: string, userId: string, deviceId: string): string;
9
39
  /**
10
- * A local store named from `storeName`, so two companies open in the same
11
- * browser -- or two fakes in the same test process -- never share one.
40
+ * A local store bound to the exchanged identity as well as `storeName`, so
41
+ * companies, homeservers and accounts in the same browser never share one.
12
42
  *
13
43
  * `indexedDB` is read at call time rather than imported, so this works both
14
44
  * in a real browser and under vitest with `fake-indexeddb/auto` installed.
15
45
  */
16
- export declare function createChatStore(storeName: string): IndexedDBStore;
46
+ export declare function createChatStore(storeName: string, homeserver: string, userId: string): IndexedDBStore;
47
+ /**
48
+ * Deletes this workspace's databases that do not belong to the exchanged
49
+ * identity: the legacy unbound one (`wtfalch-messaging-<storeName>`, which
50
+ * holds the previous account's joined history) and the `v2` ones for other
51
+ * homeservers or users. Other workspaces' databases are left alone.
52
+ * Best effort: a failure here must not stop the client from starting.
53
+ */
54
+ export declare function purgeStaleChatStores(storeName: string, homeserver: string, userId: string): Promise<void>;
@@ -1,25 +1,105 @@
1
1
  import { IndexedDBStore } from 'matrix-js-sdk';
2
- /** The `dbName` this package hands `IndexedDBStore` for a given
3
- * `storeName`. Its own function so a test can assert on it directly,
2
+ /** The `dbName` this package hands `IndexedDBStore` for a workspace,
3
+ * authenticated homeserver and user. Its own function so a test can assert on it directly,
4
4
  * without starting a client. matrix-js-sdk prefixes this again with
5
5
  * `matrix-js-sdk:` for the real IndexedDB database name
6
6
  * (`indexeddb-local-backend.ts`) -- an internal of its own, not this
7
7
  * package's to name. */
8
- export function storeDbName(storeName) {
9
- return `wtfalch-messaging-${storeName}`;
8
+ export function storeDbName(storeName, homeserver, userId) {
9
+ // A tuple prevents delimiter collisions. The versioned prefix leaves legacy, unbound
10
+ // caches behind: their contents cannot be attributed safely to the current account.
11
+ return `wtfalch-messaging-v2-${JSON.stringify([storeName, homeserver, userId])}`;
12
+ }
13
+ /** The `cryptoDatabasePrefix` this package hands `initRustCrypto()`, bound
14
+ * to the exchanged identity the same tuple `storeDbName` is -- its own
15
+ * function for the same reason: a test can assert on it without starting
16
+ * a client. Without this, matrix-js-sdk's own default
17
+ * (`RUST_SDK_STORE_PREFIX`, a fixed constant) names the same IndexedDB
18
+ * database for every client in the browser, defeating the one thing
19
+ * `storeName` exists for -- two companies, or two accounts under one
20
+ * `storeName`, open at once would either fail to start (the Rust store
21
+ * refusing a user/device id it does not recognise) or, for the very first
22
+ * account on a fresh store, succeed and then reject the next account that
23
+ * tries (chat#71 review). Binding to `userId` as well as `storeName`, not
24
+ * just the latter, is what turns a stale account's crypto store into an
25
+ * unrelated empty one rather than a rejected mismatch. matrix-js-sdk
26
+ * suffixes this again with `::matrix-sdk-crypto` and
27
+ * `::matrix-sdk-crypto-meta` for the real database names -- internals of
28
+ * its own, not this package's to name, same as `storeDbName`'s `matrix-
29
+ * js-sdk:` prefix.
30
+ *
31
+ * The user and device ids are part of the name because the Rust store
32
+ * binds to one account: the same `storeName` with a new device (a fresh
33
+ * sign-in) would make `initRustCrypto` fail with "the account in the store
34
+ * doesn't match the account in the constructor". Not `homeserver`, unlike
35
+ * `storeDbName`: `OlmMachine.initFromStore` validates `userId`/`deviceId`
36
+ * alone, never a homeserver, and a Matrix user id already carries its
37
+ * homeserver as the part after the colon. Stale crypto stores left behind
38
+ * by a device or account change are not purged the way
39
+ * `purgeStaleChatStores` purges the sync store -- a known gap, not a
40
+ * correctness bug: a mismatch now opens a fresh, empty crypto store rather
41
+ * than being rejected. */
42
+ export function cryptoStoreDbPrefix(storeName, userId, deviceId) {
43
+ return `wtfalch-messaging-crypto-${storeName}-${userId}-${deviceId}`;
10
44
  }
11
45
  /**
12
- * A local store named from `storeName`, so two companies open in the same
13
- * browser -- or two fakes in the same test process -- never share one.
46
+ * A local store bound to the exchanged identity as well as `storeName`, so
47
+ * companies, homeservers and accounts in the same browser never share one.
14
48
  *
15
49
  * `indexedDB` is read at call time rather than imported, so this works both
16
50
  * in a real browser and under vitest with `fake-indexeddb/auto` installed.
17
51
  */
18
- export function createChatStore(storeName) {
19
- const dbName = storeDbName(storeName);
52
+ export function createChatStore(storeName, homeserver, userId) {
53
+ const dbName = storeDbName(storeName, homeserver, userId);
20
54
  return new IndexedDBStore({
21
55
  indexedDB: globalThis.indexedDB,
22
56
  dbName,
23
57
  localStorage: globalThis.localStorage,
24
58
  });
25
59
  }
60
+ const SDK_PREFIX = 'matrix-js-sdk:';
61
+ const LEGACY_PREFIX = 'wtfalch-messaging-';
62
+ const V2_PREFIX = 'wtfalch-messaging-v2-';
63
+ function deleteDatabase(indexedDB, name) {
64
+ return new Promise((resolve) => {
65
+ const request = indexedDB.deleteDatabase(name);
66
+ // A blocked delete (another tab holds it open) completes once that tab
67
+ // closes; waiting on it would stall start-up, so it is not awaited.
68
+ request.onsuccess = () => resolve();
69
+ request.onerror = () => resolve();
70
+ request.onblocked = () => resolve();
71
+ });
72
+ }
73
+ /**
74
+ * Deletes this workspace's databases that do not belong to the exchanged
75
+ * identity: the legacy unbound one (`wtfalch-messaging-<storeName>`, which
76
+ * holds the previous account's joined history) and the `v2` ones for other
77
+ * homeservers or users. Other workspaces' databases are left alone.
78
+ * Best effort: a failure here must not stop the client from starting.
79
+ */
80
+ export async function purgeStaleChatStores(storeName, homeserver, userId) {
81
+ const indexedDB = globalThis.indexedDB;
82
+ if (!indexedDB)
83
+ return;
84
+ const keep = SDK_PREFIX + storeDbName(storeName, homeserver, userId);
85
+ const legacy = SDK_PREFIX + LEGACY_PREFIX + storeName;
86
+ const stale = new Set([legacy]);
87
+ try {
88
+ for (const { name } of (await indexedDB.databases?.()) ?? []) {
89
+ if (!name || name === keep || !name.startsWith(SDK_PREFIX + V2_PREFIX))
90
+ continue;
91
+ try {
92
+ const tuple = JSON.parse(name.slice(SDK_PREFIX.length + V2_PREFIX.length));
93
+ if (Array.isArray(tuple) && tuple[0] === storeName)
94
+ stale.add(name);
95
+ }
96
+ catch {
97
+ // Not one of ours.
98
+ }
99
+ }
100
+ }
101
+ catch {
102
+ // Listing is unsupported or failed; the legacy name is still deleted.
103
+ }
104
+ await Promise.all([...stale].map((name) => deleteDatabase(indexedDB, name)));
105
+ }