-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentryRouter.js
286 lines (244 loc) · 6.78 KB
/
entryRouter.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
const express = require('express')
const app = express()
const entryRouter = express.Router()
const mongoose = require('mongoose')
mongoose.Promise = global.Promise
const bodyParser = require('body-parser')
const jsonParser = bodyParser.json()
const {passport, authorize} = require('./auth')
const schedule = require('node-schedule')
//scraps webpage for title
const request = require('request')
const rp = require('request-promise-native')
const cheerio = require('cheerio')
const ImageResolver = require('image-resolver')
const resolver = new ImageResolver();
resolver.register(new ImageResolver.FileExtension());
resolver.register(new ImageResolver.MimeType());
resolver.register(new ImageResolver.Opengraph());
resolver.register(new ImageResolver.Webpage());
entryRouter.use(jsonParser)
entryRouter.use(require('cookie-parser')())
entryRouter.use(require('express-session')({secret: 'keyboard cat', resave: true, saveUninitialized: true, cookie: { secure : false, maxAge : (4 * 60 * 60 * 1000)} }))
entryRouter.use(passport.initialize())
entryRouter.use(passport.session())
const {Users, Entry} = require('./models')
const {addDays, nowDate} = require('./resources/date-module')
entryRouter.get('/', (req, res) => {
Entry
.find()
.exec()
.then(entries => {
res.json({
entries: entries.map(entry => entry.entryRepr())
})
})
.catch(err => {
console.error(err)
res.status(500).json({message: 'Internal server error'})
})
})
entryRouter.get('/entries', authorize, (req, res) => {
let user = req.user.userRepr()
Entry
.find({journalId: user.journalId})
.exec()
.then(entries => {
if (entries.length == 0){
res.status(200).json({message: 'You have no links saved'})
}
else {
res.json({
entries: entries.map(entry => entry.entryRepr())
})
}
})
.catch(err => {
console.error(err)
res.status(500).json({message: 'Internal server error'})
})
})
entryRouter.post('/', authorize, (req, res) => {
const requiredFields = ['link', 'priority'];
let priorityExpiryObject = {}
let priority = req.body.priority
let addDate = nowDate()
let expiry;
let options = {
uri: req.body.link,
transform: function (body) {
return cheerio.load(body);
}
}
requiredFields.forEach((field) => {
if (!(field in req.body)) {
const message = `Missing ${field} in request body`;
console.error(message)
return res.status(400).send(message)
}
})
Users
.find({journalId: req.user.journalId})
.exec()
.then(res => {
priorityExpiryObject = res[0].priorityExpiry
expiry = addDays(addDate, priorityExpiryObject[priority])
return expiry
})
.then(expiry => {
if (req.body.title){
Entry
.create({
title: req.body.title,
link: req.body.link,
journalId: req.user.journalId,
priority: req.body.priority,
addDate: addDate,
expiry: expiry
})
.then(entry => res.status(201).json(entry.entryRepr()))
}
else{
//scrapes title from url
rp(options)
.then(($) => {
let pageTitle = $('head title').html()
title = pageTitle.split("-")[0].split('|')[0]
if (title == null || title == ""){
linkArray = (url).split('/')
title = linkArray[linkArray.length - 1]
}
return title
})
.then(title => {
//scrapes image from url
resolver.resolve(req.body.link, (result)=>{
let image;
if (result == null){
image = '/resources/images/empty.jpg'
}
else {
image = result.image
}
Entry
.create({
title: title,
link: req.body.link,
image: image,
journalId: req.user.journalId,
priority: req.body.priority,
addDate: addDate,
expiry: expiry
})
.then(entry => res.status(201).json(entry.entryRepr()))
.catch(err => {
console.error(err)
return res.status(400).json({message: 'Internal server error'})
})
})
})
.catch(err => {
console.log('website does not exist')
return res.status(400).json({message: 'Website does not exist'})
})
}
})
})
entryRouter.put('/:entryId', (req, res) => {
if (!(req.params.entryId === req.body.entryId)){
const message = (
`Request path entryId (${req.params.entryId}) and request body entryId ` +
`(${req.body.entryId}) must match`);
console.error(message);
res.status(400).json({message: message});
}
let toUpdate = {}
const updateableFields = ['link', 'priority', 'title']
let addDate;
updateableFields.forEach(field => {
if(field in req.body){
toUpdate[field] = req.body[field]
}
})
if (!("priority" in toUpdate)){
Entry
.findByIdAndUpdate(req.body.entryId, {$set: toUpdate}, {new: true})
.then(updateEntry => res.status(201).json(updateEntry.entryRepr()))
}
else {
let addDate;
Entry
.findById(req.body.entryId)
.exec()
.then(res => {
let journalId = res.journalId
addDate = new Date(res.addDate)
return journalId
})
.then(journalId => {
Users
.find({journalId: journalId})
.exec()
.then(res => {
let priorityExpiryObject = res[0].priorityExpiry
return priorityExpiryObject
})
.then(object => {
let priority = req.body.priority
let priorityExpiry = object[priority]
return priorityExpiry
})
.then(priorityExpiry => {
expiry = addDays(addDate, priorityExpiry)
return expiry
})
.then(expiry => {
toUpdate.expiry = new Date(expiry)
Entry
.findByIdAndUpdate(req.params.entryId, {$set: toUpdate}, {new: true})
.then(updateEntry => res.status(201).json(updateEntry.entryRepr()))
.catch(err => res.status(500).json({message: 'Internal server error'}))
})
})
}
})
entryRouter.delete('/:entryId', (req, res) => {
Entry
.findByIdAndRemove(req.params.entryId)
.then(() => {
console.log(`Entry ${req.params.entryId} was deleted`)
res.status(204).end()
})
})
entryRouter.delete('/journal/:journalId', (req, res) => {
Entry
.find({journalId: req.params.journalId})
.remove()
.exec()
.then(() => {
console.log(`Journal ${req.params.journalId} was deleted`)
res.status(204).end()
})
})
//schedules delete to run on all expired entries once a day at 11:59 PM
let deleteExpiredPosts = schedule.scheduleJob('45 * * * *', function(){
let currentDate = nowDate()
Entry
.find({expiry: {$lte: currentDate}})
.exec()
.then(entries => {
entries.forEach(entry => {
let id = entry.id
Entry
.findByIdAndRemove(id)
.exec()
.then(() => {
console.log(`${entry.title} deleted`)
})
})
})
.then(() => {
console.log('All expired entries have been deleted')
})
})
module.exports = entryRouter