mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-06-15 13:20:52 -05:00
update comments
This commit is contained in:
parent
47ea7b157b
commit
815e5aba03
|
|
@ -1,3 +1,10 @@
|
|||
/*
|
||||
|
||||
api.js -
|
||||
common api returns
|
||||
|
||||
*/
|
||||
|
||||
// use for any api return. it has basic layout used for every return.
|
||||
function sendApiReturn(res, data, errors) {
|
||||
res.status(200).json(
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ small commonly used utilities
|
|||
|
||||
*/
|
||||
|
||||
// shows 404 template.
|
||||
// shows 404 template. takes express response object
|
||||
function sendDefault404(res) {
|
||||
res.status(404).send('404');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ function adminAuthenticationRequired(req, res, next) {
|
|||
}
|
||||
}
|
||||
|
||||
// middleware to use if authentication
|
||||
// middleware to use if authentication is optional
|
||||
function authenticationOptional(req, res, next) {
|
||||
return next();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ const adminUserSchema = new mongoose.Schema({
|
|||
unique: true,
|
||||
trim: true
|
||||
},
|
||||
// non hashed, gets hashed at save
|
||||
password: {
|
||||
type: String,
|
||||
required: [true, 'Password is required.'],
|
||||
|
|
|
|||
|
|
@ -21,10 +21,12 @@ const blogPostSchema = new mongoose.Schema({
|
|||
required: [true, 'Content is required.'],
|
||||
trim: true
|
||||
},
|
||||
// title of blog post
|
||||
name: {
|
||||
type: String,
|
||||
required: [true, 'Name is required']
|
||||
},
|
||||
// short description of blog post
|
||||
short: {
|
||||
type: String,
|
||||
required: [true, 'Short version is required']
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ const postAuthorSchema = new mongoose.Schema({
|
|||
required: [true, 'Description is required'],
|
||||
trim: true
|
||||
},
|
||||
// profile picture image link
|
||||
image: {
|
||||
type: String,
|
||||
trim: true
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ const progressListSchema = new mongoose.Schema({
|
|||
default: 0
|
||||
},
|
||||
isGame: {
|
||||
type: Boolean, /* true - is game list, false - is backend service */
|
||||
type: Boolean, /* true - is game, false - is backend service */
|
||||
required: [true, 'isGame is required']
|
||||
},
|
||||
title: {
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ module.exports = (app) => {
|
|||
});
|
||||
}
|
||||
));
|
||||
//Configuring app to have sessions
|
||||
//Configuring app to have sessions, dont change since it would break everything
|
||||
passport.serializeUser(function(user, done) {
|
||||
done(null, user.id);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
|
||||
admin.js -
|
||||
file for handling admin panel routes
|
||||
file for handling admin api.
|
||||
|
||||
*/
|
||||
|
||||
|
|
@ -11,12 +11,14 @@ const passport = require('passport');
|
|||
const moment = require('moment');
|
||||
const apiHelper = require('../helpers/api');
|
||||
const adminUserMiddleware = require('../middleware/admin-authentication');
|
||||
|
||||
// database models
|
||||
const adminUser = require('../models/admin-user');
|
||||
const blogPost = require('../models/blog-post');
|
||||
const postAuthor = require('../models/post-author');
|
||||
const progressList = require('../models/progress-list');
|
||||
|
||||
// display admin panel
|
||||
// renders admin.hbs
|
||||
router.get('/admin', (req, res) => {
|
||||
res.render('admin');
|
||||
});
|
||||
|
|
@ -27,18 +29,19 @@ router.get('/admin', (req, res) => {
|
|||
* signs admin user in
|
||||
*
|
||||
* post {
|
||||
* username - username of admin account
|
||||
* password - password of admin account
|
||||
* username
|
||||
* password
|
||||
* }
|
||||
* return {
|
||||
* code: http code
|
||||
* success: boolean - true if login succesfull
|
||||
* username: undefined | string - username if login was successfull
|
||||
* success: boolean
|
||||
* username: undefined | string - only if login was successfull
|
||||
* role: undefined | string - role of user if login was successfull
|
||||
* errors: Strings[messages] - not yet :(
|
||||
* }
|
||||
*/
|
||||
// TODO make login somehow display errors in correct format.
|
||||
// middleware does the authentication work. this just returns a success
|
||||
router.post('/admin/api/v1/login', passport.authenticate('adminUserStrategy'), function (req, res) {
|
||||
apiHelper.sendApiReturn(res, {
|
||||
username: req.user.username,
|
||||
|
|
@ -77,8 +80,8 @@ router.post('/admin/api/v1/register', adminUserMiddleware.adminAuthenticationReq
|
|||
password
|
||||
});
|
||||
|
||||
// saving to database
|
||||
newUser.save().then(() => {
|
||||
// successfull
|
||||
apiHelper.sendApiReturn(res, {
|
||||
username: req.user.username,
|
||||
role: req.user.role ? req.user.role : undefined
|
||||
|
|
@ -108,6 +111,7 @@ router.post('/admin/api/v1/register', adminUserMiddleware.adminAuthenticationReq
|
|||
*/
|
||||
router.post('/admin/api/v1/removeadmin', adminUserMiddleware.adminAuthenticationRequired, (req, res) => {
|
||||
if (!req.body) {
|
||||
// no post body
|
||||
apiHelper.sendApiGenericError(res);
|
||||
return;
|
||||
}
|
||||
|
|
@ -137,11 +141,13 @@ router.get('/admin/api/v1/listadmins', adminUserMiddleware.adminAuthenticationRe
|
|||
// TODO format exception so it doesnt have a huge list of errors
|
||||
if (err) return apiHelper.sendApiError(res, 500, [err]);
|
||||
|
||||
// formats admin list and removes password hash
|
||||
const output = [];
|
||||
for (let i = 0, l = admins.length; i < l; i++) {
|
||||
admins[i].password = undefined;
|
||||
output.push(admins[i]);
|
||||
}
|
||||
|
||||
apiHelper.sendApiReturn(res, {
|
||||
admins: output
|
||||
});
|
||||
|
|
@ -216,7 +222,7 @@ router.post('/admin/api/v1/newpost', adminUserMiddleware.adminAuthenticationRequ
|
|||
meta: {
|
||||
author,
|
||||
category,
|
||||
slug: title
|
||||
slug: title // convert title to slug
|
||||
.trim()
|
||||
.replace(/\s/g, '-')
|
||||
.replace(/[^A-z0-9-]/g, '')
|
||||
|
|
@ -224,8 +230,8 @@ router.post('/admin/api/v1/newpost', adminUserMiddleware.adminAuthenticationRequ
|
|||
}
|
||||
});
|
||||
|
||||
// saving post to database
|
||||
newBlogPost.save().then((post) => {
|
||||
// successfull
|
||||
apiHelper.sendApiReturn(res, {
|
||||
url: moment(post.meta.date, 'YYYY-MM-DD') + '/' + post.meta.slug
|
||||
});
|
||||
|
|
@ -269,6 +275,7 @@ router.post('/admin/api/v1/editpost', adminUserMiddleware.adminAuthenticationReq
|
|||
'meta.category': category
|
||||
}, (err, post) => {
|
||||
if (err) return apiHelper.sendApiError(res, 500, [err]);
|
||||
|
||||
apiHelper.sendApiReturn(res, {
|
||||
url: moment(post.meta.date, 'YYYY-MM-DD') + '/' + post.meta.slug
|
||||
});
|
||||
|
|
@ -303,8 +310,8 @@ router.post('/admin/api/v1/newauthor', adminUserMiddleware.adminAuthenticationRe
|
|||
image
|
||||
});
|
||||
|
||||
// saving author to database
|
||||
newAuthor.save().then((author) => {
|
||||
// successfull
|
||||
apiHelper.sendApiReturn(res, {
|
||||
id: author.id
|
||||
});
|
||||
|
|
@ -339,6 +346,7 @@ router.post('/admin/api/v1/editauthor', adminUserMiddleware.adminAuthenticationR
|
|||
|
||||
const { id, name, description, image } = req.body;
|
||||
|
||||
// updating author in database
|
||||
postAuthor.postAuthorModel.findByIdAndUpdate(id, {
|
||||
name,
|
||||
description,
|
||||
|
|
@ -373,6 +381,7 @@ router.post('/admin/api/v1/newprogress', adminUserMiddleware.adminAuthentication
|
|||
|
||||
if (!req.body) return apiHelper.sendApiGenericError(res);
|
||||
|
||||
// parses state and isGame to be valid
|
||||
const { title, description } = req.body;
|
||||
let { state } = req.body;
|
||||
let isGame = false;
|
||||
|
|
@ -390,8 +399,8 @@ router.post('/admin/api/v1/newprogress', adminUserMiddleware.adminAuthentication
|
|||
state
|
||||
});
|
||||
|
||||
// saving progress to database
|
||||
newProgress.save().then((progress) => {
|
||||
// successfull
|
||||
apiHelper.sendApiReturn(res, {
|
||||
id: progress.id
|
||||
});
|
||||
|
|
@ -424,6 +433,7 @@ router.post('/admin/api/v1/editprogress', adminUserMiddleware.adminAuthenticatio
|
|||
|
||||
if (!req.body) return apiHelper.sendApiGenericError(res);
|
||||
|
||||
// parsing state and isGame to be valid
|
||||
const { title, description, id } = req.body;
|
||||
let { state } = req.body;
|
||||
let isGame = false;
|
||||
|
|
@ -434,6 +444,7 @@ router.post('/admin/api/v1/editprogress', adminUserMiddleware.adminAuthenticatio
|
|||
isGame = true;
|
||||
}
|
||||
|
||||
// updating progress in database
|
||||
progressList.progressListModel.findByIdAndUpdate(id, {
|
||||
title,
|
||||
description,
|
||||
|
|
@ -448,7 +459,7 @@ router.post('/admin/api/v1/editprogress', adminUserMiddleware.adminAuthenticatio
|
|||
});
|
||||
});
|
||||
|
||||
// configure api 404
|
||||
// api 404
|
||||
router.use('/admin/api', (req, res) => {
|
||||
apiHelper.sendApi404(res);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ const postAuthorModel = require('../models/post-author').postAuthorModel;
|
|||
|
||||
// display single blog post
|
||||
router.get('/news/:date/:title', (req, res) => {
|
||||
// date format DD-MM-YYY
|
||||
// date format YYYY-MM-DD
|
||||
if (/[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(req.params.date) && /([a-z]|[0-9]|-)+/.test(req.params.title.toLowerCase())) {
|
||||
// params are correct format
|
||||
blogPostModel.getPost(new Date(req.params.date), req.params.title.toLowerCase(), (err, post) => {
|
||||
|
|
@ -40,11 +40,13 @@ router.get('/news/:date/:title', (req, res) => {
|
|||
|
||||
// display latest blogposts
|
||||
router.get('/news', (req, res) => {
|
||||
// sort blogposts on date descending
|
||||
blogPostModel.find({}).sort({'meta.date': 'desc'}).exec(function(err, posts) {
|
||||
if (err || !posts) {
|
||||
return utilHelper.sendDefault404(res);
|
||||
}
|
||||
|
||||
// makes posts template ready
|
||||
const postCollection = [];
|
||||
for (let i = 0, l = posts.length; i < l; i++) {
|
||||
postCollection.push(posts[i].getBlogPostShortTemplateReady());
|
||||
|
|
|
|||
|
|
@ -35,13 +35,15 @@ router.get('/contact', (req, res) => {
|
|||
router.post('/api/v1/sendmessage', function (req, res) {
|
||||
if (!req.body) return apiHelper.sendApiGenericError(res);
|
||||
|
||||
|
||||
const { email, subject, message } = req.body;
|
||||
if (email && subject && message && message.length < 2000) {
|
||||
console.log('checks passed');
|
||||
// request body has everything
|
||||
const postData = JSON.stringify({
|
||||
content: 'email: ' + email + ' \n subject: ' + subject + ' \n\n' + message
|
||||
});
|
||||
|
||||
// request object
|
||||
const request = https.request({
|
||||
hostname: config.contactWebhook.host,
|
||||
port: config.contactWebhook.port,
|
||||
|
|
@ -53,14 +55,17 @@ router.post('/api/v1/sendmessage', function (req, res) {
|
|||
'Content-Length': postData.length
|
||||
}
|
||||
}, () => {
|
||||
// sends success
|
||||
apiHelper.sendApiReturn(res, {});
|
||||
});
|
||||
|
||||
// error handling
|
||||
request.on('error', (e) => {
|
||||
apiHelper.sendApiGenericError(res);
|
||||
console.log('request errored' + e);
|
||||
});
|
||||
|
||||
// write post data to request
|
||||
request.write(postData);
|
||||
request.end();
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ router.get('/progress', (req, res) => {
|
|||
progressListModel.find({}, (err, progress) => {
|
||||
if (err) return utilHelper.sendDefault404(res);
|
||||
|
||||
// filtering games and backend
|
||||
const games = progress.filter(i => i.isGame);
|
||||
const backends = progress.filter(i => !i.isGame);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user