update guestbook logic

This commit is contained in:
b1ek 2023-02-20 16:54:08 +10:00
parent 4a601c99c4
commit 8f753a5a3d
3 changed files with 26 additions and 9 deletions

View File

@ -57,4 +57,6 @@ Object.keys(db).forEach(modelName => {
db.sequelize = sequelize; db.sequelize = sequelize;
db.Sequelize = Sequelize; db.Sequelize = Sequelize;
sequelize.sync();
module.exports = db; module.exports = db;

View File

@ -2,13 +2,8 @@ const Helpers = require('../helpers');
const Sequelize = require('../models'); const Sequelize = require('../models');
const html_escape = require('html-escaper'); const html_escape = require('html-escaper');
const send_error = async (req, res, error, data) => { const send_error = async (res, error) => {
res.send(await Helpers.ViewLoader.load('guestbook.pug', { return res.redirect('/guestbook?error=' + encodeURIComponent(error));
current_route: req.originalUrl,
ip: req.ip,
errors: error,
data
}));
}; };
async function handler(req, res, next) { async function handler(req, res, next) {
@ -43,8 +38,28 @@ async function submit(req, res, next) {
const { name, email, message } = req.body; const { name, email, message } = req.body;
const hidemail = req.body.hidemail ? (req.body.hidemail == 'on' ? true : false) : false; const hidemail = req.body.hidemail ? (req.body.hidemail == 'on' ? true : false) : false;
let errors = [];
if (message.length >= 512) { if (message.length >= 512) {
res.redirect('/guestbook?error=' + encodeURIComponent('Maximum length is 512 characters.')); errors.push('Maximum length is 512 characters.');
}
if (name == '') {
errors.push('Name must be specified.');
}
if (
!email
.toLowerCase()
.match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)
&&
email !== ''
) {
errors.push('Email is of invalid format.');
}
if (message == '') {
errors.push('Message should not be empty!');
}
if (errors.length !== 0) {
send_error(res, "<p>" + errors.join('<br/>') + "</p>");
return; return;
} }

View File

@ -100,7 +100,7 @@ block content
a(href='mailto:' + entry.email)= entry.email a(href='mailto:' + entry.email)= entry.email
else else
| Email: | Email:
span(class='gb_hidden_mail' style='width:' + (10 * entry.email.length) + 'px') span(class='gb_hidden_mail' style='width:' + (10 * (entry.email || '.').length) + 'px')
br br
| IP: #{entry.ip} | IP: #{entry.ip}
br br