mirror of https://gitgud.io/fatchan/jschan.git
because rewriting the whole page can be annoying and you couldnt access the text without styling also can change .html name, maybe that will get removed but it works atm. still needs more tweaks and proper testingjschan
parent
d7a3e55fb0
commit
52c189a153
17 changed files with 221 additions and 13 deletions
@ -0,0 +1,62 @@ |
||||
'use strict'; |
||||
|
||||
const editCustomPage = require(__dirname+'/../../models/forms/editcustompage.js') |
||||
, { CustomPages } = require(__dirname+'/../../db/') |
||||
, dynamicResponse = require(__dirname+'/../../helpers/dynamic.js') |
||||
, paramConverter = require(__dirname+'/../../helpers/paramconverter.js') |
||||
, { checkSchema, lengthBody, numberBody, minmaxBody, numberBodyVariable, |
||||
inArrayBody, arrayInBody, existsBody } = require(__dirname+'/../../helpers/schema.js') |
||||
, config = require(__dirname+'/../../config.js'); |
||||
|
||||
module.exports = { |
||||
|
||||
paramConverter: paramConverter({ |
||||
trimFields: ['message', 'title', 'page'], |
||||
processMessageLength: true, |
||||
objectIdFields: ['page_id'], |
||||
}), |
||||
|
||||
controller: async (req, res, next) => { |
||||
|
||||
const { globalLimits } = config.get; |
||||
|
||||
const errors = await checkSchema([ |
||||
{ result: existsBody(req.body.page_id), expected: true, error: 'Missing page id' }, |
||||
{ result: existsBody(req.body.message), expected: true, error: 'Missing message' }, |
||||
{ result: existsBody(req.body.title), expected: true, error: 'Missing title' }, |
||||
{ result: existsBody(req.body.page), expected: true, error: 'Missing .html name' }, |
||||
{ result: () => { |
||||
if (req.body.page) { |
||||
return /[a-z0-9_-]+/.test(req.body.page); |
||||
} |
||||
return false; |
||||
} , expected: true, error: '.html name must contain a-z 0-9 _ - only' }, |
||||
{ result: numberBody(res.locals.messageLength, 0, globalLimits.customPages.maxLength), expected: true, error: `Message must be ${globalLimits.customPages.maxLength} characters or less` }, |
||||
{ result: lengthBody(req.body.title, 0, 50), expected: false, error: 'Title must be 50 characters or less' }, |
||||
{ result: lengthBody(req.body.page, 0, 50), expected: false, error: '.html name must be 50 characters or less' }, |
||||
{ result: async () => { |
||||
const existingPage = await CustomPages.findOne(req.params.board, req.body.page); |
||||
if (existingPage && existingPage.page === req.body.page) { |
||||
return existingPage._id === req.body.page_id; |
||||
} |
||||
return true; |
||||
}, expected: true, error: '.html name must be unique'}, |
||||
]); |
||||
|
||||
if (errors.length > 0) { |
||||
return dynamicResponse(req, res, 400, 'message', { |
||||
'title': 'Bad request', |
||||
'errors': errors, |
||||
'redirect': req.headers.referer || '/${req.params.board}/manage/custompages.html' |
||||
}); |
||||
} |
||||
|
||||
try { |
||||
await editCustomPage(req, res, next); |
||||
} catch (err) { |
||||
return next(err); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,58 @@ |
||||
'use strict'; |
||||
|
||||
const { CustomPages } = require(__dirname+'/../../db/') |
||||
, uploadDirectory = require(__dirname+'/../../helpers/files/uploadDirectory.js') |
||||
, { remove } = require('fs-extra') |
||||
, dynamicResponse = require(__dirname+'/../../helpers/dynamic.js') |
||||
, buildQueue = require(__dirname+'/../../queue.js') |
||||
, { prepareMarkdown } = require(__dirname+'/../../helpers/posting/markdown.js') |
||||
, messageHandler = require(__dirname+'/../../helpers/posting/message.js'); |
||||
|
||||
module.exports = async (req, res, next) => { |
||||
|
||||
const message = prepareMarkdown(req.body.message, false); |
||||
const { message: markdownPage } = await messageHandler(message, null, null, res.locals.permLevel); |
||||
const editedDate = new Date(); |
||||
|
||||
const oldPage = await CustomPages.findOneAndUpdate(req.body.page_id, req.params.board, |
||||
req.body.page, req.body.title, message, markdownPage, editedDate).then(res => res.value); |
||||
|
||||
if (oldPage === null) { |
||||
return dynamicResponse(req, res, 400, 'message', { |
||||
'title': 'Bad request', |
||||
'errors': 'Custom page does not exist', |
||||
'redirect': req.headers.referer || '/${req.params.board}/manage/custompages.html' |
||||
}); |
||||
} |
||||
|
||||
await remove(`${uploadDirectory}/html/${req.params.board}/custompage/${oldPage.page}.html`); |
||||
|
||||
const newPage = { |
||||
'_id': oldPage._id, |
||||
'board': req.params.board, |
||||
'page': req.body.page, |
||||
'title': req.body.title, |
||||
'message': { |
||||
'raw': message, |
||||
'markdown': markdownPage |
||||
}, |
||||
'date': oldPage.date, |
||||
'edited': editedDate, |
||||
}; |
||||
|
||||
buildQueue.push({ |
||||
'task': 'buildCustomPage', |
||||
'options': { |
||||
'board': res.locals.board, |
||||
'page': newPage.page, |
||||
'customPage': newPage, |
||||
} |
||||
}); |
||||
|
||||
return dynamicResponse(req, res, 200, 'message', { |
||||
'title': 'Success', |
||||
'message': 'Updated custom page', |
||||
'redirect': `/${req.params.board}/manage/custompages.html`, |
||||
}); |
||||
|
||||
} |
@ -1,6 +1,6 @@ |
||||
'use strict'; |
||||
|
||||
const { News } = require(__dirname+'/../../db/'); |
||||
const { News } = require(__dirname+'/../../../db/'); |
||||
|
||||
module.exports = async (req, res, next) => { |
||||
|
@ -0,0 +1,26 @@ |
||||
'use strict'; |
||||
|
||||
const { CustomPages } = require(__dirname+'/../../../db/'); |
||||
|
||||
module.exports = async (req, res, next) => { |
||||
|
||||
let customPage; |
||||
try { |
||||
customPage = await CustomPages.findOneId(req.params.custompageid, req.params.board); |
||||
} catch (err) { |
||||
return next(err) |
||||
} |
||||
|
||||
if (!customPage) { |
||||
return next(); |
||||
} |
||||
|
||||
res |
||||
.set('Cache-Control', 'private, max-age=5') |
||||
.render('editcustompage', { |
||||
csrf: req.csrfToken(), |
||||
page: customPage, |
||||
board: res.locals.board, |
||||
}); |
||||
|
||||
} |
@ -0,0 +1,27 @@ |
||||
extends ../layout.pug |
||||
|
||||
block head |
||||
title Edit Custom Page |
||||
|
||||
block content |
||||
h1.board-title Edit Custom Page |
||||
include ../includes/stickynav.pug |
||||
.form-wrapper.flex-center.mv-10 |
||||
form.form-post(action=`/forms/board/${board._id}/editcustompage` method='POST') |
||||
input(type='hidden' name='_csrf' value=csrf) |
||||
input(type='hidden' name='page_id' value=page._id) |
||||
.row |
||||
.label .html name |
||||
input(type='text' name='page' pattern='[a-z0-9-_]+' title='a-z0-9-_ only' value=page.page required) |
||||
.table-container.flex-center.mv-5 |
||||
table |
||||
tr |
||||
th |
||||
input.edit.left(type='text' name='title' value=page.title required) |
||||
- const pageDate = new Date(page.date); |
||||
time.right.reltime(datetime=pageDate.toISOString()) #{pageDate.toLocaleString(undefined, {hourCycle:'h23'})} |
||||
tr |
||||
td |
||||
|
||||
textarea.edit.fw(name='message' rows='10' placeholder='Supports post styling' required) #{page.message.raw} |
||||
input(type='submit', value='save') |
Loading…
Reference in new issue