@sanity/sdk 2.11.0 → 2.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_chunks-es/createGroqSearchFilter.js +10 -5
- package/dist/_chunks-es/createGroqSearchFilter.js.map +1 -1
- package/dist/_chunks-es/version.js +1 -1
- package/dist/index.js +396 -371
- package/dist/index.js.map +1 -1
- package/package.json +12 -12
- package/src/auth/refreshStampedToken.test.ts +2 -2
- package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +116 -0
- package/src/auth/subscribeToStateAndFetchCurrentUser.ts +27 -9
- package/src/document/documentStore.ts +1 -1
- package/src/document/permissions.ts +1 -1
- package/src/document/processActions/create.ts +135 -0
- package/src/document/processActions/delete.ts +100 -0
- package/src/document/processActions/discard.ts +63 -0
- package/src/document/processActions/edit.ts +176 -0
- package/src/document/processActions/processActions.ts +168 -0
- package/src/document/processActions/publish.ts +120 -0
- package/src/document/processActions/shared.ts +47 -0
- package/src/document/processActions/unpublish.ts +85 -0
- package/src/document/processActions.test.ts +1 -1
- package/src/document/reducers.ts +1 -1
- package/src/document/processActions.ts +0 -735
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import {diffValue} from '@sanity/diff-patch'
|
|
2
|
+
import {DocumentId, getDraftId, getPublishedId, getVersionId} from '@sanity/id-utils'
|
|
3
|
+
import {type Mutation, type PatchOperations, type SanityDocument} from '@sanity/types'
|
|
4
|
+
|
|
5
|
+
import {isReleasePerspective} from '../../releases/utils/isReleasePerspective'
|
|
6
|
+
import {type EditDocumentAction} from '../actions'
|
|
7
|
+
import {getId, processMutations} from '../processMutations'
|
|
8
|
+
import {
|
|
9
|
+
ActionError,
|
|
10
|
+
type ActionHandlerContext,
|
|
11
|
+
type ActionHandlerResult,
|
|
12
|
+
checkGrant,
|
|
13
|
+
PermissionActionError,
|
|
14
|
+
} from './shared'
|
|
15
|
+
|
|
16
|
+
export function handleEdit(
|
|
17
|
+
action: EditDocumentAction,
|
|
18
|
+
ctx: ActionHandlerContext,
|
|
19
|
+
): ActionHandlerResult {
|
|
20
|
+
const {transactionId, timestamp, grants, outgoingActions, outgoingMutations} = ctx
|
|
21
|
+
let {base, working} = ctx
|
|
22
|
+
|
|
23
|
+
const documentId = getId(action.documentId)
|
|
24
|
+
|
|
25
|
+
if (action.liveEdit) {
|
|
26
|
+
// Single-document mode (liveEdit or release perspective): edit directly without draft logic
|
|
27
|
+
const userPatches = action.patches?.map((patch) => ({patch: {id: documentId, ...patch}}))
|
|
28
|
+
|
|
29
|
+
// skip this action if there are no associated patches
|
|
30
|
+
if (!userPatches?.length) return {base, working}
|
|
31
|
+
|
|
32
|
+
if (!working[documentId] || !base[documentId]) {
|
|
33
|
+
throw new ActionError({
|
|
34
|
+
documentId,
|
|
35
|
+
transactionId,
|
|
36
|
+
message: `Cannot edit document because it does not exist.`,
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const baseBefore = base[documentId] as SanityDocument
|
|
41
|
+
if (userPatches) {
|
|
42
|
+
base = processMutations({
|
|
43
|
+
documents: base,
|
|
44
|
+
transactionId,
|
|
45
|
+
mutations: userPatches,
|
|
46
|
+
timestamp,
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const baseAfter = base[documentId] as SanityDocument
|
|
51
|
+
const patches = diffValue(baseBefore, baseAfter)
|
|
52
|
+
|
|
53
|
+
const workingBefore = working[documentId] as SanityDocument
|
|
54
|
+
if (!checkGrant(grants.update, workingBefore)) {
|
|
55
|
+
throw new PermissionActionError({
|
|
56
|
+
documentId,
|
|
57
|
+
transactionId,
|
|
58
|
+
message: `You do not have permission to edit document "${documentId}".`,
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const workingMutations = patches.map((patch) => ({patch: {id: documentId, ...patch}}))
|
|
63
|
+
|
|
64
|
+
working = processMutations({
|
|
65
|
+
documents: working,
|
|
66
|
+
transactionId,
|
|
67
|
+
mutations: workingMutations,
|
|
68
|
+
timestamp,
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
// liveEdit documents use the mutation endpoint directly -- we don't send actions
|
|
72
|
+
outgoingMutations.push(...workingMutations)
|
|
73
|
+
return {base, working}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const versionId = isReleasePerspective(action.perspective)
|
|
77
|
+
? getVersionId(DocumentId(documentId), action.perspective.releaseName)
|
|
78
|
+
: undefined
|
|
79
|
+
const draftId = getDraftId(DocumentId(documentId))
|
|
80
|
+
const publishedId = getPublishedId(DocumentId(documentId))
|
|
81
|
+
const patchDocumentId = isReleasePerspective(action.perspective) ? versionId! : draftId
|
|
82
|
+
const userPatches = action.patches?.map((patch) => ({
|
|
83
|
+
patch: {id: patchDocumentId, ...patch},
|
|
84
|
+
}))
|
|
85
|
+
|
|
86
|
+
// skip this action if there are no associated patches
|
|
87
|
+
if (!userPatches?.length) return {base, working}
|
|
88
|
+
|
|
89
|
+
if (isReleasePerspective(action.perspective)) {
|
|
90
|
+
if (!working[versionId!] && !base[versionId!]) {
|
|
91
|
+
throw new ActionError({
|
|
92
|
+
documentId,
|
|
93
|
+
transactionId,
|
|
94
|
+
message: `This document does not exist in the release. Please create it or add it to the release first.`,
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
} else if (
|
|
98
|
+
(!working[draftId] && !working[publishedId]) ||
|
|
99
|
+
(!base[draftId] && !base[publishedId])
|
|
100
|
+
) {
|
|
101
|
+
throw new ActionError({
|
|
102
|
+
documentId,
|
|
103
|
+
transactionId,
|
|
104
|
+
message: `Cannot edit document because it does not exist in draft or published form.`,
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const baseMutations: Mutation[] = []
|
|
109
|
+
// don't create a draft from the published version in a release perspective
|
|
110
|
+
if (!isReleasePerspective(action.perspective) && !base[draftId] && base[publishedId]) {
|
|
111
|
+
// otherwise make a draft from the published version
|
|
112
|
+
baseMutations.push({create: {...base[publishedId], _id: draftId}})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// the above statement and guards should make this never be null or undefined
|
|
116
|
+
const baseBefore = base[patchDocumentId] ?? base[publishedId]
|
|
117
|
+
if (userPatches) {
|
|
118
|
+
baseMutations.push(...userPatches)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
base = processMutations({
|
|
122
|
+
documents: base,
|
|
123
|
+
transactionId,
|
|
124
|
+
mutations: baseMutations,
|
|
125
|
+
timestamp,
|
|
126
|
+
})
|
|
127
|
+
// this one will always be defined because a patch mutation will never
|
|
128
|
+
// delete an input document
|
|
129
|
+
const baseAfter = base[patchDocumentId] as SanityDocument
|
|
130
|
+
const patches = diffValue(baseBefore, baseAfter)
|
|
131
|
+
|
|
132
|
+
const workingMutations: Mutation[] = []
|
|
133
|
+
if (!isReleasePerspective(action.perspective) && !working[draftId] && working[publishedId]) {
|
|
134
|
+
const newDraftFromPublished = {...working[publishedId], _id: draftId}
|
|
135
|
+
|
|
136
|
+
if (!checkGrant(grants.create, newDraftFromPublished)) {
|
|
137
|
+
throw new PermissionActionError({
|
|
138
|
+
documentId,
|
|
139
|
+
transactionId,
|
|
140
|
+
message: `You do not have permission to create a draft for editing this document.`,
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
workingMutations.push({create: newDraftFromPublished})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// the first if statement should make this never be null or undefined
|
|
148
|
+
const workingBefore = working[patchDocumentId] ?? working[publishedId]
|
|
149
|
+
if (!checkGrant(grants.update, workingBefore!)) {
|
|
150
|
+
throw new PermissionActionError({
|
|
151
|
+
documentId,
|
|
152
|
+
transactionId,
|
|
153
|
+
message: `You do not have permission to edit document "${documentId}".`,
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
workingMutations.push(...patches.map((patch) => ({patch: {id: patchDocumentId, ...patch}})))
|
|
157
|
+
|
|
158
|
+
working = processMutations({
|
|
159
|
+
documents: working,
|
|
160
|
+
transactionId,
|
|
161
|
+
mutations: workingMutations,
|
|
162
|
+
timestamp,
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
outgoingMutations.push(...workingMutations)
|
|
166
|
+
outgoingActions.push(
|
|
167
|
+
...patches.map((patch) => ({
|
|
168
|
+
actionType: 'sanity.action.document.edit' as const,
|
|
169
|
+
draftId: patchDocumentId,
|
|
170
|
+
publishedId,
|
|
171
|
+
patch: patch as PatchOperations,
|
|
172
|
+
})),
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
return {base, working}
|
|
176
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import {type Mutation} from '@sanity/types'
|
|
2
|
+
import {type ExprNode} from 'groq-js'
|
|
3
|
+
|
|
4
|
+
import {type DocumentAction} from '../actions'
|
|
5
|
+
import {type Grant} from '../permissions'
|
|
6
|
+
import {type DocumentSet} from '../processMutations'
|
|
7
|
+
import {type HttpAction} from '../reducers'
|
|
8
|
+
import {handleCreate} from './create'
|
|
9
|
+
import {handleDelete} from './delete'
|
|
10
|
+
import {handleDiscard} from './discard'
|
|
11
|
+
import {handleEdit} from './edit'
|
|
12
|
+
import {handlePublish} from './publish'
|
|
13
|
+
import {
|
|
14
|
+
ActionError,
|
|
15
|
+
type ActionHandlerContext,
|
|
16
|
+
type ActionHandlerResult,
|
|
17
|
+
PermissionActionError,
|
|
18
|
+
} from './shared'
|
|
19
|
+
import {handleUnpublish} from './unpublish'
|
|
20
|
+
|
|
21
|
+
export {ActionError, PermissionActionError}
|
|
22
|
+
|
|
23
|
+
interface ProcessActionsOptions {
|
|
24
|
+
/**
|
|
25
|
+
* The ID of this transaction. This will become the resulting `_rev` for all
|
|
26
|
+
* documents affected by changes derived from the current set of actions.
|
|
27
|
+
*/
|
|
28
|
+
transactionId: string
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The actions to apply to the given documents
|
|
32
|
+
*/
|
|
33
|
+
actions: DocumentAction[]
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The set of documents these actions were intended to be applied to. These
|
|
37
|
+
* set of documents should be captured right before a queued action is
|
|
38
|
+
* applied.
|
|
39
|
+
*/
|
|
40
|
+
base: DocumentSet
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The current "working" set of documents. A patch will be created by applying
|
|
44
|
+
* the actions to the base. This patch will then be applied to the working
|
|
45
|
+
* set for conflict resolution. Initially, this value should match the base
|
|
46
|
+
* set.
|
|
47
|
+
*/
|
|
48
|
+
working: DocumentSet
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The timestamp to use for `_updateAt` and other similar timestamps for this
|
|
52
|
+
* transaction
|
|
53
|
+
*/
|
|
54
|
+
timestamp: string
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* the lookup with pre-parsed GROQ expressions
|
|
58
|
+
*/
|
|
59
|
+
grants: Record<Grant, ExprNode>
|
|
60
|
+
|
|
61
|
+
// // TODO: implement initial values from the schema?
|
|
62
|
+
// initialValues?: {[TDocumentType in string]?: {_type: string}}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface ProcessActionsResult {
|
|
66
|
+
/**
|
|
67
|
+
* The resulting document set after the actions have been applied. This is
|
|
68
|
+
* derived from the working documents.
|
|
69
|
+
*/
|
|
70
|
+
working: DocumentSet
|
|
71
|
+
/**
|
|
72
|
+
* The document set before the actions have been applied. This is simply the
|
|
73
|
+
* input of the `working` document set.
|
|
74
|
+
*/
|
|
75
|
+
previous: DocumentSet
|
|
76
|
+
/**
|
|
77
|
+
* The outgoing action that were collected when applying the actions. These
|
|
78
|
+
* are sent to the Actions HTTP API
|
|
79
|
+
*/
|
|
80
|
+
outgoingActions: HttpAction[]
|
|
81
|
+
/**
|
|
82
|
+
* The outgoing mutations that were collected when applying the actions. These
|
|
83
|
+
* are here for debugging purposes.
|
|
84
|
+
*/
|
|
85
|
+
outgoingMutations: Mutation[]
|
|
86
|
+
/**
|
|
87
|
+
* The previous revisions of the given documents before the actions were applied.
|
|
88
|
+
*/
|
|
89
|
+
previousRevs: {[TDocumentId in string]?: string}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Applies the given set of actions to the working set of documents and converts
|
|
94
|
+
* high-level actions into lower-level outgoing mutations/actions that respect
|
|
95
|
+
* the current state of the working documents.
|
|
96
|
+
*
|
|
97
|
+
* Supports a "base" and "working" set of documents to allow actions to be
|
|
98
|
+
* applied on top of a different working set of documents in a 3-way merge
|
|
99
|
+
*
|
|
100
|
+
* Actions are applied to the base set of documents first. The difference
|
|
101
|
+
* between the base before and after is used to create a patch. This patch is
|
|
102
|
+
* then applied to the working set of documents and is set as the outgoing patch
|
|
103
|
+
* sent to the server.
|
|
104
|
+
*/
|
|
105
|
+
export function processActions({
|
|
106
|
+
actions,
|
|
107
|
+
transactionId,
|
|
108
|
+
working: initialWorking,
|
|
109
|
+
base: initialBase,
|
|
110
|
+
timestamp,
|
|
111
|
+
grants,
|
|
112
|
+
}: ProcessActionsOptions): ProcessActionsResult {
|
|
113
|
+
let base: DocumentSet = {...initialBase}
|
|
114
|
+
let working: DocumentSet = {...initialWorking}
|
|
115
|
+
|
|
116
|
+
const outgoingActions: HttpAction[] = []
|
|
117
|
+
const outgoingMutations: Mutation[] = []
|
|
118
|
+
|
|
119
|
+
for (const action of actions) {
|
|
120
|
+
const result = dispatch(action, {
|
|
121
|
+
base,
|
|
122
|
+
working,
|
|
123
|
+
transactionId,
|
|
124
|
+
timestamp,
|
|
125
|
+
grants,
|
|
126
|
+
outgoingActions,
|
|
127
|
+
outgoingMutations,
|
|
128
|
+
})
|
|
129
|
+
base = result.base
|
|
130
|
+
working = result.working
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const previousRevs = Object.fromEntries(
|
|
134
|
+
Object.entries(initialWorking).map(([id, doc]) => [id, doc?._rev]),
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
working,
|
|
139
|
+
outgoingActions,
|
|
140
|
+
outgoingMutations,
|
|
141
|
+
previous: initialWorking,
|
|
142
|
+
previousRevs,
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function dispatch(action: DocumentAction, ctx: ActionHandlerContext): ActionHandlerResult {
|
|
147
|
+
switch (action.type) {
|
|
148
|
+
case 'document.create':
|
|
149
|
+
return handleCreate(action, ctx)
|
|
150
|
+
case 'document.delete':
|
|
151
|
+
return handleDelete(action, ctx)
|
|
152
|
+
case 'document.discard':
|
|
153
|
+
return handleDiscard(action, ctx)
|
|
154
|
+
case 'document.edit':
|
|
155
|
+
return handleEdit(action, ctx)
|
|
156
|
+
case 'document.publish':
|
|
157
|
+
return handlePublish(action, ctx)
|
|
158
|
+
case 'document.unpublish':
|
|
159
|
+
return handleUnpublish(action, ctx)
|
|
160
|
+
default:
|
|
161
|
+
throw new Error(
|
|
162
|
+
`Unknown action type: "${
|
|
163
|
+
// @ts-expect-error invalid input
|
|
164
|
+
action.type
|
|
165
|
+
}". Please contact support if this issue persists.`,
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import {DocumentId, getDraftId, getPublishedId} from '@sanity/id-utils'
|
|
2
|
+
import {type Mutation, type Reference, type SanityDocument} from '@sanity/types'
|
|
3
|
+
|
|
4
|
+
import {isReleasePerspective} from '../../releases/utils/isReleasePerspective'
|
|
5
|
+
import {isDeepEqual} from '../../utils/object'
|
|
6
|
+
import {type PublishDocumentAction} from '../actions'
|
|
7
|
+
import {getId, processMutations} from '../processMutations'
|
|
8
|
+
import {
|
|
9
|
+
ActionError,
|
|
10
|
+
type ActionHandlerContext,
|
|
11
|
+
type ActionHandlerResult,
|
|
12
|
+
checkGrant,
|
|
13
|
+
PermissionActionError,
|
|
14
|
+
} from './shared'
|
|
15
|
+
|
|
16
|
+
export function handlePublish(
|
|
17
|
+
action: PublishDocumentAction,
|
|
18
|
+
ctx: ActionHandlerContext,
|
|
19
|
+
): ActionHandlerResult {
|
|
20
|
+
const {transactionId, timestamp, grants, outgoingActions, outgoingMutations} = ctx
|
|
21
|
+
let {base, working} = ctx
|
|
22
|
+
|
|
23
|
+
const documentId = getId(action.documentId)
|
|
24
|
+
|
|
25
|
+
if (action.liveEdit || isReleasePerspective(action.perspective)) {
|
|
26
|
+
throw new ActionError({
|
|
27
|
+
documentId,
|
|
28
|
+
transactionId,
|
|
29
|
+
message: `Cannot publish this document. Publishing is not supported for liveEdit or version (release) documents.`,
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Standard draft/published logic
|
|
34
|
+
const draftId = getDraftId(DocumentId(documentId))
|
|
35
|
+
const publishedId = getPublishedId(DocumentId(documentId))
|
|
36
|
+
|
|
37
|
+
const workingDraft = working[draftId]
|
|
38
|
+
const baseDraft = base[draftId]
|
|
39
|
+
if (!workingDraft || !baseDraft) {
|
|
40
|
+
throw new ActionError({
|
|
41
|
+
documentId,
|
|
42
|
+
transactionId,
|
|
43
|
+
message: `Cannot publish because no draft version was found for document "${documentId}".`,
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Before proceeding, verify that the working draft is identical to the base draft.
|
|
48
|
+
// TODO: is it enough just to check for the _rev or nah?
|
|
49
|
+
if (!isDeepEqual(workingDraft, baseDraft)) {
|
|
50
|
+
throw new ActionError({
|
|
51
|
+
documentId,
|
|
52
|
+
transactionId,
|
|
53
|
+
message: `Publish aborted: The document has changed elsewhere. Please try again.`,
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const newPublishedFromDraft = {...strengthenOnPublish(workingDraft), _id: publishedId}
|
|
58
|
+
|
|
59
|
+
const mutations: Mutation[] = [{delete: {id: draftId}}, {createOrReplace: newPublishedFromDraft}]
|
|
60
|
+
|
|
61
|
+
if (working[draftId] && !checkGrant(grants.update, working[draftId])) {
|
|
62
|
+
throw new PermissionActionError({
|
|
63
|
+
documentId,
|
|
64
|
+
transactionId,
|
|
65
|
+
message: `Publish failed: You do not have permission to update the draft for "${documentId}".`,
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (working[publishedId] && !checkGrant(grants.update, newPublishedFromDraft)) {
|
|
70
|
+
throw new PermissionActionError({
|
|
71
|
+
documentId,
|
|
72
|
+
transactionId,
|
|
73
|
+
message: `Publish failed: You do not have permission to update the published version of "${documentId}".`,
|
|
74
|
+
})
|
|
75
|
+
} else if (!working[publishedId] && !checkGrant(grants.create, newPublishedFromDraft)) {
|
|
76
|
+
throw new PermissionActionError({
|
|
77
|
+
documentId,
|
|
78
|
+
transactionId,
|
|
79
|
+
message: `Publish failed: You do not have permission to publish a new version of "${documentId}".`,
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
base = processMutations({documents: base, transactionId, mutations, timestamp})
|
|
84
|
+
working = processMutations({documents: working, transactionId, mutations, timestamp})
|
|
85
|
+
|
|
86
|
+
outgoingMutations.push(...mutations)
|
|
87
|
+
outgoingActions.push({
|
|
88
|
+
actionType: 'sanity.action.document.publish',
|
|
89
|
+
draftId,
|
|
90
|
+
publishedId,
|
|
91
|
+
})
|
|
92
|
+
return {base, working}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function strengthenOnPublish(draft: SanityDocument): SanityDocument {
|
|
96
|
+
const isStrengthenReference = (
|
|
97
|
+
value: object,
|
|
98
|
+
): value is Reference & Required<Pick<Reference, '_strengthenOnPublish'>> =>
|
|
99
|
+
'_strengthenOnPublish' in value
|
|
100
|
+
|
|
101
|
+
function strengthen(value: unknown): unknown {
|
|
102
|
+
if (typeof value !== 'object' || !value) return value
|
|
103
|
+
|
|
104
|
+
if (isStrengthenReference(value)) {
|
|
105
|
+
const {_strengthenOnPublish, _weak, ...rest} = value
|
|
106
|
+
return {
|
|
107
|
+
...rest,
|
|
108
|
+
...(_strengthenOnPublish.weak && {_weak: true}),
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (Array.isArray(value)) {
|
|
113
|
+
return value.map(strengthen)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, strengthen(v)]))
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return strengthen(draft) as SanityDocument
|
|
120
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {type Mutation, type SanityDocument} from '@sanity/types'
|
|
2
|
+
import {evaluateSync, type ExprNode} from 'groq-js'
|
|
3
|
+
|
|
4
|
+
import {type Grant} from '../permissions'
|
|
5
|
+
import {type DocumentSet} from '../processMutations'
|
|
6
|
+
import {type HttpAction} from '../reducers'
|
|
7
|
+
|
|
8
|
+
export interface ActionHandlerContext {
|
|
9
|
+
base: DocumentSet
|
|
10
|
+
working: DocumentSet
|
|
11
|
+
transactionId: string
|
|
12
|
+
timestamp: string
|
|
13
|
+
grants: Record<Grant, ExprNode>
|
|
14
|
+
outgoingActions: HttpAction[]
|
|
15
|
+
outgoingMutations: Mutation[]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ActionHandlerResult {
|
|
19
|
+
base: DocumentSet
|
|
20
|
+
working: DocumentSet
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function checkGrant(grantExpr: ExprNode, document: SanityDocument): boolean {
|
|
24
|
+
const value = evaluateSync(grantExpr, {params: {document}})
|
|
25
|
+
return value.type === 'boolean' && value.data
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface ActionErrorOptions {
|
|
29
|
+
message: string
|
|
30
|
+
documentId: string
|
|
31
|
+
transactionId: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Thrown when a precondition for an action failed.
|
|
36
|
+
*/
|
|
37
|
+
export class ActionError extends Error implements ActionErrorOptions {
|
|
38
|
+
documentId!: string
|
|
39
|
+
transactionId!: string
|
|
40
|
+
|
|
41
|
+
constructor(options: ActionErrorOptions) {
|
|
42
|
+
super(options.message)
|
|
43
|
+
Object.assign(this, options)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class PermissionActionError extends ActionError {}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {DocumentId, getDraftId, getPublishedId} from '@sanity/id-utils'
|
|
2
|
+
import {type Mutation, type SanityDocument} from '@sanity/types'
|
|
3
|
+
|
|
4
|
+
import {isReleasePerspective} from '../../releases/utils/isReleasePerspective'
|
|
5
|
+
import {type UnpublishDocumentAction} from '../actions'
|
|
6
|
+
import {getId, processMutations} from '../processMutations'
|
|
7
|
+
import {
|
|
8
|
+
ActionError,
|
|
9
|
+
type ActionHandlerContext,
|
|
10
|
+
type ActionHandlerResult,
|
|
11
|
+
checkGrant,
|
|
12
|
+
PermissionActionError,
|
|
13
|
+
} from './shared'
|
|
14
|
+
|
|
15
|
+
export function handleUnpublish(
|
|
16
|
+
action: UnpublishDocumentAction,
|
|
17
|
+
ctx: ActionHandlerContext,
|
|
18
|
+
): ActionHandlerResult {
|
|
19
|
+
const {transactionId, timestamp, grants, outgoingActions, outgoingMutations} = ctx
|
|
20
|
+
let {base, working} = ctx
|
|
21
|
+
|
|
22
|
+
const documentId = getId(action.documentId)
|
|
23
|
+
|
|
24
|
+
if (action.liveEdit || isReleasePerspective(action.perspective)) {
|
|
25
|
+
throw new ActionError({
|
|
26
|
+
documentId,
|
|
27
|
+
transactionId,
|
|
28
|
+
message: `Cannot unpublish this document. Unpublishing is not supported for liveEdit or version (release) documents.`,
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Standard draft/published or version logic
|
|
33
|
+
const draftId = getDraftId(DocumentId(documentId))
|
|
34
|
+
const publishedId = getPublishedId(DocumentId(documentId))
|
|
35
|
+
|
|
36
|
+
if (!working[publishedId] && !base[publishedId]) {
|
|
37
|
+
throw new ActionError({
|
|
38
|
+
documentId,
|
|
39
|
+
transactionId,
|
|
40
|
+
message: `Cannot unpublish because the document "${documentId}" is not currently published.`,
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const sourceDoc = working[publishedId] ?? (base[publishedId] as SanityDocument)
|
|
45
|
+
const newDraftFromPublished = {...sourceDoc, _id: draftId}
|
|
46
|
+
const mutations: Mutation[] = [
|
|
47
|
+
{delete: {id: publishedId}},
|
|
48
|
+
{createIfNotExists: newDraftFromPublished},
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
if (!checkGrant(grants.update, sourceDoc)) {
|
|
52
|
+
throw new PermissionActionError({
|
|
53
|
+
documentId,
|
|
54
|
+
transactionId,
|
|
55
|
+
message: `You do not have permission to unpublish the document "${documentId}".`,
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!working[draftId] && !checkGrant(grants.create, newDraftFromPublished)) {
|
|
60
|
+
throw new PermissionActionError({
|
|
61
|
+
documentId,
|
|
62
|
+
transactionId,
|
|
63
|
+
message: `You do not have permission to create a draft from the published version of "${documentId}".`,
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
base = processMutations({
|
|
68
|
+
documents: base,
|
|
69
|
+
transactionId,
|
|
70
|
+
mutations: [
|
|
71
|
+
{delete: {id: publishedId}},
|
|
72
|
+
{createIfNotExists: {...(base[publishedId] ?? sourceDoc), _id: draftId}},
|
|
73
|
+
],
|
|
74
|
+
timestamp,
|
|
75
|
+
})
|
|
76
|
+
working = processMutations({documents: working, transactionId, mutations, timestamp})
|
|
77
|
+
|
|
78
|
+
outgoingMutations.push(...mutations)
|
|
79
|
+
outgoingActions.push({
|
|
80
|
+
actionType: 'sanity.action.document.unpublish',
|
|
81
|
+
draftId,
|
|
82
|
+
publishedId,
|
|
83
|
+
})
|
|
84
|
+
return {base, working}
|
|
85
|
+
}
|
|
@@ -3,7 +3,7 @@ import {parse} from 'groq-js'
|
|
|
3
3
|
import {describe, expect, it} from 'vitest'
|
|
4
4
|
|
|
5
5
|
import {type DocumentAction} from './actions'
|
|
6
|
-
import {ActionError, processActions} from './processActions'
|
|
6
|
+
import {ActionError, processActions} from './processActions/processActions'
|
|
7
7
|
import {type DocumentSet} from './processMutations'
|
|
8
8
|
|
|
9
9
|
// Helper: Create a sample document that conforms to SanityDocument.
|
package/src/document/reducers.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {type DocumentAction} from './actions'
|
|
|
11
11
|
import {DOCUMENT_STATE_CLEAR_DELAY} from './documentConstants'
|
|
12
12
|
import {type DocumentState, type DocumentStoreState} from './documentStore'
|
|
13
13
|
import {type RemoteDocument} from './listen'
|
|
14
|
-
import {ActionError, processActions} from './processActions'
|
|
14
|
+
import {ActionError, processActions} from './processActions/processActions'
|
|
15
15
|
import {type DocumentSet} from './processMutations'
|
|
16
16
|
|
|
17
17
|
const EMPTY_REVISIONS: NonNullable<Required<DocumentState['unverifiedRevisions']>> = {}
|