-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathpost-data.js
More file actions
294 lines (242 loc) · 8.54 KB
/
Copy pathpost-data.js
File metadata and controls
294 lines (242 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { isDeepStrictEqual } from "node:util";
import { IndiekitError } from "@indiekit/error";
import { getCanonicalUrl, getDate } from "@indiekit/util";
import makeDebug from "debug";
import { getSyndicateToProperty, normaliseProperties } from "./jf2.js";
import { getPostType } from "./post-type-discovery.js";
import * as updateMf2 from "./update.js";
import { getPostTemplateProperties, renderPath } from "./utils.js";
const debug = makeDebug("indiekit:endpoint-micropub:post-data");
export const postData = {
/**
* Create post data
* @param {object} application - Application configuration
* @param {object} publication - Publication configuration
* @param {object} properties - JF2 properties
* @param {boolean} [isDraftMode] - Draft mode
* @returns {Promise<object>} Post data
*/
async create(application, publication, properties, isDraftMode = false) {
debug(`Create %O`, { isDraftMode, properties });
const { timeZone } = application;
const { me, postTypes, syndicationTargets } = publication;
// Add syndication targets
const syndicateTo = getSyndicateToProperty(properties, syndicationTargets);
if (syndicateTo) {
properties["mp-syndicate-to"] = syndicateTo;
}
// Normalise properties
properties = normaliseProperties(publication, properties, timeZone);
// Post type
const type = getPostType(postTypes, properties);
properties["post-type"] = type;
// Get post type configuration
const typeConfig = postTypes[type];
if (!typeConfig || !typeConfig.post?.path) {
throw IndiekitError.notImplemented(type);
}
// Post paths
const path = await renderPath(
typeConfig.post.path,
properties,
application,
publication,
);
const url = await renderPath(
typeConfig.post.url || typeConfig.post.path,
properties,
application,
publication,
);
properties.url = getCanonicalUrl(url, me);
// Post status
// Draft mode: Only create post with a `draft` post-status
properties["post-status"] = isDraftMode
? "draft"
: properties["post-status"] || "published";
const data = { path, properties };
// Add data to posts collection (or replace existing if present)
const postsCollection = application?.collections?.get("posts");
if (postsCollection) {
const query = { "properties.url": properties.url };
await postsCollection.replaceOne(query, data, { upsert: true });
}
return data;
},
/**
* Read post data
* @param {object} application - Application configuration
* @param {string} url - URL of existing post
* @returns {Promise<object>} Post data
*/
async read(application, url) {
debug(`Read ${url}`);
const query = { "properties.url": url };
const postsCollection = application?.collections?.get("posts");
if (!postsCollection) {
throw IndiekitError.notImplemented(
"Reading, updating, deleting and restoring posts requires a database",
);
}
const data = await postsCollection.findOne(query);
if (!data) {
throw IndiekitError.notFound(url);
}
return data;
},
/**
* Update post data
*
* Add, delete or replace properties and/or replace property values
* @param {object} application - Application configuration
* @param {object} publication - Publication configuration
* @param {string} url - URL of existing post
* @param {object} operation - Requested operation(s)
* @returns {Promise<object>} Post data
*/
async update(application, publication, url, operation) {
debug(`Update ${url} %O`, { operation });
const { timeZone } = application;
const { me, postTypes } = publication;
const postsCollection = application?.collections?.get("posts");
// Read properties
let { path: _originalPath, properties } = await postData.read(
application,
url,
);
// Save incoming properties for later comparison
let oldProperties = structuredClone(properties);
// Add properties
if (operation.add) {
properties = updateMf2.addProperties(properties, operation.add);
}
// Replace property entries
if (operation.replace) {
properties = await updateMf2.replaceEntries(
properties,
operation.replace,
);
}
// Remove properties and/or property entries
if (operation.delete) {
properties = Array.isArray(operation.delete)
? updateMf2.deleteProperties(properties, operation.delete)
: updateMf2.deleteEntries(properties, operation.delete);
}
// Normalise properties
properties = normaliseProperties(publication, properties, timeZone);
oldProperties = normaliseProperties(publication, oldProperties, timeZone);
// Post type
const type = getPostType(postTypes, properties);
const typeConfig = postTypes[type];
properties["post-type"] = type;
// Post paths
const path = await renderPath(
typeConfig.post.path,
properties,
application,
publication,
);
const updatedUrl = await renderPath(
typeConfig.post.url,
properties,
application,
publication,
);
properties.url = getCanonicalUrl(updatedUrl, me);
// Return if no changes to template properties detected
const newProperties = getPostTemplateProperties(properties);
oldProperties = getPostTemplateProperties(oldProperties);
if (isDeepStrictEqual(newProperties, oldProperties)) {
return;
}
// Add updated date
properties.updated = getDate(timeZone);
// Update data in posts collection
const data = { _originalPath, path, properties };
const query = { "properties.url": url };
await postsCollection.replaceOne(query, data);
return data;
},
/**
* Delete post data
*
* Delete (most) properties, keeping a record of deleted for later retrieval
* @param {object} application - Application configuration
* @param {object} publication - Publication configuration
* @param {string} url - URL of existing post
* @returns {Promise<object>} Post data
*/
async delete(application, publication, url) {
debug(`Delete ${url}`);
const { timeZone } = application;
const { postTypes } = publication;
const postsCollection = application?.collections?.get("posts");
// Read properties
const { properties } = await postData.read(application, url);
// Make a copy of existing properties
const _deletedProperties = structuredClone(properties);
// Delete all properties, except those required for path creation
for (const key in _deletedProperties) {
if (!["post-type", "published", "slug", "type", "url"].includes(key)) {
delete properties[key];
}
}
// Add deleted date
properties.deleted = getDate(timeZone);
// Post type
const type = properties["post-type"];
const typeConfig = postTypes[type];
// Post paths
const path = await renderPath(
typeConfig.post.path,
properties,
application,
publication,
);
// Update data in posts collection
const data = { path, properties, _deletedProperties };
const query = { "properties.url": url };
await postsCollection.replaceOne(query, data);
return data;
},
/**
* Undelete post data
*
* Restore previously deleted properties
* @param {object} application - Application configuration
* @param {object} publication - Publication configuration
* @param {string} url - URL of existing post
* @param {boolean} [isDraftMode] - Draft mode
* @returns {Promise<object>} Post data
*/
async undelete(application, publication, url, isDraftMode) {
debug(`Undelete ${url} %O`, { isDraftMode });
const { postTypes } = publication;
const postsCollection = application?.collections?.get("posts");
// Read deleted properties
const { _deletedProperties } = await postData.read(application, url);
// Restore previously deleted properties
const properties = _deletedProperties;
// Post type
const type = properties["post-type"];
const typeConfig = postTypes[type];
// Post paths
const path = await renderPath(
typeConfig.post.path,
properties,
application,
publication,
);
// Post status
// Draft mode: Only restore post with a `draft` post-status
properties["post-status"] = isDraftMode
? "draft"
: properties["post-status"] || "published";
// Update data in posts collection
const data = { path, properties };
const query = { "properties.url": url };
await postsCollection.replaceOne(query, data);
return data;
},
};