sync changes to chat info
This commit is contained in:
+138
-129
@@ -37,7 +37,7 @@ export default class ChatModel {
|
|||||||
info: ChatInfoFileContent; /* load function called in constructor */
|
info: ChatInfoFileContent; /* load function called in constructor */
|
||||||
|
|
||||||
get secondaryChannels(): string[] {
|
get secondaryChannels(): string[] {
|
||||||
return this.info.secondaryChannels.sort(localeCompare);
|
return this.info.secondaryChannels.sort(localeCompare);
|
||||||
}
|
}
|
||||||
|
|
||||||
get color(): Colors {
|
get color(): Colors {
|
||||||
@@ -47,53 +47,54 @@ export default class ChatModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handler managers
|
// handler managers
|
||||||
chatMessageHandlerManager: HandlerManager<ChatMessage> =
|
readonly chatMessageHandlerManager: HandlerManager<ChatMessage> =
|
||||||
new HandlerManager();
|
new HandlerManager();
|
||||||
readonly reactionHandlerManager: HandlerManager<ChatMessageReaction> =
|
readonly reactionHandlerManager: HandlerManager<ChatMessageReaction> =
|
||||||
new HandlerManager();
|
new HandlerManager();
|
||||||
|
readonly changeHandlerManager: HandlerManager<null> = new HandlerManager();
|
||||||
|
|
||||||
// paths
|
// paths
|
||||||
readonly getBasePath = (): string[] => {
|
readonly getBasePath = (): string[] => {
|
||||||
return StorageModel.getPath(
|
return StorageModel.getPath(
|
||||||
StorageModelSubPaths.Chat,
|
StorageModelSubPaths.Chat,
|
||||||
filePaths.chat.chatBase(this.id),
|
filePaths.chat.chatBase(this.id),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly getInfoPath = (): string[] => {
|
readonly getInfoPath = (): string[] => {
|
||||||
return StorageModel.getPath(
|
return StorageModel.getPath(
|
||||||
StorageModelSubPaths.Chat,
|
StorageModelSubPaths.Chat,
|
||||||
filePaths.chat.info(this.id),
|
filePaths.chat.info(this.id),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly getColorPath = (): string[] => {
|
readonly getColorPath = (): string[] => {
|
||||||
return StorageModel.getPath(
|
return StorageModel.getPath(
|
||||||
StorageModelSubPaths.Chat,
|
StorageModelSubPaths.Chat,
|
||||||
filePaths.chat.color(this.id),
|
filePaths.chat.color(this.id),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly getMessageDirPath = (): string[] => {
|
readonly getMessageDirPath = (): string[] => {
|
||||||
return StorageModel.getPath(
|
return StorageModel.getPath(
|
||||||
StorageModelSubPaths.Chat,
|
StorageModelSubPaths.Chat,
|
||||||
filePaths.chat.messages(this.id),
|
filePaths.chat.messages(this.id),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly getMessagePath = (id: string): string[] => {
|
readonly getMessagePath = (id: string): string[] => {
|
||||||
return [...this.getMessageDirPath(), id];
|
return [...this.getMessageDirPath(), id];
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly getReactionDirPath = (): string[] => {
|
readonly getReactionDirPath = (): string[] => {
|
||||||
return StorageModel.getPath(
|
return StorageModel.getPath(
|
||||||
StorageModelSubPaths.Chat,
|
StorageModelSubPaths.Chat,
|
||||||
filePaths.chat.reactions(this.id),
|
filePaths.chat.reactions(this.id),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly getReactionPath = (id: string): string[] => {
|
readonly getReactionPath = (id: string): string[] => {
|
||||||
return [...this.getReactionDirPath(), id];
|
return [...this.getReactionDirPath(), id];
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly getPreviousFilterPath = (): string[] => {
|
readonly getPreviousFilterPath = (): string[] => {
|
||||||
@@ -102,168 +103,174 @@ export default class ChatModel {
|
|||||||
|
|
||||||
// handlers
|
// handlers
|
||||||
readonly handleMessage = (body: string): void => {
|
readonly handleMessage = (body: string): void => {
|
||||||
const chatMessage: ChatMessage | null = parseValidObject(
|
const chatMessage: ChatMessage | null = parseValidObject(
|
||||||
body,
|
body,
|
||||||
ChatMessageReference,
|
ChatMessageReference,
|
||||||
);
|
);
|
||||||
if (chatMessage == null) return;
|
if (chatMessage == null) return;
|
||||||
|
|
||||||
chatMessage.status = ChatMessageStatuses.Received;
|
chatMessage.status = ChatMessageStatuses.Received;
|
||||||
|
|
||||||
this.addMessage(chatMessage);
|
this.addMessage(chatMessage);
|
||||||
|
|
||||||
if (chatMessage.stringifiedFile) return;
|
if (chatMessage.stringifiedFile) return;
|
||||||
this.setReadStatus(true);
|
this.setReadStatus(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly handleReaction = (reaction: ChatMessageReaction | any): void => {
|
readonly handleReaction = (reaction: ChatMessageReaction | any): void => {
|
||||||
if (
|
if (
|
||||||
!checkMatchesObjectStructure(reaction, ChatMessageReactionReference)
|
!checkMatchesObjectStructure(reaction, ChatMessageReactionReference)
|
||||||
)
|
)
|
||||||
return;
|
return;
|
||||||
const reactionPath: string[] = this.getReactionPath(reaction.fileId);
|
const reactionPath: string[] = this.getReactionPath(reaction.fileId);
|
||||||
|
|
||||||
if (reaction.isDeleting == true) {
|
if (reaction.isDeleting == true) {
|
||||||
this.storageModel.remove(reactionPath);
|
this.storageModel.remove(reactionPath);
|
||||||
} else {
|
} else {
|
||||||
this.storageModel.writeStringifiable(reactionPath, reaction);
|
this.storageModel.writeStringifiable(reactionPath, reaction);
|
||||||
}
|
}
|
||||||
this.reactionHandlerManager.trigger(reaction);
|
this.reactionHandlerManager.trigger(reaction);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly handleMessageSent = (chatMessage: ChatMessage): void => {
|
readonly handleMessageSent = (chatMessage: ChatMessage): void => {
|
||||||
chatMessage.status = ChatMessageStatuses.Sent;
|
chatMessage.status = ChatMessageStatuses.Sent;
|
||||||
this.addMessage(chatMessage);
|
this.addMessage(chatMessage);
|
||||||
};
|
};
|
||||||
|
|
||||||
// settings
|
// settings
|
||||||
readonly setName = (name: string): void => {
|
readonly setName = (name: string): void => {
|
||||||
this.info.name = name;
|
this.info.name = name;
|
||||||
this.storeInfo();
|
this.storeInfo();
|
||||||
this.subscribe();
|
this.syncInfo();
|
||||||
|
this.subscribe();
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly setSecondaryChannels = (secondaryChannels: string[]): void => {
|
readonly setSecondaryChannels = (secondaryChannels: string[]): void => {
|
||||||
this.info.secondaryChannels = secondaryChannels;
|
this.info.secondaryChannels = secondaryChannels;
|
||||||
this.storeInfo();
|
this.storeInfo();
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly setEncryptionKey = (key: string): void => {
|
readonly setEncryptionKey = (key: string): void => {
|
||||||
this.info.encryptionKey = key;
|
this.info.encryptionKey = key;
|
||||||
this.storeInfo();
|
this.storeInfo();
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly setColor = (color: Colors): void => {
|
readonly setColor = (color: Colors): void => {
|
||||||
this.info.color = color;
|
this.info.color = color;
|
||||||
this.storeInfo();
|
this.syncInfo();
|
||||||
|
this.storeInfo();
|
||||||
};
|
};
|
||||||
|
|
||||||
// messaging
|
// messaging
|
||||||
readonly addMessage = async (chatMessage: ChatMessage): Promise<void> => {
|
readonly addMessage = async (chatMessage: ChatMessage): Promise<void> => {
|
||||||
await this.decryptMessage(chatMessage);
|
await this.decryptMessage(chatMessage);
|
||||||
|
|
||||||
// message
|
// message
|
||||||
if (chatMessage.body != "") {
|
if (chatMessage.body != "") {
|
||||||
const messagePath: string[] = this.getMessagePath(chatMessage.id);
|
const messagePath: string[] = this.getMessagePath(chatMessage.id);
|
||||||
this.storageModel.writeStringifiable(messagePath, chatMessage);
|
this.storageModel.writeStringifiable(messagePath, chatMessage);
|
||||||
this.chatMessageHandlerManager.trigger(chatMessage);
|
this.chatMessageHandlerManager.trigger(chatMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
// file
|
// file
|
||||||
this.fileModel.handleStringifiedFileContent(
|
this.fileModel.handleStringifiedFileContent(
|
||||||
chatMessage.stringifiedFile,
|
chatMessage.stringifiedFile,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly getNameAndChannel = (): [string, string] | false => {
|
readonly getNameAndChannel = (): [string, string] | false => {
|
||||||
const senderName: string = this.settingsModel.username;
|
const senderName: string = this.settingsModel.username;
|
||||||
if (senderName == "") return false;
|
if (senderName == "") return false;
|
||||||
|
|
||||||
const allChannels: string[] = [this.id];
|
const allChannels: string[] = [this.id];
|
||||||
for (const secondaryChannel of this.info.secondaryChannels) {
|
for (const secondaryChannel of this.info.secondaryChannels) {
|
||||||
allChannels.push(secondaryChannel);
|
allChannels.push(secondaryChannel);
|
||||||
}
|
}
|
||||||
|
|
||||||
const combinedChannel: string = allChannels.join("/");
|
const combinedChannel: string = allChannels.join("/");
|
||||||
|
|
||||||
return [senderName, combinedChannel];
|
return [senderName, combinedChannel];
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly sendMessage = async (
|
readonly sendMessage = async (
|
||||||
body: string,
|
body: string,
|
||||||
inlineReplyId?: string,
|
inlineReplyId?: string,
|
||||||
fileContent?: FileContent<string>,
|
fileContent?: FileContent<string>,
|
||||||
): Promise<string|false> => {
|
): Promise<string|false> => {
|
||||||
const nameAndChannel = this.getNameAndChannel();
|
const nameAndChannel = this.getNameAndChannel();
|
||||||
if (nameAndChannel == false) return false;
|
if (nameAndChannel == false) return false;
|
||||||
const [senderName, combinedChannel] = nameAndChannel;
|
const [senderName, combinedChannel] = nameAndChannel;
|
||||||
|
|
||||||
const chatMessage: ChatMessage = await ChatModel.createChatMessage(
|
const chatMessage: ChatMessage = await ChatModel.createChatMessage(
|
||||||
combinedChannel,
|
combinedChannel,
|
||||||
senderName,
|
senderName,
|
||||||
this.info.encryptionKey,
|
this.info.encryptionKey,
|
||||||
body,
|
body,
|
||||||
inlineReplyId,
|
inlineReplyId,
|
||||||
fileContent,
|
fileContent,
|
||||||
);
|
);
|
||||||
|
|
||||||
this.addMessage(chatMessage);
|
this.addMessage(chatMessage);
|
||||||
this.connectionModel.sendMessageOrStore(chatMessage);
|
this.connectionModel.sendMessageOrStore(chatMessage);
|
||||||
return chatMessage.id;
|
return chatMessage.id;
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly decryptMessage = async (
|
readonly decryptMessage = async (
|
||||||
chatMessage: ChatMessage,
|
chatMessage: ChatMessage,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
const decryptedBody: string = await decryptString(
|
const decryptedBody: string = await decryptString(
|
||||||
chatMessage.body,
|
chatMessage.body,
|
||||||
this.info.encryptionKey,
|
this.info.encryptionKey,
|
||||||
);
|
);
|
||||||
const decryptedFile: string = await decryptString(
|
const decryptedFile: string = await decryptString(
|
||||||
chatMessage.stringifiedFile ?? "",
|
chatMessage.stringifiedFile ?? "",
|
||||||
this.info.encryptionKey,
|
this.info.encryptionKey,
|
||||||
);
|
);
|
||||||
chatMessage.body = decryptedBody;
|
chatMessage.body = decryptedBody;
|
||||||
chatMessage.stringifiedFile = decryptedFile;
|
chatMessage.stringifiedFile = decryptedFile;
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly sendReaction = async (
|
readonly sendReaction = async (
|
||||||
messageId: string,
|
messageId: string,
|
||||||
content: ReactionSymbols,
|
content: ReactionSymbols,
|
||||||
isDeleting: boolean = false,
|
isDeleting: boolean = false,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
const nameAndChannel = this.getNameAndChannel();
|
const nameAndChannel = this.getNameAndChannel();
|
||||||
if (nameAndChannel == false) return;
|
if (nameAndChannel == false) return;
|
||||||
const [senderName] = nameAndChannel;
|
const [senderName] = nameAndChannel;
|
||||||
|
|
||||||
const reaction = ChatModel.createMessageReaction(
|
const reaction = ChatModel.createMessageReaction(
|
||||||
messageId,
|
messageId,
|
||||||
senderName,
|
senderName,
|
||||||
content,
|
content,
|
||||||
isDeleting,
|
isDeleting,
|
||||||
);
|
);
|
||||||
this.sendMessage("", undefined, reaction);
|
this.sendMessage("", undefined, reaction);
|
||||||
this.handleReaction(reaction);
|
this.handleReaction(reaction);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly subscribe = (): void => {
|
readonly subscribe = (): void => {
|
||||||
this.connectionModel.addChannel(this.id);
|
this.connectionModel.addChannel(this.id);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly setReadStatus = (hasUnreadMessages: boolean): void => {
|
readonly setReadStatus = (hasUnreadMessages: boolean): void => {
|
||||||
this.info.hasUnreadMessages = hasUnreadMessages;
|
this.info.hasUnreadMessages = hasUnreadMessages;
|
||||||
this.storeInfo();
|
this.storeInfo();
|
||||||
};
|
};
|
||||||
|
|
||||||
// storage
|
// storage
|
||||||
readonly storeInfo = (): void => {
|
readonly storeInfo = (): void => {
|
||||||
this.storageModel.writeStringifiable(this.getInfoPath(), this.info);
|
this.storageModel.writeStringifiable(this.getInfoPath(), this.info);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
readonly syncInfo = (): void => {
|
||||||
|
this.sendMessage("", undefined, this.info);
|
||||||
|
}
|
||||||
|
|
||||||
readonly storeFilter = (filter: string): void => {
|
readonly storeFilter = (filter: string): void => {
|
||||||
this.storageModel.write(this.getPreviousFilterPath(), filter);
|
this.storageModel.write(this.getPreviousFilterPath(), filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly getFilter = (): string=> {
|
readonly getFilter = (): string=> {
|
||||||
return this.storageModel.read(this.getPreviousFilterPath()) || "";
|
return this.storageModel.read(this.getPreviousFilterPath()) || "";
|
||||||
}
|
}
|
||||||
@@ -283,13 +290,15 @@ export default class ChatModel {
|
|||||||
this.getInfoPath(),
|
this.getInfoPath(),
|
||||||
ChatInfoReference,
|
ChatInfoReference,
|
||||||
);
|
);
|
||||||
if (info != null) {
|
this.info = (info || ChatModel.generateChatInfo("0", "0", Colors.Standard));
|
||||||
this.info = info;
|
|
||||||
} else {
|
|
||||||
this.info = ChatModel.generateChatInfo("0", "0", Colors.Standard );
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
readonly handleInfo = (info: ChatInfoFileContent): void => {
|
||||||
|
this.info = info;
|
||||||
|
this.changeHandlerManager.trigger(null);
|
||||||
|
this.storeInfo();
|
||||||
|
}
|
||||||
|
|
||||||
get messages(): ChatMessage[] {
|
get messages(): ChatMessage[] {
|
||||||
const messageIds: string[] = this.storageModel.list(
|
const messageIds: string[] = this.storageModel.list(
|
||||||
this.getMessageDirPath(),
|
this.getMessageDirPath(),
|
||||||
@@ -499,7 +508,7 @@ extends ValidObject, FileContent<"reaction"> {
|
|||||||
// references
|
// references
|
||||||
export const ChatInfoReference: ChatInfoFileContent = {
|
export const ChatInfoReference: ChatInfoFileContent = {
|
||||||
dataVersion: DATA_VERSION,
|
dataVersion: DATA_VERSION,
|
||||||
|
|
||||||
fileId: "",
|
fileId: "",
|
||||||
fileContentId: "",
|
fileContentId: "",
|
||||||
creationDate: "",
|
creationDate: "",
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ import {
|
|||||||
parseValidObject,
|
parseValidObject,
|
||||||
stringify,
|
stringify,
|
||||||
} from "../Utility/utility";
|
} from "../Utility/utility";
|
||||||
import { DATA_VERSION, ValidObject } from "../Utility/typeSafety";
|
import { checkIsValidObject, checkMatchesObjectStructure, DATA_VERSION, ValidObject } from "../Utility/typeSafety";
|
||||||
import StorageModel, {
|
import StorageModel, {
|
||||||
StorageModelSubPaths,
|
StorageModelSubPaths,
|
||||||
filePaths,
|
filePaths,
|
||||||
} from "../Global/storageModel";
|
} from "../Global/storageModel";
|
||||||
import SettingsModel from "../Global/settingsModel";
|
import SettingsModel from "../Global/settingsModel";
|
||||||
import ChatModel from "../Chat/chatModel";
|
import ChatModel, {ChatInfoFileContent, ChatInfoReference} from "../Chat/chatModel";
|
||||||
|
|
||||||
export default class FileModel {
|
export default class FileModel {
|
||||||
readonly storageModel: StorageModel;
|
readonly storageModel: StorageModel;
|
||||||
@@ -67,154 +67,160 @@ export default class FileModel {
|
|||||||
};
|
};
|
||||||
|
|
||||||
readonly handleFileContent = (fileContent: FileContent<string>): void => {
|
readonly handleFileContent = (fileContent: FileContent<string>): void => {
|
||||||
const didStore: boolean = this.storeFileContent(fileContent);
|
if (fileContent.type == "chat-info") {
|
||||||
if (didStore == false) return;
|
if (checkMatchesObjectStructure(fileContent, ChatInfoReference) == false) return;
|
||||||
|
console.log("CHANGING");
|
||||||
|
this.chatModel.handleInfo(fileContent as ChatInfoFileContent);
|
||||||
|
}
|
||||||
|
|
||||||
switch (fileContent.type) {
|
const didStore: boolean = this.storeFileContent(fileContent);
|
||||||
case "board-info":
|
if (didStore == false) return;
|
||||||
case "task":
|
|
||||||
this.boardsAndTasksModel.handleFileContent(fileContent);
|
switch (fileContent.type) {
|
||||||
break;
|
case "board-info":
|
||||||
case "reaction":
|
case "task":
|
||||||
this.chatModel.handleReaction(fileContent);
|
this.boardsAndTasksModel.handleFileContent(fileContent);
|
||||||
break;
|
break;
|
||||||
}
|
case "reaction":
|
||||||
|
this.chatModel.handleReaction(fileContent);
|
||||||
|
break;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// methods
|
// methods
|
||||||
readonly addFileContentAndSend = (
|
readonly addFileContentAndSend = (
|
||||||
fileContent: FileContent<string>,
|
fileContent: FileContent<string>,
|
||||||
): void => {
|
): void => {
|
||||||
this.handleFileContent(fileContent);
|
this.handleFileContent(fileContent);
|
||||||
this.chatModel.sendMessage("", undefined, fileContent);
|
this.chatModel.sendMessage("", undefined, fileContent);
|
||||||
};
|
};
|
||||||
|
|
||||||
// storage
|
// storage
|
||||||
readonly storeFileContent = (fileContent: FileContent<string>): boolean => {
|
readonly storeFileContent = (fileContent: FileContent<string>): boolean => {
|
||||||
const fileContentPath: string[] = this.getFileContentPath(
|
const fileContentPath: string[] = this.getFileContentPath(
|
||||||
fileContent.fileId,
|
fileContent.fileId,
|
||||||
fileContent.fileContentId,
|
fileContent.fileContentId,
|
||||||
);
|
);
|
||||||
|
|
||||||
// check if fileContent already exists
|
// check if fileContent already exists
|
||||||
const existingFileContent: string | null =
|
const existingFileContent: string | null =
|
||||||
this.storageModel.read(fileContentPath);
|
this.storageModel.read(fileContentPath);
|
||||||
if (existingFileContent != null) return false;
|
if (existingFileContent != null) return false;
|
||||||
|
|
||||||
const stringifiedContent: string = stringify(fileContent);
|
const stringifiedContent: string = stringify(fileContent);
|
||||||
this.storageModel.write(fileContentPath, stringifiedContent);
|
this.storageModel.write(fileContentPath, stringifiedContent);
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly listFileIds = (): string[] => {
|
readonly listFileIds = (): string[] => {
|
||||||
return this.storageModel.list(this.basePath);
|
return this.storageModel.list(this.basePath);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly listFileContentIds = (fileId: string): string[] => {
|
readonly listFileContentIds = (fileId: string): string[] => {
|
||||||
const filePath: string[] = this.getFilePath(fileId);
|
const filePath: string[] = this.getFilePath(fileId);
|
||||||
return this.storageModel.list(filePath);
|
return this.storageModel.list(filePath);
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly selectLatestFileContentId = (
|
readonly selectLatestFileContentId = (
|
||||||
fileContentIds: string[],
|
fileContentIds: string[],
|
||||||
): string | undefined => {
|
): string | undefined => {
|
||||||
return fileContentIds[fileContentIds.length - 1];
|
return fileContentIds[fileContentIds.length - 1];
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly getFileContent = <T extends FileContent<string>>(
|
readonly getFileContent = <T extends FileContent<string>>(
|
||||||
fileId: string,
|
fileId: string,
|
||||||
fileContentName: string,
|
fileContentName: string,
|
||||||
reference: T,
|
reference: T,
|
||||||
): T | null => {
|
): T | null => {
|
||||||
const filePath: string[] = this.getFileContentPath(
|
const filePath: string[] = this.getFileContentPath(
|
||||||
fileId,
|
fileId,
|
||||||
fileContentName,
|
fileContentName,
|
||||||
);
|
);
|
||||||
const fileContentOrNull: T | null = this.storageModel.readStringifiable(
|
const fileContentOrNull: T | null = this.storageModel.readStringifiable(
|
||||||
filePath,
|
filePath,
|
||||||
reference,
|
reference,
|
||||||
);
|
);
|
||||||
return fileContentOrNull;
|
return fileContentOrNull;
|
||||||
};
|
};
|
||||||
|
|
||||||
readonly getLatestFileContent = <T extends FileContent<string>>(
|
readonly getLatestFileContent = <T extends FileContent<string>>(
|
||||||
fileId: string,
|
fileId: string,
|
||||||
reference: T,
|
reference: T,
|
||||||
): T | null => {
|
): T | null => {
|
||||||
const fileContentsIds: string[] = this.listFileContentIds(fileId);
|
const fileContentsIds: string[] = this.listFileContentIds(fileId);
|
||||||
const latestFileContentId: string | undefined =
|
const latestFileContentId: string | undefined =
|
||||||
this.selectLatestFileContentId(fileContentsIds);
|
this.selectLatestFileContentId(fileContentsIds);
|
||||||
if (latestFileContentId == undefined) return null;
|
if (latestFileContentId == undefined) return null;
|
||||||
|
|
||||||
const fileContent: T | null = this.getFileContent(
|
const fileContent: T | null = this.getFileContent(
|
||||||
fileId,
|
fileId,
|
||||||
latestFileContentId,
|
latestFileContentId,
|
||||||
reference,
|
reference,
|
||||||
);
|
);
|
||||||
return fileContent;
|
return fileContent;
|
||||||
};
|
};
|
||||||
|
|
||||||
// init
|
// init
|
||||||
constructor(
|
constructor(
|
||||||
storageModel: StorageModel,
|
storageModel: StorageModel,
|
||||||
settingsModel: SettingsModel,
|
settingsModel: SettingsModel,
|
||||||
chatModel: ChatModel,
|
chatModel: ChatModel,
|
||||||
) {
|
) {
|
||||||
this.chatModel = chatModel;
|
this.chatModel = chatModel;
|
||||||
this.settingsModel = settingsModel;
|
this.settingsModel = settingsModel;
|
||||||
this.storageModel = storageModel;
|
this.storageModel = storageModel;
|
||||||
|
|
||||||
this.boardsAndTasksModel = new BoardsAndTasksModel(
|
this.boardsAndTasksModel = new BoardsAndTasksModel(
|
||||||
this.storageModel,
|
this.storageModel,
|
||||||
this.settingsModel,
|
this.settingsModel,
|
||||||
chatModel,
|
chatModel,
|
||||||
this,
|
this,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// utility
|
// utility
|
||||||
static generateFileContentId = (creationDate: string): string => {
|
static generateFileContentId = (creationDate: string): string => {
|
||||||
return creationDate + v4();
|
return creationDate + v4();
|
||||||
};
|
};
|
||||||
|
|
||||||
static createFileContent = <T extends string>(
|
static createFileContent = <T extends string>(
|
||||||
fileId: string,
|
fileId: string,
|
||||||
type: T,
|
type: T,
|
||||||
): FileContent<T> => {
|
): FileContent<T> => {
|
||||||
const creationDate: string = createTimestamp();
|
const creationDate: string = createTimestamp();
|
||||||
const fileContentId: string =
|
const fileContentId: string =
|
||||||
FileModel.generateFileContentId(creationDate);
|
FileModel.generateFileContentId(creationDate);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
dataVersion: DATA_VERSION,
|
dataVersion: DATA_VERSION,
|
||||||
|
|
||||||
fileId,
|
fileId,
|
||||||
fileContentId,
|
fileContentId,
|
||||||
creationDate,
|
creationDate,
|
||||||
type,
|
type,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// paths
|
// paths
|
||||||
export enum FileModelSubPath {
|
export enum FileModelSubPath {
|
||||||
Data = "data",
|
Data = "data",
|
||||||
Model = "model",
|
Model = "model",
|
||||||
Chat = "chat",
|
Chat = "chat",
|
||||||
ModelView = "view",
|
ModelView = "view",
|
||||||
ModelTask = "tasks",
|
ModelTask = "tasks",
|
||||||
ModelCalendar = "calendar",
|
ModelCalendar = "calendar",
|
||||||
}
|
}
|
||||||
|
|
||||||
// types
|
// types
|
||||||
export interface FileContent<T extends string>
|
export interface FileContent<T extends string>
|
||||||
extends ValidObject, StringEntryObject {
|
extends ValidObject, StringEntryObject {
|
||||||
readonly fileId: string;
|
readonly fileId: string;
|
||||||
readonly fileContentId: string;
|
readonly fileContentId: string;
|
||||||
readonly creationDate: string;
|
readonly creationDate: string;
|
||||||
|
|
||||||
readonly type: T;
|
readonly type: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
// references
|
// references
|
||||||
export const FileContentReference: FileContent<string> = {
|
export const FileContentReference: FileContent<string> = {
|
||||||
|
|||||||
@@ -177,7 +177,24 @@ export function SettingsPage(
|
|||||||
|
|
||||||
<hr></hr>
|
<hr></hr>
|
||||||
|
|
||||||
{ColorPicker(settingsPageViewModel.color)}
|
{ColorPicker(settingsPageViewModel.color)}
|
||||||
|
|
||||||
|
<div class="flex-row justify-end width-input">
|
||||||
|
<button
|
||||||
|
class="width-50"
|
||||||
|
aria-label={
|
||||||
|
coreViewModel.translations.chatPage.settings
|
||||||
|
.setNameButtonAudioLabel
|
||||||
|
}
|
||||||
|
on:click={
|
||||||
|
settingsPageViewModel.applyColor
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{coreViewModel.translations.general.setButton}
|
||||||
|
<span class="icon">check</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<hr></hr>
|
<hr></hr>
|
||||||
|
|
||||||
|
|||||||
+120
-119
@@ -31,7 +31,7 @@ export default class ChatViewModel extends ContextHost<ChatPageTypes> {
|
|||||||
// state
|
// state
|
||||||
displayedColor: React.State<Colors> = new React.State<any>(Colors.Standard);
|
displayedColor: React.State<Colors> = new React.State<any>(Colors.Standard);
|
||||||
selectedPage: React.State<ChatPageTypes> = new React.State<any>(
|
selectedPage: React.State<ChatPageTypes> = new React.State<any>(
|
||||||
ChatPageTypes.Messages,
|
ChatPageTypes.Messages,
|
||||||
);
|
);
|
||||||
pageContexts = new React.MapState<Context>();
|
pageContexts = new React.MapState<Context>();
|
||||||
|
|
||||||
@@ -42,185 +42,186 @@ export default class ChatViewModel extends ContextHost<ChatPageTypes> {
|
|||||||
|
|
||||||
// context
|
// context
|
||||||
get isOpen(): boolean {
|
get isOpen(): boolean {
|
||||||
return this.chatListViewModel.selectedChat.value == this;
|
return this.chatListViewModel.selectedChat.value == this;
|
||||||
}
|
}
|
||||||
|
|
||||||
get contextSelection(): ChatPageTypes {
|
get contextSelection(): ChatPageTypes {
|
||||||
return this.selectedPage.value;
|
return this.selectedPage.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// view
|
// view
|
||||||
open = (): void => {
|
open = (): void => {
|
||||||
this.coreViewModel.context = this;
|
this.coreViewModel.context = this;
|
||||||
this.chatListViewModel.openChat(this);
|
this.chatListViewModel.openChat(this);
|
||||||
//this.selectedPage.callSubscriptions();
|
//this.selectedPage.callSubscriptions();
|
||||||
};
|
};
|
||||||
|
|
||||||
openPage = (page: ChatPageTypes): void => {
|
openPage = (page: ChatPageTypes): void => {
|
||||||
this.closeCurrentContext();
|
this.closeCurrentContext();
|
||||||
this.selectedPage.value = page;
|
this.selectedPage.value = page;
|
||||||
};
|
};
|
||||||
|
|
||||||
setColor = (color: Colors): void => {
|
setColor = (color: Colors): void => {
|
||||||
this.setDisplayedColor(color);
|
this.setDisplayedColor(color);
|
||||||
this.chatModel.setColor(color);
|
this.chatModel.setColor(color);
|
||||||
};
|
};
|
||||||
|
|
||||||
setDisplayedColor = (color: Colors): void => {
|
setDisplayedColor = (color: Colors): void => {
|
||||||
this.displayedColor.value = color;
|
this.displayedColor.value = color;
|
||||||
};
|
};
|
||||||
|
|
||||||
resetColor = (): void => {
|
resetColor = (): void => {
|
||||||
this.displayedColor.value = this.settingsPageViewModel.color.value;
|
this.displayedColor.value = this.settingsPageViewModel.color.value;
|
||||||
};
|
};
|
||||||
|
|
||||||
updateIndex = (): void => {
|
updateIndex = (): void => {
|
||||||
const index: number =
|
const index: number =
|
||||||
this.chatListViewModel.chatIndexManager.getIndex(this);
|
this.chatListViewModel.chatIndexManager.getIndex(this);
|
||||||
this.index.value = index;
|
this.index.value = index;
|
||||||
};
|
};
|
||||||
|
|
||||||
// load
|
// load
|
||||||
loadPageSelection = (): void => {
|
loadPageSelection = (): void => {
|
||||||
const path: string[] = StorageModel.getPath(
|
const path: string[] = StorageModel.getPath(
|
||||||
StorageModelSubPaths.Chat,
|
StorageModelSubPaths.Chat,
|
||||||
filePaths.chat.lastUsedPage(this.chatModel.id),
|
filePaths.chat.lastUsedPage(this.chatModel.id),
|
||||||
);
|
);
|
||||||
const lastUsedPage: string | null =
|
const lastUsedPage: string | null =
|
||||||
this.coreViewModel.storageModel.read(path);
|
this.coreViewModel.storageModel.read(path);
|
||||||
if (lastUsedPage != null) {
|
if (lastUsedPage != null) {
|
||||||
this.openPage(lastUsedPage as ChatPageTypes);
|
this.openPage(lastUsedPage as ChatPageTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.selectedPage.subscribeSilent((newPage) => {
|
this.selectedPage.subscribeSilent((newPage) => {
|
||||||
this.coreViewModel.storageModel.write(path, newPage);
|
this.coreViewModel.storageModel.write(path, newPage);
|
||||||
this.resetColor();
|
this.resetColor();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
loadInfo = (): void => {
|
loadInfo = (): void => {
|
||||||
this.updateReadStatus();
|
this.updateReadStatus();
|
||||||
this.taskBoardSuggestions.set(CALENDAR_EVENT_BOARD_ID, [
|
this.taskBoardSuggestions.set(CALENDAR_EVENT_BOARD_ID, [
|
||||||
CALENDAR_EVENT_BOARD_ID,
|
CALENDAR_EVENT_BOARD_ID,
|
||||||
this.coreViewModel.translations.chatPage.calendar.eventsBoard,
|
this.coreViewModel.translations.chatPage.calendar.eventsBoard,
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
updateReadStatus = (): void => {
|
updateReadStatus = (): void => {
|
||||||
if (
|
if (
|
||||||
this.chatListViewModel.selectedChat.value == this &&
|
this.chatListViewModel.selectedChat.value == this &&
|
||||||
this.selectedPage.value == ChatPageTypes.Messages
|
this.selectedPage.value == ChatPageTypes.Messages
|
||||||
) {
|
) {
|
||||||
this.chatModel.setReadStatus(false);
|
this.chatModel.setReadStatus(false);
|
||||||
}
|
}
|
||||||
this.hasUnreadMessages.value = this.chatModel.info.hasUnreadMessages;
|
this.hasUnreadMessages.value = this.chatModel.info.hasUnreadMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
setReadStatus = (hasUnreadMessages: boolean): void => {
|
setReadStatus = (hasUnreadMessages: boolean): void => {
|
||||||
this.chatModel.setReadStatus(hasUnreadMessages);
|
this.chatModel.setReadStatus(hasUnreadMessages);
|
||||||
this.hasUnreadMessages.value = hasUnreadMessages;
|
this.hasUnreadMessages.value = hasUnreadMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
subscribeReadStatus = (): void => {
|
subscribeReadStatus = (): void => {
|
||||||
React.createProxyState(
|
React.createProxyState(
|
||||||
[this.selectedPage, this.chatListViewModel.selectedChat],
|
[this.selectedPage, this.chatListViewModel.selectedChat],
|
||||||
() => {
|
() => {
|
||||||
if (this.chatListViewModel.selectedChat.value != this) return;
|
if (this.chatListViewModel.selectedChat.value != this) return;
|
||||||
if (this.selectedPage.value != ChatPageTypes.Messages) return;
|
if (this.selectedPage.value != ChatPageTypes.Messages) return;
|
||||||
this.setReadStatus(false);
|
this.setReadStatus(false);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// exit
|
// exit
|
||||||
close = (): void => {
|
close = (): void => {
|
||||||
this.coreViewModel.closeContext(this.contextId);
|
this.coreViewModel.closeContext(this.contextId);
|
||||||
};
|
};
|
||||||
|
|
||||||
handleContextClose = (): void => {
|
handleContextClose = (): void => {
|
||||||
this.chatListViewModel.closeChat();
|
this.chatListViewModel.closeChat();
|
||||||
};
|
};
|
||||||
|
|
||||||
// init
|
// init
|
||||||
constructor(
|
constructor(
|
||||||
readonly coreViewModel: CoreViewModel,
|
readonly coreViewModel: CoreViewModel,
|
||||||
public readonly chatModel: ChatModel,
|
public readonly chatModel: ChatModel,
|
||||||
public readonly settingsViewModel: SettingsViewModel,
|
public readonly settingsViewModel: SettingsViewModel,
|
||||||
public readonly notificationViewModel: NotificationViewModel,
|
public readonly notificationViewModel: NotificationViewModel,
|
||||||
public readonly connectionViewModel: ConnectionViewModel,
|
public readonly connectionViewModel: ConnectionViewModel,
|
||||||
public readonly chatListViewModel: ChatListViewModel,
|
public readonly chatListViewModel: ChatListViewModel,
|
||||||
) {
|
) {
|
||||||
super("chat", coreViewModel);
|
super("chat", coreViewModel);
|
||||||
|
|
||||||
// page viewModels
|
// page viewModels
|
||||||
this.calendarViewModel = new CalendarPageViewModel(
|
this.calendarViewModel = new CalendarPageViewModel(
|
||||||
coreViewModel,
|
coreViewModel,
|
||||||
this,
|
this,
|
||||||
this.chatModel.fileModel.boardsAndTasksModel.calendarModel,
|
this.chatModel.fileModel.boardsAndTasksModel.calendarModel,
|
||||||
this.chatModel.fileModel.boardsAndTasksModel,
|
this.chatModel.fileModel.boardsAndTasksModel,
|
||||||
);
|
);
|
||||||
this.taskPageViewModel = new TaskPageViewModel(
|
this.taskPageViewModel = new TaskPageViewModel(
|
||||||
this.coreViewModel,
|
this.coreViewModel,
|
||||||
this,
|
this,
|
||||||
this.chatModel.fileModel.boardsAndTasksModel,
|
this.chatModel.fileModel.boardsAndTasksModel,
|
||||||
);
|
);
|
||||||
this.messagePageViewModel = new MessagePageViewModel(
|
this.messagePageViewModel = new MessagePageViewModel(
|
||||||
this.coreViewModel,
|
this.coreViewModel,
|
||||||
this,
|
this,
|
||||||
);
|
);
|
||||||
this.settingsPageViewModel = new SettingsPageViewModel(
|
this.settingsPageViewModel = new SettingsPageViewModel(
|
||||||
this.coreViewModel,
|
this.coreViewModel,
|
||||||
this,
|
this,
|
||||||
);
|
);
|
||||||
|
|
||||||
// handlers
|
// handlers
|
||||||
chatModel.chatMessageHandlerManager.setHandler(
|
chatModel.chatMessageHandlerManager.setHandler(
|
||||||
this.chatModel.id,
|
this.chatModel.id,
|
||||||
(chatMessage: ChatMessage) => {
|
(chatMessage: ChatMessage) => {
|
||||||
this.messagePageViewModel.showChatMessage(chatMessage);
|
this.messagePageViewModel.showChatMessage(chatMessage);
|
||||||
this.updateReadStatus();
|
this.updateReadStatus();
|
||||||
|
|
||||||
this.notificationViewModel.showNotification(chatMessage);
|
this.notificationViewModel.showNotification(chatMessage);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
chatModel.reactionHandlerManager.setHandler(
|
chatModel.reactionHandlerManager.setHandler(
|
||||||
this.chatModel.id,
|
this.chatModel.id,
|
||||||
(reaction: ChatMessageReaction) => {
|
(reaction: ChatMessageReaction) => {
|
||||||
this.messagePageViewModel.handleReaction(reaction);
|
this.messagePageViewModel.handleReaction(reaction);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
chatModel.changeHandlerManager.setHandler(this.chatModel.id, this.settingsPageViewModel.preloadData);
|
||||||
|
|
||||||
// load
|
// load
|
||||||
this.loadPageSelection();
|
this.loadPageSelection();
|
||||||
this.resetColor();
|
this.resetColor();
|
||||||
this.loadInfo();
|
this.loadInfo();
|
||||||
this.subscribeReadStatus();
|
this.subscribeReadStatus();
|
||||||
|
|
||||||
// keystrokes
|
// keystrokes
|
||||||
this.registerKeyStroke(CommonKeys.Home, this.close);
|
this.registerKeyStroke(CommonKeys.Home, this.close);
|
||||||
this.registerKeyStroke("u", () =>
|
this.registerKeyStroke("u", () =>
|
||||||
this.openPage(ChatPageTypes.Messages),
|
this.openPage(ChatPageTypes.Messages),
|
||||||
);
|
);
|
||||||
this.registerKeyStroke("i", () => this.openPage(ChatPageTypes.Tasks));
|
this.registerKeyStroke("i", () => this.openPage(ChatPageTypes.Tasks));
|
||||||
this.registerKeyStroke("o", () =>
|
this.registerKeyStroke("o", () =>
|
||||||
this.openPage(ChatPageTypes.Calendar),
|
this.openPage(ChatPageTypes.Calendar),
|
||||||
);
|
);
|
||||||
this.registerKeyStroke(CommonKeys.Settings, () =>
|
this.registerKeyStroke(CommonKeys.Settings, () =>
|
||||||
this.openPage(ChatPageTypes.Settings),
|
this.openPage(ChatPageTypes.Settings),
|
||||||
);
|
);
|
||||||
|
|
||||||
// context
|
// context
|
||||||
this.chatListViewModel.selectedChat.subscribeSilent(
|
this.chatListViewModel.selectedChat.subscribeSilent(
|
||||||
this.updateContexts,
|
this.updateContexts,
|
||||||
);
|
);
|
||||||
this.selectedPage.subscribeSilent(this.updateContexts);
|
this.selectedPage.subscribeSilent(this.updateContexts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// types
|
// types
|
||||||
export enum ChatPageTypes {
|
export enum ChatPageTypes {
|
||||||
Settings = "settings",
|
Settings = "settings",
|
||||||
Messages = "messages",
|
Messages = "messages",
|
||||||
Tasks = "tasks",
|
Tasks = "tasks",
|
||||||
Calendar = "calendar",
|
Calendar = "calendar",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,8 +71,8 @@ export default class SettingsPageViewModel extends Context {
|
|||||||
this.encryptionKeyInput.callSubscriptions();
|
this.encryptionKeyInput.callSubscriptions();
|
||||||
};
|
};
|
||||||
|
|
||||||
applyColor = (newColor: Colors): void => {
|
applyColor = (): void => {
|
||||||
this.chatViewModel.setColor(newColor);
|
this.chatViewModel.setColor(this.color.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
remove = (): void => {
|
remove = (): void => {
|
||||||
@@ -122,10 +122,6 @@ export default class SettingsPageViewModel extends Context {
|
|||||||
this.chatViewModel.chatModel.info.encryptionKey,
|
this.chatViewModel.chatModel.info.encryptionKey,
|
||||||
);
|
);
|
||||||
|
|
||||||
this.color.subscribe((newColor) => {
|
|
||||||
this.applyColor(newColor);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.chatViewModel.registerContext(ChatPageTypes.Settings, this);
|
this.chatViewModel.registerContext(ChatPageTypes.Settings, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user