-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathindex.js
295 lines (254 loc) · 8.25 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
'use strict'
const config = require('../config')
const logger = require('../logger')
const { Note, User, Revision } = require('../models')
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require('../response')
const { updateHistory, historyDelete } = require('../history')
const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions')
const realtime = require('../realtime/realtime')
const service = require('./service')
exports.showNote = async (req, res) => {
const noteId = req.params.noteId
const userId = req.user ? req.user.id : null
let note = await service.getNote(noteId)
if (!note) {
// if allow free url enable, auto create note
if (!config.allowFreeURL || config.forbiddenNoteIDs.includes(noteId)) {
return errorNotFound(req, res)
} else if (!config.allowAnonymous && !userId) {
return errorForbidden(req, res)
}
note = await service.createNote(userId, noteId)
}
if (!newCheckViewPermission(note, req.isAuthenticated(), userId)) {
return errorForbidden(req, res)
}
// force to use note id
const id = Note.encodeNoteId(note.id)
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) {
return res.redirect(config.serverURL + '/' + (note.alias || id))
}
return responseCodiMD(res, note)
}
exports.showPublishNote = async (req, res) => {
const shortid = req.params.shortid
const note = await service.getNote(shortid, {
includeUser: true
})
if (!note) {
return errorNotFound(req, res)
}
if (!service.canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) {
return errorForbidden(req, res)
}
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
return res.redirect(config.serviceerURL + '/s/' + (note.alias || note.shortid))
}
await note.increment('viewcount')
const body = note.content
const extracted = Note.extractMeta(body)
const markdown = extracted.markdown
const meta = Note.parseMeta(extracted.meta)
const createTime = note.createdAt
const updateTime = note.lastchangeAt
const title = Note.generateWebTitle(meta.title || Note.decodeTitle(note.title))
const data = {
title: title,
description: meta.description || (markdown ? Note.generateDescription(markdown) : null),
image: meta.image,
viewcount: note.viewcount,
createtime: createTime,
updatetime: updateTime,
body: body,
owner: note.owner ? note.owner.id : null,
ownerprofile: note.owner ? User.getProfile(note.owner) : null,
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
lastchangeuserprofile: note.lastchangeuser ? User.getProfile(note.lastchangeuser) : null,
robots: meta.robots || false, // default allow robots
GA: meta.GA,
disqus: meta.disqus,
cspNonce: res.locals.nonce
}
res.set({
'Cache-Control': 'private' // only cache by client
})
res.render('pretty.ejs', data)
}
exports.noteActions = async (req, res) => {
const noteId = req.params.noteId
const note = await service.getNote(noteId)
if (!note) {
return errorNotFound(req, res)
}
if (!service.canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) {
return errorForbidden(req, res)
}
const action = req.params.action
switch (action) {
case 'publish':
case 'pretty': // pretty deprecated
return actionPublish(req, res, note)
case 'slide':
return actionSlide(req, res, note)
case 'download':
actionDownload(req, res, note)
break
case 'info':
actionInfo(req, res, note)
break
case 'pdf':
if (config.allowPDFExport) {
actionPDF(req, res, note)
} else {
logger.error('PDF export failed: Disabled by config. Set "allowPDFExport: true" to enable. Check the documentation for details')
errorForbidden(req, res)
}
break
case 'gist':
actionGist(req, res, note)
break
case 'revision':
actionRevision(req, res, note)
break
case 'pandoc':
actionPandoc(req, res, note)
break
default:
return res.redirect(config.serviceerURL + '/' + noteId)
}
}
exports.listMyNotes = (req, res) => {
if (req.isAuthenticated()) {
service.getMyNoteList(req.user.id, (err, myNoteList) => {
if (err) return errorInternalError(req, res)
if (!myNoteList) return errorNotFound(req, res)
res.send({
myNotes: myNoteList
})
})
} else {
return errorForbidden(req, res)
}
}
exports.deleteNote = async (req, res) => {
if (req.isAuthenticated()) {
const noteId = await Note.parseNoteIdAsync(req.params.noteId)
try {
const destroyed = await Note.destroy({
where: {
id: noteId,
ownerId: req.user.id
}
})
if (!destroyed) {
logger.error('Delete note failed: Make sure the noteId and ownerId are correct.')
return errorNotFound(req, res)
}
historyDelete(req, res)
if (realtime.isNoteExistsInPool(noteId)) {
const note = realtime.getNoteFromNotePool(noteId)
realtime.disconnectSocketOnNote(note)
}
res.send({
status: 'ok'
})
} catch (err) {
logger.error('Delete note failed: Internal Error.')
return errorInternalError(req, res)
}
} else {
return errorForbidden(req, res)
}
}
exports.updateNote = async (req, res) => {
if (req.isAuthenticated() || config.allowAnonymousEdits) {
const noteId = await Note.parseNoteIdAsync(req.params.noteId)
try {
const note = await Note.findOne({
where: {
id: noteId
}
})
if (!note) {
logger.error('Update note failed: Can\'t find the note.')
return errorNotFound(req, res)
}
if (realtime.isNoteExistsInPool(noteId)) {
logger.error('Update note failed: There are online users opening this note.')
return res.status('403').json({ status: 'error', message: 'Update API can only be used when no users is online' })
}
const now = Date.now()
const content = req.body.content
const updated = await note.update({
title: Note.parseNoteTitle(content),
content: content,
lastchangeAt: now,
authorship: [
[
req.isAuthenticated() ? req.user.id : null,
0,
content.length,
now,
now
]
]
})
if (!updated) {
logger.error('Update note failed: Write note content error.')
return errorInternalError(req, res)
}
if (req.isAuthenticated()) {
updateHistory(req.user.id, noteId, content)
}
Revision.saveNoteRevision(note, (err, revision) => {
if (err) {
logger.error(err)
return errorInternalError(req, res)
}
if (!revision) return errorNotFound(req, res)
res.send({
status: 'ok'
})
})
} catch (err) {
logger.error(err.stack)
logger.error('Update note failed: Internal Error.')
return errorInternalError(req, res)
}
} else {
return errorForbidden(req, res)
}
}
exports.updateNoteAlias = async (req, res) => {
const originAliasOrNoteId = req.params.originAliasOrNoteId
const alias = req.body.alias || ''
const userId = req.user ? req.user.id : null
const note = await service.getNote(originAliasOrNoteId)
.catch((err) => {
logger.error('get note failed:' + err.message)
return false
})
if (!note) {
logger.error('update note alias failed: note not found.')
return res.status(404).json({ status: 'error', message: 'Not found' })
}
if (note.ownerId !== userId) {
return res.status(403).json({ status: 'error', message: 'Forbidden' })
}
const result = await service.updateAlias(originAliasOrNoteId, alias)
.catch((err) => {
logger.error('update note alias failed:' + err.message)
return false
})
if (!result) {
return res.status(500).json({ status: 'error', message: 'Internal Error' })
}
const { isSuccess, errorMessage } = result
if (!isSuccess) {
res.status(400).json({ status: 'error', message: errorMessage })
return
}
res.send({
status: 'ok'
})
}