fix sage, param/body data types and projectons

merge-requests/208/head
fatchan 5 years ago
parent 54b5f4c9e1
commit c0be2c0c9f
  1. 7
      controllers/api.js
  2. 7
      controllers/pages.js
  3. 3
      db-models/boards.js
  4. 26
      db-models/posts.js
  5. 24
      helpers/number-converter.js
  6. 1
      models/api/make-post.js
  7. 1
      models/pages/thread.js
  8. 6
      wipe.js

@ -10,10 +10,11 @@ const express = require('express')
, getRecent = require(__dirname+'/../models/api/get-recent.js') , getRecent = require(__dirname+'/../models/api/get-recent.js')
, getThread = require(__dirname+'/../models/api/get-thread.js') , getThread = require(__dirname+'/../models/api/get-thread.js')
, getCatalog = require(__dirname+'/../models/api/get-catalog.js') , getCatalog = require(__dirname+'/../models/api/get-catalog.js')
, getBoards = require(__dirname+'/../models/api/get-boards.js'); , getBoards = require(__dirname+'/../models/api/get-boards.js')
, numberConverter = require(__dirname+'/../helpers/number-converter.js');
// make new post // make new post
router.post('/board/:board', Boards.exists, (req, res, next) => { router.post('/board/:board', Boards.exists, numberConverter, (req, res, next) => {
let numFiles = 0; let numFiles = 0;
if (req.files && req.files.file) { if (req.files && req.files.file) {
@ -59,7 +60,7 @@ router.post('/board/:board', Boards.exists, (req, res, next) => {
}); });
// delete a post. using POST isntead of DELETE because of html forms supprot // delete a post. using POST isntead of DELETE because of html forms supprot
router.post('/board/:board/delete', Boards.exists, (req, res, next) => { router.post('/board/:board/delete', Boards.exists, numberConverter, (req, res, next) => {
const errors = []; const errors = [];

@ -7,7 +7,8 @@ const express = require('express')
, register = require(__dirname+'/../models/pages/register.js') , register = require(__dirname+'/../models/pages/register.js')
, login = require(__dirname+'/../models/pages/login.js') , login = require(__dirname+'/../models/pages/login.js')
, board = require(__dirname+'/../models/pages/board.js') , board = require(__dirname+'/../models/pages/board.js')
, thread = require(__dirname+'/../models/pages/thread.js'); , thread = require(__dirname+'/../models/pages/thread.js')
, numberConverter = require(__dirname+'/../helpers/number-converter.js');
//login page //login page
router.get('/login', login); router.get('/login', login);
@ -19,7 +20,7 @@ router.get('/register', register);
router.get('/', home); router.get('/', home);
// board page/recents // board page/recents
router.get('/:board/:page(\\d+)?', Boards.exists, (req, res, next) => { router.get('/:board/:page(\\d+)?', Boards.exists, numberConverter, (req, res, next) => {
const errors = []; const errors = [];
@ -40,7 +41,7 @@ router.get('/:board/:page(\\d+)?', Boards.exists, (req, res, next) => {
}); });
// thread view page // thread view page
router.get('/:board/thread/:id(\\d+)', Boards.exists, thread); router.get('/:board/thread/:id(\\d+)', Boards.exists, numberConverter, thread);
module.exports = router; module.exports = router;

@ -58,8 +58,7 @@ module.exports = {
} }
); );
// faster than toString() return increment.value.sequence_value;
return increment.value.sequence_value + '';
}, },

@ -104,13 +104,18 @@ module.exports = {
getPost: async (board, id, admin) => { getPost: async (board, id, admin) => {
// get a post // get a post
if (admin) {
return db.collection(board).findOne({
'_id': id
});
}
return db.collection(board).findOne({ return db.collection(board).findOne({
'_id': id '_id': id
}, { }, {
'projection': { 'projection': {
'salt': admin || false, 'salt': 0,
'password': admin || false 'password': 0
//only reveal passwords when admin is true (e.g. getting to check salt)
} }
}); });
@ -119,15 +124,22 @@ module.exports = {
//takes array "ids" of post ids //takes array "ids" of post ids
getPosts: async(board, ids, admin) => { getPosts: async(board, ids, admin) => {
if (admin) {
return db.collection(board).find({
'_id': {
'$in': ids
}
}).toArray();
}
return db.collection(board).find({ return db.collection(board).find({
'_id': { '_id': {
'$in': ids '$in': ids
} }
}, { }, {
'projection': { 'projection': {
'salt': admin || false, 'salt': 0,
'password': admin || false 'password': 0
//only reveal passwords when admin is true (e.g. when fetching for deletion)
} }
}).toArray(); }).toArray();
@ -136,7 +148,7 @@ module.exports = {
insertOne: async (board, data) => { insertOne: async (board, data) => {
// bump thread if name not sage // bump thread if name not sage
if (data.thread !== null && data.author !== 'sage') { if (data.thread !== null && data.name !== 'sage') {
await db.collection(board).updateOne({ await db.collection(board).updateOne({
'_id': data.thread '_id': data.thread
}, { }, {

@ -0,0 +1,24 @@
'use strict';
module.exports = (req, res, next) => {
//for body
if (req.body.thread) {
req.body.thread = +req.body.thread;
}
if (req.body.checked) {
//syntax casts all string to number
req.body.checked = req.body.checked.map(Number);
}
//and for params
if (req.params.id) {
req.params.id = +req.params.id;
}
if (req.params.page) {
req.params.page = +req.params.page;
}
next();
}

@ -165,6 +165,7 @@ module.exports = async (req, res, numFiles) => {
'files': files, 'files': files,
'salt': salt, 'salt': salt,
}; };
const post = await Posts.insertOne(req.params.board, data) const post = await Posts.insertOne(req.params.board, data)
const successRedirect = `/${req.params.board}/thread/${req.body.thread || post.insertedId}`; const successRedirect = `/${req.params.board}/thread/${req.body.thread || post.insertedId}`;

@ -3,6 +3,7 @@
const Posts = require(__dirname+'/../../db-models/posts.js'); const Posts = require(__dirname+'/../../db-models/posts.js');
module.exports = async (req, res) => { module.exports = async (req, res) => {
//get the recently bumped thread & preview posts //get the recently bumped thread & preview posts
let thread; let thread;
try { try {

@ -37,13 +37,15 @@ const Mongo = require(__dirname+'/helpers/db.js')
}) })
console.log('creating indexes') console.log('creating indexes')
await Posts.db.collection('b').createIndex({"thread": 1}); await Posts.db.collection('b').createIndex({"thread": 1});
await Posts.db.collection('b').createIndex({"bumped": 1});
await Posts.db.collection('pol').createIndex({"thread": 1}); await Posts.db.collection('pol').createIndex({"thread": 1});
await Posts.db.collection('pol').createIndex({"bumped": 1});
await readdir('static/img/').then(async files => { await readdir('static/img/').then(async files => {
await Promise.all(files.map(async file => { await Promise.all(files.map(async file => {
unlink(path.join('static/img/', file)); unlink(path.join('static/img/', file));
})) }))
}); });
console.log('creating admin account: admin:changeme'); // console.log('creating admin account: admin:changeme');
await Accounts.insertOne('admin', 'changeme', 3); // await Accounts.insertOne('admin', 'changeme', 3);
console.log('done'); console.log('done');
})(); })();

Loading…
Cancel
Save