2020-04-10 18:05:18 -04:00
|
|
|
import { NextFunction, Response } from "express";
|
|
|
|
import { NoteUtils } from "./util";
|
2020-04-12 11:39:00 -04:00
|
|
|
import { errors } from '../../errors'
|
2020-04-12 07:11:06 -04:00
|
|
|
import { logger } from '../../logger'
|
|
|
|
import { config } from '../../config'
|
2020-04-10 18:05:18 -04:00
|
|
|
import { User } from "../../models/user";
|
|
|
|
import { Note } from "../../models/note";
|
2020-02-26 08:21:00 -05:00
|
|
|
|
|
|
|
|
2020-04-10 18:05:18 -04:00
|
|
|
export module SlideController {
|
|
|
|
export function publishSlideActions(req: any, res: Response, next: NextFunction) {
|
|
|
|
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
2020-02-26 08:21:00 -05:00
|
|
|
const action = req.params.action
|
|
|
|
if (action === 'edit') {
|
2020-04-10 18:05:18 -04:00
|
|
|
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both')
|
2020-02-26 08:21:00 -05:00
|
|
|
} else { res.redirect(config.serverURL + '/p/' + note.shortid) }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function showPublishSlide(req: any, res: Response, next: NextFunction) {
|
|
|
|
const include = [{
|
2020-04-10 18:05:18 -04:00
|
|
|
model: User,
|
2020-02-26 08:21:00 -05:00
|
|
|
as: 'owner'
|
|
|
|
}, {
|
2020-04-10 18:05:18 -04:00
|
|
|
model: User,
|
2020-02-26 08:21:00 -05:00
|
|
|
as: 'lastchangeuser'
|
|
|
|
}]
|
2020-04-10 18:05:18 -04:00
|
|
|
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
2020-02-26 08:21:00 -05:00
|
|
|
// force to use short id
|
|
|
|
const shortid = req.params.shortid
|
|
|
|
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
|
|
|
|
return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
|
|
|
|
}
|
|
|
|
note.increment('viewcount').then(function (note) {
|
|
|
|
if (!note) {
|
|
|
|
return errors.errorNotFound(res)
|
|
|
|
}
|
|
|
|
NoteUtils.getPublishData(req, res, note, (data) => {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private' // only cache by client
|
|
|
|
})
|
|
|
|
return res.render('slide.ejs', data)
|
|
|
|
})
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return errors.errorInternalError(res)
|
|
|
|
})
|
|
|
|
}, include)
|
|
|
|
}
|
|
|
|
}
|