Skip to content

Commit

Permalink
fix(comments): weaken references in content snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanwikner committed Mar 27, 2024
1 parent 78caa54 commit a50bd75
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type CommentIntentGetter,
type CommentPostPayload,
} from '../../types'
import {weakenReferencesInContentSnapshot} from '../../utils'

interface CreateOperationProps {
activeTool: Tool | undefined
Expand Down Expand Up @@ -109,6 +110,12 @@ export async function createOperation(props: CreateOperationProps): Promise<void

const intent = getIntent?.({id: documentId, type: documentType, path: comment.fieldPath})

// If the content snapshot contains a reference, we need to weaken it.
// This prevents Content Lake from validating the references, which could,
// for example, prevent the deletion of the document that the reference
// in the content snapshot points to.
const contentSnapshot = weakenReferencesInContentSnapshot(comment.contentSnapshot)

nextComment = {
_id: commentId,
_type: 'comment',
Expand All @@ -129,7 +136,7 @@ export async function createOperation(props: CreateOperationProps): Promise<void
tool: activeTool?.name || '',
},

contentSnapshot: comment.contentSnapshot,
contentSnapshot,

target: {
documentRevisionId: documentRevisionId || '',
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/structure/comments/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './buildCommentBreadcrumbs'
export * from './buildCommentThreadItems'
export * from './inline-comments'
export * from './mergeCommentReactions'
export * from './weakenReferencesInContentSnapshot'
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {type CommentDocument} from '../types'

function weakenReferences(
node: Partial<Record<string, any>>,
): Partial<Record<string, any>> | undefined {
// Check if the node is directly a reference
if (node && typeof node === 'object' && node.hasOwnProperty('_ref')) {
// Return a new object with the _weak property added
return {...node, _weak: true}
} else if (Array.isArray(node)) {
// Check if the node is an array
// Process each item in the array
return node.map((item) => weakenReferences(item))
} else if (node && typeof node === 'object') {
// For all other objects, create a new object to accumulate the results
const result: Partial<Record<string, any>> = {}

Object.keys(node).forEach((key) => {
const value = node[key]
if (typeof value === 'object' && value !== null) {
// Recursively apply the function to object properties or array items
result[key] = weakenReferences(value)
} else {
// Directly copy other values
result[key] = value
}
})
return result
}

// Return undefined for non-objects
return node
}

export function weakenReferencesInContentSnapshot(
snapshot: CommentDocument['contentSnapshot'],
): CommentDocument['contentSnapshot'] {
if (!snapshot) return snapshot

return Array.isArray(snapshot) ? snapshot.map(weakenReferences) : weakenReferences(snapshot)
}

0 comments on commit a50bd75

Please sign in to comment.