powr-sdk-api 4.16.1 → 4.18.0
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/index.js +5 -1
- package/dist/managers/activities.js +154 -0
- package/dist/managers/activities.test.js +154 -0
- package/dist/managers/audit.js +26 -0
- package/dist/managers/audit.test.js +64 -0
- package/dist/managers/index.js +9 -1
- package/dist/routes/activities.js +60 -17
- package/dist/routes/audit.js +80 -0
- package/dist/routes/index.js +6 -1
- package/dist/routes/posts.js +757 -0
- package/dist/utils/eventLog.js +117 -0
- package/dist/utils/eventLog.test.js +120 -0
- package/dist/utils/posts.js +155 -0
- package/dist/utils/posts.test.js +78 -0
- package/package.json +1 -1
|
@@ -0,0 +1,757 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const {
|
|
5
|
+
ObjectId
|
|
6
|
+
} = require("mongodb");
|
|
7
|
+
const {
|
|
8
|
+
getDb
|
|
9
|
+
} = require("../services/mongo");
|
|
10
|
+
const {
|
|
11
|
+
verifyToken
|
|
12
|
+
} = require("../middleware/jwtToken");
|
|
13
|
+
const {
|
|
14
|
+
isPageviewAdmin
|
|
15
|
+
} = require("../utils/track");
|
|
16
|
+
const {
|
|
17
|
+
getPhoneLookupVariants
|
|
18
|
+
} = require("../utils/phone");
|
|
19
|
+
const {
|
|
20
|
+
slugify,
|
|
21
|
+
parsePostWrite,
|
|
22
|
+
parseAuthorWrite,
|
|
23
|
+
publicAuthor,
|
|
24
|
+
publicCard,
|
|
25
|
+
publicPost,
|
|
26
|
+
staffPost,
|
|
27
|
+
authorView
|
|
28
|
+
} = require("../utils/posts");
|
|
29
|
+
const router = express.Router();
|
|
30
|
+
const POSTS = "powrPosts";
|
|
31
|
+
const AUTHORS = "powrAuthors";
|
|
32
|
+
async function callerIsAdmin(db, req) {
|
|
33
|
+
const rawId = String(req.user && req.user.powrId || "");
|
|
34
|
+
let profileAccess;
|
|
35
|
+
let userAccess;
|
|
36
|
+
if (rawId.length === 24 && ObjectId.isValid(rawId)) {
|
|
37
|
+
const oid = new ObjectId(rawId);
|
|
38
|
+
const projectId = String(req.projectId || "");
|
|
39
|
+
const [profile, user] = await Promise.all([db.collection("profiles").findOne({
|
|
40
|
+
userId: {
|
|
41
|
+
$in: [oid, rawId]
|
|
42
|
+
},
|
|
43
|
+
projectId
|
|
44
|
+
}, {
|
|
45
|
+
projection: {
|
|
46
|
+
access: 1
|
|
47
|
+
}
|
|
48
|
+
}), db.collection("users").findOne({
|
|
49
|
+
_id: oid
|
|
50
|
+
}, {
|
|
51
|
+
projection: {
|
|
52
|
+
access: 1
|
|
53
|
+
}
|
|
54
|
+
})]);
|
|
55
|
+
if (profile) profileAccess = profile.access;
|
|
56
|
+
if (user) userAccess = user.access;
|
|
57
|
+
}
|
|
58
|
+
return isPageviewAdmin({
|
|
59
|
+
jwtAccess: req.user && req.user.access,
|
|
60
|
+
profileAccess,
|
|
61
|
+
userAccess
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
const writes = new Map();
|
|
65
|
+
let indexReady = null;
|
|
66
|
+
function ensureIndexes(db) {
|
|
67
|
+
if (!indexReady) {
|
|
68
|
+
indexReady = Promise.all([db.collection(POSTS).createIndex({
|
|
69
|
+
projectId: 1,
|
|
70
|
+
slug: 1
|
|
71
|
+
}, {
|
|
72
|
+
unique: true,
|
|
73
|
+
name: "project_slug"
|
|
74
|
+
}), db.collection(POSTS).createIndex({
|
|
75
|
+
projectId: 1,
|
|
76
|
+
status: 1,
|
|
77
|
+
publishedAt: -1
|
|
78
|
+
}, {
|
|
79
|
+
name: "project_status_published"
|
|
80
|
+
}), db.collection(POSTS).createIndex({
|
|
81
|
+
projectId: 1,
|
|
82
|
+
authorId: 1,
|
|
83
|
+
status: 1
|
|
84
|
+
}, {
|
|
85
|
+
name: "project_author_status"
|
|
86
|
+
}), db.collection(POSTS).createIndex({
|
|
87
|
+
projectId: 1,
|
|
88
|
+
tags: 1
|
|
89
|
+
}, {
|
|
90
|
+
name: "project_tags"
|
|
91
|
+
}), db.collection(AUTHORS).createIndex({
|
|
92
|
+
projectId: 1,
|
|
93
|
+
powrId: 1
|
|
94
|
+
}, {
|
|
95
|
+
unique: true,
|
|
96
|
+
name: "project_user"
|
|
97
|
+
}), db.collection(AUTHORS).createIndex({
|
|
98
|
+
projectId: 1,
|
|
99
|
+
handle: 1
|
|
100
|
+
}, {
|
|
101
|
+
unique: true,
|
|
102
|
+
name: "project_handle"
|
|
103
|
+
})]).catch(error => {
|
|
104
|
+
indexReady = null;
|
|
105
|
+
console.error("posts index:", error.message);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return indexReady;
|
|
109
|
+
}
|
|
110
|
+
function allowWrite(powrId) {
|
|
111
|
+
const now = Date.now();
|
|
112
|
+
const recent = (writes.get(powrId) || []).filter(at => now - at < 60000);
|
|
113
|
+
if (recent.length >= 20) return false;
|
|
114
|
+
recent.push(now);
|
|
115
|
+
writes.set(powrId, recent);
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
function fail(res, error, label) {
|
|
119
|
+
const status = error.statusCode || 500;
|
|
120
|
+
if (status >= 500) console.error(label, error);
|
|
121
|
+
return res.status(status).json({
|
|
122
|
+
success: false,
|
|
123
|
+
message: status >= 500 ? "Something went wrong." : error.message
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function asId(value) {
|
|
127
|
+
const id = String(value || "");
|
|
128
|
+
if (!ObjectId.isValid(id) || id.length !== 24) return null;
|
|
129
|
+
return new ObjectId(id);
|
|
130
|
+
}
|
|
131
|
+
async function roleOf(db, req) {
|
|
132
|
+
const powrId = String(req.user && req.user.powrId || "");
|
|
133
|
+
const projectId = String(req.projectId);
|
|
134
|
+
const author = powrId ? await db.collection(AUTHORS).findOne({
|
|
135
|
+
projectId,
|
|
136
|
+
powrId
|
|
137
|
+
}) : null;
|
|
138
|
+
const admin = await callerIsAdmin(db, req);
|
|
139
|
+
const editor = admin || !!(author && author.role === "editor");
|
|
140
|
+
return {
|
|
141
|
+
powrId,
|
|
142
|
+
projectId,
|
|
143
|
+
author,
|
|
144
|
+
editor,
|
|
145
|
+
writer: editor || !!author
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
async function authorsById(db, projectId, rows) {
|
|
149
|
+
const ids = [...new Set(rows.map(row => row.authorId).filter(Boolean))];
|
|
150
|
+
if (!ids.length) return new Map();
|
|
151
|
+
const authors = await db.collection(AUTHORS).find({
|
|
152
|
+
projectId,
|
|
153
|
+
powrId: {
|
|
154
|
+
$in: ids
|
|
155
|
+
}
|
|
156
|
+
}).toArray();
|
|
157
|
+
return new Map(authors.map(author => [String(author.powrId), author]));
|
|
158
|
+
}
|
|
159
|
+
async function uniqueSlug(db, projectId, base, exceptId) {
|
|
160
|
+
let slug = base;
|
|
161
|
+
for (let n = 2; n < 50; n += 1) {
|
|
162
|
+
const query = {
|
|
163
|
+
projectId,
|
|
164
|
+
slug
|
|
165
|
+
};
|
|
166
|
+
if (exceptId) query._id = {
|
|
167
|
+
$ne: exceptId
|
|
168
|
+
};
|
|
169
|
+
const hit = await db.collection(POSTS).findOne(query, {
|
|
170
|
+
projection: {
|
|
171
|
+
_id: 1
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
if (!hit) return slug;
|
|
175
|
+
slug = `${base.slice(0, 76)}-${n}`;
|
|
176
|
+
}
|
|
177
|
+
const error = new Error("Could not make a free link for this title.");
|
|
178
|
+
error.statusCode = 409;
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
async function findUserId(db, {
|
|
182
|
+
powrId,
|
|
183
|
+
phone
|
|
184
|
+
}) {
|
|
185
|
+
if (powrId) {
|
|
186
|
+
const id = asId(powrId);
|
|
187
|
+
if (!id) {
|
|
188
|
+
const error = new Error("That user id is not valid.");
|
|
189
|
+
error.statusCode = 400;
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
const user = await db.collection("users").findOne({
|
|
193
|
+
_id: id
|
|
194
|
+
}, {
|
|
195
|
+
projection: {
|
|
196
|
+
_id: 1,
|
|
197
|
+
fullName: 1
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
if (!user) {
|
|
201
|
+
const error = new Error("No Powr user with that id.");
|
|
202
|
+
error.statusCode = 404;
|
|
203
|
+
throw error;
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
powrId: String(user._id),
|
|
207
|
+
fullName: user.fullName || ""
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
const variants = getPhoneLookupVariants(phone);
|
|
211
|
+
if (!variants.length) {
|
|
212
|
+
const error = new Error("That phone is not valid.");
|
|
213
|
+
error.statusCode = 400;
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
const user = await db.collection("users").findOne({
|
|
217
|
+
phoneNumber: {
|
|
218
|
+
$in: variants
|
|
219
|
+
}
|
|
220
|
+
}, {
|
|
221
|
+
projection: {
|
|
222
|
+
_id: 1,
|
|
223
|
+
fullName: 1
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
if (!user) {
|
|
227
|
+
const error = new Error("No Powr user with that phone.");
|
|
228
|
+
error.statusCode = 404;
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
powrId: String(user._id),
|
|
233
|
+
fullName: user.fullName || ""
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
router.get("/mine", verifyToken, async (req, res) => {
|
|
237
|
+
try {
|
|
238
|
+
const db = await getDb();
|
|
239
|
+
await ensureIndexes(db);
|
|
240
|
+
const {
|
|
241
|
+
powrId,
|
|
242
|
+
projectId,
|
|
243
|
+
writer
|
|
244
|
+
} = await roleOf(db, req);
|
|
245
|
+
if (!writer) {
|
|
246
|
+
return res.status(403).json({
|
|
247
|
+
success: false,
|
|
248
|
+
message: "You are not a writer on this product."
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
const rows = await db.collection(POSTS).find({
|
|
252
|
+
projectId,
|
|
253
|
+
authorId: powrId
|
|
254
|
+
}).sort({
|
|
255
|
+
updatedAt: -1
|
|
256
|
+
}).limit(100).toArray();
|
|
257
|
+
const authors = await authorsById(db, projectId, rows);
|
|
258
|
+
return res.json({
|
|
259
|
+
success: true,
|
|
260
|
+
data: rows.map(row => staffPost(row, authors.get(String(row.authorId))))
|
|
261
|
+
});
|
|
262
|
+
} catch (error) {
|
|
263
|
+
return fail(res, error, "GET /posts/mine");
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
router.get("/all", verifyToken, async (req, res) => {
|
|
267
|
+
try {
|
|
268
|
+
const db = await getDb();
|
|
269
|
+
await ensureIndexes(db);
|
|
270
|
+
const {
|
|
271
|
+
projectId,
|
|
272
|
+
editor
|
|
273
|
+
} = await roleOf(db, req);
|
|
274
|
+
if (!editor) return res.status(403).json({
|
|
275
|
+
success: false,
|
|
276
|
+
message: "Access denied."
|
|
277
|
+
});
|
|
278
|
+
const status = String(req.query.status || "").trim();
|
|
279
|
+
const query = {
|
|
280
|
+
projectId
|
|
281
|
+
};
|
|
282
|
+
if (status) query.status = status;
|
|
283
|
+
const rows = await db.collection(POSTS).find(query).sort({
|
|
284
|
+
updatedAt: -1
|
|
285
|
+
}).limit(200).toArray();
|
|
286
|
+
const authors = await authorsById(db, projectId, rows);
|
|
287
|
+
return res.json({
|
|
288
|
+
success: true,
|
|
289
|
+
data: rows.map(row => staffPost(row, authors.get(String(row.authorId))))
|
|
290
|
+
});
|
|
291
|
+
} catch (error) {
|
|
292
|
+
return fail(res, error, "GET /posts/all");
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
router.get("/authors", verifyToken, async (req, res) => {
|
|
296
|
+
try {
|
|
297
|
+
const db = await getDb();
|
|
298
|
+
await ensureIndexes(db);
|
|
299
|
+
const {
|
|
300
|
+
projectId,
|
|
301
|
+
editor
|
|
302
|
+
} = await roleOf(db, req);
|
|
303
|
+
if (!editor) return res.status(403).json({
|
|
304
|
+
success: false,
|
|
305
|
+
message: "Access denied."
|
|
306
|
+
});
|
|
307
|
+
const rows = await db.collection(AUTHORS).find({
|
|
308
|
+
projectId
|
|
309
|
+
}).sort({
|
|
310
|
+
displayName: 1
|
|
311
|
+
}).toArray();
|
|
312
|
+
return res.json({
|
|
313
|
+
success: true,
|
|
314
|
+
data: rows.map(authorView)
|
|
315
|
+
});
|
|
316
|
+
} catch (error) {
|
|
317
|
+
return fail(res, error, "GET /posts/authors");
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
router.post("/authors", verifyToken, async (req, res) => {
|
|
321
|
+
try {
|
|
322
|
+
const db = await getDb();
|
|
323
|
+
await ensureIndexes(db);
|
|
324
|
+
const {
|
|
325
|
+
projectId,
|
|
326
|
+
editor
|
|
327
|
+
} = await roleOf(db, req);
|
|
328
|
+
if (!editor) return res.status(403).json({
|
|
329
|
+
success: false,
|
|
330
|
+
message: "Access denied."
|
|
331
|
+
});
|
|
332
|
+
const fields = parseAuthorWrite(req.body || {});
|
|
333
|
+
const user = await findUserId(db, fields);
|
|
334
|
+
const taken = await db.collection(AUTHORS).findOne({
|
|
335
|
+
projectId,
|
|
336
|
+
$or: [{
|
|
337
|
+
powrId: user.powrId
|
|
338
|
+
}, {
|
|
339
|
+
handle: fields.handle
|
|
340
|
+
}]
|
|
341
|
+
});
|
|
342
|
+
if (taken) {
|
|
343
|
+
return res.status(409).json({
|
|
344
|
+
success: false,
|
|
345
|
+
message: "That writer or handle is already on this product."
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
const now = new Date();
|
|
349
|
+
const doc = {
|
|
350
|
+
projectId,
|
|
351
|
+
powrId: user.powrId,
|
|
352
|
+
handle: fields.handle,
|
|
353
|
+
displayName: fields.displayName || user.fullName,
|
|
354
|
+
bio: fields.bio,
|
|
355
|
+
avatarUrl: fields.avatarUrl,
|
|
356
|
+
role: fields.role,
|
|
357
|
+
createdAt: now
|
|
358
|
+
};
|
|
359
|
+
const result = await db.collection(AUTHORS).insertOne(doc);
|
|
360
|
+
return res.status(201).json({
|
|
361
|
+
success: true,
|
|
362
|
+
data: authorView({
|
|
363
|
+
...doc,
|
|
364
|
+
_id: result.insertedId
|
|
365
|
+
})
|
|
366
|
+
});
|
|
367
|
+
} catch (error) {
|
|
368
|
+
return fail(res, error, "POST /posts/authors");
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
router.patch("/authors/:id", verifyToken, async (req, res) => {
|
|
372
|
+
try {
|
|
373
|
+
const db = await getDb();
|
|
374
|
+
const {
|
|
375
|
+
projectId,
|
|
376
|
+
editor
|
|
377
|
+
} = await roleOf(db, req);
|
|
378
|
+
if (!editor) return res.status(403).json({
|
|
379
|
+
success: false,
|
|
380
|
+
message: "Access denied."
|
|
381
|
+
});
|
|
382
|
+
const id = asId(req.params.id);
|
|
383
|
+
if (!id) return res.status(400).json({
|
|
384
|
+
success: false,
|
|
385
|
+
message: "Invalid author id."
|
|
386
|
+
});
|
|
387
|
+
const current = await db.collection(AUTHORS).findOne({
|
|
388
|
+
_id: id,
|
|
389
|
+
projectId
|
|
390
|
+
});
|
|
391
|
+
if (!current) return res.status(404).json({
|
|
392
|
+
success: false,
|
|
393
|
+
message: "Author not found."
|
|
394
|
+
});
|
|
395
|
+
const fields = parseAuthorWrite({
|
|
396
|
+
displayName: req.body.displayName !== undefined ? req.body.displayName : current.displayName,
|
|
397
|
+
handle: req.body.handle !== undefined ? req.body.handle : current.handle,
|
|
398
|
+
role: req.body.role !== undefined ? req.body.role : current.role,
|
|
399
|
+
bio: req.body.bio !== undefined ? req.body.bio : current.bio,
|
|
400
|
+
avatarUrl: req.body.avatarUrl !== undefined ? req.body.avatarUrl : current.avatarUrl,
|
|
401
|
+
powrId: current.powrId
|
|
402
|
+
});
|
|
403
|
+
if (fields.handle !== current.handle) {
|
|
404
|
+
const clash = await db.collection(AUTHORS).findOne({
|
|
405
|
+
projectId,
|
|
406
|
+
handle: fields.handle,
|
|
407
|
+
_id: {
|
|
408
|
+
$ne: id
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
if (clash) return res.status(409).json({
|
|
412
|
+
success: false,
|
|
413
|
+
message: "That handle is taken."
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
const next = {
|
|
417
|
+
handle: fields.handle,
|
|
418
|
+
displayName: fields.displayName,
|
|
419
|
+
bio: fields.bio,
|
|
420
|
+
avatarUrl: fields.avatarUrl,
|
|
421
|
+
role: fields.role
|
|
422
|
+
};
|
|
423
|
+
await db.collection(AUTHORS).updateOne({
|
|
424
|
+
_id: id,
|
|
425
|
+
projectId
|
|
426
|
+
}, {
|
|
427
|
+
$set: next
|
|
428
|
+
});
|
|
429
|
+
return res.json({
|
|
430
|
+
success: true,
|
|
431
|
+
data: authorView({
|
|
432
|
+
...current,
|
|
433
|
+
...next
|
|
434
|
+
})
|
|
435
|
+
});
|
|
436
|
+
} catch (error) {
|
|
437
|
+
return fail(res, error, "PATCH /posts/authors/:id");
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
router.delete("/authors/:id", verifyToken, async (req, res) => {
|
|
441
|
+
try {
|
|
442
|
+
const db = await getDb();
|
|
443
|
+
const {
|
|
444
|
+
projectId,
|
|
445
|
+
editor
|
|
446
|
+
} = await roleOf(db, req);
|
|
447
|
+
if (!editor) return res.status(403).json({
|
|
448
|
+
success: false,
|
|
449
|
+
message: "Access denied."
|
|
450
|
+
});
|
|
451
|
+
const id = asId(req.params.id);
|
|
452
|
+
if (!id) return res.status(400).json({
|
|
453
|
+
success: false,
|
|
454
|
+
message: "Invalid author id."
|
|
455
|
+
});
|
|
456
|
+
const result = await db.collection(AUTHORS).deleteOne({
|
|
457
|
+
_id: id,
|
|
458
|
+
projectId
|
|
459
|
+
});
|
|
460
|
+
if (!result.deletedCount) return res.status(404).json({
|
|
461
|
+
success: false,
|
|
462
|
+
message: "Author not found."
|
|
463
|
+
});
|
|
464
|
+
return res.json({
|
|
465
|
+
success: true
|
|
466
|
+
});
|
|
467
|
+
} catch (error) {
|
|
468
|
+
return fail(res, error, "DELETE /posts/authors/:id");
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
router.get("/authors/:handle", async (req, res) => {
|
|
472
|
+
try {
|
|
473
|
+
const db = await getDb();
|
|
474
|
+
await ensureIndexes(db);
|
|
475
|
+
const projectId = String(req.projectId);
|
|
476
|
+
const handle = slugify(req.params.handle);
|
|
477
|
+
const author = await db.collection(AUTHORS).findOne({
|
|
478
|
+
projectId,
|
|
479
|
+
handle
|
|
480
|
+
});
|
|
481
|
+
if (!author) return res.status(404).json({
|
|
482
|
+
success: false,
|
|
483
|
+
message: "Author not found."
|
|
484
|
+
});
|
|
485
|
+
const rows = await db.collection(POSTS).find({
|
|
486
|
+
projectId,
|
|
487
|
+
authorId: author.powrId,
|
|
488
|
+
status: "published"
|
|
489
|
+
}, {
|
|
490
|
+
projection: {
|
|
491
|
+
bodyMd: 0
|
|
492
|
+
}
|
|
493
|
+
}).sort({
|
|
494
|
+
publishedAt: -1
|
|
495
|
+
}).limit(50).toArray();
|
|
496
|
+
return res.json({
|
|
497
|
+
success: true,
|
|
498
|
+
data: {
|
|
499
|
+
author: publicAuthor(author),
|
|
500
|
+
posts: rows.map(row => publicCard(row, author))
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
} catch (error) {
|
|
504
|
+
return fail(res, error, "GET /posts/authors/:handle");
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
router.get("/", async (req, res) => {
|
|
508
|
+
try {
|
|
509
|
+
const db = await getDb();
|
|
510
|
+
await ensureIndexes(db);
|
|
511
|
+
const projectId = String(req.projectId);
|
|
512
|
+
const limit = Math.min(50, Math.max(1, Number(req.query.limit) || 20));
|
|
513
|
+
const page = Math.max(1, Number(req.query.page) || 1);
|
|
514
|
+
const query = {
|
|
515
|
+
projectId,
|
|
516
|
+
status: "published"
|
|
517
|
+
};
|
|
518
|
+
const tag = String(req.query.tag || "").trim().toLowerCase();
|
|
519
|
+
if (tag) query.tags = tag;
|
|
520
|
+
const handle = String(req.query.author || "").trim();
|
|
521
|
+
if (handle) {
|
|
522
|
+
const author = await db.collection(AUTHORS).findOne({
|
|
523
|
+
projectId,
|
|
524
|
+
handle: slugify(handle)
|
|
525
|
+
});
|
|
526
|
+
if (!author) return res.json({
|
|
527
|
+
success: true,
|
|
528
|
+
data: [],
|
|
529
|
+
page,
|
|
530
|
+
limit
|
|
531
|
+
});
|
|
532
|
+
query.authorId = author.powrId;
|
|
533
|
+
}
|
|
534
|
+
const rows = await db.collection(POSTS).find(query, {
|
|
535
|
+
projection: {
|
|
536
|
+
bodyMd: 0
|
|
537
|
+
}
|
|
538
|
+
}).sort({
|
|
539
|
+
publishedAt: -1,
|
|
540
|
+
_id: -1
|
|
541
|
+
}).skip((page - 1) * limit).limit(limit).toArray();
|
|
542
|
+
const authors = await authorsById(db, projectId, rows);
|
|
543
|
+
return res.json({
|
|
544
|
+
success: true,
|
|
545
|
+
page,
|
|
546
|
+
limit,
|
|
547
|
+
data: rows.map(row => publicCard(row, authors.get(String(row.authorId))))
|
|
548
|
+
});
|
|
549
|
+
} catch (error) {
|
|
550
|
+
return fail(res, error, "GET /posts");
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
router.get("/:slug", async (req, res) => {
|
|
554
|
+
try {
|
|
555
|
+
const db = await getDb();
|
|
556
|
+
const projectId = String(req.projectId);
|
|
557
|
+
const row = await db.collection(POSTS).findOne({
|
|
558
|
+
projectId,
|
|
559
|
+
slug: slugify(req.params.slug),
|
|
560
|
+
status: "published"
|
|
561
|
+
});
|
|
562
|
+
if (!row) return res.status(404).json({
|
|
563
|
+
success: false,
|
|
564
|
+
message: "Post not found."
|
|
565
|
+
});
|
|
566
|
+
const author = row.authorId ? await db.collection(AUTHORS).findOne({
|
|
567
|
+
projectId,
|
|
568
|
+
powrId: String(row.authorId)
|
|
569
|
+
}) : null;
|
|
570
|
+
return res.json({
|
|
571
|
+
success: true,
|
|
572
|
+
data: publicPost(row, author)
|
|
573
|
+
});
|
|
574
|
+
} catch (error) {
|
|
575
|
+
return fail(res, error, "GET /posts/:slug");
|
|
576
|
+
}
|
|
577
|
+
});
|
|
578
|
+
router.post("/", verifyToken, async (req, res) => {
|
|
579
|
+
try {
|
|
580
|
+
const db = await getDb();
|
|
581
|
+
await ensureIndexes(db);
|
|
582
|
+
const access = await roleOf(db, req);
|
|
583
|
+
if (!access.writer) {
|
|
584
|
+
return res.status(403).json({
|
|
585
|
+
success: false,
|
|
586
|
+
message: "You are not a writer on this product."
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
if (!allowWrite(access.powrId)) {
|
|
590
|
+
return res.status(429).json({
|
|
591
|
+
success: false,
|
|
592
|
+
message: "Slow down a moment, then save again."
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
const fields = parsePostWrite(req.body || {});
|
|
596
|
+
if (!fields.title) {
|
|
597
|
+
const error = new Error("Title is required.");
|
|
598
|
+
error.statusCode = 400;
|
|
599
|
+
throw error;
|
|
600
|
+
}
|
|
601
|
+
const slug = await uniqueSlug(db, access.projectId, fields.slug || slugify(fields.title));
|
|
602
|
+
const now = new Date();
|
|
603
|
+
const doc = {
|
|
604
|
+
projectId: access.projectId,
|
|
605
|
+
slug,
|
|
606
|
+
title: fields.title,
|
|
607
|
+
description: fields.description || "",
|
|
608
|
+
coverUrl: fields.coverUrl || "",
|
|
609
|
+
bodyMd: fields.bodyMd || "",
|
|
610
|
+
tags: fields.tags || [],
|
|
611
|
+
readMinutes: fields.readMinutes || 1,
|
|
612
|
+
authorId: access.powrId,
|
|
613
|
+
status: "draft",
|
|
614
|
+
slugLocked: false,
|
|
615
|
+
publishedAt: null,
|
|
616
|
+
createdAt: now,
|
|
617
|
+
updatedAt: now
|
|
618
|
+
};
|
|
619
|
+
const result = await db.collection(POSTS).insertOne(doc);
|
|
620
|
+
const author = access.author;
|
|
621
|
+
return res.status(201).json({
|
|
622
|
+
success: true,
|
|
623
|
+
data: staffPost({
|
|
624
|
+
...doc,
|
|
625
|
+
_id: result.insertedId
|
|
626
|
+
}, author)
|
|
627
|
+
});
|
|
628
|
+
} catch (error) {
|
|
629
|
+
return fail(res, error, "POST /posts");
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
router.patch("/:id", verifyToken, async (req, res) => {
|
|
633
|
+
try {
|
|
634
|
+
const db = await getDb();
|
|
635
|
+
const access = await roleOf(db, req);
|
|
636
|
+
if (!allowWrite(access.powrId)) {
|
|
637
|
+
return res.status(429).json({
|
|
638
|
+
success: false,
|
|
639
|
+
message: "Slow down a moment, then save again."
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
const id = asId(req.params.id);
|
|
643
|
+
if (!id) return res.status(400).json({
|
|
644
|
+
success: false,
|
|
645
|
+
message: "Invalid post id."
|
|
646
|
+
});
|
|
647
|
+
const current = await db.collection(POSTS).findOne({
|
|
648
|
+
_id: id,
|
|
649
|
+
projectId: access.projectId
|
|
650
|
+
});
|
|
651
|
+
if (!current) return res.status(404).json({
|
|
652
|
+
success: false,
|
|
653
|
+
message: "Post not found."
|
|
654
|
+
});
|
|
655
|
+
const owns = String(current.authorId) === access.powrId;
|
|
656
|
+
if (!owns && !access.editor) {
|
|
657
|
+
return res.status(403).json({
|
|
658
|
+
success: false,
|
|
659
|
+
message: "Access denied."
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
if (current.status === "published" && !access.editor) {
|
|
663
|
+
return res.status(403).json({
|
|
664
|
+
success: false,
|
|
665
|
+
message: "A published post is edited by an editor."
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
const fields = parsePostWrite(req.body || {}, true);
|
|
669
|
+
if (fields.slug && (current.slugLocked || current.status === "published")) delete fields.slug;
|
|
670
|
+
if (fields.slug && fields.slug !== current.slug) {
|
|
671
|
+
fields.slug = await uniqueSlug(db, access.projectId, fields.slug, id);
|
|
672
|
+
}
|
|
673
|
+
fields.updatedAt = new Date();
|
|
674
|
+
await db.collection(POSTS).updateOne({
|
|
675
|
+
_id: id
|
|
676
|
+
}, {
|
|
677
|
+
$set: fields
|
|
678
|
+
});
|
|
679
|
+
const author = await db.collection(AUTHORS).findOne({
|
|
680
|
+
projectId: access.projectId,
|
|
681
|
+
powrId: String(current.authorId)
|
|
682
|
+
});
|
|
683
|
+
return res.json({
|
|
684
|
+
success: true,
|
|
685
|
+
data: staffPost({
|
|
686
|
+
...current,
|
|
687
|
+
...fields
|
|
688
|
+
}, author)
|
|
689
|
+
});
|
|
690
|
+
} catch (error) {
|
|
691
|
+
return fail(res, error, "PATCH /posts/:id");
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
async function setStatus(req, res, nextStatus) {
|
|
695
|
+
const db = await getDb();
|
|
696
|
+
const access = await roleOf(db, req);
|
|
697
|
+
const id = asId(req.params.id);
|
|
698
|
+
if (!id) return res.status(400).json({
|
|
699
|
+
success: false,
|
|
700
|
+
message: "Invalid post id."
|
|
701
|
+
});
|
|
702
|
+
const current = await db.collection(POSTS).findOne({
|
|
703
|
+
_id: id,
|
|
704
|
+
projectId: access.projectId
|
|
705
|
+
});
|
|
706
|
+
if (!current) return res.status(404).json({
|
|
707
|
+
success: false,
|
|
708
|
+
message: "Post not found."
|
|
709
|
+
});
|
|
710
|
+
const owns = String(current.authorId) === access.powrId;
|
|
711
|
+
if (nextStatus === "in_review") {
|
|
712
|
+
if (!owns) return res.status(403).json({
|
|
713
|
+
success: false,
|
|
714
|
+
message: "Access denied."
|
|
715
|
+
});
|
|
716
|
+
if (current.status !== "draft") {
|
|
717
|
+
return res.status(400).json({
|
|
718
|
+
success: false,
|
|
719
|
+
message: "Only a draft can be sent for review."
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
} else if (!access.editor) {
|
|
723
|
+
return res.status(403).json({
|
|
724
|
+
success: false,
|
|
725
|
+
message: "Access denied."
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
const patch = {
|
|
729
|
+
status: nextStatus,
|
|
730
|
+
updatedAt: new Date()
|
|
731
|
+
};
|
|
732
|
+
if (nextStatus === "published") {
|
|
733
|
+
patch.slugLocked = true;
|
|
734
|
+
if (!current.publishedAt) patch.publishedAt = patch.updatedAt;
|
|
735
|
+
}
|
|
736
|
+
await db.collection(POSTS).updateOne({
|
|
737
|
+
_id: id
|
|
738
|
+
}, {
|
|
739
|
+
$set: patch
|
|
740
|
+
});
|
|
741
|
+
const author = await db.collection(AUTHORS).findOne({
|
|
742
|
+
projectId: access.projectId,
|
|
743
|
+
powrId: String(current.authorId)
|
|
744
|
+
});
|
|
745
|
+
return res.json({
|
|
746
|
+
success: true,
|
|
747
|
+
data: staffPost({
|
|
748
|
+
...current,
|
|
749
|
+
...patch
|
|
750
|
+
}, author)
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
router.post("/:id/submit", verifyToken, (req, res) => setStatus(req, res, "in_review").catch(error => fail(res, error, "POST /posts/:id/submit")));
|
|
754
|
+
router.post("/:id/publish", verifyToken, (req, res) => setStatus(req, res, "published").catch(error => fail(res, error, "POST /posts/:id/publish")));
|
|
755
|
+
router.post("/:id/unpublish", verifyToken, (req, res) => setStatus(req, res, "draft").catch(error => fail(res, error, "POST /posts/:id/unpublish")));
|
|
756
|
+
router.post("/:id/archive", verifyToken, (req, res) => setStatus(req, res, "archived").catch(error => fail(res, error, "POST /posts/:id/archive")));
|
|
757
|
+
module.exports = router;
|