Skip to content

AI-native design editor. Open-source Figma alternative.

fix(cloud): sign comments in a Cloud document with the member's account (#1307)

Danila Poyarkov64ae135470f3
diff --git a/src/app/comments/use.ts b/src/app/comments/use.ts
index 30fa04ea8..152d0cbce 100644
--- a/src/app/comments/use.ts
+++ b/src/app/comments/use.ts
@@ -14,6 +14,7 @@ import {
import type { Vector } from '@open-pencil/scene-graph/primitives'
import { useCollabIdentity } from '@/app/collab/identity'
+import { roomForStore } from '@/app/collab/rooms'
import { useActiveEditorStoreRef } from '@/app/editor/active-store'
import type { PagePoint } from '@/app/editor/pages/point'
import { setCommentsOnCanvas } from '@/app/settings/preferences/apply'
@@ -65,9 +66,15 @@ export const useComments = createGlobalState(() => {
})
const { threads, mutate, update, pinPosition } = documentComments
- function me() {
- return { name: identity.name.value, color: identity.color }
- }
+ /**
+ * Who writes here: who the document's tab is in its room, such as a Cloud member under their
+ * account, or this device's own identity outside one.
+ */
+ const me = computed(() => {
+ const store = storeRef.value
+ const own = store ? roomForStore(store)?.identity : undefined
+ return { name: own?.name.value ?? identity.name.value, color: own?.color ?? identity.color }
+ })
function startDraft(at: PagePoint) {
activeThreadId.value = null
@@ -95,7 +102,7 @@ export const useComments = createGlobalState(() => {
const thread = createCommentThread(store.graph, {
pageId: place.pageId,
at: place,
- author: me(),
+ author: me.value,
text: body,
now: now()
})
@@ -114,7 +121,7 @@ export const useComments = createGlobalState(() => {
function reply(threadId: string, text: string) {
const body = text.trim()
if (!body) return
- const entry = createCommentReply(me(), body, now())
+ const entry = createCommentReply(me.value, body, now())
update(threadId, (thread) => replyToCommentThread(thread, entry))
}
@@ -163,7 +170,7 @@ export const useComments = createGlobalState(() => {
activeThreadId,
draft,
onDeleteRequested: deleteRequested.on,
- author: identity.name,
+ author: computed(() => me.value.name),
showOnCanvas,
listQuery,
listShowResolved,
diff --git a/tests/app/comments/session.test.ts b/tests/app/comments/session.test.ts
index e3adc2811..2123167ab 100644
--- a/tests/app/comments/session.test.ts
+++ b/tests/app/comments/session.test.ts
@@ -1,11 +1,21 @@
import 'fake-indexeddb/auto'
import { afterEach, expect, test } from 'bun:test'
+import { shallowRef } from 'vue'
+
import { FigmaAPI } from '@open-pencil/core/figma-api'
import { ALL_TOOLS } from '@open-pencil/core/tools'
-import { readComments, writeComments, type CommentThread } from '@open-pencil/scene-graph'
+import {
+ COMMENT_SORTS,
+ listCommentThreads,
+ readComments,
+ writeComments,
+ type CommentThread
+} from '@open-pencil/scene-graph'
import { executeWithDocumentUndo } from '@/app/automation/execution/editor'
+import { openDocumentRoom, roomForStore, leaveRoom } from '@/app/collab/rooms'
+import type { JoinCollabRoom } from '@/app/collab/transport'
import { useComments } from '@/app/comments/use'
import { setActiveEditorStore } from '@/app/editor/active-store'
import { createEditorStore } from '@/app/editor/session/create'
@@ -148,3 +158,41 @@ test('an agent’s comment is not an undo step either, as comments stay out of u
expect(useComments().threads.value).toHaveLength(1)
expect(store.undo.canUndo).toBe(canUndo)
})
+
+test('a comment is signed by who the tab is in its room, and “Only mine” follows that', () => {
+ const store = openDocument()
+ const color = { r: 0.13, g: 0.59, b: 0.95, a: 1 }
+ // A room that never connects: only who the tab is there matters here.
+ const silent: JoinCollabRoom = () => ({
+ makeAction: () => [() => undefined, () => undefined],
+ onPeerJoin: () => undefined,
+ onPeerLeave: () => undefined,
+ signalingConnected: () => false,
+ discoveryMs: 0,
+ media: null,
+ leave: async () => undefined
+ })
+ openDocumentRoom(store, `cloud:${crypto.randomUUID()}:0`, silent, {
+ name: shallowRef('Grace Hopper'),
+ color
+ })
+ try {
+ // Someone else's thread first, then this tab's own.
+ writeComments(store.graph, [thread('theirs', store.state.currentPageId)])
+ const added = comment(store, 10, 10, 'from the account')
+ expect(added).toMatchObject({ author: 'Grace Hopper', authorColor: color })
+ const mine = listCommentThreads(useComments().threads.value, {
+ query: '',
+ showResolved: true,
+ onlyPage: false,
+ pageId: store.state.currentPageId,
+ onlyMine: true,
+ author: useComments().author.value,
+ sort: COMMENT_SORTS[0]
+ })
+ expect(ids(mine)).toEqual([added.id])
+ } finally {
+ const room = roomForStore(store)
+ if (room) leaveRoom(room)
+ }
+})