-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.js
321 lines (237 loc) · 8.24 KB
/
controllers.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
const slugify = require('slugify');
const jwt = require('jsonwebtoken');
const showdown = require('showdown');
const converter = new showdown.Converter({ 'noHeaderId': true });
// const functions = require('./views/functions.js');
const db = require('./db.js');
const User = require('./models/User');
const Entry = require('./models/Post'); // 🟠 When the database is rebuilt, change to models/Entry
const { fixHtmlTags, limit, maxAge } = require('./util');
const { ppid } = require('process'); // can't remove even though it appears to not be in use
const e = require('express'); // is this still needed?
/*
* LOCAL METHODS
*/
const createToken = id => { // 🟠 why can't this work from util.js?
return jwt.sign({ id }, 'net ninja secret', { // 🟠 fix secret & make more secure
expiresIn: maxAge
});
}
/*
* EXPORTED METHODS
*/
// * GET LIST OF RECENT ARTICLES
module.exports.getListByPubDate = async (req, res) => {
// css
res.locals.css = 'list';
res.locals.type = 'list';
// data for list pagination
res.locals.page = parseInt(req.params.page) || 1;
const docs = await db.getEntryCount(null, res.locals.user);
const skip = (res.locals.page * limit) - limit;
res.locals.pages = parseInt(Math.ceil(docs / limit));
// queries
res.locals.entries = await db.getListOfEntriesByDate( skip );
res.locals.topics = await db.getCategories(res.locals.user);
res.locals.archive = await db.getArchive(res.locals.user);
// disabled items
res.locals.adjacentEntries = null;
res.locals.publish = true;
res.locals.requestedTag = null;
res.render('page');
}
// * GET LIST OF UNPUBLISHED ARTICLES
module.exports.getListUnpublished = async (req, res) => {
// data for sidebar
res.locals.topics = await db.getCategories(res.locals.user);
res.locals.archive = await db.getArchive(res.locals.user);
// css
res.locals.css = 'list';
res.locals.type = 'list';
// entry data
res.locals.entries = await db.getListOfUnpublishedEntries();
// disabled items
res.locals.pages = 0;
res.locals.page = 0;
res.locals.adjacentEntries = null;
res.locals.publish = false;
res.locals.requestedTag = null;
res.render('page');
}
// * GET ARTICLE LIST BASED ON A TOPIC
module.exports.getListByTag = async (req, res) => {
//
res.locals.css = 'list';
res.locals.type = 'list';
// page elements
res.locals.topics = await db.getCategories(res.locals.user);
res.locals.archive = await db.getArchive(res.locals.user);
res.locals.requestedTag = req.params.tag;
// pageination
res.locals.page = parseInt(req.params.page) || 1;
const docs = await db.getEntryCount(req.params.tag, res.locals.user);
const skip = (res.locals.page * limit) - limit;
res.locals.pages = parseInt(Math.ceil(docs / limit));
// entry data
res.locals.entries = await db.getListOfEntriesByCategory(req.params.tag, res.locals.user);
// enabled items
res.locals.publish = true;
// disabled items
res.locals.adjacentEntries = null;
res.render('page');
}
// * OPEN ARTICLES IN READER
module.exports.getEntry = async (req, res) => {
// page elements
res.locals.topics = await db.getCategories(res.locals.user);
res.locals.archive = await db.getArchive(res.locals.user);
// rendering variables
res.locals.css = 'reader';
res.locals.type = 'reader';
// retrieve chosen entry to read
const { slug = null, _id } = req.params;
res.locals.entry = await db.getOneEntry( slug, _id );
res.locals.entry.HTML = converter.makeHtml(res.locals.entry.markdown);
// pagination
res.locals.pagination = await db.getAdjacents(res.locals.entry.pubDate);
// disabled items
res.locals.page = null;
res.locals.pages = null;
res.locals.requestedTag = null
res.locals.preview = false;
console.log("🔸 res.locals.pagination:\n", res.locals.pagination); //🔴
res.render('page');
}
// * OPEN ARTICLE IN EDITOR OR SERVE EMPTY EDITOR
module.exports.getEditor = async (req, res) => {
// page elements
res.locals.topics = await db.getCategories(res.locals.user);
res.locals.archive = await db.getArchive(res.locals.user);
// rendering variavles
res.locals.css = 'editor';
res.locals.type = 'editor';
// chosen entry to edit or new entry
res.locals.entry = {};
const { slug } = req.params;
if(slug){
const entry = await db.getOneEntry(slug);
// body
entry.HTML = converter.makeHtml(entry.markdown); // 🔸 move to util and add to scrubbing HTML
// entry reader to render 🔸 is the seaparate entry variable still needed?
res.locals.entry = entry;
// disabled items
res.locals.pagination = { next: null, previous: null };
}
res.render('page');
}
// * SAVE NEW OR EXISTING ENTRIES
module.exports.postEntry = async (req, res) => {
// HANDLE ENTRY AND DB
const entry = req.body;
const { tags } = req.body;
entry.slug = slugify(entry.title, { lower: true });
if(entry.entryID) {
entry.id = entry.entryID;
delete entry.entryID;
}
// prep date format
entry.pubDate = !entry.datePicker || !entry.timePicker ? "" :
new Date(`${entry.datePicker}T${entry.timePicker}`);
// prep
entry.tags = tags
.split(",")
.filter(tags => tags.trim() !== "")
.map(tags => tags.trim());
const result = await db.addOrUpdateEntry(entry);
res.send((result.message));
}
// * GET HTML FOR EDITOR PREVIEW
module.exports.getEditorPreview = async (req, res) => {
res.locals.entry = req.body;
res.locals.preview = true;
// * PREP DATA
res.locals.entry.slug = slugify(res.locals.entry.title, { lower: true });
const tags = res.locals.entry.tags;
// prep date format
res.locals.entry.pubDate = !res.locals.entry.datePicker || !res.locals.entry.timePicker ? "" :
new Date(`${res.locals.entry.datePicker}T${res.locals.entry.timePicker}`);
res.locals.entry.tags = Array.isArray(tags) ? tags : tags.split(",").map(element => element.trim());
res.locals.entry.HTML = converter.makeHtml(res.locals.entry.markdown);
res.render('partials/content');
}
// * DELETE EXISTING ENTRIES
module.exports.deleteEntry = async (req, res) => {
const { _id } = req.params;
const result = await db.deleteOneEntry({_id});
res.send(result.message);
}
// * RENDER LIST BY DATE WITH ERROR MESSAGE
// ! IS THIS STILL NEEDED?
module.exports.getError = async (req, res) => {
// css
res.locals.css = "list";
res.locals.errMessage = `The requested URL is invalid.`;
// data for list pagination
res.locals.page = parseInt(req.params.page) || 1;
const docs = await db.getEntryCount(null, res.locals.user);
const skip = (res.locals.page * limit) - limit;
res.locals.pages = parseInt(Math.ceil(docs / limit));
// queries
res.locals.entries = await db.getListOfEntriesByDate( skip );
res.locals.topics = await db.getCategories(res.locals.user);
res.locals.archive = await db.getArchive(res.locals.user);
// disabled items
res.locals.adjacentEntries = null;
res.locals.publish = true;
res.locals.requestedTag = null;
res.render('list');
}
// * RENDER ADMIN PAGE
module.exports.getAdmin = async (req, res) => {
// rendering variables
res.locals.css = "editor";
res.locals.type = 'admin';
// queries
res.locals.topics = await db.getCategories(res.locals.user);
res.locals.archive = await db.getArchive(res.locals.user);
res.render('page');
}
// * CREATER NEW USERS
module.exports.createAccount = async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.create({ email, password });
const token = createToken(user._id);
res.cookie('jwt', token, {
httpOnly: true,
maxAge: maxAge * 1000
});
res.status(200).json({ user: user._id });
}
catch(err) {
const errors = handleErrors(err);
res.status(400).json({ errors });
}
}
// * ALLOW SIGN IN
module.exports.login = async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.login(email, password);
const token = createToken(user._id);
res.cookie('jwt', token, {
httpOnly: true,
maxAge: maxAge * 1000
});
res.status(200).json({ user: user._id });
}
catch (err) {
const errors = console.log(err);
res.status(400).json({ errors });
}
}
// * EXPIRE TOKEN TO SIGN USER OUT
module.exports.logout = async (req, res) => {
res.cookie('jwt', '', { maxAge: 1 });
res.redirect('/');
}