From aeb07a2f3871f2dbe5df2e8a0170a75a6f72b808 Mon Sep 17 00:00:00 2001 From: Jemma Poffinbarger Date: Sun, 16 Apr 2023 18:18:16 -0500 Subject: [PATCH] Fixed topics and communities response --- src/database.js | 2 +- src/services/miiverse-api/routes/communities.js | 3 ++- src/services/miiverse-api/routes/people.js | 5 +++-- src/services/miiverse-api/routes/post.js | 5 ++++- src/services/miiverse-api/routes/topics.js | 9 ++++----- src/util/xmlResponseGenerator.js | 9 +++++---- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/database.js b/src/database.js index 394cbde..87ad0ad 100644 --- a/src/database.js +++ b/src/database.js @@ -68,7 +68,7 @@ async function getCommunityByTitleID(title_id) { async function getCommunityByID(community_id) { verifyConnected(); return COMMUNITY.findOne({ - olive_community_id: community_id + community_id: community_id }); } diff --git a/src/services/miiverse-api/routes/communities.js b/src/services/miiverse-api/routes/communities.js index e8603f8..90fdfd6 100644 --- a/src/services/miiverse-api/routes/communities.js +++ b/src/services/miiverse-api/routes/communities.js @@ -80,7 +80,8 @@ router.get('/:appID/posts', async function (req, res) { let options = { name: 'posts', with_mii: req.query.with_mii === '1', - app_data: true + app_data: true, + topic_tag: true } res.contentType("application/xml"); res.send(await comPostGen.PostsResponse(posts, community, options)); diff --git a/src/services/miiverse-api/routes/people.js b/src/services/miiverse-api/routes/people.js index 859830f..8f918cc 100644 --- a/src/services/miiverse-api/routes/people.js +++ b/src/services/miiverse-api/routes/people.js @@ -8,7 +8,7 @@ const util = require('../../../util/util') /* GET post titles. */ router.get('/', async function (req, res) { let userContent = await database.getUserContent(req.pid); - + if(!userContent) return res.sendStatus(404); let query = { removed: false, is_spoiler: 0, @@ -45,7 +45,8 @@ router.get('/', async function (req, res) { /* Build formatted response and send it off. */ let options = { name: 'posts', - with_mii: req.query.with_mii === '1' + with_mii: req.query.with_mii === '1', + topic_tag: true } res.contentType("application/xml"); res.send(await xmlGenerator.People(posts, options)); diff --git a/src/services/miiverse-api/routes/post.js b/src/services/miiverse-api/routes/post.js index 0312c37..51f9c9a 100644 --- a/src/services/miiverse-api/routes/post.js +++ b/src/services/miiverse-api/routes/post.js @@ -21,6 +21,8 @@ router.post('/:post_id/replies', upload.none(), async function (req, res) { awai router.post('/:post_id.delete', async function (req, res) { const post = await database.getPostByID(req.params.post_id); let user = await database.getUserContent(req.pid); + if(!post || !user) + return res.sendStatus(504); if(post.pid === user.pid) { await post.remove('User requested removal'); res.sendStatus(200); @@ -77,7 +79,8 @@ router.get('/:post_id/replies', async function (req, res) { return res.sendStatus(404); let options = { name: 'replies', - with_mii: req.query.with_mii === 1 + with_mii: req.query.with_mii === 1, + topic_tag: true } /* Build formatted response and send it off. */ let response = await communityPostGen.RepliesResponse(posts, options) diff --git a/src/services/miiverse-api/routes/topics.js b/src/services/miiverse-api/routes/topics.js index 862af09..6151d3e 100644 --- a/src/services/miiverse-api/routes/topics.js +++ b/src/services/miiverse-api/routes/topics.js @@ -17,7 +17,7 @@ router.get('/', async function (req, res) { if(!discovery.topics) return res.sendStatus(404); let communities = await calculateMostPopularCommunities(24, 10); - if(communities === null) return res.sendStatus(404); + if(communities === null || communities.length < 10) return res.sendStatus(404); let response = await memoized(communities); res.contentType("application/xml"); @@ -27,9 +27,8 @@ router.get('/', async function (req, res) { async function calculateMostPopularCommunities(hours, limit) { const now = new Date(); const last24Hours = new Date(now.getTime() - hours * 60 * 60 * 1000); - - const posts = await POST.find({ created_at: { $gte: last24Hours }, message_to_pid: null }).lean(); - + const posts = await POST.find({ created_at: { $gte: last24Hours }, message_to_pid: null }); + if(!posts) return; const communityIds = {}; for (const post of posts) { const communityId = post.community_id; @@ -39,7 +38,7 @@ async function calculateMostPopularCommunities(hours, limit) { .sort((a, b) => b[1] - a[1]) .map((entry) => entry[0]); if(communities.size < limit) - return calculateMostPopularCommunities(hours + hours, limit); + return COMMUNITY.find().limit(limit).sort({followers: -1}); let response = await COMMUNITY.aggregate([ { $match: { olive_community_id: { $in: communities }, parent: null } }, diff --git a/src/util/xmlResponseGenerator.js b/src/util/xmlResponseGenerator.js index 796884e..8a51d5f 100644 --- a/src/util/xmlResponseGenerator.js +++ b/src/util/xmlResponseGenerator.js @@ -152,7 +152,7 @@ class XmlResponseGenerator { for (const post of posts) { xml = xml.e("person") .e("posts") - postObj(xml, post, { with_mii: true, app_data: false }); + postObj(xml, post, { with_mii: true, app_data: false, topic_tag: false, topics: true }, community); xml = xml.up().up(); } xml = xml.up().up() @@ -212,14 +212,15 @@ class XmlResponseGenerator { * @param xml * @param post * @param options + * @param community */ -function postObj(xml, post, options) { +function postObj(xml, post, options, community) { xml = xml.e("post"); if (post.app_data && options.app_data) { xml.e("app_data", post.app_data.replace(/[^A-Za-z0-9+/=]/g, "").replace(/[\n\r]+/gm, '').trim()).up(); } xml.e("body", post.body ? post.body.replace(/[^A-Za-z\d\s-_!@#$%^&*(){}+=,.<>/?;:'"\[\]]/g, "").replace(/[\n\r]+/gm, '') : "").up() - .e("community_id", post.community_id).up() + .e("community_id", options.topics ? community.community_id : post.community_id).up() .e("country_id", post.country_id ? post.country_id : 254).up() .e("created_at", new moment(post.created_at).format('YYYY-MM-DD HH:MM:SS')).up() .e("feeling_id", post.feeling_id).up() @@ -254,7 +255,7 @@ function postObj(xml, post, options) { .e("url", `https://pretendo-cdn.b-cdn.net/screenshots/${post.pid}/${post.id}.jpg`).up() .up(); } - if (post.topic_tag) { + if (post.topic_tag && options.topic_tag) { xml.e("topic_tag") .e("name", post.topic_tag).up() .e("title_id", post.title_id).up()