diff --git a/src/database.js b/src/database.js index 4dde3e9..18d7751 100644 --- a/src/database.js +++ b/src/database.js @@ -2,6 +2,8 @@ const mongoose = require('mongoose'); const { mongoose: mongooseConfig } = require('./config.json'); const { TOPIC } = require('./models/topic'); const { ENDPOINT } = require('./models/endpoint'); +const { COMMUNITY } = require('./models/communities'); +const { POST } = require('./models/post'); const { uri, database, options } = mongooseConfig; let connection; @@ -45,16 +47,36 @@ async function getTopicByCommunityID(communityID) { async function getCommunityByTitleID(title_id) { verifyConnected(); - - if (typeof communityID !== 'string') { - return null; - } - - return TOPIC.findOne({ + return COMMUNITY.findOne({ title_id: title_id }); } +async function getCommunityByID(community_id) { + verifyConnected(); + + return COMMUNITY.findOne({ + community_id: community_id + }); +} + +async function getPostsByCommunity(community, numberOfPosts) { + verifyConnected(); + + return POST.find({ + title_id: community.title_id + }).limit(numberOfPosts.value); +} + +async function getPostsByCommunityKey(community, numberOfPosts, search_key) { + verifyConnected(); + console.log(community.title_id); + return POST.find({ + title_id: community.title_id, + search_key: search_key + }).limit(numberOfPosts); +} + async function getDiscoveryHosts() { verifyConnected(); return ENDPOINT.findOne({ @@ -73,6 +95,10 @@ module.exports = { connect, getTopicByName, getTopicByCommunityID, + getCommunityByTitleID, + getCommunityByID, getDiscoveryHosts, + getPostsByCommunity, + getPostsByCommunityKey, getServerConfig }; \ No newline at end of file diff --git a/src/models/communities.js b/src/models/communities.js index e69de29..31023c9 100644 --- a/src/models/communities.js +++ b/src/models/communities.js @@ -0,0 +1,49 @@ +const { Schema, model } = require('mongoose'); + +const CommunitySchema = new Schema({ + empathy_count: { + type: Number, + default: 0 + }, + id: { + type: Number, + default: 0 + }, + has_shop_page: { + type: Number, + default: 0 + }, + icon: String, + title_ids: { + type: [Number], + default: undefined + }, + title_id: String, + community_id: String, + is_recommended: { + type: Number, + default: 0 + }, + name: String, +}); + +CommunitySchema.methods.upEmpathy = async function() { + const empathy = this.get('empathy_count'); + this.set('empathy_count', empathy + 1); + + await this.save(); +}; + +CommunitySchema.methods.downEmpathy = async function() { + const empathy = this.get('empathy_count'); + this.set('empathy_count', empathy - 1); + + await this.save(); +}; + +const COMMUNITY = model('COMMUNITY', CommunitySchema); + +module.exports = { + CommunitySchema, + COMMUNITY +}; \ No newline at end of file diff --git a/src/models/post.js b/src/models/post.js index 0bce41c..6304e79 100644 --- a/src/models/post.js +++ b/src/models/post.js @@ -9,6 +9,10 @@ const PostSchema = new Schema({ type: [String], default: undefined }, + topic_tag: { + type: [String], + default: undefined + }, community_id: String, country_id: Number, created_at: String, @@ -52,7 +56,7 @@ const PostSchema = new Schema({ default: 0 }, screen_name: String, - title_id: Number, + title_id: String, }); diff --git a/src/services/miiverse-api/index.js b/src/services/miiverse-api/index.js index a22598c..6a936e1 100644 --- a/src/services/miiverse-api/index.js +++ b/src/services/miiverse-api/index.js @@ -17,6 +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('d4c59198', api)); logger.info('[MIIVERSE] Importing middleware'); @@ -26,5 +27,6 @@ discovery.use(pnidMiddleware); // Setup routes discovery.use('/v1/endpoint', routes.DISCOVERY); api.use('/v1/posts', routes.POST); +api.use('/v1/communities/', routes.COMMUNITY); module.exports = router; \ No newline at end of file diff --git a/src/services/miiverse-api/routes/communities.js b/src/services/miiverse-api/routes/communities.js index 65e6ef1..3c3e6a2 100644 --- a/src/services/miiverse-api/routes/communities.js +++ b/src/services/miiverse-api/routes/communities.js @@ -1,38 +1,24 @@ var express = require('express'); var router = express.Router(); -const database = require('.../database'); +const database = require('../../../database'); +const comPostGen = require('../../../util/CommunityPostGen'); +const processHeaders = require('../../../util/processHeaders'); /* GET post titles. */ -router.get('/', function (req, res) { - /* Parse out parameters from URL and headers */ - const communityID = parseInt(req.params[0].split('/')[0], 10); - const paramPack = this.decodeParamPack(req.headers["x-nintendo-parampack"]); +router.get('/0/posts', 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.getCommunityByTitleID(paramPack.title_id); + /* Go to the database for posts. */ + let posts = await database.getPostsByCommunity(community, parseInt(req.query.limit)); - /* Get community details. communityID is usually 0, for some reason. */ - let community = null; - if (communityID === 0) { - community = await database.getCommunityByTitleID(paramPack.title_id); - } else { - community = await DataStorage.getDataStorage() - .getCommunityByID(communityID); - } - if (!community) { - this.reqdie(res); - return; - } - - /* Go to the database for posts. */ - const posts = await DataStorage.getDataStorage() - .getPostsByCommunity(community, req.query.limit); - if (!posts) { - this.reqdie(res); - return; - } - - /* Build formatted response and send it off. */ - const response = await ResponseGen.PostsResponse(posts, community); - res.contentType("application/xml"); - res.send(response); + /* Build formatted response and send it off. */ + let response = await comPostGen.PostsResponse(posts, community); + res.contentType("application/xml"); + res.send(response); + }); }); module.exports = router; \ No newline at end of file diff --git a/src/services/miiverse-api/routes/index.js b/src/services/miiverse-api/routes/index.js index 3ea5a3d..aca9287 100644 --- a/src/services/miiverse-api/routes/index.js +++ b/src/services/miiverse-api/routes/index.js @@ -1,4 +1,5 @@ module.exports = { DISCOVERY: require('./discovery'), POST: require('./post'), + COMMUNITY: require('./communities'), }; \ No newline at end of file diff --git a/src/services/miiverse-api/routes/post.js b/src/services/miiverse-api/routes/post.js index d7020ff..89a226c 100644 --- a/src/services/miiverse-api/routes/post.js +++ b/src/services/miiverse-api/routes/post.js @@ -8,22 +8,33 @@ var upload = multer(); /* GET post titles. */ router.post('/', upload.none(), function (req, res, next) { - - let paramPack = req.get('x-nintendo-parampack'); - let paramPackData = processHeaders.data.decodeParamPack(paramPack); + let paramPackData = processHeaders.data.decodeParamPack(req.headers["x-nintendo-parampack"]); /*let dec = Buffer.from(paramPack, "base64").toString("ascii"); dec = dec.slice(1, -1).split("\\"); const paramPackData = {}; for (let i = 0; i < dec.length; i += 2) { paramPackData[dec[i].trim()] = dec[i + 1].trim(); }*/ - const creationDate = moment().format('YYYY-MM-DDTHH:MM:SS'); + const creationDate = moment().format('YYYY-MM-DD HH:MM:SS'); + let appData = ""; + if (req.body.app_data) { + appData = req.body.app_data.replace(/\0/g, "").trim(); + } + let painting = ""; + if (req.body.painting) { + painting = req.body.painting.replace(/\0/g, "").trim(); + } + let screenshot = ""; + if (req.body.screenshot) { + screenshot = req.body.screenshot.replace(/\0/g, "").trim(); + } const document = { body: req.body.body, - app_data: req.body.app_data, - painting: req.body.painting, - screenshot: req.body.screenshot, + app_data: appData, + painting: painting, + screenshot: screenshot, search_key: req.body.search_key, + topic_tag: req.body.topic_tag, community_id: req.body.community_id, country_id: paramPackData.country_id, created_at: creationDate, diff --git a/src/testingDatabase.js b/src/testingDatabase.js index 31a9a1f..cb3500d 100644 --- a/src/testingDatabase.js +++ b/src/testingDatabase.js @@ -1,22 +1,11 @@ const { ENDPOINT } = require('./models/endpoint'); const database = require('./database'); - - -const doc = { - has_error: 0, - version: 1, - endpoint: { - host: "host1", - api_host: "host2", - portal_host: "host3", - n3ds_host: "host4" - } -}; -const newEndpoint = new ENDPOINT(doc); database.connect().then(async yeet => { - const temp = await database.getServerConfig(); - console.log(temp); + const temp = await database.getCommunityByID('0'); + console.log(temp.name); + const temp2 = await database.getPostsByCommunityKey(temp, 2, 'Warawara_03'); + console.log(temp2[1].painting.length); }); //database.getDiscoveryHosts().then(r => console.log(r.endpoint)); diff --git a/src/util/CommunityPostGen.js b/src/util/CommunityPostGen.js index 48a76a1..75465a5 100644 --- a/src/util/CommunityPostGen.js +++ b/src/util/CommunityPostGen.js @@ -9,39 +9,39 @@ class CommunityPostGen { .e("version", "1").up() .e("request_name", "posts").up() .e("topic") - .e("community_id", community.id).up() + .e("community_id", community.community_id).up() .up() .e("posts"); for (let i = 0; i < posts.length; i++) { xml = xml.e("post") - .e("app_data", posts[i].appData).up() + .e("app_data", posts[i].app_data).up() .e("body", posts[i].body).up() - .e("community_id", community.id).up() + .e("community_id", community.community_id).up() .e("country_id", "254").up() - .e("created_at", moment(posts[i].created).tz("GMT").format("YYYY-MM-DD hh:mm:ss")).up() + .e("created_at", posts[i].created_at).up() .e("feeling_id", "1").up() - .e("id", posts[i].id).up() + .e("id", i + 1).up() .e("is_autopost", "0").up() .e("is_community_private_autopost", "0").up() .e("is_spoiler", "0").up() .e("is_app_jumpable", "0").up() - .e("empathy_count", posts[i].empathy).up() + .e("empathy_count", posts[i].empathy_count).up() .e("language_id", "1").up() .e("number", "0").up(); if (posts[i].painting) { xml = xml.e("painting") .e("format", "tga").up() .e("content", posts[i].painting).up() - .e("size", posts[i].paintingSz).up() + .e("size", posts[i].painting.length).up() .e("url", "https://s3.amazonaws.com/olv-public/pap/WVW69koebmETvBVqm1").up() .up(); } - xml = xml.e("pid", posts[i].pid).up() + xml = xml.e("pid", i + 1).up() .e("platform_id", "1").up() .e("region_id", "4").up() .e("reply_count", "0").up() - .e("screen_name", posts[i].screenName).up() - .e("title_id", posts[i].tid).up() + .e("screen_name", posts[i].screen_name).up() + .e("title_id", posts[i].title_id).up() .up(); } @@ -61,27 +61,27 @@ class CommunityPostGen { .e("has_error", "0").up() .e("version", "1").up() .e("post"); - if (post.appData) { - xml = xml.e("app_data", post.appData).up(); + if (post.app_data) { + xml = xml.e("app_data", post.app_data).up(); } xml = xml.e("body", post.body).up() - .e("community_id", post.communityID).up() + .e("community_id", post.community_id).up() .e("country_id", "254").up() - .e("created_at", moment(post.created).tz("GMT").format("YYYY-MM-DD hh:mm:ss")).up() + .e("created_at", post.created_at).up() .e("feeling_id", "1").up() .e("id", post.id).up() .e("is_autopost", "0").up() .e("is_community_private_autopost", "0").up() .e("is_spoiler", "0").up() .e("is_app_jumpable", "0").up() - .e("empathy_count", post.empathy).up() + .e("empathy_count", post.empathy_count).up() .e("language_id", "1").up() .e("number", "0").up(); if (post.painting) { xml = xml.e("painting") .e("format", "tga").up() .e("content", post.painting).up() - .e("size", post.paintingSz).up() + .e("size", post.painting.length).up() .e("url", "https://s3.amazonaws.com/olv-public/pap/WVW69koebmETvBVqm1").up() .up(); } @@ -89,8 +89,8 @@ class CommunityPostGen { .e("platform_id", "1").up() .e("region_id", "4").up() .e("reply_count", "0").up() - .e("screen_name", post.screenName).up() - .e("title_id", post.tid).up() + .e("screen_name", post.screen_name).up() + .e("title_id", post.title_id).up() .up(); return xml.end({ pretty: true, allowEmpty: true }); }