remodel fileModel, start board methods of taskModel

This commit is contained in:
marlon-erler
2024-07-22 17:23:48 +02:00
parent 76737a390a
commit 3cc961a82b
4 changed files with 328 additions and 308 deletions
+148 -135
View File
@@ -533,92 +533,81 @@
fileModel; fileModel;
// paths // paths
getBasePath = () => { getBasePath = () => {
return this.fileModel.getModelDirectoryPath("taskModel"); return this.fileModel.getModelContainerPath("taskModel");
}; };
getLocationDirPath = () => { getBoardFilePath = (boardId) => {
return [...this.getBasePath(), subDirectories.locations]; return [...this.fileModel.getFilePath(boardId), boardId];
}; };
getTaskLocationPath = (fileId) => { getBoardContainerPath = () => {
return [...this.getLocationDirPath(), fileId];
};
getBoardDirPath = () => {
return [...this.getBasePath(), subDirectories.boards]; return [...this.getBasePath(), subDirectories.boards];
}; };
getBoardPath = (boardId) => { getBoardDirectoryPath = (boardId) => {
return [...this.getBoardDirPath(), boardId]; return [...this.getBoardContainerPath(), boardId];
}; };
getBoardInfoPath = (boardId) => { getTaskContainerPath = (boardId) => {
return [...this.getBoardPath(boardId), subDirectories.boardInfo]; return [...this.getBoardDirectoryPath(boardId), subDirectories.boardTasks];
};
getTaskDirPath = (boardId) => {
return [...this.getBoardPath(boardId), subDirectories.boardTasks];
}; };
getTaskPath = (boardId, fileId) => { getTaskPath = (boardId, fileId) => {
return [...this.getTaskDirPath(boardId), fileId]; return [...this.getTaskContainerPath(boardId), fileId];
}; };
getTaskContentPath = (boardId, fileId, contentName) => { getTaskContentPath = (boardId, fileId, versionId) => {
return [...this.getTaskPath(boardId, fileId), contentName]; return [...this.getTaskPath(boardId, fileId), versionId];
}; };
// handler // handler
handleFileContent = (file, fileContent) => { handleFileVersion = (fileVersion) => {
if (checkMatchesObjectStructure(fileContent, BoardInfoFileContentReference) == true) { if (checkMatchesObjectStructure(fileVersion, BoardInfoFileVersionReference) == true) {
} else if (checkMatchesObjectStructure(fileContent, TaskFileContentReference) == true) { this.handleBoard(fileVersion);
} else if (checkMatchesObjectStructure(fileVersion, TaskFileVersionReference) == true) {
this.handleTask(fileVersion);
} }
}; };
handleBoard = (boardInfoFileVersion) => {
console.log("board", boardInfoFileVersion);
};
handleTask = (taskFileVersion) => {
};
// boards // boards
createBoard = (name) => { createBoard = (name) => {
const file = FileModel.createFile(); const boardInfoFileVersion = _TaskModel.createBoardInfoFileVersion(v4_default(), name, "standard" /* Standard */);
const boardInfoFileContent = _TaskModel.createBoardInfoFileContent(name, "standard" /* Standard */); this.createOrUpdateBoard(boardInfoFileVersion);
this.fileModel.addOrUpdateFile(file, boardInfoFileContent);
}; };
listBoardIds = () => { listBoardIds = () => {
const boardDirPath = this.getBoardDirPath(); const boardContainerPath = this.getBoardContainerPath();
const boardNames = this.storageModel.list(boardDirPath); const boardNames = this.storageModel.list(boardContainerPath);
return boardNames; return boardNames;
}; };
getBoardInfo = (boardId) => { getBoardInfo = (fileId) => {
const boardInfoPath = this.getBoardInfoPath(boardId); const boardInfoFileVersionOrNull = this.fileModel.getLatestFileVersion(
const boardInfoFileContentOrNull = this.storageModel.readStringifiable( fileId,
boardInfoPath, BoardInfoFileVersionReference
BoardInfoFileContentReference
); );
return boardInfoFileContentOrNull; return boardInfoFileVersionOrNull;
};
createOrUpdateBoard = (boardInfoFileVersion) => {
this.fileModel.addFileVersion(boardInfoFileVersion);
const boardDirectoryPath = this.getBoardDirectoryPath(
boardInfoFileVersion.fileId
);
this.storageModel.write(boardDirectoryPath, "");
}; };
//tasks //tasks
createTask = (boardId, name) => { createTask = (boardId, name) => {
const file = FileModel.createFile(); const taskFileVersion = _TaskModel.createTaskFileVersion(
const taskFileContent = _TaskModel.createTaskFileContent( v4_default(),
boardId, boardId,
name name
); );
this.fileModel.addOrUpdateFile(file, taskFileContent); this.fileModel.addFileVersion(taskFileVersion);
}; };
listTaskIds = (boardName) => { listTaskIds = (boardName) => {
const getTaskDirPath = this.getTaskDirPath(boardName); const getTaskDirPath = this.getTaskContainerPath(boardName);
const fileIds = this.storageModel.list(getTaskDirPath); const fileIds = this.storageModel.list(getTaskDirPath);
return fileIds; return fileIds;
}; };
getTaskVersionNames = (boardName, fileId) => {
const fileDirectoryPath = this.getTaskPath(boardName, fileId);
const versionNames = this.storageModel.list(fileDirectoryPath);
return versionNames;
};
getTaskFileContent = (boardName, fileId, versionName) => {
const fileContentPath = this.getTaskContentPath(
boardName,
fileId,
versionName
);
const taskFileContentOrNull = this.storageModel.readStringifiable(
fileContentPath,
TaskFileContentReference
);
return taskFileContentOrNull;
};
moveTask = (taskFileId, oldBoardId, newBoardId) => { moveTask = (taskFileId, oldBoardId, newBoardId) => {
const sourcePath = this.getTaskPath(oldBoardId, taskFileId); const sourcePath = this.getTaskPath(oldBoardId, taskFileId);
const destinationPath = this.getTaskPath(newBoardId, taskFileId); const destinationPath = this.getTaskPath(newBoardId, taskFileId);
return this.storageModel.rename(sourcePath, destinationPath); this.storageModel.renameRecursively(sourcePath, destinationPath);
}; };
// init // init
constructor(storageModel2, chatModel, fileModel) { constructor(storageModel2, chatModel, fileModel) {
@@ -627,40 +616,46 @@
this.storageModel = storageModel2; this.storageModel = storageModel2;
} }
// utility // utility
static createBoardInfoFileContent = (name, color) => { static createBoardInfoFileVersion = (fileId, name, color) => {
const fileContent = FileModel.createFileContent("board"); const fileVersion = FileModel.createFileVersion(
fileId,
"board-info"
);
return { return {
...fileContent, ...fileVersion,
name, name,
color color
}; };
}; };
static createTaskFileContent = (name, boardId) => { static createTaskFileVersion = (fileId, name, boardId) => {
const fileContent = FileModel.createFileContent("task"); const fileVersion = FileModel.createFileVersion(
fileId,
"task"
);
return { return {
...fileContent, ...fileVersion,
name, name,
boardId boardId
}; };
}; };
}; };
var subDirectories = { var subDirectories = {
locations: "locations",
boards: "boards", boards: "boards",
boardInfo: "info",
boardTasks: "tasks" boardTasks: "tasks"
}; };
var BoardInfoFileContentReference = { var BoardInfoFileVersionReference = {
dataVersion: DATA_VERSION, dataVersion: DATA_VERSION,
id: "", fileId: "string",
fileVersionId: "",
creationDate: "", creationDate: "",
type: "board", type: "board-info",
name: "", name: "",
color: "" color: ""
}; };
var TaskFileContentReference = { var TaskFileVersionReference = {
dataVersion: DATA_VERSION, dataVersion: DATA_VERSION,
id: "", fileId: "string",
fileVersionId: "",
creationDate: "", creationDate: "",
type: "task", type: "task",
name: "", name: "",
@@ -679,107 +674,125 @@
filePaths.chat.files(this.chatModel.id) filePaths.chat.files(this.chatModel.id)
); );
}; };
getModelDirectoryPath = (modelName) => { getFileContainerPath = () => {
return [...this.getBasePath(), modelName]; return [...this.getBasePath(), subDirectories2.data];
};
getModelContainerPath = (modelName) => {
return [...this.getBasePath(), subDirectories2.model, modelName];
}; };
getFilePath = (fileId) => { getFilePath = (fileId) => {
return [...this.getBasePath(), fileId]; return [...this.getFileContainerPath(), fileId];
}; };
getFileContentPath = (file, fileContentName) => { getFileVersionPath = (fileId, fileVersionId) => {
const filePath = this.getFilePath(file.id); const filePath = this.getFilePath(fileId);
return [...filePath, fileContentName]; return [...filePath, fileVersionId];
}; };
// handler // handler
handleStringifiedFile = (stringifiedFile) => { handleStringifiedFileVersion = (stringifiedFileVersion) => {
const file = parseValidObject(stringifiedFile, FileReference); const fileVersion = parseValidObject(
if (file == null) return; stringifiedFileVersion,
this.handleFileAndClearContents(file); FileVersionReference
);
if (fileVersion == null) return;
this.handleFileVersion(fileVersion);
}; };
handleFileAndClearContents = (file) => { handleFileVersion = (fileVersion) => {
for (const fileContent of file.contentVersions) { const didStore = this.storeFileVersion(fileVersion);
this.storeFileContent(file, fileContent); if (didStore == false) return;
this.taskModel.handleFileContent(file, fileContent); this.taskModel.handleFileVersion(fileVersion);
}
file.contentVersions = [];
}; };
// methods // methods
addOrUpdateFile = (file, fileContent) => { addFileVersion = (fileVersion) => {
file.contentVersions.push(fileContent); this.handleFileVersion(fileVersion);
this.chatModel.sendMessage("", file); this.chatModel.sendMessage("", fileVersion);
this.handleFileAndClearContents(file);
}; };
// storage // storage
storeFileContent = (file, fileContent) => { storeFileVersion = (fileVersion) => {
const fileContentName = _FileModel.getFileContentName(fileContent); const fileVersionPath = this.getFileVersionPath(
const fileContentPath = this.getFileContentPath( fileVersion.fileId,
file, fileVersion.fileVersionId
fileContentName
); );
const existingFileContent = this.storageModel.read(fileContentPath); const existingFileVersion = this.storageModel.read(fileVersionPath);
if (existingFileContent != null) return; if (existingFileVersion != null) return false;
const stringifiedContent = stringify(fileContent); const stringifiedContent = stringify(fileVersion);
this.storageModel.write(fileContentPath, stringifiedContent); this.storageModel.write(fileVersionPath, stringifiedContent);
return true;
}; };
listFileIds = () => { listFileIds = () => {
return this.storageModel.list(this.getBasePath()); return this.storageModel.list(this.getBasePath());
}; };
getFile = (fileId) => { listFileVersionIds = (fileId) => {
const filePath = this.getFilePath(fileId); const filePath = this.getFilePath(fileId);
const fileOrNull = this.storageModel.readStringifiable(
filePath,
FileReference
);
return fileOrNull;
};
listFileContents = (file) => {
const filePath = this.getFilePath(file.id);
return this.storageModel.list(filePath); return this.storageModel.list(filePath);
}; };
getLatestFileContentName = (fileContentNames) => { selectLatestFileVersionId = (fileVersionIds) => {
return fileContentNames[fileContentNames.length - 1]; return fileVersionIds[fileVersionIds.length - 1];
}; };
getFileContent = (file, fileContentName) => { getFileVersion = (fileId, fileVersionName, reference) => {
const filePath = this.getFileContentPath(file, fileContentName); const filePath = this.getFileVersionPath(fileId, fileVersionName);
const fileContentOrNull = this.storageModel.readStringifiable(filePath, FileContentReference2); const fileVersionOrNull = this.storageModel.readStringifiable(
return fileContentOrNull; filePath,
reference
);
return fileVersionOrNull;
};
getLatestFileVersion = (fileId, reference) => {
const fileVersionsIds = this.listFileVersionIds(fileId);
const latestFileVersionId = this.selectLatestFileVersionId(fileVersionsIds);
if (latestFileVersionId == void 0) return null;
const fileVersion = this.getFileVersion(
fileId,
latestFileVersionId,
reference
);
return fileVersion;
}; };
// init // init
constructor(chatModel, storageModel2) { constructor(chatModel, storageModel2) {
this.chatModel = chatModel; this.chatModel = chatModel;
this.storageModel = storageModel2; this.storageModel = storageModel2;
this.taskModel = new TaskModel(this.storageModel, chatModel, this); this.taskModel = new TaskModel(this.storageModel, chatModel, this);
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",
"coral" /* Coral */
);
this.taskModel.createOrUpdateBoard(newInfo);
} }
// utility // utility
static getFileContentName = (fileContent) => { static generateFileVersionId = (creationDate) => {
return fileContent.creationDate + fileContent.id; return creationDate + v4_default();
}; };
static createFile = () => { static createFileVersion = (fileId, type) => {
const creationDate = createTimestamp();
const fileVersionId = _FileModel.generateFileVersionId(creationDate);
return { return {
dataVersion: DATA_VERSION, dataVersion: DATA_VERSION,
id: v4_default(), fileId,
contentVersions: [] fileVersionId,
}; creationDate,
};
static createFileContent = (type) => {
return {
dataVersion: DATA_VERSION,
id: v4_default(),
creationDate: createTimestamp(),
type type
}; };
}; };
}; };
var FileContentReference2 = { var subDirectories2 = {
data: "data",
model: "model",
taskModel: "tasks"
};
var FileVersionReference = {
dataVersion: DATA_VERSION, dataVersion: DATA_VERSION,
id: "", fileId: "",
fileVersionId: "",
creationDate: "", creationDate: "",
type: "" type: ""
}; };
var FileReference = {
dataVersion: DATA_VERSION,
id: "",
contentVersions: [FileContentReference2]
};
// src/Model/Utility/crypto.ts // src/Model/Utility/crypto.ts
var IV_SIZE = 12; var IV_SIZE = 12;
@@ -909,7 +922,7 @@
this.storeColor(); this.storeColor();
}; };
// messaging // messaging
sendMessage = async (body, file) => { sendMessage = async (body, fileVersion) => {
const senderName = this.settingsModel.username; const senderName = this.settingsModel.username;
if (senderName == "") return false; if (senderName == "") return false;
const allChannels = [this.info.primaryChannel]; const allChannels = [this.info.primaryChannel];
@@ -922,7 +935,7 @@
senderName, senderName,
this.info.encryptionKey, this.info.encryptionKey,
body, body,
file fileVersion
); );
this.addMessage(chatMessage); this.addMessage(chatMessage);
this.connectionModel.sendMessageOrStore(chatMessage); this.connectionModel.sendMessageOrStore(chatMessage);
@@ -973,7 +986,7 @@
this.storageModel.writeStringifiable(messagePath, chatMessage); this.storageModel.writeStringifiable(messagePath, chatMessage);
this.chatMessageHandler(chatMessage); this.chatMessageHandler(chatMessage);
} }
this.fileModel.handleStringifiedFile(chatMessage.stringifiedFile); this.fileModel.handleStringifiedFileVersion(chatMessage.stringifiedFile);
}; };
delete = () => { delete = () => {
this.chatListModel.untrackChat(this); this.chatListModel.untrackChat(this);
@@ -1040,7 +1053,7 @@
hasUnreadMessages: false hasUnreadMessages: false
}; };
}; };
static createChatMessage = async (channel, sender, encryptionKey, body, file) => { static createChatMessage = async (channel, sender, encryptionKey, body, fileVersion) => {
const chatMessage = { const chatMessage = {
dataVersion: DATA_VERSION, dataVersion: DATA_VERSION,
id: v4_default(), id: v4_default(),
@@ -1051,8 +1064,8 @@
status: "outbox" /* Outbox */, status: "outbox" /* Outbox */,
stringifiedFile: "" stringifiedFile: ""
}; };
if (file != void 0) { if (fileVersion != void 0) {
const stringifiedFile = stringify(file); const stringifiedFile = stringify(fileVersion);
chatMessage.stringifiedFile = stringifiedFile; chatMessage.stringifiedFile = stringifiedFile;
} }
if (encryptionKey != "") { if (encryptionKey != "") {
+10 -7
View File
@@ -1,7 +1,7 @@
// this file is responsible for managing chats. // this file is responsible for managing chats.
import { DATA_VERSION, ValidObject } from "../Utility/typeSafety"; import { DATA_VERSION, ValidObject } from "../Utility/typeSafety";
import FileModel, { File } from "../Files/fileModel"; import FileModel, { FileVersion } from "../Files/fileModel";
import StorageModel, { filePaths } from "../Global/storageModel"; import StorageModel, { filePaths } from "../Global/storageModel";
import { import {
createTimestamp, createTimestamp,
@@ -83,7 +83,10 @@ export default class ChatModel {
}; };
// messaging // messaging
sendMessage = async (body: string, file?: File): Promise<boolean> => { sendMessage = async (
body: string,
fileVersion: FileVersion<string>
): Promise<boolean> => {
const senderName = this.settingsModel.username; const senderName = this.settingsModel.username;
if (senderName == "") return false; if (senderName == "") return false;
@@ -99,7 +102,7 @@ export default class ChatModel {
senderName, senderName,
this.info.encryptionKey, this.info.encryptionKey,
body, body,
file fileVersion
); );
this.addMessage(chatMessage); this.addMessage(chatMessage);
@@ -164,7 +167,7 @@ export default class ChatModel {
} }
// file // file
this.fileModel.handleStringifiedFile(chatMessage.stringifiedFile); this.fileModel.handleStringifiedFileVersion(chatMessage.stringifiedFile);
}; };
delete = () => { delete = () => {
@@ -260,7 +263,7 @@ export default class ChatModel {
sender: string, sender: string,
encryptionKey: string, encryptionKey: string,
body: string, body: string,
file?: File fileVersion: FileVersion<string>
): Promise<ChatMessage> => { ): Promise<ChatMessage> => {
const chatMessage: ChatMessage = { const chatMessage: ChatMessage = {
dataVersion: DATA_VERSION, dataVersion: DATA_VERSION,
@@ -275,8 +278,8 @@ export default class ChatModel {
status: ChatMessageStatus.Outbox, status: ChatMessageStatus.Outbox,
stringifiedFile: "", stringifiedFile: "",
}; };
if (file != undefined) { if (fileVersion != undefined) {
const stringifiedFile = stringify(file); const stringifiedFile = stringify(fileVersion);
chatMessage.stringifiedFile = stringifiedFile; chatMessage.stringifiedFile = stringifiedFile;
} }
+92 -83
View File
@@ -9,6 +9,7 @@ import {
} from "../Utility/utility"; } from "../Utility/utility";
import ChatModel from "../Chat/chatModel"; import ChatModel from "../Chat/chatModel";
import { Color } from "../../ViewModel/colors";
import TaskModel from "./taskModel"; import TaskModel from "./taskModel";
import { v4 } from "uuid"; import { v4 } from "uuid";
@@ -26,92 +27,109 @@ export default class FileModel {
); );
}; };
getModelDirectoryPath = ( getFileContainerPath = (): string[] => {
modelName: keyof typeof modelDirectories return [...this.getBasePath(), subDirectories.data];
};
getModelContainerPath = (
modelName: keyof typeof subDirectories
): string[] => { ): string[] => {
return [...this.getBasePath(), modelName]; return [...this.getBasePath(), subDirectories.model, modelName];
}; };
getFilePath = (fileId: string): string[] => { getFilePath = (fileId: string): string[] => {
return [...this.getBasePath(), fileId]; return [...this.getFileContainerPath(), fileId];
}; };
getFileContentPath = (file: File, fileContentName: string): string[] => { getFileVersionPath = (fileId: string, fileVersionId: string): string[] => {
const filePath: string[] = this.getFilePath(file.id); const filePath: string[] = this.getFilePath(fileId);
return [...filePath, fileContentName]; return [...filePath, fileVersionId];
}; };
// handler // handler
handleStringifiedFile = (stringifiedFile: string): void => { handleStringifiedFileVersion = (stringifiedFileVersion: string): void => {
const file: File | null = parseValidObject(stringifiedFile, FileReference); const fileVersion: FileVersion<string> | null = parseValidObject(
if (file == null) return; stringifiedFileVersion,
FileVersionReference
);
if (fileVersion == null) return;
this.handleFileAndClearContents(file); this.handleFileVersion(fileVersion);
}; };
handleFileAndClearContents = (file: File): void => { handleFileVersion = (fileVersion: FileVersion<string>): void => {
for (const fileContent of file.contentVersions) { const didStore: boolean = this.storeFileVersion(fileVersion);
this.storeFileContent(file, fileContent); if (didStore == false) return;
this.taskModel.handleFileContent(file, fileContent);
}
file.contentVersions = []; this.taskModel.handleFileVersion(fileVersion);
}; };
// methods // methods
addOrUpdateFile = (file: File, fileContent: FileContent<string>): void => { addFileVersion = (fileVersion: FileVersion<string>): void => {
file.contentVersions.push(fileContent); this.handleFileVersion(fileVersion);
this.chatModel.sendMessage("", file); this.chatModel.sendMessage("", fileVersion);
this.handleFileAndClearContents(file);
}; };
// storage // storage
storeFileContent = (file: File, fileContent: FileContent<string>): void => { storeFileVersion = (fileVersion: FileVersion<string>): boolean => {
const fileContentName: string = FileModel.getFileContentName(fileContent); const fileVersionPath: string[] = this.getFileVersionPath(
const fileContentPath: string[] = this.getFileContentPath( fileVersion.fileId,
file, fileVersion.fileVersionId
fileContentName
); );
// check if fileContent already exists // check if fileVersion already exists
const existingFileContent: string | null = const existingFileVersion: string | null =
this.storageModel.read(fileContentPath); this.storageModel.read(fileVersionPath);
if (existingFileContent != null) return; if (existingFileVersion != null) return false;
const stringifiedContent: string = stringify(fileContent); const stringifiedContent: string = stringify(fileVersion);
this.storageModel.write(fileContentPath, stringifiedContent); this.storageModel.write(fileVersionPath, stringifiedContent);
return true;
}; };
listFileIds = (): string[] => { listFileIds = (): string[] => {
return this.storageModel.list(this.getBasePath()); return this.storageModel.list(this.getBasePath());
}; };
getFile = (fileId: string): File | null => { listFileVersionIds = (fileId: string): string[] => {
const filePath: string[] = this.getFilePath(fileId); const filePath: string[] = this.getFilePath(fileId);
const fileOrNull: File | null = this.storageModel.readStringifiable(
filePath,
FileReference
);
return fileOrNull;
};
listFileContents = (file: File): string[] => {
const filePath: string[] = this.getFilePath(file.id);
return this.storageModel.list(filePath); return this.storageModel.list(filePath);
}; };
getLatestFileContentName = (fileContentNames: string[]): string | null => { selectLatestFileVersionId = (
return fileContentNames[fileContentNames.length - 1]; fileVersionIds: string[]
): string | undefined => {
return fileVersionIds[fileVersionIds.length - 1];
}; };
getFileContent = ( getFileVersion = <T extends FileVersion<string>>(
file: File, fileId: string,
fileContentName: string fileVersionName: string,
): FileContent<string> | null => { reference: T
const filePath: string[] = this.getFileContentPath(file, fileContentName); ): T | null => {
const fileContentOrNull: FileContent<string> | null = const filePath: string[] = this.getFileVersionPath(fileId, fileVersionName);
this.storageModel.readStringifiable(filePath, FileContentReference); const fileVersionOrNull: T | null = this.storageModel.readStringifiable(
return fileContentOrNull; 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;
}; };
// init // init
@@ -123,60 +141,51 @@ export default class FileModel {
} }
// utility // utility
static getFileContentName = (fileContent: FileContent<string>): string => { static generateFileVersionId = (creationDate): string => {
return fileContent.creationDate + fileContent.id; return creationDate + v4();
}; };
static createFile = (): File => { static createFileVersion = <T extends string>(
fileId: string,
type: T
): FileVersion<T> => {
const creationDate: string = createTimestamp();
const fileVersionId: string = FileModel.generateFileVersionId(creationDate);
return { return {
dataVersion: DATA_VERSION, dataVersion: DATA_VERSION,
id: v4(), fileId,
contentVersions: [], fileVersionId,
}; creationDate,
};
static createFileContent = <T extends string>(type: T): FileContent<T> => {
return {
dataVersion: DATA_VERSION,
id: v4(),
creationDate: createTimestamp(),
type, type,
}; };
}; };
} }
// paths // paths
export const modelDirectories = { export const subDirectories = {
data: "data",
model: "model",
taskModel: "tasks", taskModel: "tasks",
}; };
// types // types
export interface File extends ValidObject { export interface FileVersion<T extends string> extends ValidObject {
id: string; fileId: string;
contentVersions: FileContent<string>[]; fileVersionId: string;
}
export interface FileContent<T extends string> extends ValidObject {
id: string;
creationDate: string; creationDate: string;
type: T; type: T;
} }
// references // references
export const FileContentReference: FileContent<string> = { export const FileVersionReference: FileVersion<string> = {
dataVersion: DATA_VERSION, dataVersion: DATA_VERSION,
id: "", fileId: "",
fileVersionId: "",
creationDate: "", creationDate: "",
type: "", type: "",
}; };
export const FileReference: File = {
dataVersion: DATA_VERSION,
id: "",
contentVersions: [FileContentReference],
};
+78 -83
View File
@@ -2,18 +2,14 @@
import { import {
DATA_VERSION, DATA_VERSION,
ValidObject,
checkMatchesObjectStructure, checkMatchesObjectStructure,
} from "../Utility/typeSafety"; } from "../Utility/typeSafety";
import FileModel, { import FileModel, { FileVersion } from "./fileModel";
File,
FileContent,
FileContentReference,
} from "./fileModel";
import ChatModel from "../Chat/chatModel"; import ChatModel from "../Chat/chatModel";
import { Color } from "../../ViewModel/colors"; import { Color } from "../../ViewModel/colors";
import StorageModel from "../Global/storageModel"; import StorageModel from "../Global/storageModel";
import { v4 } from "uuid";
export default class TaskModel { export default class TaskModel {
storageModel: StorageModel; storageModel: StorageModel;
@@ -22,115 +18,106 @@ export default class TaskModel {
// paths // paths
getBasePath = (): string[] => { getBasePath = (): string[] => {
return this.fileModel.getModelDirectoryPath("taskModel"); return this.fileModel.getModelContainerPath("taskModel");
}; };
getBoardDirPath = (): string[] => { getBoardFilePath = (boardId: string): string[] => {
return [...this.fileModel.getFilePath(boardId), boardId];
};
getBoardContainerPath = (): string[] => {
return [...this.getBasePath(), subDirectories.boards]; return [...this.getBasePath(), subDirectories.boards];
}; };
getBoardPath = (boardId: string): string[] => { getBoardDirectoryPath = (boardId: string): string[] => {
return [...this.getBoardDirPath(), boardId]; return [...this.getBoardContainerPath(), boardId];
}; };
getBoardInfoPath = (boardId: string): string[] => { getTaskContainerPath = (boardId: string): string[] => {
return [...this.getBoardPath(boardId), subDirectories.boardInfo]; return [...this.getBoardDirectoryPath(boardId), subDirectories.boardTasks];
};
getTaskDirPath = (boardId: string): string[] => {
return [...this.getBoardPath(boardId), subDirectories.boardTasks];
}; };
getTaskPath = (boardId: string, fileId: string): string[] => { getTaskPath = (boardId: string, fileId: string): string[] => {
return [...this.getTaskDirPath(boardId), fileId]; return [...this.getTaskContainerPath(boardId), fileId];
}; };
getTaskContentPath = ( getTaskContentPath = (
boardId: string, boardId: string,
fileId: string, fileId: string,
contentName: string versionId: string
): string[] => { ): string[] => {
return [...this.getTaskPath(boardId, fileId), contentName]; return [...this.getTaskPath(boardId, fileId), versionId];
}; };
// handler // handler
handleFileContent = (file: File, fileContent: FileContent<string>): void => { handleFileVersion = (fileVersion: FileVersion<string>): void => {
if ( if (
checkMatchesObjectStructure(fileContent, BoardInfoFileContentReference) == checkMatchesObjectStructure(fileVersion, BoardInfoFileVersionReference) ==
true true
) { ) {
// todo this.handleBoard(fileVersion as BoardInfoVersion);
} else if ( } else if (
checkMatchesObjectStructure(fileContent, TaskFileContentReference) == true checkMatchesObjectStructure(fileVersion, TaskFileVersionReference) == true
) { ) {
// todo this.handleTask(fileVersion as TaskFileVersion);
} }
}; };
handleBoard = (boardInfoFileVersion: BoardInfoVersion) => {
console.log("board", boardInfoFileVersion);
};
handleTask = (taskFileVersion: TaskFileVersion) => {};
// boards // boards
createBoard = (name: string): void => { createBoard = (name: string): void => {
const file: File = FileModel.createFile(); const boardInfoFileVersion: BoardInfoVersion =
const boardInfoFileContent: BoardInfoFileContent = TaskModel.createBoardInfoFileVersion(v4(), name, Color.Standard);
TaskModel.createBoardInfoFileContent(name, Color.Standard); this.createOrUpdateBoard(boardInfoFileVersion);
this.fileModel.addOrUpdateFile(file, boardInfoFileContent);
}; };
listBoardIds = (): string[] => { listBoardIds = (): string[] => {
const boardDirPath: string[] = this.getBoardDirPath(); const boardContainerPath: string[] = this.getBoardContainerPath();
const boardNames: string[] = this.storageModel.list(boardDirPath); const boardNames: string[] = this.storageModel.list(boardContainerPath);
return boardNames; return boardNames;
}; };
getBoardInfo = (boardId: string): BoardInfoFileContent | null => { getBoardInfo = (fileId: string): BoardInfoVersion | null => {
const boardInfoPath: string[] = this.getBoardInfoPath(boardId); const boardInfoFileVersionOrNull: BoardInfoVersion | null =
const boardInfoFileContentOrNull: BoardInfoFileContent | null = this.fileModel.getLatestFileVersion(
this.storageModel.readStringifiable( fileId,
boardInfoPath, BoardInfoFileVersionReference
BoardInfoFileContentReference
); );
return boardInfoFileContentOrNull; return boardInfoFileVersionOrNull;
};
createOrUpdateBoard = (boardInfoFileVersion: BoardInfoVersion): void => {
// store info
this.fileModel.addFileVersion(boardInfoFileVersion);
// add to list
const boardDirectoryPath: string[] = this.getBoardDirectoryPath(
boardInfoFileVersion.fileId
);
this.storageModel.write(boardDirectoryPath, "");
}; };
//tasks //tasks
createTask = (boardId: string, name: string): void => { createTask = (boardId: string, name: string): void => {
const file: File = FileModel.createFile(); const taskFileVersion: TaskFileVersion = TaskModel.createTaskFileVersion(
const taskFileContent: TaskFileContent = TaskModel.createTaskFileContent( v4(),
boardId, boardId,
name name
); );
this.fileModel.addOrUpdateFile(file, taskFileContent); this.fileModel.addFileVersion(taskFileVersion);
}; };
listTaskIds = (boardName: string): string[] => { listTaskIds = (boardName: string): string[] => {
const getTaskDirPath: string[] = this.getTaskDirPath(boardName); const getTaskDirPath: string[] = this.getTaskContainerPath(boardName);
const fileIds: string[] = this.storageModel.list(getTaskDirPath); const fileIds: string[] = this.storageModel.list(getTaskDirPath);
return fileIds; return fileIds;
}; };
getTaskVersionNames = (boardName: string, fileId: string): string[] => {
const fileDirectoryPath: string[] = this.getTaskPath(boardName, fileId);
const versionNames: string[] = this.storageModel.list(fileDirectoryPath);
return versionNames;
};
getTaskFileContent = (
boardName: string,
fileId: string,
versionName: string
): TaskFileContent | null => {
const fileContentPath: string[] = this.getTaskContentPath(
boardName,
fileId,
versionName
);
const taskFileContentOrNull: TaskFileContent | null =
this.storageModel.readStringifiable(
fileContentPath,
TaskFileContentReference
);
return taskFileContentOrNull;
};
moveTask = ( moveTask = (
taskFileId: string, taskFileId: string,
oldBoardId: string, oldBoardId: string,
@@ -153,28 +140,34 @@ export default class TaskModel {
} }
// utility // utility
static createBoardInfoFileContent = ( static createBoardInfoFileVersion = (
fileId: string,
name: string, name: string,
color: Color color: Color
): BoardInfoFileContent => { ): BoardInfoVersion => {
const fileContent: FileContent<"board"> = const fileVersion: FileVersion<"board-info"> = FileModel.createFileVersion(
FileModel.createFileContent("board"); fileId,
"board-info"
);
return { return {
...fileContent, ...fileVersion,
name, name,
color, color,
}; };
}; };
static createTaskFileContent = ( static createTaskFileVersion = (
fileId: string,
name: string, name: string,
boardId: string boardId: string
): TaskFileContent => { ): TaskFileVersion => {
const fileContent: FileContent<"task"> = const fileVersion: FileVersion<"task"> = FileModel.createFileVersion(
FileModel.createFileContent("task"); fileId,
"task"
);
return { return {
...fileContent, ...fileVersion,
name, name,
boardId, boardId,
@@ -184,17 +177,16 @@ export default class TaskModel {
export const subDirectories = { export const subDirectories = {
boards: "boards", boards: "boards",
boardInfo: "info",
boardTasks: "tasks", boardTasks: "tasks",
}; };
// types // types
export interface BoardInfoFileContent extends FileContent<"board"> { export interface BoardInfoVersion extends FileVersion<"board-info"> {
name: string; name: string;
color: Color; color: Color;
} }
export interface TaskFileContent extends FileContent<"task"> { export interface TaskFileVersion extends FileVersion<"task"> {
name: string; name: string;
boardId: string; boardId: string;
@@ -209,21 +201,24 @@ export interface TaskFileContent extends FileContent<"task"> {
} }
// reference // reference
export const BoardInfoFileContentReference: BoardInfoFileContent = { export const BoardInfoFileVersionReference: BoardInfoVersion = {
dataVersion: DATA_VERSION, dataVersion: DATA_VERSION,
id: "", fileId: "string",
fileVersionId: "",
creationDate: "", creationDate: "",
type: "board",
type: "board-info",
name: "", name: "",
color: "" as Color, color: "" as Color,
}; };
export const TaskFileContentReference: TaskFileContent = { export const TaskFileVersionReference: TaskFileVersion = {
dataVersion: DATA_VERSION, dataVersion: DATA_VERSION,
id: "", fileId: "string",
fileVersionId: "",
creationDate: "", creationDate: "",
type: "task", type: "task",