Added adminpanel to this repo

This commit is contained in:
Kasper Rynning-Tønnesen
2018-02-05 19:46:03 +01:00
parent 38bceb1a53
commit 01a42c7d68
40 changed files with 1561 additions and 35 deletions

230
server/routing/admin/api.js Normal file
View File

@@ -0,0 +1,230 @@
var express = require('express');
var router = express.Router();
var path = require('path');
var mongo_db_cred = require(path.join(__dirname, '../../config/mongo_config.js'));
var mongojs = require('mongojs');
var db = mongojs(mongo_db_cred.config);
router.use(function(req, res, next) {
next(); // make sure we go to the next routes and don't stop here
});
router.route('/api/lists').get(function(req, res){
if(req.isAuthenticated()){
db.collection("frontpage_lists").find().sort({count: -1},function(err, docs){
res.json(docs);
});
} else {
res.send(false);
}
});
router.route('/api/thumbnails').get(function(req, res){
if(req.isAuthenticated()){
db.collection("suggested_thumbnails").find(function(err, docs){
res.json(docs);
});
} else {
res.send(false);
}
});
router.route('/api/descriptions').get(function(req, res){
if(req.isAuthenticated()){
db.collection("suggested_descriptions").find(function(err, docs){
res.json(docs);
});
} else {
res.send(false);
}
});
router.route('/api/approve_thumbnail').post(function(req, res){
if(req.isAuthenticated()){
var channel = req.param("channel");
db.collection("suggested_thumbnails").find({channel: channel}, function(err, docs){
var thumbnail = docs[0].thumbnail;
db.collection("frontpage_lists").update({_id: channel}, {$set:{thumbnail: thumbnail}}, {upsert: true}, function(err, docs){
db.collection(channel).update({views:{$exists:true}}, {$set:{thumbnail: thumbnail}}, {upsert: true}, function(err, docs){
db.collection("suggested_thumbnails").remove({channel: channel}, function(err, docs){
res.send(true);
});
});
});
});
} else {
res.send(false);
}
});
router.route('/api/deny_thumbnail').post(function(req, res){
if(req.isAuthenticated()){
var channel = req.param("channel");
db.collection("suggested_thumbnails").remove({channel: channel},function(err, docs){
res.send(true);
});
} else {
res.send(false);
}
});
router.route('/api/approve_description').post(function(req, res){
if(req.isAuthenticated()){
var channel = req.param("channel");
db.collection("suggested_descriptions").find({channel: channel}, function(err, docs){
var description = docs[0].description;
db.collection("frontpage_lists").update({_id: channel}, {$set:{description: description}}, {upsert: true}, function(err, docs){
db.collection(channel).update({views:{$exists:true}}, {$set:{description: description}}, function(err, docs){
db.collection("suggested_descriptions").remove({channel: channel}, function(err, docs){
res.send(true);
});
});
});
});
} else {
res.send(false);
}
});
router.route('/api/deny_description').post(function(req, res){
if(req.isAuthenticated()){
var channel = req.param("channel");
db.collection("suggested_descriptions").remove({channel: channel}, 1,function(err, docs){
res.send(true);
});
} else {
res.send(false);
}
});
router.route('/api/remove_thumbnail').post(function(req, res){
if(req.isAuthenticated()){
var channel = req.param("channel");
db.collection("frontpage_lists").update({_id: channel}, {$set:{thumbnail: ""}}, function(err, docs){
db.collection(channel).update({views:{$exists:true}}, {$set:{thumbnail: ""}}, function(err, docs){
res.send(true);
});
});
} else {
res.send(false);
}
});
router.route('/api/remove_description').post(function(req, res){
if(req.isAuthenticated()){
var channel = req.param("channel");
db.collection("frontpage_lists").update({_id: channel}, {$set:{description: ""}}, function(err, docs){
db.collection(channel).update({views:{$exists:true}}, {$set:{description: ""}}, function(err, docs){
res.send(true);
});
});
} else {
res.send(false);
}
});
router.route('/api/names').get(function(req, res) {
if(req.isAuthenticated()){
db.collection("registered_users").find({_id: {$exists: true}}, {_id: 1, icon: 1}, function(err, docs) {
res.json(docs);
})
} else {
res.send(false);
}
});
router.route('/api/names').post(function(req, res) {
if(req.isAuthenticated()) {
var icon = req.param("icon");
var name = req.param("name");
db.collection("registered_users").update({_id: name}, {$set: {icon: icon}}, function(err, docs) {
if(err) res.send(false);
else res.send(true);
});
} else {
res.send(false);
}
})
router.route('/api/token').get(function(req, res){
if(req.isAuthenticated()){
token_db.collection("tokens").find(function(err, docs){
if(docs.length == 1){
res.json({token: docs[0].token});
} else {
var id = new Buffer(makeid()).toString('base64');
token_db.collection("tokens").insert({token: id}, function(err, docs){
res.json({token: id});
});
}
})
} else {
res.send(false);
}
});
router.route('/api/delete').post(function(req, res){
if(req.isAuthenticated()){
var list = req.param("_id");
db.collection(list).drop(function(err, docs){
db.collection("frontpage_lists").remove({_id: list}, function(err, docs){
res.send(true);
})
});
} else {
res.send(false);
}
});
router.route('/api/remove_token').get(function(req, res){
if(req.isAuthenticated()){
token_db.collection("tokens").find(function(err, docs){
if(docs.length == 1){
token_db.collection("tokens").remove({token: docs[0].token}, function(err, docs){
res.send(true);
})
} else {
res.send(false);
}
})
} else {
res.send(false);
}
});
router.route('/api/pinned').post(function(req, res){
if(req.isAuthenticated()){
var to_pin = req.param("_id");
db.collection("frontpage_lists").update({pinned:1}, {$set:{pinned:0}}, function(err, resp){
db.collection("frontpage_lists").update({_id:to_pin}, {$set:{pinned:1}}, function(err, resp){
res.send(true);
});
});
} else {
res.send(false);
}
});
router.route('/api/admin').post(function(req, res){
if(req.isAuthenticated()){
var to_remove = req.param("_id");
db.collection(to_remove).update({views: {$exists: true}}, {$set:{adminpass: ""}}, function(err, docs){
res.send(true);
});
} else {
res.send(false);
}
});
router.route('/api/userpass').post(function(req, res){
if(req.isAuthenticated()){
var to_remove = req.param("_id");
db.collection(to_remove).update({views: {$exists: true}}, {$set:{userpass: ""}}, function(err, docs){
res.send(true);
});
} else {
res.send(false);
}
});
module.exports = router;

View File

@@ -94,8 +94,8 @@ router.route('/api/imageblob').post(function(req, res) {
var nodemailer = require('nodemailer');
try {
var mailconfig = require(path.join(__dirname, '../config/mailconfig.js'));
var recaptcha_config = require(path.join(__dirname, '../config/recaptcha.js'));
var mailconfig = require(path.join(__dirname, '../../config/mailconfig.js'));
var recaptcha_config = require(path.join(__dirname, '../../config/recaptcha.js'));
var Recaptcha = require('express-recaptcha');
var RECAPTCHA_SITE_KEY = recaptcha_config.site;
var RECAPTCHA_SECRET_KEY = recaptcha_config.key;

View File

@@ -6,7 +6,7 @@ var path = require('path');
try {
var Recaptcha = require('express-recaptcha');
var recaptcha_config = require(path.join(path.join(__dirname, '../config/'), 'recaptcha.js'));
var recaptcha_config = require(path.join(path.join(__dirname, '../../config/'), 'recaptcha.js'));
var RECAPTCHA_SITE_KEY = recaptcha_config.site;
var RECAPTCHA_SECRET_KEY = recaptcha_config.key;
var recaptcha = new Recaptcha(RECAPTCHA_SITE_KEY, RECAPTCHA_SECRET_KEY);
@@ -56,7 +56,7 @@ function root(req, res, next) {
javascript_file: "remote.min.js",
captcha: res.recaptcha
}
res.render('layouts/remote', data);
res.render('layouts/client/remote', data);
} else if(subdomain[0] == "www") {
res.redirect("https://zoff.me");
} else {
@@ -65,7 +65,7 @@ function root(req, res, next) {
javascript_file: "main.min.js",
captcha: res.recaptcha
}
res.render('layouts/frontpage', data);
res.render('layouts/client/frontpage', data);
}
} catch(e) {
//console.log(e);
@@ -87,7 +87,7 @@ function channel(req, res, next) {
javascript_file: "remote.min.js",
captcha: res.recaptcha
}
res.render('layouts/remote', data);
res.render('layouts/client/remote', data);
} else if(subdomain.length >= 2 && subdomain[0] == "www") {
res.redirect("https://zoff.me");
} else {
@@ -109,7 +109,7 @@ function channel(req, res, next) {
res.status(404);
}
res.render('layouts/channel', data);
res.render('layouts/client/channel', data);
}
}
} catch(e) {