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