mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 11:16:31 -05:00
Merge pull request #170 from ErikMichelson/post-note-url
Added endpoint for note-creation with given alias
This commit is contained in:
commit
e313b47b92
2 changed files with 19 additions and 13 deletions
|
@ -41,12 +41,12 @@ var response = {
|
||||||
errorServiceUnavailable: function (res) {
|
errorServiceUnavailable: function (res) {
|
||||||
res.status(503).send("I'm busy right now, try again later.")
|
res.status(503).send("I'm busy right now, try again later.")
|
||||||
},
|
},
|
||||||
newNote: newNote,
|
|
||||||
showNote: showNote,
|
showNote: showNote,
|
||||||
showPublishNote: showPublishNote,
|
showPublishNote: showPublishNote,
|
||||||
showPublishSlide: showPublishSlide,
|
showPublishSlide: showPublishSlide,
|
||||||
showIndex: showIndex,
|
showIndex: showIndex,
|
||||||
noteActions: noteActions,
|
noteActions: noteActions,
|
||||||
|
postNote: postNote,
|
||||||
publishNoteActions: publishNoteActions,
|
publishNoteActions: publishNoteActions,
|
||||||
publishSlideActions: publishSlideActions,
|
publishSlideActions: publishSlideActions,
|
||||||
githubActions: githubActions,
|
githubActions: githubActions,
|
||||||
|
@ -107,8 +107,7 @@ function responseCodiMD (res, note) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function newNote (req, res, next) {
|
function postNote (req, res, next) {
|
||||||
var owner = null
|
|
||||||
var body = ''
|
var body = ''
|
||||||
if (req.body && req.body.length > config.documentMaxLength) {
|
if (req.body && req.body.length > config.documentMaxLength) {
|
||||||
return response.errorTooLong(res)
|
return response.errorTooLong(res)
|
||||||
|
@ -116,17 +115,28 @@ function newNote (req, res, next) {
|
||||||
body = req.body
|
body = req.body
|
||||||
}
|
}
|
||||||
body = body.replace(/[\r]/g, '')
|
body = body.replace(/[\r]/g, '')
|
||||||
|
return newNote(req, res, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
function newNote (req, res, body) {
|
||||||
|
var owner = null
|
||||||
|
var noteId = req.params.noteId ? req.params.noteId : null
|
||||||
if (req.isAuthenticated()) {
|
if (req.isAuthenticated()) {
|
||||||
owner = req.user.id
|
owner = req.user.id
|
||||||
} else if (!config.allowAnonymous) {
|
} else if (!config.allowAnonymous) {
|
||||||
return response.errorForbidden(res)
|
return response.errorForbidden(res)
|
||||||
}
|
}
|
||||||
|
if (config.allowFreeURL && noteId && !config.forbiddenNoteIDs.includes(noteId)) {
|
||||||
|
req.alias = noteId
|
||||||
|
} else if (noteId) {
|
||||||
|
return req.method === 'POST' ? response.errorForbidden(res) : response.errorNotFound(res)
|
||||||
|
}
|
||||||
models.Note.create({
|
models.Note.create({
|
||||||
ownerId: owner,
|
ownerId: owner,
|
||||||
alias: req.alias ? req.alias : null,
|
alias: req.alias ? req.alias : null,
|
||||||
content: body
|
content: body
|
||||||
}).then(function (note) {
|
}).then(function (note) {
|
||||||
return res.redirect(config.serverURL + '/' + models.Note.encodeNoteId(note.id))
|
return res.redirect(config.serverURL + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)))
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
return response.errorInternalError(res)
|
return response.errorInternalError(res)
|
||||||
|
@ -144,7 +154,6 @@ function checkViewPermission (req, note) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function findNote (req, res, callback, include) {
|
function findNote (req, res, callback, include) {
|
||||||
var noteId = req.params.noteId
|
|
||||||
var id = req.params.noteId || req.params.shortid
|
var id = req.params.noteId || req.params.shortid
|
||||||
models.Note.parseNoteId(id, function (err, _id) {
|
models.Note.parseNoteId(id, function (err, _id) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -158,12 +167,7 @@ function findNote (req, res, callback, include) {
|
||||||
include: include || null
|
include: include || null
|
||||||
}).then(function (note) {
|
}).then(function (note) {
|
||||||
if (!note) {
|
if (!note) {
|
||||||
if (config.allowFreeURL && noteId && !config.forbiddenNoteIDs.includes(noteId)) {
|
return newNote(req, res, null)
|
||||||
req.alias = noteId
|
|
||||||
return newNote(req, res)
|
|
||||||
} else {
|
|
||||||
return response.errorNotFound(res)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (!checkViewPermission(req, note)) {
|
if (!checkViewPermission(req, note)) {
|
||||||
return response.errorForbidden(res)
|
return response.errorForbidden(res)
|
||||||
|
|
|
@ -9,9 +9,11 @@ const { markdownParser } = require('./utils')
|
||||||
const noteRouter = module.exports = Router()
|
const noteRouter = module.exports = Router()
|
||||||
|
|
||||||
// get new note
|
// get new note
|
||||||
noteRouter.get('/new', response.newNote)
|
noteRouter.get('/new', response.postNote)
|
||||||
// post new note with content
|
// post new note with content
|
||||||
noteRouter.post('/new', markdownParser, response.newNote)
|
noteRouter.post('/new', markdownParser, response.postNote)
|
||||||
|
// post new note with content and alias
|
||||||
|
noteRouter.post('/new/:noteId', markdownParser, response.postNote)
|
||||||
// get publish note
|
// get publish note
|
||||||
noteRouter.get('/s/:shortid', response.showPublishNote)
|
noteRouter.get('/s/:shortid', response.showPublishNote)
|
||||||
// publish note actions
|
// publish note actions
|
||||||
|
|
Loading…
Reference in a new issue