-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
366 lines (319 loc) · 11.5 KB
/
index.js
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
const path = require('path')
module.exports = {
init: ({
keystone,
connectionString,
liveUrl,
previewUrl,
showLiveContentUrl,
publishCheckedByDefault,
forcePublishRegardlessOfStatus
}) => {
let dbConnection
const thisConnection = process.env.MONGO_URI || keystone.get('mongo')
const isLiveDatabase = thisConnection === connectionString
keystone.List.prototype.oldRegister = keystone.List.prototype.register
const connectToDatabase = (connectionString) =>
new Promise((resolve, reject) => {
if (dbConnection) {
resolve(dbConnection)
} else {
// Keep connection alive longer to prevent timeout issues
const option = {
server: {
socketOptions: {
keepAlive: 300000,
connectTimeoutMS: 30000
}
},
replset: {
socketOptions: {
keepAlive: 300000,
connectTimeoutMS: 30000
}
}
}
const db = keystone.mongoose.createConnection(connectionString, option)
db.on('open', err => {
if (err) {
reject(err)
} else {
dbConnection = db
resolve(dbConnection)
}
})
db.on('close', () => { dbConnection = undefined })
}
})
keystone.List.prototype.register = function () {
// Only add fields and hooks if List is not inheriting from
// another List to avoid hooks from being run twice.
if (this.options.inherits) {
this.oldRegister()
return
}
// Make lists on live site view only
(['noedit', 'nocreate', 'nodelete']).forEach(key => {
this.options[key] = this.options[key] || isLiveDatabase
})
const urlPath = this.options.publishable && this.options.publishable.path
const publishByDefault = this.options.publishable && this.options.publishable.publishByDefault
const noUnpublish = this.options.publishable && this.options.publishable.noUnpublish
const trackPublishDate = this.options.publishable && this.options.publishable.trackPublishDate
addFields(this, { urlPath, publishByDefault, noUnpublish, trackPublishDate })
this.oldRegister()
addHooks(this, { urlPath, publishByDefault, trackPublishDate })
}
const addFields = (list, {urlPath, publishByDefault, noUnpublish, trackPublishDate}) => {
list.add(
{ heading: 'Publishing Workflow' },
{
// Statuses
publish__status: {
label: 'Publishing Status',
type: keystone.Field.Types.Select,
options: [
{ value: 'unpublished', label: 'Unpublished' },
{ value: 'published', label: 'Published' },
{ value: 'draft', label: 'Draft' }
],
default: 'unpublished',
noedit: true,
index: true
}
}
)
list.schema.index({ publish__status: 1 })
if (trackPublishDate) {
list.add({
publish__date: {
type: keystone.Field.Types.Date,
label: 'Published Date',
utc: true
},
publish__displayDate: {
type: keystone.Field.Types.Text,
label: 'Displayed Date',
note: 'Use this field to override the autogenerated formatted Published Date (MM/DD/YYYY) on the website. The website will display the date exactly as entered above.'
}
})
list.schema.index({ publish__date: -1 })
list.schema.index({ publish__date: 1 })
}
list.add({
// URLs
publish__previewUrl: {
label: 'Preview URL',
type: keystone.Field.Types.Url,
noedit: true,
hidden: isLiveDatabase || !urlPath || previewUrl === undefined,
dependsOn: { publish__status: ['unpublished', 'draft'] }
},
publish__liveUrl: {
label: 'Live URL',
type: keystone.Field.Types.Url,
noedit: true,
hidden: isLiveDatabase || !urlPath || liveUrl === undefined,
dependsOn: { publish__status: ['published'] }
},
publish__contentUrl: {
label: 'Content URL',
type: keystone.Field.Types.Url,
noedit: true,
hidden: isLiveDatabase || (showLiveContentUrl === false) || liveUrl === undefined,
dependsOn: { publish__status: ['published'] }
},
// Actions
publish__publishOnSave: {
label: 'Publish on save',
type: Boolean,
default: (publishCheckedByDefault === true || publishByDefault === true),
hidden: isLiveDatabase
},
publish__unpublishOnSave: {
label: 'Unpublish on save',
type: Boolean,
dependsOn: { publish__status: 'published', publish__publishOnSave: false },
hidden: isLiveDatabase || noUnpublish
},
publish__rollbackOnSave: {
label: 'Rollback draft to live version',
type: Boolean,
dependsOn: { publish__status: 'draft', publish__publishOnSave: false },
hidden: isLiveDatabase
}
})
}
const addHooks = (list, { urlPath, publishByDefault, trackPublishDate }) => {
const collectionName = list.options.schema.collection
list.schema.pre('save', function (next) {
const doc = this
const getShouldPublish = () => {
if (forcePublishRegardlessOfStatus && (doc.publish__status === 'published' || doc.publish__publishOnSave)) {
return true
}
if (doc.saveWithSameStatus) {
return doc.publish__status === 'published'
}
// If document is new, just create and dont publish.
// When the document is saved, then it will be published if
// publishOnSave is enabled.
return doc.isNew
? false
: doc.publish__publishOnSave
}
const shouldPublish = getShouldPublish()
const shouldUnpublish = doc.publish__unpublishOnSave
const shouldRollback = doc.publish__rollbackOnSave
doc.publish__publishOnSave = (publishCheckedByDefault === true || publishByDefault)
doc.publish__unpublishOnSave = false
doc.publish__rollbackOnSave = false
if (shouldPublish) {
doc.publish__status = 'published'
if (liveUrl) {
doc.publish__contentUrl = getContentUrl(doc)
if (urlPath) {
doc.publish__liveUrl = getLiveUrl(urlPath, doc)
}
}
publish(doc)
.then(next)
.catch(createCatchHandler(next))
} else if (shouldRollback) {
getPublishedDocument(doc)
.then(publishedDoc => {
Object.keys(publishedDoc)
.map(key => { doc[key] = publishedDoc[key] })
})
.then(next)
.catch(createCatchHandler(next))
} else {
if (previewUrl && urlPath) {
doc.publish__previewUrl = getPreviewUrl(urlPath, doc)
}
if (shouldUnpublish) {
doc.publish__status = 'unpublished'
unpublish(doc)
.then(next)
.catch(createCatchHandler(next))
} else {
checkIfDifferentThanLive(doc)
.then((differsFromLive) => {
if (differsFromLive) {
doc.publish__status = 'draft'
}
})
.then(next)
.catch(createCatchHandler(next))
}
}
})
const getContentUrl = (doc) => {
const id = doc._doc._id
return `${liveUrl}/keystone/${list.path}/${id}`
}
const getUrl = (baseUrl) => (urlPath, doc) => {
const regex = /:([\w\-\.]+)/g
const newUrlPath = urlPath.replace(regex, (match, param) => doc[param])
return path.join(baseUrl, newUrlPath)
}
const getLiveUrl = getUrl(liveUrl)
const getPreviewUrl = (urlPath, doc) =>
doc.slug
? getUrl(previewUrl)(urlPath, doc)
: 'Save to generate preview link'
const performDatabaseOperation = (fn) => (doc) =>
connectToDatabase(connectionString)
.then(db => fn(db, doc))
const getPublishedDoc = (doc, db) =>
db.collection(collectionName)
.findOne({ '_id': doc._id })
const publish = performDatabaseOperation((db, doc) =>
getPublishedDoc(doc, db)
.then(_doc => (_doc == null)
? db.collection(collectionName)
.insert(doc)
.then(response => db)
: db.collection(collectionName)
.update({ '_id': doc._id }, doc)
.then(response => db)
)
)
const unpublish = performDatabaseOperation((db, doc) =>
getPublishedDoc(doc, db)
.then(_doc => (_doc == null)
? Promise.resolve(db)
: db.collection(collectionName)
.remove({ '_id': doc._id })
.then(response => db)
)
)
const getPublishedDocument = performDatabaseOperation((db, doc) =>
getPublishedDoc(doc, db)
)
const checkIfDifferentThanLive = performDatabaseOperation((db, doc) =>
getPublishedDoc(doc, db)
.then(_doc => (_doc == null)
? false
: objectsDiffer(doc, _doc)
)
)
const createCatchHandler = next => error => {
console.warn('Error with Database Operation: ', error)
next(new Error('There was an error saving. Please try again.'))
}
const objectsDiffer = (doc, _doc) => {
const current = doc.toObject()
const live = _doc
return !objectEquals(current, live)
}
const objectEquals = (x, y) => {
if (x === null || x === undefined || y === null || y === undefined) {
return x === y
}
// after this just checking type of one would be enough
if (x.constructor !== y.constructor) {
return false
}
// if they are functions, they should exactly refer to same one (because of closures)
if (x instanceof Function) {
return x === y
}
// if they are regexps, they should exactly refer to same one (it is hard to better equality check on current ES)
if (x instanceof RegExp) {
return x === y
}
if (x === y || x.valueOf() === y.valueOf()) {
return true
}
if (Array.isArray(x) && x.length !== y.length) {
return false
}
// if they are dates, they must had equal valueOf
if (x instanceof Date) {
return false
}
// if they are strictly equal, they both need to be object at least
if (!(x instanceof Object)) {
return false
}
if (!(y instanceof Object)) {
return false
}
const excludeFields = item => {
const excludedFields = {
updatedAt: true
}
return !excludedFields[item]
}
// recursive object equality check
const currentKeys = Object.keys(x).filter(excludeFields)
const liveKeys = Object.keys(y).filter(excludeFields)
const hasSameKeys = liveKeys.every(key => currentKeys.indexOf(key) !== -1)
return hasSameKeys && liveKeys.every(key => objectEquals(x[key], y[key]))
}
}
// don't start integration until after connection is set to avoid mass connection creation
return connectToDatabase(connectionString)
}
}