-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfeed.js
208 lines (191 loc) · 5.78 KB
/
feed.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
import express from 'express';
import mongoose from 'mongoose';
import { mergeSocialLinks } from '../../frontend/js/helpers/editorHelpers.js';
import { validateGhostWebhook } from '../helpers/ghostAuth.js';
import { updateTagsOnCreate, updateTagsOnEdit } from '../helpers/tags.js';
import { Book } from '../models/book.model.js';
import { ShortformEntry } from '../models/entry.model.js';
import {
FeedEntryCitationNeeded,
FeedEntryReading,
} from '../models/feed/feedEntry.model.js';
import { authenticated } from './auth.js';
const router = express.Router();
router.post('/citationNeeded', validateGhostWebhook, async (req, res) => {
try {
const { post } = req.body;
const result = await new FeedEntryCitationNeeded({
entryType: 'citationNeeded',
title: post.current.title,
slug: post.current.slug,
excerpt: `<p>${post.current.excerpt}</p>`,
image: post.current.feature_image,
imageAlt: post.current.feature_image_alt,
}).save();
res.json(result);
} catch (err) {
console.error(err);
res.status(500).send({ error: err });
}
});
router.get('/book', async (req, res) => {
try {
const { title, author } = req.query;
// Necessary for books where translators/etc. are listed as authors
let firstAuthor = author;
if (author.includes(',')) {
firstAuthor = author.split(',')[0].trim();
}
const bookEntry = await Book.findOne({
title: title,
author: { $regex: firstAuthor },
});
if (bookEntry) {
res.json(bookEntry);
} else {
res.status(404).send({ error: 'Book not found' });
}
} catch (err) {
console.error(err);
res.status(500).send({ error: err });
}
});
router.post(
'/book',
authenticated({ redirectTo: '/micro/login' }),
async function (req, res) {
try {
const postToFeed = req.body.postToFeed;
delete req.body.postToFeed;
if (req.body.tags && req.body.tags.length) {
// Update tags (create new ones if necessary, increment usage frequency)
// Has to happen before the entry is created, or else we don't have the tag IDs to reference
}
// Make or update book record
let bookEntries = await Book.find({
title: req.body.title,
author: req.body.author,
});
let bookEntry;
let bookEntryId;
let tags = [];
if (!bookEntries.length) {
// Create new
bookEntryId = new mongoose.Types.ObjectId();
if (req.body.tags && req.body.tags.length) {
tags = await updateTagsOnCreate(req.body.tags, 'book', true);
}
bookEntry = await new Book({
...req.body,
tags,
_id: bookEntryId,
}).save();
} else {
// Update existing
bookEntry = bookEntries[0];
bookEntryId = bookEntry._id;
if (req.body.tags && req.body.tags.length) {
tags = await updateTagsOnEdit(
bookEntry.tags,
req.body.tags,
'book',
true,
);
}
Object.keys(req.body)
.filter(
(key) => !['_id', 'createdAt', 'updatedAt', '__v'].includes(key),
)
.forEach((key) => {
if (
(Array.isArray(req.body[key]) && req.body[key].length > 0) || // Don't erase arrays if the new array is empty
bookEntry[key] !== req.body[key]
) {
bookEntry[key] = req.body[key];
}
});
await bookEntry.save();
}
// Add entry to feed
let feedEntry;
if (postToFeed) {
feedEntry = await new FeedEntryReading({
book: bookEntryId,
status: req.body.status,
tags,
}).save();
}
res.json([bookEntry, feedEntry]);
} catch (err) {
console.log(err);
res.status(500).send({ error: err });
}
},
);
router.post(
'/bookUpdate',
authenticated({ redirectTo: '/micro/login' }),
async function (req, res) {
try {
const postToFeed = req.body.postToFeed;
delete req.body.postToFeed;
// Get book record
let bookEntryId = req.body._id;
let bookEntry = await Book.findById(bookEntryId);
if (!bookEntry) {
res.status(404).send({ error: 'Book not found' });
return;
}
['status', 'completed'].forEach((key) => {
bookEntry[key] = req.body[key];
});
await bookEntry.save();
// Add entry to feed
let feedEntry;
if (postToFeed) {
feedEntry = await new FeedEntryReading({
book: bookEntryId,
status: req.body.status,
tags: bookEntry.tags,
}).save();
}
res.json([bookEntry, feedEntry]);
} catch (err) {
console.log(err);
res.status(500).send({ error: err });
}
},
);
// Tagger
router.post('/tags/:entryType', authenticated(), async (req, res) => {
let FeedEntryModel;
let ReadingEntryModel;
let category;
if (req.params.entryType === 'citationNeeded') {
FeedEntryModel = FeedEntryCitationNeeded;
category = 'citationNeeded';
} else if (req.params.entryType === 'readingShortform') {
FeedEntryModel = FeedEntryReading;
ReadingEntryModel = ShortformEntry;
category = 'shortform';
}
const { id, tags, socialLinks } = req.body;
let tagIds = null;
const feedEntry = await FeedEntryModel.findById(id);
if (tags && tags.length) {
const tagIds = await updateTagsOnEdit(feedEntry.tags, tags, category);
feedEntry.tags = tagIds;
}
if (socialLinks && Object.keys(socialLinks).length) {
feedEntry.socialLinks = mergeSocialLinks(
feedEntry.socialLinks,
socialLinks,
);
}
const resp = await feedEntry.save();
if (tagIds && ReadingEntryModel) {
await ReadingEntryModel.findByIdAndUpdate(id, { tags: tagIds });
}
res.json(resp);
});
export default router;