2024-07-21 21:41:19 +02:00
|
|
|
// this file is responsible for managing files within chats.
|
|
|
|
|
|
2024-07-22 02:05:27 +02:00
|
|
|
import { DATA_VERSION, ValidObject } from "../Utility/typeSafety";
|
2024-07-22 03:01:04 +02:00
|
|
|
import StorageModel, { filePaths } from "../Global/storageModel";
|
2024-07-22 11:49:05 +02:00
|
|
|
import {
|
|
|
|
|
createTimestamp,
|
|
|
|
|
parseValidObject,
|
|
|
|
|
stringify,
|
|
|
|
|
} from "../Utility/utility";
|
2024-07-21 22:42:32 +02:00
|
|
|
|
2024-07-22 02:05:27 +02:00
|
|
|
import ChatModel from "../Chat/chatModel";
|
2024-07-22 17:23:48 +02:00
|
|
|
import { Color } from "../../ViewModel/colors";
|
2024-07-22 02:16:58 +02:00
|
|
|
import TaskModel from "./taskModel";
|
2024-07-22 00:37:36 +02:00
|
|
|
import { v4 } from "uuid";
|
2024-07-21 21:45:18 +02:00
|
|
|
|
|
|
|
|
export default class FileModel {
|
|
|
|
|
chatModel: ChatModel;
|
2024-07-21 23:32:20 +02:00
|
|
|
storageModel: StorageModel;
|
2024-07-21 21:45:18 +02:00
|
|
|
|
2024-07-22 02:16:58 +02:00
|
|
|
taskModel: TaskModel;
|
|
|
|
|
|
2024-07-22 03:01:04 +02:00
|
|
|
// paths
|
|
|
|
|
getBasePath = (): string[] => {
|
|
|
|
|
return StorageModel.getPath(
|
|
|
|
|
"chat",
|
|
|
|
|
filePaths.chat.files(this.chatModel.id)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
getFileContainerPath = (): string[] => {
|
|
|
|
|
return [...this.getBasePath(), subDirectories.data];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getModelContainerPath = (
|
|
|
|
|
modelName: keyof typeof subDirectories
|
2024-07-22 12:39:26 +02:00
|
|
|
): string[] => {
|
2024-07-22 17:23:48 +02:00
|
|
|
return [...this.getBasePath(), subDirectories.model, modelName];
|
2024-07-22 12:39:26 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 03:01:04 +02:00
|
|
|
getFilePath = (fileId: string): string[] => {
|
2024-07-22 17:23:48 +02:00
|
|
|
return [...this.getFileContainerPath(), fileId];
|
2024-07-22 03:01:04 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
getFileVersionPath = (fileId: string, fileVersionId: string): string[] => {
|
|
|
|
|
const filePath: string[] = this.getFilePath(fileId);
|
|
|
|
|
return [...filePath, fileVersionId];
|
2024-07-22 03:01:04 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-21 22:42:32 +02:00
|
|
|
// handler
|
2024-07-22 17:23:48 +02:00
|
|
|
handleStringifiedFileVersion = (stringifiedFileVersion: string): void => {
|
|
|
|
|
const fileVersion: FileVersion<string> | null = parseValidObject(
|
|
|
|
|
stringifiedFileVersion,
|
|
|
|
|
FileVersionReference
|
|
|
|
|
);
|
|
|
|
|
if (fileVersion == null) return;
|
2024-07-22 00:37:36 +02:00
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
this.handleFileVersion(fileVersion);
|
2024-07-21 22:42:32 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
handleFileVersion = (fileVersion: FileVersion<string>): void => {
|
|
|
|
|
const didStore: boolean = this.storeFileVersion(fileVersion);
|
|
|
|
|
if (didStore == false) return;
|
2024-07-22 14:36:03 +02:00
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
this.taskModel.handleFileVersion(fileVersion);
|
2024-07-21 23:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 11:49:05 +02:00
|
|
|
// methods
|
2024-07-22 17:23:48 +02:00
|
|
|
addFileVersion = (fileVersion: FileVersion<string>): void => {
|
|
|
|
|
this.handleFileVersion(fileVersion);
|
|
|
|
|
this.chatModel.sendMessage("", fileVersion);
|
2024-07-22 11:49:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// storage
|
2024-07-22 17:23:48 +02:00
|
|
|
storeFileVersion = (fileVersion: FileVersion<string>): boolean => {
|
|
|
|
|
const fileVersionPath: string[] = this.getFileVersionPath(
|
|
|
|
|
fileVersion.fileId,
|
|
|
|
|
fileVersion.fileVersionId
|
2024-07-21 23:32:20 +02:00
|
|
|
);
|
2024-07-22 00:37:36 +02:00
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
// check if fileVersion already exists
|
|
|
|
|
const existingFileVersion: string | null =
|
|
|
|
|
this.storageModel.read(fileVersionPath);
|
|
|
|
|
if (existingFileVersion != null) return false;
|
2024-07-22 00:37:36 +02:00
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
const stringifiedContent: string = stringify(fileVersion);
|
|
|
|
|
this.storageModel.write(fileVersionPath, stringifiedContent);
|
|
|
|
|
return true;
|
2024-07-21 23:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
listFileIds = (): string[] => {
|
2024-07-22 03:01:04 +02:00
|
|
|
return this.storageModel.list(this.getBasePath());
|
2024-07-21 23:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
listFileVersionIds = (fileId: string): string[] => {
|
2024-07-22 13:09:33 +02:00
|
|
|
const filePath: string[] = this.getFilePath(fileId);
|
2024-07-22 11:57:47 +02:00
|
|
|
return this.storageModel.list(filePath);
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
selectLatestFileVersionId = (
|
|
|
|
|
fileVersionIds: string[]
|
|
|
|
|
): string | undefined => {
|
|
|
|
|
return fileVersionIds[fileVersionIds.length - 1];
|
2024-07-21 23:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
getFileVersion = <T extends FileVersion<string>>(
|
|
|
|
|
fileId: string,
|
|
|
|
|
fileVersionName: string,
|
|
|
|
|
reference: T
|
|
|
|
|
): T | null => {
|
|
|
|
|
const filePath: string[] = this.getFileVersionPath(fileId, fileVersionName);
|
|
|
|
|
const fileVersionOrNull: T | null = this.storageModel.readStringifiable(
|
|
|
|
|
filePath,
|
|
|
|
|
reference
|
|
|
|
|
);
|
|
|
|
|
return fileVersionOrNull;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getLatestFileVersion = <T extends FileVersion<string>>(
|
|
|
|
|
fileId: string,
|
|
|
|
|
reference: T
|
|
|
|
|
): T | null => {
|
|
|
|
|
const fileVersionsIds: string[] = this.listFileVersionIds(fileId);
|
|
|
|
|
const latestFileVersionId: string | undefined =
|
|
|
|
|
this.selectLatestFileVersionId(fileVersionsIds);
|
|
|
|
|
if (latestFileVersionId == undefined) return null;
|
|
|
|
|
|
|
|
|
|
const fileVersion: T | null = this.getFileVersion(
|
|
|
|
|
fileId,
|
|
|
|
|
latestFileVersionId,
|
|
|
|
|
reference
|
|
|
|
|
);
|
|
|
|
|
return fileVersion;
|
2024-07-21 23:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-21 21:45:18 +02:00
|
|
|
// init
|
2024-07-21 23:32:20 +02:00
|
|
|
constructor(chatModel: ChatModel, storageModel: StorageModel) {
|
2024-07-21 21:45:18 +02:00
|
|
|
this.chatModel = chatModel;
|
2024-07-21 23:32:20 +02:00
|
|
|
this.storageModel = storageModel;
|
2024-07-22 02:16:58 +02:00
|
|
|
|
2024-07-22 13:09:33 +02:00
|
|
|
this.taskModel = new TaskModel(this.storageModel, chatModel, this);
|
2024-07-22 18:09:58 +02:00
|
|
|
|
|
|
|
|
// DEV
|
|
|
|
|
// this.taskModel.createBoard("Hello");
|
|
|
|
|
// const boards = this.taskModel.listBoardIds();
|
|
|
|
|
// console.log(boards);
|
|
|
|
|
|
|
|
|
|
// const boardId = boards[0];
|
|
|
|
|
|
|
|
|
|
// const firstBoardInfo = this.taskModel.getBoardInfo(boardId);
|
|
|
|
|
// console.log(firstBoardInfo);
|
|
|
|
|
|
|
|
|
|
// const newInfo = TaskModel.createBoardInfoFileVersion(
|
|
|
|
|
// boardId,
|
|
|
|
|
// "renamed",
|
|
|
|
|
// Color.Coral
|
|
|
|
|
// );
|
|
|
|
|
// this.taskModel.createOrUpdateBoard(newInfo);
|
|
|
|
|
|
|
|
|
|
// setTimeout(() => this.taskModel.deleteBoard(boardId), 7000);
|
2024-07-21 21:45:18 +02:00
|
|
|
}
|
2024-07-21 21:41:19 +02:00
|
|
|
|
2024-07-21 23:32:20 +02:00
|
|
|
// utility
|
2024-07-22 17:23:48 +02:00
|
|
|
static generateFileVersionId = (creationDate): string => {
|
|
|
|
|
return creationDate + v4();
|
2024-07-21 23:32:20 +02:00
|
|
|
};
|
2024-07-21 20:54:18 +02:00
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
static createFileVersion = <T extends string>(
|
|
|
|
|
fileId: string,
|
|
|
|
|
type: T
|
|
|
|
|
): FileVersion<T> => {
|
|
|
|
|
const creationDate: string = createTimestamp();
|
|
|
|
|
const fileVersionId: string = FileModel.generateFileVersionId(creationDate);
|
2024-07-22 11:49:05 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
dataVersion: DATA_VERSION,
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId,
|
|
|
|
|
fileVersionId,
|
|
|
|
|
creationDate,
|
2024-07-22 11:49:05 +02:00
|
|
|
type,
|
2024-07-22 01:22:59 +02:00
|
|
|
};
|
|
|
|
|
};
|
2024-07-21 20:54:18 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 12:39:26 +02:00
|
|
|
// paths
|
2024-07-22 17:23:48 +02:00
|
|
|
export const subDirectories = {
|
|
|
|
|
data: "data",
|
|
|
|
|
model: "model",
|
2024-07-22 12:39:26 +02:00
|
|
|
taskModel: "tasks",
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-21 23:32:20 +02:00
|
|
|
// types
|
2024-07-22 17:23:48 +02:00
|
|
|
export interface FileVersion<T extends string> extends ValidObject {
|
|
|
|
|
fileId: string;
|
|
|
|
|
fileVersionId: string;
|
2024-07-22 01:05:19 +02:00
|
|
|
creationDate: string;
|
2024-07-21 23:32:20 +02:00
|
|
|
|
2024-07-22 11:49:05 +02:00
|
|
|
type: T;
|
2024-07-21 20:54:18 +02:00
|
|
|
}
|
2024-07-22 01:22:59 +02:00
|
|
|
|
|
|
|
|
// references
|
2024-07-22 17:23:48 +02:00
|
|
|
export const FileVersionReference: FileVersion<string> = {
|
2024-07-22 01:22:59 +02:00
|
|
|
dataVersion: DATA_VERSION,
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId: "",
|
|
|
|
|
fileVersionId: "",
|
2024-07-22 01:22:59 +02:00
|
|
|
creationDate: "",
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
type: "",
|
2024-07-22 01:38:08 +02:00
|
|
|
};
|