|
1 |
| -//import { conat } from "@cocalc/backend/conat"; |
| 1 | +import { conat } from "@cocalc/backend/conat"; |
2 | 2 | import isCollaborator from "@cocalc/server/projects/is-collaborator";
|
| 3 | +import { patchesStreamName } from "@cocalc/conat/sync/synctable-stream"; |
| 4 | +import { type Patch, type HistoryInfo } from "@cocalc/conat/hub/api/sync"; |
| 5 | +import { client_db } from "@cocalc/util/db-schema/client-db"; |
3 | 6 |
|
4 | 7 | export async function history({
|
5 | 8 | account_id,
|
6 | 9 | project_id,
|
7 | 10 | path,
|
| 11 | + start_seq = 0, |
| 12 | + end_seq, |
8 | 13 | }: {
|
9 | 14 | account_id?: string;
|
10 | 15 | project_id: string;
|
11 | 16 | path: string;
|
12 |
| -}): Promise<any[]> { |
| 17 | + start_seq?: number; |
| 18 | + end_seq?: number; |
| 19 | +}): Promise<{ patches: Patch[]; info: HistoryInfo }> { |
13 | 20 | if (!account_id || !(await isCollaborator({ account_id, project_id }))) {
|
14 | 21 | throw Error("user must be collaborator on source project");
|
15 | 22 | }
|
16 | 23 |
|
17 |
| - // const client = conat(); |
18 |
| - // this will be much easier once the fs2 branch is merged |
19 |
| - throw Error(`not implemented yet -- can't get history of ${path} yet`); |
| 24 | + const client = conat(); |
| 25 | + const name = patchesStreamName({ project_id, path }); |
| 26 | + const astream = client.sync.astream({ |
| 27 | + name, |
| 28 | + project_id, |
| 29 | + noInventory: true, |
| 30 | + }); |
| 31 | + const patches: Patch[] = []; |
| 32 | + for await (const patch of await astream.getAll({ |
| 33 | + start_seq, |
| 34 | + end_seq, |
| 35 | + })) { |
| 36 | + patches.push(patch as any); |
| 37 | + } |
| 38 | + |
| 39 | + const akv = client.sync.akv({ |
| 40 | + name: `__dko__syncstrings:${client_db.sha1(project_id, path)}`, |
| 41 | + project_id, |
| 42 | + noInventory: true, |
| 43 | + }); |
| 44 | + const keys = await akv.keys(); |
| 45 | + const info: Partial<HistoryInfo> = {}; |
| 46 | + for (const key of keys) { |
| 47 | + if (key[0] != "[") continue; |
| 48 | + info[JSON.parse(key)[1]] = await akv.get(key); |
| 49 | + } |
| 50 | + |
| 51 | + return { patches, info: info as HistoryInfo }; |
20 | 52 | }
|
0 commit comments