convert message to satisfy FileModel type

This commit is contained in:
marlon-erler
2026-09-28 00:17:07 +02:00
parent 4bcaa34d08
commit 8ddc55395f
9 changed files with 281 additions and 615 deletions
+12 -11
View File
@@ -166,7 +166,7 @@ export default class ChatModel {
// message
if (chatMessage.body != "") {
const messagePath: string[] = this.getMessagePath(chatMessage.id);
const messagePath: string[] = this.getMessagePath(chatMessage.fileId);
this.storageModel.writeStringifiable(messagePath, chatMessage);
this.chatMessageHandlerManager.trigger(chatMessage);
}
@@ -210,7 +210,7 @@ export default class ChatModel {
this.addMessage(chatMessage);
this.connectionModel.sendMessageOrStore(chatMessage);
return chatMessage.id;
return chatMessage.fileId;
};
readonly decryptMessage = async (
@@ -397,10 +397,10 @@ export default class ChatModel {
inlineReplyId?: string,
fileContent?: FileContent<string>,
): Promise<ChatMessage> => {
const chatMessage: ChatMessage = {
dataVersion: DATA_VERSION,
const messageFileContent = FileModel.createFileContent(v4(), "message");
id: v4(),
const chatMessage: ChatMessage = {
...messageFileContent,
channel,
senderName,
@@ -488,9 +488,7 @@ export enum ChatMessageStatuses {
Other = "other",
}
export interface ChatMessage extends ValidObject {
readonly id: string;
export interface ChatMessage extends FileContent<"message"> {
readonly channel: string;
readonly senderId: string;
readonly senderName: string;
@@ -504,7 +502,7 @@ export interface ChatMessage extends ValidObject {
}
export interface ChatMessageReaction
extends ValidObject, FileContent<"reaction"> {
extends FileContent<"reaction"> {
readonly messageId: string;
readonly senderId: string;
readonly senderName: string;
@@ -533,8 +531,11 @@ export const ChatInfoReference: ChatInfoFileContent = {
export const ChatMessageReference: ChatMessage = {
dataVersion: DATA_VERSION,
id: "",
fileId: "",
fileContentId: "",
creationDate: "",
type: "message",
channel: "",
senderName: "",
+2 -2
View File
@@ -149,12 +149,12 @@ export default class ConnectionModel {
};
readonly addToOutbox = (chatMessage: ChatMessage): void => {
const messagePath: string[] = [...this.getOutboxPath(), chatMessage.id];
const messagePath: string[] = [...this.getOutboxPath(), chatMessage.fileId];
this.storageModel.writeStringifiable(messagePath, chatMessage);
};
readonly removeFromOutbox = (chatMessage: ChatMessage): void => {
const messagePath: string[] = [...this.getOutboxPath(), chatMessage.id];
const messagePath: string[] = [...this.getOutboxPath(), chatMessage.fileId];
this.storageModel.remove(messagePath);
};
+2
View File
@@ -104,6 +104,7 @@ export default class FileTransferModel {
readonly handleDecryptedFile = (data: string): void => {
const parsed: any = parse(data);
const isFileData: boolean = checkMatchesObjectStructure(
parsed,
FileDataReference,
@@ -111,6 +112,7 @@ export default class FileTransferModel {
if (isFileData == false) throw "not file data";
const fileData: FileData = parsed;
this.storageModel.write(fileData.path, fileData.body);
const pathString: string = StorageModel.pathComponentsToString(
+66 -50
View File
@@ -1,6 +1,7 @@
import {
bytesToMB,
localeCompare,
parse,
parseValidObject,
stringify,
} from "../Utility/utility";
@@ -214,46 +215,61 @@ export default class StorageModel {
return stringify(this.storageEntryTree);
};
// upgrade
readonly upgrade = (): void => {
const v2To2610 = () => {
this.recurse(["v2"], (path) => {
const body = this.read(path);
if (!body) return;
const parsed = parse(body);
console.log(parsed.type);
});
}
v2To2610();
}
// init
constructor() {
this.initializeTree();
this.upgrade();
}
// utility
static getFileName = (pathComponents: string[]): string => {
return pathComponents[pathComponents.length - 1] || "\\";
return pathComponents[pathComponents.length - 1] || "\\";
};
static getFileNameFromString = (pathString: string): string => {
const pathComponents: string[] =
this.stringToPathComponents(pathString);
return pathComponents[pathComponents.length - 1] || "\\";
const pathComponents: string[] =
this.stringToPathComponents(pathString);
return pathComponents[pathComponents.length - 1] || "\\";
};
static pathComponentsToString = (...pathComponents: string[]): string => {
return pathComponents
.filter((x) => x != "")
.join(PATH_COMPONENT_SEPARATOR);
return pathComponents
.filter((x) => x != "")
.join(PATH_COMPONENT_SEPARATOR);
};
static stringToPathComponents = (string: string): string[] => {
return string.split(PATH_COMPONENT_SEPARATOR).filter((x) => x != "");
return string.split(PATH_COMPONENT_SEPARATOR).filter((x) => x != "");
};
static join = (...items: string[]): string => {
let allComponents: string[] = [];
for (const item of items) {
const parts = this.stringToPathComponents(item);
allComponents.push(...parts);
}
return StorageModel.pathComponentsToString(...allComponents);
let allComponents: string[] = [];
for (const item of items) {
const parts = this.stringToPathComponents(item);
allComponents.push(...parts);
}
return StorageModel.pathComponentsToString(...allComponents);
};
static getPath(
locationName: StorageModelSubPaths,
filePath: string[],
locationName: StorageModelSubPaths,
filePath: string[],
): string[] {
return [DATA_VERSION, locationName, ...filePath];
return [DATA_VERSION, locationName, ...filePath];
}
}
@@ -264,52 +280,52 @@ export type StorageEntry = { [key: string]: StorageEntry };
export enum StorageModelSubPaths {
Chat = "chat",
ConnectionModel = "connection",
NotificationModel = "notifications",
SettingsModel = "settings",
ContactsModel = "contacts",
ConnectionModel = "connection",
NotificationModel = "notifications",
SettingsModel = "settings",
ContactsModel = "contacts",
}
export const filePaths = {
connectionModel: {
socketAddress: ["socket-address"],
reconnectAddress: ["reconnect-address"],
outbox: ["outbox"],
mailboxes: ["mailboxes"],
socketAddress: ["socket-address"],
reconnectAddress: ["reconnect-address"],
outbox: ["outbox"],
mailboxes: ["mailboxes"],
previousAddresses: ["previous-addresses"],
previousAddresses: ["previous-addresses"],
},
chat: {
base: [],
chatBase: (id: string) => [id],
info: (id: string) => [...filePaths.chat.chatBase(id), "info"],
color: (id: string) => [...filePaths.chat.chatBase(id), "color"],
messages: (id: string) => [...filePaths.chat.chatBase(id), "messages"],
reactions: (id: string) => [
...filePaths.chat.chatBase(id),
"reactions",
],
previousFilter: (id: string) => [
...filePaths.chat.chatBase(id),
"previous-filter",
],
lastUsedPage: (id: string) => [
...filePaths.chat.chatBase(id),
"last-used-page",
],
files: (id: string) => [...filePaths.chat.chatBase(id), "files"],
base: [],
chatBase: (id: string) => [id],
info: (id: string) => [...filePaths.chat.chatBase(id), "info"],
color: (id: string) => [...filePaths.chat.chatBase(id), "color"],
messages: (id: string) => [...filePaths.chat.chatBase(id), "messages"],
reactions: (id: string) => [
...filePaths.chat.chatBase(id),
"reactions",
],
previousFilter: (id: string) => [
...filePaths.chat.chatBase(id),
"previous-filter",
],
lastUsedPage: (id: string) => [
...filePaths.chat.chatBase(id),
"last-used-page",
],
files: (id: string) => [...filePaths.chat.chatBase(id), "files"],
},
notificationModel: {
base: [],
base: [],
},
settingsModel: {
username: ["user-name"],
userid: ["user-id"],
firstDayOfWeek: ["first-day-of-week"],
language: ["language"],
theme: ["theme"],
username: ["user-name"],
userid: ["user-id"],
firstDayOfWeek: ["first-day-of-week"],
language: ["language"],
theme: ["theme"],
},
};
+1 -1
View File
@@ -7,7 +7,7 @@ export function InlineReply(chatMessageViewModel: ChatMessageViewModel) {
if (reply == undefined) return <div></div>;
const scroll = () => {
ViewController.scrollToView(reply.chatMessage.id);
ViewController.scrollToView(reply.chatMessage.fileId);
};
return (
+2 -2
View File
@@ -175,7 +175,7 @@ export default class ChatMessageViewModel {
sendReaction = (content: ReactionSymbols, isDeleting: boolean): void => {
this.messagePageViewModel.sendReaction(
this.chatMessage.id,
this.chatMessage.fileId,
content,
isDeleting,
);
@@ -194,7 +194,7 @@ export default class ChatMessageViewModel {
this.messagePageViewModel.chatMessageViewModels.value.get(
this.chatMessage.inlineReplyId,
);
this.inlineReply?.replies.set(this.chatMessage.id, this);
this.inlineReply?.replies.set(this.chatMessage.fileId, this);
}
};
@@ -13,7 +13,7 @@ export default class NotificationViewModel {
// main
showNotification = (message: ChatMessage): void => {
if (this.seenMessageIds.has(message.id)) return;
if (this.seenMessageIds.has(message.fileId)) return;
if (this.chatListViewModel.selectedChat.value == undefined) return;
const notification: Notification = this.createNotification(message);
@@ -84,7 +84,7 @@ export default class NotificationViewModel {
const fullChannel = ChatModel.splitChannel(message.channel)[0];
const chat = this.chatListViewModel.getDisplayName(fullChannel);
return {
messageId: message.id,
messageId: message.fileId,
chat,
fullChannel,
sender: message.senderName,
+3 -3
View File
@@ -59,7 +59,7 @@ export default class MessagePageViewModel extends Context {
sendMessageFromBody = (body: string): Promise<string | false> => {
let replyId: string | undefined = undefined;
if (this.replyingMessage.value) {
replyId = this.replyingMessage.value.chatMessage.id;
replyId = this.replyingMessage.value.chatMessage.fileId;
}
const id = this.chatViewModel.chatModel.sendMessage(body, replyId);
@@ -118,13 +118,13 @@ export default class MessagePageViewModel extends Context {
);
const existingChatMessageViewModel: ChatMessageViewModel | undefined =
this.chatMessageViewModels.value.get(chatMessage.id);
this.chatMessageViewModels.value.get(chatMessage.fileId);
if (existingChatMessageViewModel != undefined) {
existingChatMessageViewModel.body.value = chatMessage.body;
existingChatMessageViewModel.status.value = chatMessage.status;
} else {
this.chatMessageViewModels.set(
chatMessage.id,
chatMessage.fileId,
chatMessageViewModel,
);
}