From dac1193a78419f6855343d8776039acdab6c1f6f Mon Sep 17 00:00:00 2001 From: Ro Date: Sat, 16 Nov 2019 09:10:05 -0800 Subject: [PATCH] removed all sequelize declarations... whew... --- brain/api/content/posts.js | 100 +------- brain/api/content/settings.js | 235 +++++++++--------- brain/app.js | 14 +- brain/routes/{back => dash}/index.js | 0 .../routes/{back/dash_nav.js => dash/nav.js} | 0 .../{back/dash_posts.js => dash/posts.js} | 7 +- .../dash_settings.js => dash/settings.js} | 0 7 files changed, 130 insertions(+), 226 deletions(-) rename brain/routes/{back => dash}/index.js (100%) rename brain/routes/{back/dash_nav.js => dash/nav.js} (100%) rename brain/routes/{back/dash_posts.js => dash/posts.js} (95%) rename brain/routes/{back/dash_settings.js => dash/settings.js} (100%) diff --git a/brain/api/content/posts.js b/brain/api/content/posts.js index edcbb23..051a386 100644 --- a/brain/api/content/posts.js +++ b/brain/api/content/posts.js @@ -1,18 +1,17 @@ -import DateUtils from '../../tools/utilities/DateUtils'; -import StringUtils from '../../tools/utilities/StringUtils'; -import * as DataEvent from '../../tools/events/DataEvent'; +import DateUtils from '../../utils/tools/DateUtils'; +import StringUtils from '../../utils/tools/StringUtils'; +import * as DataEvent from '../../utils/events/DataEvent'; import sanitize from 'sanitize-html'; import RightsManager, { TASK_CREATE, TASK_UPDATE, OBJECT_POST -} from '../../tools/utilities/RightsManager'; +} from '../../utils/tools/RightsManager'; const express = require('express'); const router = express.Router(); const multer = require('multer'); const md = require('markdown-it')('commonmark'); const fs = require('fs-extra'); -const Models = require('../../models'); const dateUtils = new DateUtils(); const rightsManager = new RightsManager(); const uploadPath = @@ -36,6 +35,7 @@ var storage = multer.diskStorage({ cb(null, splice[0]); } }); + var feature_upload = multer({ storage: storage }).array('feature_image'); @@ -45,99 +45,9 @@ var post_upload = multer({ //** SYNC POSTS */ router.post('/sync', (req, res) => { let payload = req.body; - Models.User.findById(req.session.user.id).then(user => { - if (rightsManager.check(user.role, OBJECT_POST, TASK_UPDATE)) { - for (let index = 0; index < payload.length; index++) { - const item = payload[index]; - Models.FreshPost.findOne({ - where: { - post: { - [Op.contains]: { - uuid: item.post.uuid - } - } - } - }) - .then(found => { - let buffed = sanitize(item.post.plaintext, { - allowedTags: ['del', 'a', 'iframe', 'img'], - allowedAttributes: { - a: ['href', 'name', 'target'], - img: ['src'], - iframe: [ - 'height', - 'width', - 'src', - 'frameborder', - 'allow', - 'allowfullscreen' - ] - } - }); - buffed = new StringUtils().decodeHTML(buffed); - item.post.plaintext = buffed; - item.post.html = md.render(buffed, { - html: true, - xhtmlOut: true - }); - if (!_.isEqual(item.post, found.post)) { - found - .update(item) - .then(() => { - //console.log('UPDATED', updated); - }) - .catch(() => { - //console.log("***ERROR***", err); - }); - } else { - //chilld - } - }) - .catch(() => { - //console.log("***ERRRORZ****", err); - Models.FreshPost.create(item).then(() => { - //console.log(fresh) - }); - }); - } - res.json({ - message: 'postsSynced' - }); - } else { - res.json({ - message: "Nah. You can't do that. Talk to the admin, sport." - }); - } - }); }); router.get('/json/:filter?', function(req, res) { var filter = req.params.filter; - Models.FreshPost.findAll({ - order: [['id', 'DESC']] - }) - .then(function(posts) { - let newlist = []; - for (let index = 0; index < posts.length; index++) { - let item = posts[index].post; - switch (filter) { - case 'not-deleted': - if (typeof item.deleted == 'undefined' || item.deleted == false) { - newlist.push(posts[index]); - } else { - continue; - } - break; - - default: - newlist.push(posts[index]); - break; - } - } - res.json(newlist); - }) - .catch(() => { - //next(err); - }); }); /*** POST IMAGE diff --git a/brain/api/content/settings.js b/brain/api/content/settings.js index 5357429..0f468a7 100644 --- a/brain/api/content/settings.js +++ b/brain/api/content/settings.js @@ -1,16 +1,15 @@ -import DateUtils from '../../tools/utilities/DateUtils'; -import * as DataEvent from '../../tools/events/DataEvent'; +import DateUtils from '../../utils/tools/DateUtils'; +import * as DataEvent from '../../utils/events/DataEvent'; import RightsManager, { TASK_CREATE, TASK_UPDATE, OBJECT_POST, OBJECT_SETTINGS -} from '../../tools/utilities/RightsManager'; +} from '../../utils/tools/RightsManager'; const express = require('express'); const router = express.Router(); const multer = require('multer'); const fs = require('fs-extra'); -const Models = require('../../models'); const dateUtils = new DateUtils(); const rightsManager = new RightsManager(); const uploadPath = @@ -40,56 +39,54 @@ var background_upload = multer({ //** SYNC POSTS */ router.post('/sync', (req, res) => { let payload = req.body; - Models.User.findById(req.session.user.id).then(user => { - if (rightsManager.check(user.role, OBJECT_SETTINGS, TASK_UPDATE)) { - fs.readJson('config/site-settings.json') - .then(obj => { - if (user.hande != payload.handle || user.email != payload.email) { - user.update({ - handle: payload.handle, - email: payload.email - }) - .then(() => { - //console.log('UPDATED'); - }) - .catch(() => { - //console.log('ERR', err); - }); - } else { - //console.log('USER NOT UPDATED'); - } - obj.url = payload.url; - obj.title = payload.title; - obj.description = payload.descriptions; - obj.private = payload.private; - obj.theme = payload.theme; - obj.background = payload.background; //TODO: make this url relative - //mail stuff - obj.email.active = payload.mailSettings.activeProtocol; - obj.email.smtp.domain = payload.mailSettings.smtp.domain; - obj.email.smtp.email = payload.mailSettings.smtp.email; - obj.email.smtp.password = payload.mailSettings.smtp.password; - obj.email.mailgun.domain = payload.mailSettings.mailgun.domain; - obj.email.mailgun['api-key'] = payload.mailSettings.mailgun.key; - fs.writeJson('config/site-settings.json', obj) + if (rightsManager.check(user.role, OBJECT_SETTINGS, TASK_UPDATE)) { + fs.readJson('config/site-settings.json') + .then(obj => { + if (user.hande != payload.handle || user.email != payload.email) { + user.update({ + handle: payload.handle, + email: payload.email + }) .then(() => { - res.json({ - message: DataEvent.SETTINGS_UPDATED - }); + //console.log('UPDATED'); }) .catch(() => { - //console.error(err); + //console.log('ERR', err); }); - }) - .catch(() => { - //console.error(err); - }); - } else { - res.json({ - message: "Nah. You can't do that. Talk to the admin, sport." + } else { + //console.log('USER NOT UPDATED'); + } + obj.url = payload.url; + obj.title = payload.title; + obj.description = payload.descriptions; + obj.private = payload.private; + obj.theme = payload.theme; + obj.background = payload.background; //TODO: make this url relative + //mail stuff + obj.email.active = payload.mailSettings.activeProtocol; + obj.email.smtp.domain = payload.mailSettings.smtp.domain; + obj.email.smtp.email = payload.mailSettings.smtp.email; + obj.email.smtp.password = payload.mailSettings.smtp.password; + obj.email.mailgun.domain = payload.mailSettings.mailgun.domain; + obj.email.mailgun['api-key'] = payload.mailSettings.mailgun.key; + fs.writeJson('config/site-settings.json', obj) + .then(() => { + res.json({ + message: DataEvent.SETTINGS_UPDATED + }); + }) + .catch(() => { + //console.error(err); + }); + }) + .catch(() => { + //console.error(err); }); - } - }); + } else { + res.json({ + message: "Nah. You can't do that. Talk to the admin, sport." + }); + } }); router.get('/nav', function(req, res) { @@ -110,30 +107,28 @@ router.get('/nav', function(req, res) { router.post('/nav-sync', (req, res) => { let payload = req.body; - Models.User.findById(req.session.user.id).then(user => { - if (rightsManager.check(user.role, OBJECT_SETTINGS, TASK_UPDATE)) { - fs.readJson('config/site-settings.json') - .then(obj => { - obj.menu = payload; - fs.writeJson('config/site-settings.json', obj) - .then(() => { - res.json({ - message: DataEvent.SETTINGS_UPDATED - }); - }) - .catch(() => { - //console.error(err); + if (rightsManager.check(user.role, OBJECT_SETTINGS, TASK_UPDATE)) { + fs.readJson('config/site-settings.json') + .then(obj => { + obj.menu = payload; + fs.writeJson('config/site-settings.json', obj) + .then(() => { + res.json({ + message: DataEvent.SETTINGS_UPDATED }); - }) - .catch(() => { - //console.error(err); - }); - } else { - res.json({ - message: "Nah. You can't do that. Talk to the admin, sport." + }) + .catch(() => { + //console.error(err); + }); + }) + .catch(() => { + //console.error(err); }); - } - }); + } else { + res.json({ + message: "Nah. You can't do that. Talk to the admin, sport." + }); + } }); /*** @@ -145,37 +140,35 @@ router.post('/add-avatar', function(req, res) { return res.json({ message: 'You need to be logged in, champ.' }); - Models.User.findById(req.session.user.id).then(user => { - if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE)) { - avatar_upload(req, res, function(err) { - if (err) { - res.json({ - message: err - }); - throw err; - } else { - var avatar = req.files[0].path; - user.update({ - avatar: avatar.substr(7, avatar.length) + if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE)) { + avatar_upload(req, res, function(err) { + if (err) { + res.json({ + message: err + }); + throw err; + } else { + var avatar = req.files[0].path; + user.update({ + avatar: avatar.substr(7, avatar.length) + }) + .then(updated => { + req.session.user = updated; }) - .then(updated => { - req.session.user = updated; - }) - .catch(() => { - //console.log('ERR', err); - }); - return res.json({ - message: DataEvent.AVATAR_UPLOADED, - url: avatar.substr(7, avatar.length) + .catch(() => { + //console.log('ERR', err); }); - } - }); - } else { - res.json({ - message: "Nah. You can't do that. Talk to the admin, sport." - }); - } - }); + return res.json({ + message: DataEvent.AVATAR_UPLOADED, + url: avatar.substr(7, avatar.length) + }); + } + }); + } else { + res.json({ + message: "Nah. You can't do that. Talk to the admin, sport." + }); + } }); /*** UPLOAD FEATURE BACKGROUND @@ -186,27 +179,25 @@ router.post('/add-feature-background', function(req, res) { return res.json({ message: 'You need to be logged in, champ.' }); - Models.User.findById(req.session.user.id).then(user => { - if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE)) { - background_upload(req, res, function(err) { - if (err) { - res.json({ - message: err - }); - throw err; - } else { - var bgImage = req.files[0].path; - return res.json({ - message: DataEvent.SITE_BACKGROUND_UPLOADED, - url: bgImage.substr(7, bgImage.length) - }); - } - }); - } else { - res.json({ - message: "Nah. You can't do that. Talk to the admin, sport." - }); - } - }); + if (rightsManager.check(user.role, OBJECT_POST, TASK_CREATE)) { + background_upload(req, res, function(err) { + if (err) { + res.json({ + message: err + }); + throw err; + } else { + var bgImage = req.files[0].path; + return res.json({ + message: DataEvent.SITE_BACKGROUND_UPLOADED, + url: bgImage.substr(7, bgImage.length) + }); + } + }); + } else { + res.json({ + message: "Nah. You can't do that. Talk to the admin, sport." + }); + } }); module.exports = router; diff --git a/brain/app.js b/brain/app.js index befe22d..876ac4b 100644 --- a/brain/app.js +++ b/brain/app.js @@ -41,11 +41,11 @@ app.use( ); app.use(flash()); //sections -var front = require('./routes/front/index')(session); -var back = require('./routes/back/index'); -var postDashboard = require('./routes/back/dash_posts'); -var settingsDashboard = require('./routes/back/dash_settings'); -var navDashboard = require('./routes/back/dash_nav'); +//var front = require('./routes/front/index')(session); +var dash = require('./routes/dash/index'); +var postDashboard = require('./routes/dash/posts'); +var settingsDashboard = require('./routes/dash/settings'); +var navDashboard = require('./routes/dash/nav'); //api var postLibrary = require('./api/content/posts'); var settings = require('./api/content/settings'); @@ -58,8 +58,8 @@ var auth = require('./api/v1/auth'); app.use('/api/mail', mailer); app.use('/api/v1/auth', auth); // PAGES -app.use('/', front); -app.use('/@/dashboard', back); +//app.use('/', front); +app.use('/@/dashboard', dash); app.use('/@/dashboard/posts', postDashboard); app.use('/@/dashboard/settings', settingsDashboard); app.use('/@/dashboard/navigation', navDashboard); diff --git a/brain/routes/back/index.js b/brain/routes/dash/index.js similarity index 100% rename from brain/routes/back/index.js rename to brain/routes/dash/index.js diff --git a/brain/routes/back/dash_nav.js b/brain/routes/dash/nav.js similarity index 100% rename from brain/routes/back/dash_nav.js rename to brain/routes/dash/nav.js diff --git a/brain/routes/back/dash_posts.js b/brain/routes/dash/posts.js similarity index 95% rename from brain/routes/back/dash_posts.js rename to brain/routes/dash/posts.js index fd14a9a..f5b958b 100644 --- a/brain/routes/back/dash_posts.js +++ b/brain/routes/dash/posts.js @@ -1,4 +1,4 @@ -import DateUtils from '../../tools/utilities/DateUtils'; +import DateUtils from '../../utils/tools/DateUtils'; var express = require('express'); var router = express.Router(); var hljs = require('highlight.js/lib/highlight'); @@ -12,8 +12,10 @@ const dateUtils = new DateUtils(); router.get('/list/:filter?/:page?', function(req, res) { var pageNum = req.params.page; var filter = req.params.filter; + if (pageNum == '' || pageNum == null) pageNum = 1; if (filter == '' || filter == null) filter = 'all'; + /* if (req.session.user) { .then(() => { @@ -25,6 +27,7 @@ router.get('/list/:filter?/:page?', function(req, res) { } else { res.redirect('/@/dashboard'); } + */ }); //-------------------------- // BLOG POST ADD DISPLAY @@ -54,7 +57,7 @@ router.get('/add/new', function(req, res) { //-------------------------- router.get('/edit/:id', function(req, res) { if (req.session.user) { - } else { + } else { res.redirect('/@/dashboard'); } }); diff --git a/brain/routes/back/dash_settings.js b/brain/routes/dash/settings.js similarity index 100% rename from brain/routes/back/dash_settings.js rename to brain/routes/dash/settings.js