Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync/editor/generic/legacy.ts
1450 views
1
/*
2
Support legacy TimeTravel history from before the switch to NATS.
3
*/
4
5
import { type Client } from "./types";
6
import { type DB } from "@cocalc/conat/hub/api/db";
7
8
export interface LegacyPatch {
9
time: Date;
10
patch: string;
11
user_id: number;
12
snapshot?: string;
13
sent?: Date; // when patch actually sent, which may be later than when made
14
prev?: Date; // timestamp of previous patch sent from this session
15
size: number; // size of the patch (by defn length of string representation)
16
}
17
18
export class LegacyHistory {
19
private db: DB;
20
private project_id: string;
21
private path: string;
22
23
constructor({
24
client,
25
project_id,
26
path,
27
}: {
28
client: Client;
29
project_id: string;
30
path: string;
31
}) {
32
// this is only available on the frontend browser, which is all that matters.
33
this.db = (client as any).conat_client?.hub.db as any;
34
this.project_id = project_id;
35
this.path = path;
36
}
37
38
// Returns '' if no legacy data. Returns sha1 hash of blob
39
// with the legacy data if there is legacy data.
40
private info?: { uuid: string; users?: string[] };
41
getInfo = async (): Promise<{ uuid: string; users?: string[] }> => {
42
if (this.info == null) {
43
this.info = await this.db.getLegacyTimeTravelInfo({
44
project_id: this.project_id,
45
path: this.path,
46
});
47
}
48
return this.info!;
49
};
50
51
getPatches = async (): Promise<{
52
patches: LegacyPatch[];
53
users: string[];
54
}> => {
55
const info = await this.getInfo();
56
if (!info.uuid || !info.users) {
57
return { patches: [], users: [] };
58
}
59
const s = await this.db.getLegacyTimeTravelPatches({
60
// long timeout, since response may be large or take a while to pull out of cold storage
61
timeout: 90000,
62
uuid: info.uuid,
63
});
64
let patches;
65
const t = JSON.parse(s);
66
if (t?.patches) {
67
patches = t.patches;
68
} else {
69
patches = t;
70
}
71
return { patches, users: info.users };
72
};
73
}
74
75