Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Refactor message attributes

Hakan Shehu6f9c09b121c8
diff --git a/apps/desktop/src/main/data/workspace/migrations.ts b/apps/desktop/src/main/data/workspace/migrations.ts
index 46e6f28e..26c139b4 100644
--- a/apps/desktop/src/main/data/workspace/migrations.ts
+++ b/apps/desktop/src/main/data/workspace/migrations.ts
@@ -123,11 +123,16 @@ const createMessagesTable: Migration = {
await db.schema
.createTable('messages')
.addColumn('id', 'text', (col) => col.notNull().primaryKey())
- .addColumn('type', 'text', (col) => col.notNull())
+ .addColumn('type', 'integer', (col) =>
+ col
+ .notNull()
+ .generatedAlwaysAs(sql`json_extract(attributes, '$.type')`)
+ .stored()
+ )
.addColumn('parent_id', 'text', (col) => col.notNull())
.addColumn('entry_id', 'text', (col) => col.notNull())
.addColumn('root_id', 'text', (col) => col.notNull())
- .addColumn('content', 'text', (col) => col.notNull())
+ .addColumn('attributes', 'text', (col) => col.notNull())
.addColumn('created_at', 'text', (col) => col.notNull())
.addColumn('created_by', 'text', (col) => col.notNull())
.addColumn('updated_at', 'text')
diff --git a/apps/desktop/src/main/data/workspace/schema.ts b/apps/desktop/src/main/data/workspace/schema.ts
index e3763b4c..fb8fc4cb 100644
--- a/apps/desktop/src/main/data/workspace/schema.ts
+++ b/apps/desktop/src/main/data/workspace/schema.ts
@@ -1,10 +1,10 @@
import {
FileStatus,
FileType,
- MessageType,
MutationType,
EntryType,
WorkspaceRole,
+ MessageType,
} from '@colanode/core';
import { ColumnType, Insertable, Selectable, Updateable } from 'kysely';
@@ -99,11 +99,11 @@ export type UpdateCollaboration = Updateable<CollaborationTable>;
interface MessageTable {
id: ColumnType<string, string, never>;
- type: ColumnType<MessageType, MessageType, MessageType>;
+ type: ColumnType<MessageType, never, never>;
parent_id: ColumnType<string, string, string>;
entry_id: ColumnType<string, string, string>;
root_id: ColumnType<string, string, string>;
- content: ColumnType<string, string, string>;
+ attributes: ColumnType<string, string, string>;
created_at: ColumnType<string, string, never>;
created_by: ColumnType<string, string, never>;
updated_at: ColumnType<string | null, string | null, string | null>;
diff --git a/apps/desktop/src/main/mutations/messages/message-create.ts b/apps/desktop/src/main/mutations/messages/message-create.ts
index d0d2b235..1cfe5740 100644
--- a/apps/desktop/src/main/mutations/messages/message-create.ts
+++ b/apps/desktop/src/main/mutations/messages/message-create.ts
@@ -7,7 +7,8 @@ import {
FileStatus,
generateId,
IdType,
- MessageContent,
+ MessageAttributes,
+ MessageType,
} from '@colanode/core';
import { fileService } from '@/main/services/file-service';
@@ -178,8 +179,10 @@ export class MessageCreateMutationHandler
{} as Record<string, Block>
);
- const messageContent: MessageContent = {
+ const messageAttributes: MessageAttributes = {
+ type: MessageType.Standard,
blocks: blocksRecord,
+ referenceId: input.referenceId,
};
const { createdMessage, createdFiles, createdFileStates } =
@@ -189,11 +192,10 @@ export class MessageCreateMutationHandler
.returningAll()
.values({
id: messageId,
- type: 'standard',
entry_id: input.conversationId,
parent_id: input.conversationId,
root_id: input.rootId,
- content: JSON.stringify(messageContent),
+ attributes: JSON.stringify(messageAttributes),
created_at: createdAt,
created_by: input.userId,
version: 0n,
@@ -206,11 +208,10 @@ export class MessageCreateMutationHandler
const createMessageMutationData: CreateMessageMutationData = {
id: messageId,
- type: 'standard',
entryId: input.conversationId,
parentId: input.conversationId,
rootId: input.rootId,
- content: messageContent,
+ attributes: messageAttributes,
createdAt: createdAt,
};
diff --git a/apps/desktop/src/main/queries/index.ts b/apps/desktop/src/main/queries/index.ts
index 687363e7..6964faa3 100644
--- a/apps/desktop/src/main/queries/index.ts
+++ b/apps/desktop/src/main/queries/index.ts
@@ -5,6 +5,7 @@ import { FileListQueryHandler } from '@/main/queries/files/file-list';
import { FileGetQueryHandler } from '@/main/queries/files/file-get';
import { FileMetadataGetQueryHandler } from '@/main/queries/files/file-metadata-get';
import { IconsGetQueryHandler } from '@/main/queries/icons/icons-get';
+import { MessageGetQueryHandler } from '@/main/queries/messages/message-get';
import { MessageListQueryHandler } from '@/main/queries/messages/message-list';
import { MessageReactionsGetQueryHandler } from '@/main/queries/messages/message-reactions-get';
import { EntryChildrenGetQueryHandler } from '@/main/queries/entries/entry-children-get';
@@ -33,6 +34,7 @@ export const queryHandlerMap: QueryHandlerMap = {
account_list: new AccountListQueryHandler(),
message_list: new MessageListQueryHandler(),
message_reactions_get: new MessageReactionsGetQueryHandler(),
+ message_get: new MessageGetQueryHandler(),
entry_get: new EntryGetQueryHandler(),
record_list: new RecordListQueryHandler(),
server_list: new ServerListQueryHandler(),
diff --git a/apps/desktop/src/main/queries/messages/message-get.ts b/apps/desktop/src/main/queries/messages/message-get.ts
new file mode 100644
index 00000000..04fe0696
--- /dev/null
+++ b/apps/desktop/src/main/queries/messages/message-get.ts
@@ -0,0 +1,87 @@
+import { databaseService } from '@/main/data/database-service';
+import { SelectMessage } from '@/main/data/workspace/schema';
+import { ChangeCheckResult, QueryHandler } from '@/main/types';
+import { mapMessage } from '@/main/utils';
+import { MessageGetQueryInput } from '@/shared/queries/messages/message-get';
+import { Event } from '@/shared/types/events';
+import { MessageNode } from '@/shared/types/messages';
+
+export class MessageGetQueryHandler
+ implements QueryHandler<MessageGetQueryInput>
+{
+ public async handleQuery(
+ input: MessageGetQueryInput
+ ): Promise<MessageNode | null> {
+ const row = await this.fetchMessage(input);
+ return row ? mapMessage(row) : null;
+ }
+
+ public async checkForChanges(
+ event: Event,
+ input: MessageGetQueryInput,
+ _: MessageNode | null
+ ): Promise<ChangeCheckResult<MessageGetQueryInput>> {
+ if (
+ event.type === 'workspace_deleted' &&
+ event.workspace.userId === input.userId
+ ) {
+ return {
+ hasChanges: true,
+ result: null,
+ };
+ }
+
+ if (
+ event.type === 'message_created' &&
+ event.userId === input.userId &&
+ event.message.id === input.messageId
+ ) {
+ return {
+ hasChanges: true,
+ result: event.message,
+ };
+ }
+
+ if (
+ event.type === 'message_updated' &&
+ event.userId === input.userId &&
+ event.message.id === input.messageId
+ ) {
+ return {
+ hasChanges: true,
+ result: event.message,
+ };
+ }
+
+ if (
+ event.type === 'message_deleted' &&
+ event.userId === input.userId &&
+ event.message.id === input.messageId
+ ) {
+ return {
+ hasChanges: true,
+ result: null,
+ };
+ }
+
+ return {
+ hasChanges: false,
+ };
+ }
+
+ private async fetchMessage(
+ input: MessageGetQueryInput
+ ): Promise<SelectMessage | undefined> {
+ const workspaceDatabase = await databaseService.getWorkspaceDatabase(
+ input.userId
+ );
+
+ const row = await workspaceDatabase
+ .selectFrom('messages')
+ .selectAll()
+ .where('id', '=', input.messageId)
+ .executeTakeFirst();
+
+ return row;
+ }
+}
diff --git a/apps/desktop/src/main/services/message-service.ts b/apps/desktop/src/main/services/message-service.ts
index 86ba4ddb..23718a00 100644
--- a/apps/desktop/src/main/services/message-service.ts
+++ b/apps/desktop/src/main/services/message-service.ts
@@ -42,8 +42,7 @@ class MessageService {
.updateTable('messages')
.returningAll()
.set({
- content: JSON.stringify(message.content),
- type: message.type,
+ attributes: JSON.stringify(message.attributes),
updated_at: message.updatedAt,
updated_by: message.updatedBy,
root_id: message.rootId,
@@ -61,7 +60,7 @@ class MessageService {
.where('id', '=', message.id)
.execute();
- const text = extractMessageText(message.id, message.content);
+ const text = extractMessageText(message.id, message.attributes);
if (text) {
await workspaceDatabase
.insertInto('texts')
@@ -89,11 +88,10 @@ class MessageService {
.values({
id: message.id,
version,
- type: message.type,
parent_id: message.entryId,
entry_id: message.entryId,
root_id: message.rootId,
- content: JSON.stringify(message.content),
+ attributes: JSON.stringify(message.attributes),
created_at: message.createdAt,
created_by: message.createdBy,
updated_at: message.updatedAt,
@@ -105,7 +103,7 @@ class MessageService {
return;
}
- const text = extractMessageText(message.id, message.content);
+ const text = extractMessageText(message.id, message.attributes);
if (text) {
await workspaceDatabase
.insertInto('texts')
diff --git a/apps/desktop/src/main/utils.ts b/apps/desktop/src/main/utils.ts
index 21326546..51287f3e 100644
--- a/apps/desktop/src/main/utils.ts
+++ b/apps/desktop/src/main/utils.ts
@@ -298,7 +298,7 @@ export const mapMessage = (row: SelectMessage): MessageNode => {
type: row.type,
parentId: row.parent_id,
rootId: row.root_id,
- content: JSON.parse(row.content),
+ attributes: JSON.parse(row.attributes),
createdAt: row.created_at,
createdBy: row.created_by,
updatedAt: row.updated_at,
diff --git a/apps/desktop/src/renderer/components/messages/message-content.tsx b/apps/desktop/src/renderer/components/messages/message-content.tsx
index cd7ca830..1bfd6bec 100644
--- a/apps/desktop/src/renderer/components/messages/message-content.tsx
+++ b/apps/desktop/src/renderer/components/messages/message-content.tsx
@@ -7,7 +7,7 @@ interface MessageContentProps {
}
export const MessageContent = ({ message }: MessageContentProps) => {
- const nodeBlocks = Object.values(message.content.blocks ?? {});
+ const nodeBlocks = Object.values(message.attributes.blocks ?? {});
const contents = mapBlocksToContents(message.id, nodeBlocks);
return (
diff --git a/apps/desktop/src/renderer/components/messages/message-reference.tsx b/apps/desktop/src/renderer/components/messages/message-reference.tsx
index 4c286636..04a8c98a 100644
--- a/apps/desktop/src/renderer/components/messages/message-reference.tsx
+++ b/apps/desktop/src/renderer/components/messages/message-reference.tsx
@@ -1,35 +1,42 @@
-// import { MessageNode } from '@/shared/types/messages';
-// import { MessageAuthorAvatar } from '@/renderer/components/messages/message-author-avatar';
-// import { MessageAuthorName } from '@/renderer/components/messages/message-author-name';
-// import { MessageContent } from '@/renderer/components/messages/message-content';
-// import { useWorkspace } from '@/renderer/contexts/workspace';
-// import { useQuery } from '@/renderer/hooks/use-query';
+import { MessageAuthorAvatar } from '@/renderer/components/messages/message-author-avatar';
+import { MessageAuthorName } from '@/renderer/components/messages/message-author-name';
+import { MessageContent } from '@/renderer/components/messages/message-content';
+import { useWorkspace } from '@/renderer/contexts/workspace';
+import { useQuery } from '@/renderer/hooks/use-query';
-// interface MessageReferenceProps {
-// messageId: string;
-// }
+interface MessageReferenceProps {
+ messageId: string;
+}
-// export const MessageReference = ({ messageId }: MessageReferenceProps) => {
-// const workspace = useWorkspace();
-// const { data } = useQuery({
-// type: 'node_get',
-// nodeId: messageId,
-// userId: workspace.userId,
-// });
+export const MessageReference = ({ messageId }: MessageReferenceProps) => {
+ const workspace = useWorkspace();
+ const { data, isPending } = useQuery({
+ type: 'message_get',
+ messageId,
+ userId: workspace.userId,
+ });
-// if (!data || data.type !== 'message') {
-// return null;
-// }
+ if (isPending) {
+ return null;
+ }
-// const message = data as MessageNode;
+ if (!data) {
+ return (
+ <div className="flex flex-row gap-2 border-l-4 p-2">
+ <span className="text-sm text-muted-foreground">
+ Message not found or has been deleted
+ </span>
+ </div>
+ );
+ }
-// return (
-// <div className="flex flex-row gap-2 border-l-4 p-2">
-// <MessageAuthorAvatar message={message} className="size-5 mt-1" />
-// <div className='"flex-grow flex-col gap-1'>
-// <MessageAuthorName message={message} />
-// <MessageContent message={message} />
-// </div>
-// </div>
-// );
-// };
+ return (
+ <div className="flex flex-row gap-2 border-l-4 p-2">
+ <MessageAuthorAvatar message={data} className="size-5 mt-1" />
+ <div className='"flex-grow flex-col gap-1'>
+ <MessageAuthorName message={data} />
+ <MessageContent message={data} />
+ </div>
+ </div>
+ );
+};
diff --git a/apps/desktop/src/renderer/components/messages/message.tsx b/apps/desktop/src/renderer/components/messages/message.tsx
index 09c25ee5..43bf1c93 100644
--- a/apps/desktop/src/renderer/components/messages/message.tsx
+++ b/apps/desktop/src/renderer/components/messages/message.tsx
@@ -6,6 +6,7 @@ import { MessageAuthorName } from '@/renderer/components/messages/message-author
import { MessageContent } from '@/renderer/components/messages/message-content';
import { MessageReactions } from '@/renderer/components/messages/message-reactions';
import { MessageTime } from '@/renderer/components/messages/message-time';
+import { MessageReference } from '@/renderer/components/messages/message-reference';
import { useRadar } from '@/renderer/contexts/radar';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { MessageNode } from '@/shared/types/messages';
@@ -65,9 +66,9 @@ export const Message = ({ message, previousMessage }: MessageProps) => {
}}
>
<MessageActions message={message} />
- {/* {message.type === 'reply' && (
- <MessageReference messageId={message.referenceId} />
- )} */}
+ {message.attributes.referenceId && (
+ <MessageReference messageId={message.attributes.referenceId} />
+ )}
<MessageContent message={message} />
<MessageReactions message={message} />
</InView>
diff --git a/apps/desktop/src/shared/queries/messages/message-get.ts b/apps/desktop/src/shared/queries/messages/message-get.ts
new file mode 100644
index 00000000..0f7a6ea7
--- /dev/null
+++ b/apps/desktop/src/shared/queries/messages/message-get.ts
@@ -0,0 +1,16 @@
+import { MessageNode } from '@/shared/types/messages';
+
+export type MessageGetQueryInput = {
+ type: 'message_get';
+ messageId: string;
+ userId: string;
+};
+
+declare module '@/shared/queries' {
+ interface QueryMap {
+ message_get: {
+ input: MessageGetQueryInput;
+ output: MessageNode | null;
+ };
+ }
+}
diff --git a/apps/desktop/src/shared/types/messages.ts b/apps/desktop/src/shared/types/messages.ts
index a2aa579f..49f4be75 100644
--- a/apps/desktop/src/shared/types/messages.ts
+++ b/apps/desktop/src/shared/types/messages.ts
@@ -1,11 +1,11 @@
-import { MessageContent, MessageType } from '@colanode/core';
+import { MessageAttributes, MessageType } from '@colanode/core';
export type MessageNode = {
id: string;
type: MessageType;
parentId: string;
rootId: string;
- content: MessageContent;
+ attributes: MessageAttributes;
createdAt: string;
createdBy: string;
updatedAt: string | null;
diff --git a/apps/server/src/data/migrations.ts b/apps/server/src/data/migrations.ts
index 5d6261f8..e3fe79b5 100644
--- a/apps/server/src/data/migrations.ts
+++ b/apps/server/src/data/migrations.ts
@@ -374,12 +374,14 @@ const createMessagesTable: Migration = {
await db.schema
.createTable('messages')
.addColumn('id', 'varchar(30)', (col) => col.notNull().primaryKey())
- .addColumn('type', 'varchar(30)', (col) => col.notNull())
+ .addColumn('type', 'integer', (col) =>
+ col.generatedAlwaysAs(sql`(attributes->>'type')::INTEGER`).stored()
+ )
.addColumn('parent_id', 'varchar(30)', (col) => col.notNull())
.addColumn('entry_id', 'varchar(30)', (col) => col.notNull())
.addColumn('root_id', 'varchar(30)', (col) => col.notNull())
.addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())
- .addColumn('content', 'text', (col) => col.notNull())
+ .addColumn('attributes', 'jsonb', (col) => col.notNull())
.addColumn('created_at', 'timestamptz', (col) => col.notNull())
.addColumn('created_by', 'varchar(30)', (col) => col.notNull())
.addColumn('updated_at', 'timestamptz')
diff --git a/apps/server/src/data/schema.ts b/apps/server/src/data/schema.ts
index 761ecc21..ab3e3759 100644
--- a/apps/server/src/data/schema.ts
+++ b/apps/server/src/data/schema.ts
@@ -6,6 +6,7 @@ import {
EntryType,
WorkspaceRole,
MessageType,
+ MessageAttributes,
} from '@colanode/core';
import {
ColumnType,
@@ -172,12 +173,12 @@ export type UpdateEntryPath = Updateable<EntryPathTable>;
interface MessageTable {
id: ColumnType<string, string, never>;
- type: ColumnType<MessageType, MessageType, MessageType>;
+ type: ColumnType<MessageType, never, never>;
parent_id: ColumnType<string, string, never>;
entry_id: ColumnType<string, string, never>;
root_id: ColumnType<string, string, never>;
workspace_id: ColumnType<string, string, never>;
- content: ColumnType<string, string, never>;
+ attributes: JSONColumnType<MessageAttributes, string, string>;
created_at: ColumnType<Date, Date, never>;
created_by: ColumnType<string, string, never>;
updated_at: ColumnType<Date | null, Date | null, Date>;
diff --git a/apps/server/src/jobs/embed-message.ts b/apps/server/src/jobs/embed-message.ts
index ca117172..9b234d3e 100644
--- a/apps/server/src/jobs/embed-message.ts
+++ b/apps/server/src/jobs/embed-message.ts
@@ -1,4 +1,5 @@
import { OpenAIEmbeddings } from '@langchain/openai';
+import { extractMessageText } from '@colanode/core';
import { JobHandler } from '@/types/jobs';
import { ChunkingService } from '@/services/chunking-service';
@@ -39,8 +40,13 @@ export const embedMessageHandler: JobHandler<EmbedMessageInput> = async (
}
const chunkingService = new ChunkingService();
- const chunks = await chunkingService.chunkText(message.content);
+ const text = extractMessageText(message.id, message.attributes);
+ if (!text || !text.text) {
+ return;
+ }
+
+ const chunks = await chunkingService.chunkText(text.text);
const embeddings = new OpenAIEmbeddings({
apiKey: configuration.ai.openai.apiKey,
modelName: configuration.ai.openai.embeddingModel,
diff --git a/apps/server/src/services/message-service.ts b/apps/server/src/services/message-service.ts
index 170dac0f..4ee48f51 100644
--- a/apps/server/src/services/message-service.ts
+++ b/apps/server/src/services/message-service.ts
@@ -56,12 +56,11 @@ class MessageService {
.returningAll()
.values({
id: mutation.data.id,
- type: mutation.data.type,
entry_id: mutation.data.entryId,
parent_id: mutation.data.parentId,
root_id: mutation.data.rootId,
workspace_id: root.workspace_id,
- content: JSON.stringify(mutation.data.content),
+ attributes: JSON.stringify(mutation.data.attributes),
created_by: user.id,
created_at: new Date(mutation.data.createdAt),
})
diff --git a/apps/server/src/synchronizers/messages.ts b/apps/server/src/synchronizers/messages.ts
index ededbc7c..0e5b1bdf 100644
--- a/apps/server/src/synchronizers/messages.ts
+++ b/apps/server/src/synchronizers/messages.ts
@@ -58,12 +58,11 @@ export class MessageSynchronizer extends BaseSynchronizer<SyncMessagesInput> {
): SynchronizerOutputMessage<SyncMessagesInput> {
const items: SyncMessageData[] = unsyncedMessages.map((message) => ({
id: message.id,
- type: message.type,
entryId: message.entry_id,
parentId: message.parent_id,
rootId: message.root_id,
workspaceId: message.workspace_id,
- content: JSON.parse(message.content),
+ attributes: message.attributes,
createdAt: message.created_at.toISOString(),
createdBy: message.created_by,
updatedAt: message.updated_at?.toISOString() ?? null,
diff --git a/packages/core/src/lib/texts.ts b/packages/core/src/lib/texts.ts
index 4872d9b9..c2935e76 100644
--- a/packages/core/src/lib/texts.ts
+++ b/packages/core/src/lib/texts.ts
@@ -1,6 +1,6 @@
import { Block } from '../registry/block';
import { EntryAttributes } from '../registry';
-import { MessageContent } from '../types/messages';
+import { MessageAttributes, MessageType } from '../types/messages';
export type TextResult = {
id: string;
@@ -33,13 +33,17 @@ export const extractEntryText = (
export const extractMessageText = (
id: string,
- content: MessageContent
+ attributes: MessageAttributes
): TextResult | undefined => {
- return {
- id,
- name: null,
- text: extractBlockTexts(id, content.blocks),
- };
+ if (attributes.type === MessageType.Standard) {
+ return {
+ id,
+ name: null,
+ text: extractBlockTexts(id, attributes.blocks),
+ };
+ }
+
+ return undefined;
};
const extractBlockTexts = (
diff --git a/packages/core/src/synchronizers/messages.ts b/packages/core/src/synchronizers/messages.ts
index 0850da2c..42ffe68d 100644
--- a/packages/core/src/synchronizers/messages.ts
+++ b/packages/core/src/synchronizers/messages.ts
@@ -1,4 +1,4 @@
-import { MessageContent, MessageType } from '../types/messages';
+import { MessageAttributes } from '../types/messages';
export type SyncMessagesInput = {
type: 'messages';
@@ -7,12 +7,11 @@ export type SyncMessagesInput = {
export type SyncMessageData = {
id: string;
- type: MessageType;
parentId: string;
entryId: string;
rootId: string;
workspaceId: string;
- content: MessageContent;
+ attributes: MessageAttributes;
createdAt: string;
createdBy: string;
updatedAt: string | null;
diff --git a/packages/core/src/types/messages.ts b/packages/core/src/types/messages.ts
index b4bf90e8..c59f2b06 100644
--- a/packages/core/src/types/messages.ts
+++ b/packages/core/src/types/messages.ts
@@ -1,7 +1,13 @@
import { Block } from '../registry/block';
-export type MessageType = 'standard';
+export enum MessageType {
+ Standard = 1,
+}
-export type MessageContent = {
+export type StandardMessageAttributes = {
+ type: MessageType.Standard;
blocks: Record<string, Block>;
+ referenceId?: string | null;
};
+
+export type MessageAttributes = StandardMessageAttributes;
diff --git a/packages/core/src/types/mutations.ts b/packages/core/src/types/mutations.ts
index d034fed1..f6fd9343 100644
--- a/packages/core/src/types/mutations.ts
+++ b/packages/core/src/types/mutations.ts
@@ -1,5 +1,5 @@
import { FileType } from './files';
-import { MessageContent, MessageType } from './messages';
+import { MessageAttributes } from './messages';
import {
LocalCreateTransaction,
LocalDeleteTransaction,
@@ -73,10 +73,9 @@ export type ApplyDeleteTransactionMutation = MutationBase & {
export type CreateMessageMutationData = {
id: string;
- type: MessageType;
entryId: string;
parentId: string;
- content: MessageContent;
+ attributes: MessageAttributes;
rootId: string;
createdAt: string;
};