Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

feat: comments count realtime

mertmit3d69a0a3e99a
diff --git a/packages/nc-gui/composables/useInfiniteData.ts b/packages/nc-gui/composables/useInfiniteData.ts
index 38ce2860fd..e18d77d9af 100644
--- a/packages/nc-gui/composables/useInfiniteData.ts
+++ b/packages/nc-gui/composables/useInfiniteData.ts
@@ -2,6 +2,7 @@ import type { ComputedRef, Ref } from 'vue'
import {
type Api,
type ColumnType,
+ type CommentPayload,
type DataPayload,
EventType,
type FilterType,
@@ -2027,7 +2028,8 @@ export function useInfiniteData(args: {
}
})
- const activeChannel = ref<string | null>(null)
+ const activeDataChannel = ref<string | null>(null)
+ const activeCommentChannel = ref<string | null>(null)
watch(
meta,
@@ -2035,15 +2037,21 @@ export function useInfiniteData(args: {
if (newMeta?.fk_workspace_id && newMeta?.base_id && newMeta?.id) {
if (oldMeta?.id && oldMeta.id === newMeta.id) return
- if (activeChannel.value) {
- $ncSocket.offMessage(activeChannel.value)
+ if (activeDataChannel.value) {
+ $ncSocket.offMessage(activeDataChannel.value)
}
- activeChannel.value = `${EventType.DATA_EVENT}:${newMeta.fk_workspace_id}:${newMeta.base_id}:${newMeta.id}`
+ if (activeCommentChannel.value) {
+ $ncSocket.offMessage(activeCommentChannel.value)
+ }
+
+ activeDataChannel.value = `${EventType.DATA_EVENT}:${newMeta.fk_workspace_id}:${newMeta.base_id}:${newMeta.id}`
+ activeCommentChannel.value = `${EventType.COMMENT_EVENT}:${newMeta.fk_workspace_id}:${newMeta.base_id}:${newMeta.id}`
- $ncSocket.subscribe(activeChannel.value)
+ $ncSocket.subscribe(activeDataChannel.value)
+ $ncSocket.subscribe(activeCommentChannel.value)
- $ncSocket.onMessage(activeChannel.value, (data: DataPayload) => {
+ $ncSocket.onMessage(activeDataChannel.value, (data: DataPayload) => {
const { id, action, payload, before } = data
if (action === 'add') {
@@ -2321,6 +2329,38 @@ export function useInfiniteData(args: {
}
}
})
+
+ $ncSocket.onMessage(activeCommentChannel.value, (data: CommentPayload) => {
+ const { action, id } = data
+
+ const dataCache = getDataCache()
+
+ let row = null
+
+ for (const [_, cachedRow] of dataCache.cachedRows.value.entries()) {
+ const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
+ if (pk && `${pk}` === `${id}`) {
+ row = cachedRow
+ break
+ }
+ }
+
+ if (row) {
+ if (action === 'add') {
+ if (row) {
+ row.rowMeta.commentCount = (row.rowMeta.commentCount || 0) + 1
+ }
+ } else if (action === 'update') {
+ // Handle updated comment
+ } else if (action === 'delete') {
+ if (row) {
+ row.rowMeta.commentCount = Math.max((row.rowMeta.commentCount || 0) - 1, 0)
+ }
+ }
+
+ callbacks?.syncVisibleData?.()
+ }
+ })
}
},
{ immediate: true },
diff --git a/packages/nocodb-sdk/src/lib/realtime/index.ts b/packages/nocodb-sdk/src/lib/realtime/index.ts
index 2153287311..a6297e3838 100644
--- a/packages/nocodb-sdk/src/lib/realtime/index.ts
+++ b/packages/nocodb-sdk/src/lib/realtime/index.ts
@@ -8,6 +8,7 @@ export enum EventType {
USER_EVENT = 'event-user',
DATA_EVENT = 'event-data',
META_EVENT = 'event-meta',
+ COMMENT_EVENT = 'event-comment',
DASHBOARD_EVENT = 'event-dashboard',
WIDGET_EVENT = 'event-widget',
SCRIPT_EVENT = 'event-script',
@@ -45,6 +46,12 @@ export interface DataPayload extends BaseSocketPayload {
before?: string;
}
+export interface CommentPayload extends BaseSocketPayload {
+ id: string; // rowId
+ action: 'add' | 'update' | 'delete';
+ payload: Record<string, any>;
+}
+
export interface MetaPayload<T = any> extends BaseSocketPayload {
action:
| 'source_create'
@@ -84,7 +91,8 @@ export type SocketEventPayload =
| ConnectionWelcomePayload
| ConnectionErrorPayload
| DataPayload
- | MetaPayload;
+ | MetaPayload
+ | CommentPayload;
// Type mapping for event types to their corresponding payloads
export type SocketEventPayloadMap = {
@@ -93,6 +101,7 @@ export type SocketEventPayloadMap = {
[EventType.DATA_EVENT]: DataPayload;
[EventType.META_EVENT]: MetaPayload;
[EventType.USER_EVENT]: UserEventPayload;
+ [EventType.COMMENT_EVENT]: CommentPayload;
[key: string]: BaseSocketPayload;
};
diff --git a/packages/nocodb/src/services/comments.service.ts b/packages/nocodb/src/services/comments.service.ts
index 6f30f079ec..f6fb59b98b 100644
--- a/packages/nocodb/src/services/comments.service.ts
+++ b/packages/nocodb/src/services/comments.service.ts
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
-import { AppEvents } from 'nocodb-sdk';
+import { AppEvents, EventType } from 'nocodb-sdk';
import { Base, Model } from '../models';
import type {
CommentReqType,
@@ -13,6 +13,7 @@ import { AppHooksService } from '~/services/app-hooks/app-hooks.service';
import Comment from '~/models/Comment';
import { MailService } from '~/services/mail/mail.service';
import { MailEvent } from '~/interface/Mail';
+import NocoSocket from '~/socket/NocoSocket';
@Injectable()
export class CommentsService {
@@ -65,6 +66,20 @@ export class CommentsService {
context,
});
+ NocoSocket.broadcastEvent(
+ context,
+ {
+ event: EventType.COMMENT_EVENT,
+ payload: {
+ action: 'add',
+ payload: res,
+ id: param.body.row_id,
+ },
+ scopes: [model.id],
+ },
+ context.socket_id,
+ );
+
return res;
}
@@ -97,6 +112,21 @@ export class CommentsService {
req: param.req,
context,
});
+
+ NocoSocket.broadcastEvent(
+ context,
+ {
+ event: EventType.COMMENT_EVENT,
+ payload: {
+ action: 'delete',
+ payload: comment,
+ id: comment.row_id,
+ },
+ scopes: [model.id],
+ },
+ context.socket_id,
+ );
+
return res;
}
@@ -177,6 +207,20 @@ export class CommentsService {
context,
});
+ NocoSocket.broadcastEvent(
+ context,
+ {
+ event: EventType.COMMENT_EVENT,
+ payload: {
+ action: 'update',
+ payload: res,
+ id: comment.row_id,
+ },
+ scopes: [model.id],
+ },
+ context.socket_id,
+ );
+
return res;
}
}