From 7175e203645d1011746ff880bddf5293eaa8cd77 Mon Sep 17 00:00:00 2001 From: CaramelKat <32065563+caramelkat@users.noreply.github.com> Date: Tue, 28 Sep 2021 21:23:13 -0500 Subject: [PATCH] added support for /v1/topics --- src/models/endpoint.js | 8 ++- src/services/miiverse-api/index.js | 2 +- src/services/miiverse-api/routes/index.js | 1 + src/services/miiverse-api/routes/topics.js | 50 +++------------- src/util/CommunityPostGen.js | 66 ++++++++++++++++++++++ 5 files changed, 83 insertions(+), 44 deletions(-) diff --git a/src/models/endpoint.js b/src/models/endpoint.js index 41c29d8..ae4ac2a 100644 --- a/src/models/endpoint.js +++ b/src/models/endpoint.js @@ -8,7 +8,8 @@ const endpointSchema = new Schema({ api_host: String, portal_host: String, n3ds_host: String - } + }, + guest: Boolean, }); endpointSchema.methods.updateHosts = async function({host, api_host, portal_host, n3ds_host}) { @@ -19,6 +20,11 @@ endpointSchema.methods.updateHosts = async function({host, api_host, portal_host await this.save(); }; +endpointSchema.methods.updateGuest = async function(mode) { + this.set('guest', mode); + await this.save(); +} + const ENDPOINT = model('ENDPOINT', endpointSchema); module.exports = { diff --git a/src/services/miiverse-api/index.js b/src/services/miiverse-api/index.js index a1b343f..1fdb21d 100644 --- a/src/services/miiverse-api/index.js +++ b/src/services/miiverse-api/index.js @@ -17,7 +17,6 @@ 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('f57cd744', discovery)); logger.info('[MIIVERSE] Importing middleware'); @@ -29,6 +28,7 @@ discovery.use('/v1/endpoint', routes.DISCOVERY); api.use('/v1/posts', routes.POST); api.use('/v1/communities/', routes.COMMUNITY); api.use('/v1/people/', routes.PEOPLE); +api.use('/v1/topics/', routes.TOPICS); discovery.use('/p01/', routes.POLICY); 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 63bb8c5..f9948b3 100644 --- a/src/services/miiverse-api/routes/index.js +++ b/src/services/miiverse-api/routes/index.js @@ -4,4 +4,5 @@ module.exports = { COMMUNITY: require('./communities'), PEOPLE: require('./people'), POLICY: require('./policyLists'), + TOPICS: require('./topics') }; \ No newline at end of file diff --git a/src/services/miiverse-api/routes/topics.js b/src/services/miiverse-api/routes/topics.js index d61ac08..4b20e4c 100644 --- a/src/services/miiverse-api/routes/topics.js +++ b/src/services/miiverse-api/routes/topics.js @@ -3,53 +3,19 @@ var router = express.Router(); const database = require('../../../database'); const comPostGen = require('../../../util/CommunityPostGen'); const processHeaders = require('../../../util/authentication'); +const xmlbuilder = require("xmlbuilder"); +const moment = require("moment"); /* GET post titles. */ router.get('/', function (req, res) { database.connect().then(async e => { const paramPack = processHeaders.data.decodeParamPack(req.headers["x-nintendo-parampack"]); - let community = await database.getCommunityByTitleID(paramPack.title_id); - if (community != null) { - let response = await comPostGen.Communities(community); - res.contentType("application/xml"); - res.send(response); - } else { - res.status(404); - res.send(); - } - - }); -}); - -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); - if(community != null) - { - let posts; - if(req.query.search_key) - posts = await database.getPostsByCommunityKey(community, parseInt(req.query.limit), req.query.search_key); - else - posts = await database.getPostsByCommunity(community, parseInt(req.query.limit)); - - /* Build formatted response and send it off. */ - let response; - if(req.query.with_mii === 1) - response = await comPostGen.PostsResponseWithMii(posts, community); - else - response = await comPostGen.PostsResponse(posts, community); - res.contentType("application/xml"); - res.send(response); - } - else - { - res.status(404); - res.send(); - } - + let communities = await database.getCommunities(10); + if(communities === null) + return res.sendStatus(404); + let response = await comPostGen.topics(communities); + res.contentType("application/xml"); + res.send(response); }); }); diff --git a/src/util/CommunityPostGen.js b/src/util/CommunityPostGen.js index 5c06ffe..9191fcd 100644 --- a/src/util/CommunityPostGen.js +++ b/src/util/CommunityPostGen.js @@ -1,5 +1,6 @@ const xmlbuilder = require("xmlbuilder"); const moment = require("moment"); +const database = require('../database'); class CommunityPostGen { /* TODO lots of stubs and constants in here */ @@ -188,6 +189,71 @@ class CommunityPostGen { .up(); return xml.end({ pretty: true, allowEmpty: true }); } + + static async topics(communities) { + const expirationDate = moment().add(1, 'days'); + let xml = xmlbuilder.create("result") + .e("has_error", "0").up() + .e("version", "1").up() + .e("request_name", "topics").up() + .e("expire", expirationDate.format('YYYY-MM-DD HH:MM:SS')).up() + .e("topics").up(); + for (const community of communities) { + let posts = await database.getPostsByCommunity(community, 30); + console.log(posts.length); + xml = xml.e('topic') + .e('empathy_count', community.empathy_count).up() + .e('has_shop_page', community.has_shop_page).up() + .e('icon', community.icon).up() + .e('title_ids'); + community.title_ids.forEach(function (title_id) { + xml = xml.e('title_id', title_id).up() + }) + xml = xml.up() + .e('title_id', community.title_ids[0]).up() + .e('community_id', community.community_id).up() + .e('is_recommended', community.is_recommended).up() + .e('name', community.name).up() + .e("people"); + for (const post of posts) { + xml = xml.e("person") + .e("posts") + .e("post") + .e("body", post.body).up() + .e("community_id", community.community_id).up() + .e("country_id", post.country_id).up() + .e("created_at", moment(post.created_at).format('YYYY-MM-DD HH:MM:SS')).up() + .e("feeling_id", post.feeling_id).up() + .e("id", post.id).up() + .e("is_autopost", post.is_autopost).up() + .e("is_community_private_autopost", post.is_community_private_autopost).up() + .e("is_spoiler", post.is_spoiler).up() + .e("is_app_jumpable", post.is_app_jumpable).up() + .e("empathy_count", post.empathy_count).up() + .e("language_id", post.language_id).up() + .e("mii", post.mii).up() + .e("mii_face_url", "https://s3.amazonaws.com/olv-public/pap/WVW69koebmETvBVqm1").up(); + xml = xml.e("number", "0").up(); + if (post.painting) { + xml = xml.e("painting") + .e("format", "tga").up() + .e("content", post.painting).up() + .e("size", post.painting.length).up() + .e("url", "https://s3.amazonaws.com/olv-public/pap/WVW69koebmETvBVqm1").up() + .up(); + } + xml = xml.e("pid", post.pid).up() + .e("platform_id", post.platform_id).up() + .e("region_id", post.region_id).up() + .e("reply_count", post.reply_count).up() + .e("screen_name", post.screen_name).up() + .e("title_id", post.title_id).up() + .up().up().up(); + } + xml = xml.up().up() + } + return xml.end({ pretty: true, allowEmpty: true }); + } } if (typeof module !== "undefined") {