Project Files
src / state.ts
import fs from "fs";
import path from "path";
export type AttachmentRecord = {
a: number;
preview?: string;
createdAt: string;
[key: string]: unknown;
};
export type VariantRecord = {
v: number;
preview: string;
createdAt: string;
[key: string]: unknown;
};
export type PictureRecord = {
p: number;
preview: string;
sourceUrl: string;
createdAt: string;
[key: string]: unknown;
};
export type ImageRecord = {
i: number;
preview: string;
createdAt: string;
[key: string]: unknown;
};
export type ChatMediaState = {
attachments: AttachmentRecord[];
variants: VariantRecord[];
pictures: PictureRecord[];
images: ImageRecord[];
counters: {
nextAttachmentA?: number;
nextVariantV?: number;
nextPictureP?: number;
nextImageI?: number;
};
};
export async function readState(chatWd: string): Promise<ChatMediaState> {
const p = path.join(chatWd, "chat_media_state.json");
try {
const raw = await fs.promises.readFile(p, "utf-8");
const json = JSON.parse(raw);
return {
attachments: Array.isArray(json?.attachments) ? json.attachments : [],
variants: Array.isArray(json?.variants) ? json.variants : [],
pictures: Array.isArray(json?.pictures) ? json.pictures : [],
images: Array.isArray(json?.images) ? json.images : [],
counters: json?.counters || {},
};
} catch {
return {
attachments: [],
variants: [],
pictures: [],
images: [],
counters: {},
};
}
}