2019-10-27 08:51:53 -04:00
|
|
|
const models = require('../../models')
|
|
|
|
const logger = require('../../logger')
|
|
|
|
const config = require('../../config')
|
|
|
|
const errors = require('../../errors')
|
2019-10-27 10:22:14 -04:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
2019-10-27 08:51:53 -04:00
|
|
|
|
2023-07-14 14:01:23 -04:00
|
|
|
exports.findNote = function (req, res, callback, include = null, createIfNotFound = false) {
|
2019-10-27 08:51:53 -04:00
|
|
|
const id = req.params.noteId || req.params.shortid
|
|
|
|
models.Note.parseNoteId(id, function (err, _id) {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return errors.errorInternalError(res)
|
|
|
|
}
|
|
|
|
models.Note.findOne({
|
|
|
|
where: {
|
|
|
|
id: _id
|
|
|
|
},
|
|
|
|
include: include || null
|
|
|
|
}).then(function (note) {
|
2021-02-17 16:02:47 -05:00
|
|
|
if (!note && createIfNotFound) {
|
2024-07-17 18:25:20 -04:00
|
|
|
if (config.disableNoteCreation) {
|
|
|
|
return errors.errorNotFound(res)
|
|
|
|
} else {
|
|
|
|
return exports.newNote(req, res, '')
|
|
|
|
}
|
2019-10-27 08:51:53 -04:00
|
|
|
}
|
2021-02-17 16:02:47 -05:00
|
|
|
if (!note && !createIfNotFound) {
|
|
|
|
return errors.errorNotFound(res)
|
|
|
|
}
|
2019-10-27 08:51:53 -04:00
|
|
|
if (!exports.checkViewPermission(req, note)) {
|
|
|
|
return errors.errorForbidden(res)
|
|
|
|
} else {
|
|
|
|
return callback(note)
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return errors.errorInternalError(res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.checkViewPermission = function (req, note) {
|
|
|
|
if (note.permission === 'private') {
|
|
|
|
return !(!req.isAuthenticated() || note.ownerId !== req.user.id)
|
|
|
|
} else if (note.permission === 'limited' || note.permission === 'protected') {
|
|
|
|
return req.isAuthenticated()
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 17:00:34 -04:00
|
|
|
exports.newNote = async function (req, res, body) {
|
2019-10-27 08:51:53 -04:00
|
|
|
let owner = null
|
|
|
|
const noteId = req.params.noteId ? req.params.noteId : null
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
owner = req.user.id
|
|
|
|
} else if (!config.allowAnonymous) {
|
|
|
|
return errors.errorForbidden(res)
|
|
|
|
}
|
2021-01-22 09:36:47 -05:00
|
|
|
if (noteId) {
|
2021-01-22 10:47:47 -05:00
|
|
|
if (config.allowFreeURL && !config.forbiddenNoteIDs.includes(noteId) && (!config.requireFreeURLAuthentication || req.isAuthenticated())) {
|
2021-01-22 09:36:47 -05:00
|
|
|
req.alias = noteId
|
|
|
|
} else {
|
|
|
|
return req.method === 'POST' ? errors.errorForbidden(res) : errors.errorNotFound(res)
|
|
|
|
}
|
2023-07-18 15:37:27 -04:00
|
|
|
|
2021-03-29 17:00:34 -04:00
|
|
|
try {
|
2023-07-18 15:37:27 -04:00
|
|
|
const id = await new Promise((resolve, reject) => {
|
|
|
|
models.Note.parseNoteId(noteId, (err, id) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve(id)
|
|
|
|
}
|
|
|
|
})
|
2021-03-29 17:00:34 -04:00
|
|
|
})
|
2023-07-18 15:37:27 -04:00
|
|
|
|
|
|
|
if (id) {
|
2021-03-29 17:00:34 -04:00
|
|
|
return errors.errorConflict(res)
|
|
|
|
}
|
2023-07-18 15:37:27 -04:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(error)
|
2021-03-29 17:00:34 -04:00
|
|
|
return errors.errorInternalError(res)
|
|
|
|
}
|
2019-10-27 08:51:53 -04:00
|
|
|
}
|
|
|
|
models.Note.create({
|
|
|
|
ownerId: owner,
|
|
|
|
alias: req.alias ? req.alias : null,
|
2020-11-11 15:24:55 -05:00
|
|
|
content: body,
|
|
|
|
title: models.Note.parseNoteTitle(body)
|
2019-10-27 08:51:53 -04:00
|
|
|
}).then(function (note) {
|
|
|
|
return res.redirect(config.serverURL + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)))
|
|
|
|
}).catch(function (err) {
|
2021-03-29 17:07:11 -04:00
|
|
|
logger.error('Note could not be created: ' + err)
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorInternalError(res)
|
|
|
|
})
|
|
|
|
}
|
2019-10-27 10:22:14 -04:00
|
|
|
|
|
|
|
exports.getPublishData = function (req, res, note, callback) {
|
|
|
|
const body = note.content
|
|
|
|
const extracted = models.Note.extractMeta(body)
|
2022-10-03 08:19:44 -04:00
|
|
|
let markdown = extracted.markdown
|
2019-10-27 10:22:14 -04:00
|
|
|
const meta = models.Note.parseMeta(extracted.meta)
|
2022-10-03 08:19:44 -04:00
|
|
|
// extractMeta() will remove the meta part from markdown,
|
|
|
|
// so we need to re-add the `breaks` option for proper rendering
|
|
|
|
if (typeof extracted.meta.breaks === 'boolean') {
|
|
|
|
markdown = '---\nbreaks: ' + extracted.meta.breaks + '\n---\n' + markdown
|
|
|
|
}
|
2019-10-27 10:22:14 -04:00
|
|
|
const createtime = note.createdAt
|
|
|
|
const updatetime = note.lastchangeAt
|
|
|
|
let title = models.Note.decodeTitle(note.title)
|
|
|
|
title = models.Note.generateWebTitle(meta.title || title)
|
|
|
|
const ogdata = models.Note.parseOpengraph(meta, title)
|
|
|
|
const data = {
|
2022-05-01 15:14:27 -04:00
|
|
|
title,
|
2019-10-27 10:22:14 -04:00
|
|
|
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
|
2020-07-03 21:37:19 -04:00
|
|
|
lang: meta.lang || null,
|
2019-10-27 10:22:14 -04:00
|
|
|
viewcount: note.viewcount,
|
2022-05-01 15:14:27 -04:00
|
|
|
createtime,
|
|
|
|
updatetime,
|
2019-10-27 10:22:14 -04:00
|
|
|
body: markdown,
|
|
|
|
theme: meta.slideOptions && isRevealTheme(meta.slideOptions.theme),
|
|
|
|
meta: JSON.stringify(extracted.meta),
|
|
|
|
owner: note.owner ? note.owner.id : null,
|
|
|
|
ownerprofile: note.owner ? models.User.getProfile(note.owner) : null,
|
|
|
|
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
|
|
|
|
lastchangeuserprofile: note.lastchangeuser ? models.User.getProfile(note.lastchangeuser) : null,
|
|
|
|
robots: meta.robots || false, // default allow robots
|
|
|
|
GA: meta.GA,
|
|
|
|
disqus: meta.disqus,
|
|
|
|
cspNonce: res.locals.nonce,
|
|
|
|
dnt: req.headers.dnt,
|
|
|
|
opengraph: ogdata
|
|
|
|
}
|
|
|
|
callback(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isRevealTheme (theme) {
|
2020-03-21 11:19:17 -04:00
|
|
|
if (fs.existsSync(path.join(__dirname, '..', '..', '..', 'public', 'build', 'reveal.js', 'css', 'theme', theme + '.css'))) {
|
2019-10-27 10:22:14 -04:00
|
|
|
return theme
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|