add comment cleanup and add globals

This commit is contained in:
Alf Hammerseth
2019-11-16 21:13:45 +01:00
parent 54b745ec68
commit 33e6220e43
31 changed files with 4480 additions and 2552 deletions

View File

@@ -1,3 +1,7 @@
import {
pathThumbnails
} from "../settings/globals";
var express = require("express");
var router = express.Router();
var path = require("path");
@@ -21,15 +25,17 @@ var ObjectId = mongojs.ObjectId;
var sIO = require(path.join(__dirname, "../../apps/client.js")).socketIO;
var projects = require(pathThumbnails + "/handlers/aggregates.js");
router.use(function(req, res, next) {
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) {
router.route("/api/lists").get(function (req, res) {
if (req.isAuthenticated()) {
db.collection("frontpage_lists")
.find()
.sort({ count: -1 }, function(err, docs) {
.sort({
count: -1
}, function (err, docs) {
res.json(docs);
});
} else {
@@ -37,9 +43,9 @@ router.route("/api/lists").get(function(req, res) {
}
});
router.route("/api/thumbnails").get(function(req, res) {
router.route("/api/thumbnails").get(function (req, res) {
if (req.isAuthenticated()) {
db.collection("suggested_thumbnails").find(function(err, docs) {
db.collection("suggested_thumbnails").find(function (err, docs) {
res.json(docs);
});
} else {
@@ -47,9 +53,9 @@ router.route("/api/thumbnails").get(function(req, res) {
}
});
router.route("/api/descriptions").get(function(req, res) {
router.route("/api/descriptions").get(function (req, res) {
if (req.isAuthenticated()) {
db.collection("suggested_descriptions").find(function(err, docs) {
db.collection("suggested_descriptions").find(function (err, docs) {
res.json(docs);
});
} else {
@@ -57,9 +63,9 @@ router.route("/api/descriptions").get(function(req, res) {
}
});
router.route("/api/rules").get(function(req, res) {
router.route("/api/rules").get(function (req, res) {
if (req.isAuthenticated()) {
db.collection("suggested_rules").find(function(err, docs) {
db.collection("suggested_rules").find(function (err, docs) {
res.json(docs);
});
} else {
@@ -67,30 +73,44 @@ router.route("/api/rules").get(function(req, res) {
}
});
router.route("/api/approve_thumbnail").post(function(req, res) {
router.route("/api/approve_thumbnail").post(function (req, res) {
if (req.isAuthenticated()) {
var channel = req.body.channel;
db.collection("suggested_thumbnails").find({ channel: channel }, function(
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 + "_settings").update(
{ views: { $exists: true } },
{ $set: { thumbnail: thumbnail } },
{ upsert: true },
function(err, docs) {
db.collection("suggested_thumbnails").remove(
{ channel: channel },
function(err, docs) {
db.collection("frontpage_lists").update({
_id: channel
}, {
$set: {
thumbnail: thumbnail
}
}, {
upsert: true
},
function (err, docs) {
db.collection(channel + "_settings").update({
views: {
$exists: true
}
}, {
$set: {
thumbnail: thumbnail
}
}, {
upsert: true
},
function (err, docs) {
db.collection("suggested_thumbnails").remove({
channel: channel
},
function (err, docs) {
db.collection(channel + "_settings").aggregate(
[
{
[{
$match: {
id: "config"
}
@@ -99,7 +119,7 @@ router.route("/api/approve_thumbnail").post(function(req, res) {
$project: projects.toShowConfig
}
],
function(err, docs) {
function (err, docs) {
if (docs[0].adminpass !== "") docs[0].adminpass = true;
if (
docs[0].hasOwnProperty("userpass") &&
@@ -123,10 +143,12 @@ router.route("/api/approve_thumbnail").post(function(req, res) {
}
});
router.route("/api/deny_thumbnail").post(function(req, res) {
router.route("/api/deny_thumbnail").post(function (req, res) {
if (req.isAuthenticated()) {
var channel = req.body.channel;
db.collection("suggested_thumbnails").remove({ channel: channel }, function(
db.collection("suggested_thumbnails").remove({
channel: channel
}, function (
err,
docs
) {
@@ -137,25 +159,34 @@ router.route("/api/deny_thumbnail").post(function(req, res) {
}
});
router.route("/api/approve_rules").post(function(req, res) {
router.route("/api/approve_rules").post(function (req, res) {
if (req.isAuthenticated()) {
var channel = req.body.channel;
db.collection("suggested_rules").find({ channel: channel }, function(
db.collection("suggested_rules").find({
channel: channel
}, function (
err,
docs
) {
var rules = docs[0].rules;
db.collection(channel + "_settings").update(
{ views: { $exists: true } },
{ $set: { rules: rules } },
{ upsert: true },
function(err, docs) {
db.collection("suggested_rules").remove(
{ channel: channel },
function(err, docs) {
db.collection(channel + "_settings").update({
views: {
$exists: true
}
}, {
$set: {
rules: rules
}
}, {
upsert: true
},
function (err, docs) {
db.collection("suggested_rules").remove({
channel: channel
},
function (err, docs) {
db.collection(channel + "_settings").aggregate(
[
{
[{
$match: {
id: "config"
}
@@ -164,7 +195,7 @@ router.route("/api/approve_rules").post(function(req, res) {
$project: projects.toShowConfig
}
],
function(err, docs) {
function (err, docs) {
if (docs[0].adminpass !== "") docs[0].adminpass = true;
if (
docs[0].hasOwnProperty("userpass") &&
@@ -186,10 +217,12 @@ router.route("/api/approve_rules").post(function(req, res) {
}
});
router.route("/api/deny_rules").post(function(req, res) {
router.route("/api/deny_rules").post(function (req, res) {
if (req.isAuthenticated()) {
var channel = req.body.channel;
db.collection("suggested_rules").remove({ channel: channel }, function(
db.collection("suggested_rules").remove({
channel: channel
}, function (
err,
docs
) {
@@ -200,16 +233,21 @@ router.route("/api/deny_rules").post(function(req, res) {
}
});
router.route("/api/remove_rules").post(function(req, res) {
router.route("/api/remove_rules").post(function (req, res) {
if (req.isAuthenticated()) {
var channel = req.body.channel;
db.collection(channel + "_settings").update(
{ views: { $exists: true } },
{ $set: { rules: "" } },
function(err, docs) {
db.collection(channel + "_settings").update({
views: {
$exists: true
}
}, {
$set: {
rules: ""
}
},
function (err, docs) {
db.collection(channel + "_settings").aggregate(
[
{
[{
$match: {
id: "config"
}
@@ -218,7 +256,7 @@ router.route("/api/remove_rules").post(function(req, res) {
$project: projects.toShowConfig
}
],
function(err, docs) {
function (err, docs) {
if (docs[0].adminpass !== "") docs[0].adminpass = true;
if (docs[0].hasOwnProperty("userpass") && docs[0].userpass != "")
docs[0].userpass = true;
@@ -234,29 +272,42 @@ router.route("/api/remove_rules").post(function(req, res) {
}
});
router.route("/api/approve_description").post(function(req, res) {
router.route("/api/approve_description").post(function (req, res) {
if (req.isAuthenticated()) {
var channel = req.body.channel;
db.collection("suggested_descriptions").find({ channel: channel }, function(
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 + "_settings").update(
{ views: { $exists: true } },
{ $set: { description: description } },
function(err, docs) {
db.collection("suggested_descriptions").remove(
{ channel: channel },
function(err, docs) {
db.collection("frontpage_lists").update({
_id: channel
}, {
$set: {
description: description
}
}, {
upsert: true
},
function (err, docs) {
db.collection(channel + "_settings").update({
views: {
$exists: true
}
}, {
$set: {
description: description
}
},
function (err, docs) {
db.collection("suggested_descriptions").remove({
channel: channel
},
function (err, docs) {
db.collection(channel + "_settings").aggregate(
[
{
[{
$match: {
id: "config"
}
@@ -265,7 +316,7 @@ router.route("/api/approve_description").post(function(req, res) {
$project: projects.toShowConfig
}
],
function(err, docs) {
function (err, docs) {
if (docs[0].adminpass !== "") docs[0].adminpass = true;
if (
docs[0].hasOwnProperty("userpass") &&
@@ -289,13 +340,14 @@ router.route("/api/approve_description").post(function(req, res) {
}
});
router.route("/api/deny_description").post(function(req, res) {
router.route("/api/deny_description").post(function (req, res) {
if (req.isAuthenticated()) {
var channel = req.body.channel;
db.collection("suggested_descriptions").remove(
{ channel: channel },
db.collection("suggested_descriptions").remove({
channel: channel
},
1,
function(err, docs) {
function (err, docs) {
res.send(true);
}
);
@@ -304,20 +356,29 @@ router.route("/api/deny_description").post(function(req, res) {
}
});
router.route("/api/remove_thumbnail").post(function(req, res) {
router.route("/api/remove_thumbnail").post(function (req, res) {
if (req.isAuthenticated()) {
var channel = req.body.channel;
db.collection("frontpage_lists").update(
{ _id: channel },
{ $set: { thumbnail: "" } },
function(err, docs) {
db.collection(channel + "_settings").update(
{ views: { $exists: true } },
{ $set: { thumbnail: "" } },
function(err, docs) {
db.collection("frontpage_lists").update({
_id: channel
}, {
$set: {
thumbnail: ""
}
},
function (err, docs) {
db.collection(channel + "_settings").update({
views: {
$exists: true
}
}, {
$set: {
thumbnail: ""
}
},
function (err, docs) {
db.collection(channel + "_settings").aggregate(
[
{
[{
$match: {
id: "config"
}
@@ -326,7 +387,7 @@ router.route("/api/remove_thumbnail").post(function(req, res) {
$project: projects.toShowConfig
}
],
function(err, docs) {
function (err, docs) {
if (docs[0].adminpass !== "") docs[0].adminpass = true;
if (
docs[0].hasOwnProperty("userpass") &&
@@ -347,20 +408,29 @@ router.route("/api/remove_thumbnail").post(function(req, res) {
}
});
router.route("/api/remove_description").post(function(req, res) {
router.route("/api/remove_description").post(function (req, res) {
if (req.isAuthenticated()) {
var channel = req.body.channel;
db.collection("frontpage_lists").update(
{ _id: channel },
{ $set: { description: "" } },
function(err, docs) {
db.collection(channel + "_settings").update(
{ views: { $exists: true } },
{ $set: { description: "" } },
function(err, docs) {
db.collection("frontpage_lists").update({
_id: channel
}, {
$set: {
description: ""
}
},
function (err, docs) {
db.collection(channel + "_settings").update({
views: {
$exists: true
}
}, {
$set: {
description: ""
}
},
function (err, docs) {
db.collection(channel + "_settings").aggregate(
[
{
[{
$match: {
id: "config"
}
@@ -369,7 +439,7 @@ router.route("/api/remove_description").post(function(req, res) {
$project: projects.toShowConfig
}
],
function(err, docs) {
function (err, docs) {
if (docs[0].adminpass !== "") docs[0].adminpass = true;
if (
docs[0].hasOwnProperty("userpass") &&
@@ -390,12 +460,17 @@ router.route("/api/remove_description").post(function(req, res) {
}
});
router.route("/api/names").get(function(req, res) {
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) {
db.collection("registered_users").find({
_id: {
$exists: true
}
}, {
_id: 1,
icon: 1
},
function (err, docs) {
res.json(docs);
}
);
@@ -404,14 +479,18 @@ router.route("/api/names").get(function(req, res) {
}
});
router.route("/api/names").post(function(req, res) {
router.route("/api/names").post(function (req, res) {
if (req.isAuthenticated()) {
var icon = req.body.icon;
var name = req.body.name;
db.collection("registered_users").update(
{ _id: name },
{ $set: { icon: icon } },
function(err, docs) {
db.collection("registered_users").update({
_id: name
}, {
$set: {
icon: icon
}
},
function (err, docs) {
if (err) res.send(false);
else res.send(true);
}
@@ -421,10 +500,12 @@ router.route("/api/names").post(function(req, res) {
}
});
router.route("/api/names").delete(function(req, res) {
router.route("/api/names").delete(function (req, res) {
if (req.isAuthenticated()) {
var name = req.body.name;
db.collection("registered_users").remove({ _id: name }, function(
db.collection("registered_users").remove({
_id: name
}, function (
err,
docs
) {
@@ -436,17 +517,23 @@ router.route("/api/names").delete(function(req, res) {
}
});
router.route("/api/token").get(function(req, res) {
router.route("/api/token").get(function (req, res) {
if (req.isAuthenticated()) {
token_db.collection("tokens").find(function(err, docs) {
token_db.collection("tokens").find(function (err, docs) {
if (docs.length == 1) {
res.json({ token: docs[0].token });
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 });
.insert({
token: id
}, function (err, docs) {
res.json({
token: id
});
});
}
});
@@ -455,11 +542,15 @@ router.route("/api/token").get(function(req, res) {
}
});
router.route("/api/api_token").get(function(req, res) {
router.route("/api/api_token").get(function (req, res) {
if (req.isAuthenticated()) {
token_db
.collection("api_token")
.find({ token: { $exists: true } }, function(err, all) {
.find({
token: {
$exists: true
}
}, function (err, all) {
res.json(all);
});
} else {
@@ -467,12 +558,14 @@ router.route("/api/api_token").get(function(req, res) {
}
});
router.route("/api/api_token").delete(function(req, res) {
router.route("/api/api_token").delete(function (req, res) {
if (req.isAuthenticated()) {
var id = req.body.id;
token_db
.collection("api_token")
.remove({ _id: ObjectId(id) }, function(err, success) {
.remove({
_id: ObjectId(id)
}, function (err, success) {
if (err) {
res.send("failed");
return;
@@ -482,7 +575,7 @@ router.route("/api/api_token").delete(function(req, res) {
}
});
router.route("/api/api_token").put(function(req, res) {
router.route("/api/api_token").put(function (req, res) {
if (req.isAuthenticated()) {
var id = req.body.id;
var limit = req.body.limit;
@@ -492,7 +585,13 @@ router.route("/api/api_token").put(function(req, res) {
}
token_db
.collection("api_token")
.update({ _id: ObjectId(id) }, { $set: { limit: limit } }, function(
.update({
_id: ObjectId(id)
}, {
$set: {
limit: limit
}
}, function (
err,
success
) {
@@ -505,7 +604,7 @@ router.route("/api/api_token").put(function(req, res) {
}
});
router.route("/api/api_token").post(function(req, res) {
router.route("/api/api_token").post(function (req, res) {
if (req.isAuthenticated()) {
var name = req.body.name;
var id = crypto
@@ -514,9 +613,18 @@ router.route("/api/api_token").post(function(req, res) {
.digest("base64");
token_db
.collection("api_token")
.insert({ name: name, token: id, usage: 0 }, function(err, docs) {
token_db.collection("api_token").find({ token: id }, function(err, d) {
res.json({ token: id, _id: d[0]._id });
.insert({
name: name,
token: id,
usage: 0
}, function (err, docs) {
token_db.collection("api_token").find({
token: id
}, function (err, d) {
res.json({
token: id,
_id: d[0]._id
});
});
});
} else {
@@ -524,12 +632,14 @@ router.route("/api/api_token").post(function(req, res) {
}
});
router.route("/api/delete").post(function(req, res) {
router.route("/api/delete").post(function (req, res) {
if (req.isAuthenticated()) {
var list = req.body._id;
db.collection(list).drop(function(err, docs) {
db.collection(list + "_settings").drop(function(err, docs) {
db.collection("frontpage_lists").remove({ _id: list }, function(
db.collection(list).drop(function (err, docs) {
db.collection(list + "_settings").drop(function (err, docs) {
db.collection("frontpage_lists").remove({
_id: list
}, function (
err,
docs
) {
@@ -542,13 +652,15 @@ router.route("/api/delete").post(function(req, res) {
}
});
router.route("/api/remove_token").get(function(req, res) {
router.route("/api/remove_token").get(function (req, res) {
if (req.isAuthenticated()) {
token_db.collection("tokens").find(function(err, docs) {
token_db.collection("tokens").find(function (err, docs) {
if (docs.length == 1) {
token_db
.collection("tokens")
.remove({ token: docs[0].token }, function(err, docs) {
.remove({
token: docs[0].token
}, function (err, docs) {
res.send(true);
});
} else {
@@ -560,17 +672,25 @@ router.route("/api/remove_token").get(function(req, res) {
}
});
router.route("/api/pinned").post(function(req, res) {
router.route("/api/pinned").post(function (req, res) {
if (req.isAuthenticated()) {
var to_pin = req.body._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) {
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);
}
);
@@ -581,13 +701,19 @@ router.route("/api/pinned").post(function(req, res) {
}
});
router.route("/api/admin").post(function(req, res) {
router.route("/api/admin").post(function (req, res) {
if (req.isAuthenticated()) {
var to_remove = req.body._id;
db.collection(to_remove + "_settings").update(
{ views: { $exists: true } },
{ $set: { adminpass: "" } },
function(err, docs) {
db.collection(to_remove + "_settings").update({
views: {
$exists: true
}
}, {
$set: {
adminpass: ""
}
},
function (err, docs) {
res.send(true);
}
);
@@ -596,13 +722,19 @@ router.route("/api/admin").post(function(req, res) {
}
});
router.route("/api/userpass").post(function(req, res) {
router.route("/api/userpass").post(function (req, res) {
if (req.isAuthenticated()) {
var to_remove = req.body._id;
db.collection(to_remove + "_settings").update(
{ views: { $exists: true } },
{ $set: { userpass: "" } },
function(err, docs) {
db.collection(to_remove + "_settings").update({
views: {
$exists: true
}
}, {
$set: {
userpass: ""
}
},
function (err, docs) {
res.send(true);
}
);
@@ -622,4 +754,4 @@ function makeid() {
return text;
}
module.exports = router;
module.exports = router;