added admin panel for viewing community posts in browser

This commit is contained in:
CaramelKat
2020-05-10 04:06:03 -05:00
committed by jay.poff@outlook.com
parent 0fd53d43cd
commit b2aac3f167
13 changed files with 220 additions and 9 deletions

18
package-lock.json generated
View File

@@ -1032,6 +1032,11 @@
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
"integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA=="
},
"pako": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz",
"integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw=="
},
"parseurl": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
@@ -1048,6 +1053,11 @@
"integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=",
"dev": true
},
"pngjs": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/pngjs/-/pngjs-5.0.0.tgz",
"integrity": "sha512-40QW5YalBNfQo5yRYmiw7Yz6TKKVr3h6970B2YE+3fQpsWcrbj1PzJgxeJ19DRQjhMbKPIuMY8rFaXc8moolVw=="
},
"process-nextick-args": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
@@ -1296,6 +1306,14 @@
"safe-buffer": "~5.1.0"
}
},
"tga": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/tga/-/tga-1.0.3.tgz",
"integrity": "sha1-fokbRRMIshi0gtvMna5nWWboIrM=",
"requires": {
"debug": "^2.6.1"
}
},
"toidentifier": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz",

View File

@@ -23,6 +23,9 @@
"morgan": "^1.10.0",
"node-rsa": "^1.0.8",
"node-snowflake": "0.0.1",
"pako": "^1.0.11",
"pngjs": "^5.0.0",
"tga": "^1.0.3",
"xmlbuilder": "^13.0.2",
"xmlbuilder2": "0.0.4"
},

View File

@@ -46,6 +46,11 @@ async function getTopicByCommunityID(communityID) {
});
}
async function getCommunities(numberOfPosts) {
verifyConnected();
return COMMUNITY.find({}).limit(numberOfPosts);
}
async function getCommunityByTitleID(title_id) {
verifyConnected();
return COMMUNITY.findOne({
@@ -71,10 +76,9 @@ async function getPostByID(postID) {
async function getPostsByCommunity(community, numberOfPosts) {
verifyConnected();
return POST.find({
title_id: community.title_id
}).limit(numberOfPosts.value);
}).limit(numberOfPosts);
}
async function getPostsByCommunityKey(community, numberOfPosts, search_key) {
@@ -109,8 +113,7 @@ async function getServerConfig() {
module.exports = {
connect,
getTopicByName,
getTopicByCommunityID,
getCommunities,
getCommunityByTitleID,
getCommunityByID,
getDiscoveryHosts,

View File

@@ -6,6 +6,7 @@ const PostSchema = new Schema({
body: String,
app_data: String,
painting: String,
painting_uri: String,
screenshot: String,
url: String,
search_key: {

View File

@@ -10,6 +10,7 @@ const { http: { port } } = config;
const app = express();
const miiverse = require('./services/miiverse-api');
const webApi = require('./services/web-api');
app.set('etag', false);
app.disable('x-powered-by');
@@ -26,6 +27,7 @@ app.use(xmlparser);
// import the servers into one
app.use(miiverse);
app.use(webApi);
// 404 handler
logger.info('Creating 404 status handler');

View File

@@ -17,7 +17,7 @@ logger.info('[MIIVERSE] Creating \'discovery\' subdomain');
router.use(subdomain('discovery.olv', discovery));
logger.info('[MIIVERSE] Creating \'api\' subdomain');
router.use(subdomain('api.olv', api));
router.use(subdomain('4034a63d', discovery));
router.use(subdomain('d551f9ba', api));
logger.info('[MIIVERSE] Importing middleware');

View File

@@ -23,8 +23,10 @@ router.post('/', upload.none(), async function (req, res, next) {
appData = req.body.app_data.replace(/\0/g, "").trim();
}
let painting = "";
let painting_uri = "";
if (req.body.painting) {
painting = req.body.painting.replace(/\0/g, "").trim();
painting_uri = util.data.processPainting(painting);
}
let screenshot = "";
if (req.body.screenshot) {
@@ -36,6 +38,7 @@ router.post('/', upload.none(), async function (req, res, next) {
body: req.body.body,
app_data: appData,
painting: painting,
painting_uri: painting_uri,
screenshot: screenshot,
url: req.body.url,
search_key: req.body.search_key,
@@ -62,9 +65,6 @@ router.post('/', upload.none(), async function (req, res, next) {
catch (e)
{
console.error(e);
}
finally
{
res.set("Content-Type", "application/xml");
res.statusCode = 400;
response = {

View File

@@ -0,0 +1,23 @@
const express = require('express');
const subdomain = require('express-subdomain');
const sessionMiddleware = require('../../middleware/session');
const pnidMiddleware = require('../../middleware/pnid');
const logger = require('../../logger');
const routes = require('./routes');
// Main router for endpointsindex.js
const router = express.Router();
// Router to handle the subdomain restriction
const api = express.Router();
// Create subdomains
logger.info('[MIIVERSE] Creating \'web api\' subdomain');
router.use(subdomain('web.olv', api));
// Setup routes
api.use('/', routes.PORTAL);
api.use('/v1/communities/', routes.COMMUNITY);
api.use('/v1/post/', routes.POST);
module.exports = router;

View File

@@ -0,0 +1,30 @@
var express = require('express');
var router = express.Router();
const database = require('../../../database');
const comPostGen = require('../../../util/CommunityPostGen');
const processHeaders = require('../../../util/authentication');
/* GET post titles. */
router.get('/list', function (req, res) {
database.connect().then(async e => {
/* Parse out parameters from URL and headers */
//const paramPack = processHeaders.data.decodeParamPack(req.headers["x-nintendo-parampack"]);
//"[0:1]=1407375153523200"
let community = await database.getCommunities(5);
console.log(community.length);
let body = '<form action="">\n' +
' <select id="communities" onchange="getPosts(this.value)">' +
' <option value="">Select a community:</option>\n';
for(let i = 0; i < community.length; i++)
{
body += '<option value=' + community[i].community_id + '>' + community[i].name + '</option>';
}
body += ' </select> </form>';
body += '<button type="button" onClick="getPosts(-1)">Refresh Table</button>';
res.send('<!DOCTYPE html>'
+ '<html><head>' + '</head><body>' + body + '</body></html>');
});
});
module.exports = router;

View File

@@ -0,0 +1,6 @@
module.exports = {
//DISCOVERY: require('./discovery'),
POST: require('./post'),
COMMUNITY: require('./communities'),
PORTAL: require('./portal'),
};

View File

@@ -0,0 +1,69 @@
var express = require('express');
var router = express.Router();
const database = require('../../../database');
const comPostGen = require('../../../util/CommunityPostGen');
const processHeaders = require('../../../util/authentication');
/* GET post titles. */
router.get('/', function (req, res) {
res.send(
'<!DOCTYPE html>\n' +
'<html>\n' +
'<style>\n' +
' table,th,td {\n' +
' border : 1px solid black;\n' +
' border-collapse: collapse;\n' +
' }\n' +
' th,td {\n' +
' padding: 5px;\n' +
' }\n' +
'</style>\n' +
'<body>\n' +
'<h1>Miiverse Temp Admin Pannel</h1>\n' +
'\n' +
'<form action="" id="communitySelection">\n' +
'</form>\n' +
'<br>\n' +
'<div id="Posts"></div>\n' +
'\n' +
'<script>\n' +
' function getCommunities() {\n' +
' var xhttp;\n' +
' xhttp = new XMLHttpRequest();\n' +
' xhttp.onreadystatechange = function() {\n' +
' if (this.readyState === 4 && this.status === 200) {\n' +
' document.getElementById("communitySelection").innerHTML = this.responseText;\n' +
' }\n' +
' };\n' +
' xhttp.open("GET", "/v1/communities/list", true);\n' +
' xhttp.send();\n' +
' }\n' +
' function getPosts(str) {\n' +
' var xhttp;\n' +
' if (str === "") {\n' +
' document.getElementById("Posts").innerHTML = "";\n' +
' return;\n' +
' }\n' +
' if (str === -1) {\n' +
' var a = document.getElementById(\'communities\');\n' +
' str = a.options[a.selectedIndex].value;' +
' console.log(str);' +
' }\n' +
' xhttp = new XMLHttpRequest();\n' +
' xhttp.onreadystatechange = function() {\n' +
' if (this.readyState === 4 && this.status === 200) {\n' +
' document.getElementById("Posts").innerHTML = this.responseText;\n' +
' }\n' +
' };\n' +
' xhttp.open("GET", "/v1/post?community_id=" + str + "&limit=100", true);\n' +
' xhttp.send();\n' +
' }\n' +
'getCommunities()' +
'</script>\n' +
'\n' +
'</body>\n' +
'</html>\n'
)
});
module.exports = router;

View File

@@ -0,0 +1,34 @@
var express = require('express');
var router = express.Router();
const database = require('../../../database');
router.get('/', function (req, res) {
database.connect().then(async e => {
/* Parse out parameters from URL and headers */
//const paramPack = processHeaders.data.decodeParamPack(req.headers["x-nintendo-parampack"]);
//"[0:1]=1407375153523200"
let community = await database.getCommunityByID(req.query.community_id);
console.log(community);
let posts = await database.getPostsByCommunity(community, parseInt(req.query.limit));
console.log(posts.length);
let body = '<table id="posts"><tbody><tr>' +
'<th onClick="sortTable(0)">Screen Name</th>' +
'<th onClick="sortTable(1)">Body Text</th>' +
'<th onClick="sortTable(2)">Yeah Count</th>' +
'<th onClick="sortTable(3)">Painting</th>' +
'</tr>';
for(let i = 0; i < posts.length; i++)
{
body += '<tr id="' + posts[i]._id + '_row">' + '<td>' + posts[i].screen_name + '</td>' +
'<td>' + posts[i].body + '</td>' +
'<td>' + posts[i].empathy_count + '</td>' +
'<td><img src="' + posts[i].painting_uri + '" width=auto height=auto></td>';
}
body += '</tbody>' + '</table>';
res.send('<!DOCTYPE html>'
+ '<html><head>' + '</head><body>' + body + '</body></html>');
});
});
module.exports = router;

View File

@@ -6,6 +6,9 @@ const xmlParser = require('xml2json');
const request = require("request");
const moment = require('moment');
const { USER } = require('../models/user');
let TGA = require('tga');
let pako = require('pako');
let PNG = require('pngjs').PNG;
let methods = {
processUser: function(pid) {
@@ -44,7 +47,26 @@ let methods = {
});
});
},
processPainting: function (painting) {
let paintingBuffer = Buffer.from(painting, 'base64');
let output = '';
try
{
output = pako.inflate(paintingBuffer);
}
catch (err)
{
console.log(err);
}
let tga = new TGA(Buffer.from(output));
let png = new PNG({
width: tga.width,
height: tga.height
});
png.data = tga.pixels;
let pngBuffer = PNG.sync.write(png);
return `data:image/png;base64,${pngBuffer.toString('base64')}`;
},
decodeParamPack: function (paramPack) {
/* Decode base64 */
let dec = Buffer.from(paramPack, "base64").toString("ascii");