Skip to content

Commit

Permalink
recovered old joi
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Jul 16, 2020
1 parent 6e31d64 commit edd5024
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 121 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"he": "1.2.0",
"humanize": "0.0.9",
"ioredis": "4.17.3",
"joi": "17.1.1",
"joi": "14.3.1",
"mongodb": "3.5.9",
"morgan": "1.10.0",
"multer": "1.4.2",
Expand Down
180 changes: 60 additions & 120 deletions routes/account/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,38 @@ const Joi = require('joi');
const apiClient = require('../../lib/api-client');

const filterBaseSchema = {
name: Joi.string()
.empty('')
.trim()
.max(100)
.label('Name'),

query_from: Joi.string()
.empty('')
.trim()
.max(100)
.label('From'),
query_to: Joi.string()
.empty('')
.trim()
.max(100)
.label('To'),
query_subject: Joi.string()
.empty('')
.trim()
.max(100)
.label('Subject'),
query_text: Joi.string()
.empty('')
.trim()
.max(100)
.label('Includes'),
query_listId: Joi.string()
.empty('')
.trim()
.max(100)
.label('List-ID'),
query_haYes: Joi.boolean()
.optional()
.label('Has attachments'),
query_haNo: Joi.boolean()
.optional()
.label('Does not have attachments'),
name: Joi.string().empty('').trim().max(100).label('Name'),

query_from: Joi.string().empty('').trim().max(100).label('From'),
query_to: Joi.string().empty('').trim().max(100).label('To'),
query_subject: Joi.string().empty('').trim().max(100).label('Subject'),
query_text: Joi.string().empty('').trim().max(100).label('Includes'),
query_listId: Joi.string().empty('').trim().max(100).label('List-ID'),
query_haYes: Joi.boolean().optional().label('Has attachments'),
query_haNo: Joi.boolean().optional().label('Does not have attachments'),
query_sizeType: Joi.number().label('Size type'),
query_sizeValue: Joi.number()
.empty('')
.label('Size'),
query_sizeUnit: Joi.string()
.valid(['MB', 'kB', 'B'])
.label('Size unit'),

action_seenYes: Joi.boolean()
.optional()
.label('Mark as seen'),
action_flagYes: Joi.boolean()
.optional()
.label('Flag it'),
action_deleteYes: Joi.boolean()
.optional()
.label('Delete'),
action_spamYes: Joi.boolean()
.optional()
.label('Mark as spam'),
action_spamNo: Joi.boolean()
.optional()
.label('Do not mark as spam'),
query_sizeValue: Joi.number().empty('').label('Size'),
query_sizeUnit: Joi.string().valid('MB', 'kB', 'B').label('Size unit'),

action_seenYes: Joi.boolean().optional().label('Mark as seen'),
action_flagYes: Joi.boolean().optional().label('Flag it'),
action_deleteYes: Joi.boolean().optional().label('Delete'),
action_spamYes: Joi.boolean().optional().label('Mark as spam'),
action_spamNo: Joi.boolean().optional().label('Do not mark as spam'),

action_targets: Joi.array()
.items(
Joi.string()
.email()
.trim()
.empty(''),
Joi.string().email().trim().empty(''),
Joi.string()
.trim()
.uri({
scheme: [/smtps?/, /https?/],
allowRelative: false,
relativeOnly: false
relativeOnly: false,
})
.empty('')
)
.empty('')
.empty(''),
};

router.get('/', (req, res, next) => {
Expand All @@ -95,11 +50,11 @@ router.get('/', (req, res, next) => {
activeFilters: true,
filters: filters.map((filter, i) => ({
id: filter.id.toString(),
query: filter.query.map(item => item.filter(val => val).join(': ')).join(', '),
action: filter.action.map(item => item.filter(val => val).join(': ')).join(', '),
index: i + 1
query: filter.query.map((item) => item.filter((val) => val).join(': ')).join(', '),
action: filter.action.map((item) => item.filter((val) => val).join(': ')).join(', '),
index: i + 1,
})),
csrfToken: req.csrfToken()
csrfToken: req.csrfToken(),
});
});
});
Expand All @@ -114,30 +69,25 @@ router.get('/create', (req, res, next) => {
title: 'Create filter',
activeFilters: true,
csrfToken: req.csrfToken(),
mailboxes
mailboxes,
});
});
});

router.get('/edit', (req, res) => {
const updateSchema = Joi.object().keys({
id: Joi.string()
.trim()
.hex()
.length(24)
.label('Filtri ID')
.required()
id: Joi.string().trim().hex().length(24).label('Filtri ID').required(),
});

let result = Joi.validate(req.query, updateSchema, {
abortEarly: false,
convert: true,
allowUnknown: false
allowUnknown: false,
});

if (result.error) {
if (result.error && result.error.details) {
result.error.details.forEach(detail => {
result.error.details.forEach((detail) => {
req.flash('danger', detail.message);
});
}
Expand All @@ -161,45 +111,40 @@ router.get('/edit', (req, res) => {
title: 'Edit filter',
activeFilters: true,
csrfToken: req.csrfToken(),
mailboxes: mailboxes.map(mailbox => {
mailboxes: mailboxes.map((mailbox) => {
if (filter.action.mailbox) {
mailbox.selected = mailbox.id === filter.action.mailbox;
}
return mailbox;
}),
values: filter
values: filter,
});
});
});
});

router.post('/delete', (req, res) => {
const updateSchema = Joi.object().keys({
id: Joi.string()
.trim()
.hex()
.length(24)
.label('Filter ID')
.required()
id: Joi.string().trim().hex().length(24).label('Filter ID').required(),
});

delete req.body._csrf;
let result = Joi.validate(req.body, updateSchema, {
abortEarly: false,
convert: true,
allowUnknown: false
allowUnknown: false,
});

if (result.error) {
if (result.error && result.error.details) {
result.error.details.forEach(detail => {
result.error.details.forEach((detail) => {
req.flash('danger', detail.message);
});
}
return res.redirect('/account/filters');
}

apiClient.filters.del(req.user, result.value.id, err => {
apiClient.filters.del(req.user, result.value.id, (err) => {
if (err) {
req.flash('danger', 'Database Error, failed to update user and delete filter');
return res.redirect('/account/filters');
Expand All @@ -221,15 +166,15 @@ router.post('/create', (req, res, next) => {
.keys({
action_mailbox: Joi.string()
.empty('')
.valid(mailboxes.map(mailbox => mailbox.id))
.valid(mailboxes.map((mailbox) => mailbox.id))
.label('Move to mailbox')
.options({
language: {
any: {
allowOnly: '!!Unknown mailbox'
}
}
})
allowOnly: '!!Unknown mailbox',
},
},
}),
});

delete req.body._csrf;
Expand All @@ -239,7 +184,7 @@ router.post('/create', (req, res, next) => {
let result = Joi.validate(req.body, createSchema, {
abortEarly: false,
convert: true,
allowUnknown: false
allowUnknown: false,
});

let showErrors = (errors, disableDefault) => {
Expand All @@ -254,18 +199,18 @@ router.post('/create', (req, res, next) => {
values: result.value,
errors,
activeFilters: true,
mailboxes: mailboxes.map(mailbox => {
mailboxes: mailboxes.map((mailbox) => {
mailbox.selected = mailbox.id.toString() === result.value.action.mailbox;
return mailbox;
}),
csrfToken: req.csrfToken()
csrfToken: req.csrfToken(),
});
};

if (result.error) {
let errors = {};
if (result.error && result.error.details) {
result.error.details.forEach(detail => {
result.error.details.forEach((detail) => {
let path = detail.path;
if (/^query_size/.test(path)) {
path = 'query_size';
Expand Down Expand Up @@ -300,26 +245,21 @@ router.post('/edit', (req, res, next) => {

const createSchema = Joi.object()
.keys({
id: Joi.string()
.trim()
.hex()
.length(24)
.label('Filtri ID')
.required()
id: Joi.string().trim().hex().length(24).label('Filtri ID').required(),
})
.keys(filterBaseSchema)
.keys({
action_mailbox: Joi.string()
.empty('')
.valid(mailboxes.map(mailbox => mailbox.id))
.valid(mailboxes.map((mailbox) => mailbox.id))
.label('Liiguta kausta')
.options({
language: {
any: {
allowOnly: '!!Tundmatu postkast'
}
}
})
allowOnly: '!!Tundmatu postkast',
},
},
}),
});

delete req.body._csrf;
Expand All @@ -329,7 +269,7 @@ router.post('/edit', (req, res, next) => {
let result = Joi.validate(req.body, createSchema, {
abortEarly: false,
convert: true,
allowUnknown: false
allowUnknown: false,
});

let showErrors = (errors, disableDefault) => {
Expand All @@ -344,18 +284,18 @@ router.post('/edit', (req, res, next) => {
values: result.value,
errors,
activeFilters: true,
mailboxes: mailboxes.map(mailbox => {
mailboxes: mailboxes.map((mailbox) => {
mailbox.selected = mailbox.id.toString() === result.value.action.mailbox;
return mailbox;
}),
csrfToken: req.csrfToken()
csrfToken: req.csrfToken(),
});
};

if (result.error) {
let errors = {};
if (result.error && result.error.details) {
result.error.details.forEach(detail => {
result.error.details.forEach((detail) => {
let path = detail.path;
if (/^query_size/.test(path)) {
path = 'query_size';
Expand All @@ -371,7 +311,7 @@ router.post('/edit', (req, res, next) => {
return showErrors(errors);
}

apiClient.filters.update(req.user, result.value.id, getFilterObject(result.value), err => {
apiClient.filters.update(req.user, result.value.id, getFilterObject(result.value), (err) => {
if (err) {
req.flash('danger', err.message);
return showErrors(false, true);
Expand All @@ -386,7 +326,7 @@ function prepareFilter(filter) {
filter.query = filter.query || {};
filter.action = filter.action || {};

['from', 'to', 'subject', 'text', 'listId'].forEach(key => {
['from', 'to', 'subject', 'text', 'listId'].forEach((key) => {
if (key in filter.query) {
filter['query_' + key] = filter.query[key];
}
Expand Down Expand Up @@ -422,26 +362,26 @@ function prepareFilter(filter) {
filter.query_sizeUnitB = filter.query_sizeUnit === 'B';
}

['seen', 'flag', 'delete', 'spam'].forEach(key => {
['seen', 'flag', 'delete', 'spam'].forEach((key) => {
if (key in filter.action) {
filter['action_' + key + 'Yes'] = !!filter.action[key];
filter['action_' + key + 'No'] = !filter.action[key];
}
});

if (filter.action.targets) {
filter.action_targets = filter.action.targets.map(targetData => (typeof targetData === 'string' ? targetData : targetData.value)).join(', ');
filter.action_targets = filter.action.targets.map((targetData) => (typeof targetData === 'string' ? targetData : targetData.value)).join(', ');
}
}

function getFilterObject(data) {
let filter = {
query: {},
action: {}
action: {},
};

// exact values
['name', 'query_from', 'query_to', 'query_subject', 'query_listId', 'query_text', 'action_mailbox', 'action_targets'].forEach(key => {
['name', 'query_from', 'query_to', 'query_subject', 'query_listId', 'query_text', 'action_mailbox', 'action_targets'].forEach((key) => {
let parts = key.split('_');
let keyName = parts.pop();
let keyPrefix = parts[0] || false;
Expand All @@ -456,7 +396,7 @@ function getFilterObject(data) {
});

// booleans
['query_ha', 'action_seen', 'action_flag', 'action_delete', 'action_spam'].forEach(key => {
['query_ha', 'action_seen', 'action_flag', 'action_delete', 'action_spam'].forEach((key) => {
let parts = key.split('_');
let keyName = parts.pop();
let keyPrefix = parts[0] || false;
Expand Down

0 comments on commit edd5024

Please sign in to comment.