From a9828d89ed1ebbf3eae80a5af4b486a480df6055 Mon Sep 17 00:00:00 2001 From: CaramelKat <32065563+caramelkat@users.noreply.github.com> Date: Fri, 22 May 2020 02:50:08 -0500 Subject: [PATCH] Fixed posting in portal, added support for video posts --- src/database.js | 9 +- src/html/portal/communities.html | 52 +------ src/html/portal/communities_body.html | 171 ++++++++++----------- src/html/portal/community.html | 27 +++- src/html/portal/community_body.html | 22 ++- src/html/portal/feed_body.html | 1 + src/html/portal/post.html | 2 +- src/html/portal/post_body.html | 18 +-- src/models/post.js | 15 ++ src/services/web-api/index.js | 14 +- src/services/web-api/routes/portal.js | 204 +++++++++++++++++++++----- src/services/web-api/routes/post.js | 12 +- 12 files changed, 340 insertions(+), 207 deletions(-) diff --git a/src/database.js b/src/database.js index 3b8e3a4..ead78db 100644 --- a/src/database.js +++ b/src/database.js @@ -81,12 +81,18 @@ async function getPostsByUserID(userID) { }); } -async function getPostsByCommunity(community, numberOfPosts) { +async function getHotPostsByCommunity(community, numberOfPosts) { verifyConnected(); return POST.find({ title_id: community.title_id }).sort({empathy_count: -1}).limit(numberOfPosts); } +async function getPostsByCommunity(community, numberOfPosts) { + verifyConnected(); + return POST.find({ + title_id: community.title_id + }).limit(numberOfPosts); +} async function getPostsByCommunityKey(community, numberOfPosts, search_key) { verifyConnected(); @@ -125,6 +131,7 @@ module.exports = { getCommunityByID, getDiscoveryHosts, getPostsByCommunity, + getHotPostsByCommunity, getPostsByCommunityKey, getPostsByUserID, getPostByID, diff --git a/src/html/portal/communities.html b/src/html/portal/communities.html index e47329f..df080aa 100644 --- a/src/html/portal/communities.html +++ b/src/html/portal/communities.html @@ -104,19 +104,7 @@ - + OLV_WIIU_NEW_COMMUNITIES Show More

Special

@@ -185,43 +173,5 @@ - \ No newline at end of file diff --git a/src/html/portal/communities_body.html b/src/html/portal/communities_body.html index bbff359..f5607c8 100644 --- a/src/html/portal/communities_body.html +++ b/src/html/portal/communities_body.html @@ -1,109 +1,96 @@ - Communities - -
+
- -
-

New Communities

+ +
+

New Communities

-
- -
+
+ +
+ +
+ OLV_WIIU_NEW_COMMUNITIES + Show More + +

Special

+
    +
+ +
-
    -
  • - - -
    -
    - GameName (UniqueID: 0x01769) - - Wii U Games + Search
    -
    -
  • -
-Show More - -

Special

-
    -
- - - -
-Search -
- diff --git a/src/html/portal/community.html b/src/html/portal/community.html index 827899a..421a22e 100644 --- a/src/html/portal/community.html +++ b/src/html/portal/community.html @@ -16,7 +16,7 @@ - + @@ -48,9 +48,9 @@
  • - All Posts + All Posts
  • -
  • Popular posts
  • +
  • Popular posts
  • @@ -112,7 +112,7 @@

    Post to OLV_PAGE_TITLE_STANDIN Community

    -
    + @@ -184,7 +184,7 @@
    -
    diff --git a/src/html/portal/community_body.html b/src/html/portal/community_body.html index dadd0cf..947313c 100644 --- a/src/html/portal/community_body.html +++ b/src/html/portal/community_body.html @@ -22,9 +22,9 @@
  • - All Posts + All Posts
  • -
  • Popular posts
  • +
  • Popular posts
  • @@ -158,7 +158,7 @@
    diff --git a/src/html/portal/feed_body.html b/src/html/portal/feed_body.html index c000418..31f0e0c 100644 --- a/src/html/portal/feed_body.html +++ b/src/html/portal/feed_body.html @@ -23,6 +23,7 @@

    This would normally be loading the activity feed. But it has not been implemented yet. Check back later!

    +
    diff --git a/src/html/portal/post.html b/src/html/portal/post.html index 13e261d..8d48c7d 100644 --- a/src/html/portal/post.html +++ b/src/html/portal/post.html @@ -16,7 +16,7 @@ - + diff --git a/src/html/portal/post_body.html b/src/html/portal/post_body.html index 57b4085..ce6c8b5 100644 --- a/src/html/portal/post_body.html +++ b/src/html/portal/post_body.html @@ -31,7 +31,7 @@ data-is-post="1" data-is-permalink="1" data-can-report-spoiler="1" - data-community-id="4490545270232728351" data-url-id="AYECAAAEAAAsUZ8kcv4hUQ" data-track-label="default" data-title-id="4490545270232728350" data-track-action="openReportModal" data-track-category="reportViolation" + data-community-id="4490545270232728351" data-url-id="OLV_POST_ID" data-track-label="default" data-title-id="4490545270232728350" data-track-action="openReportModal" data-track-category="reportViolation" >Report Violation
    @@ -41,7 +41,7 @@ data-action="/posts/OLV_POST_ID/empathies" data-other-empathy-count="0" data-sound="SE_WAVE_MII_ADD" - data-community-id="4490545270232728351" data-url-id="AYECAAAEAAAsUZ8kcv4hUQ" data-track-label="default" data-title-id="4490545270232728350" data-track-action="yeah" data-track-category="empathy" + data-community-id="4490545270232728351" data-url-id="OLV_POST_ID" data-track-label="default" data-title-id="4490545270232728350" data-track-action="yeah" data-track-category="empathy" >Yeah!
    @@ -79,7 +79,7 @@

    Comment on OLV_DISPLAY_NAME's Post

    - +
    @@ -178,7 +178,7 @@

    Report Violation to Miiverse Administrators

    - +

    You are about to report a post with content which violates the Miiverse Code of Conduct. This report will be sent to Nintendo's Miiverse administrators and not to the creator of the post.

    @@ -260,7 +260,7 @@
    + data-community-id="4490545270232728351" data-url-id="OLV_POST_ID" data-track-label="default" data-title-id="4490545270232728350" data-track-action="openReportModal" data-track-category="reportViolation">
    @@ -277,9 +277,9 @@
    Select an option. diff --git a/src/models/post.js b/src/models/post.js index e5bb78e..ce26f9c 100644 --- a/src/models/post.js +++ b/src/models/post.js @@ -55,6 +55,7 @@ const PostSchema = new Schema({ pid: Number, platform_id: Number, region_id: Number, + parent_post: Number, reply_count: { type: Number, default: 0 @@ -76,6 +77,20 @@ PostSchema.methods.downEmpathy = async function() { await this.save(); }; +PostSchema.methods.upReply = async function() { + const replyCount = this.get('reply_count'); + this.set('reply_count', replyCount + 1); + + await this.save(); +}; + +PostSchema.methods.downReply = async function() { + const replyCount = this.get('reply_count'); + this.set('reply_count', replyCount - 1); + + await this.save(); +}; + const POST = model('POST', PostSchema); module.exports = { diff --git a/src/services/web-api/index.js b/src/services/web-api/index.js index 5352668..e8df2c6 100644 --- a/src/services/web-api/index.js +++ b/src/services/web-api/index.js @@ -8,23 +8,27 @@ const router = express.Router(); // Router to handle the subdomain restriction const api = express.Router(); +const web = express.Router(); +const ctr = express.Router(); const portal = express.Router(); // Create subdomains logger.info('[MIIVERSE] Creating \'web api\' subdomain'); -router.use(subdomain('web.olv', api)); -router.use(subdomain('ctr.olv', api)); +router.use(subdomain('web.olv', web)); +router.use(subdomain('ctr.olv', ctr)); router.use(subdomain('portal.olv', portal)); // Setup routes -api.use('/web', routes.WEB); -api.use('/titles/show', routes.CTR); +web.use('/', routes.WEB); +ctr.use('/titles/show', routes.CTR); portal.use('/', routes.PORTAL); portal.use('/v1/communities/', routes.COMMUNITY); portal.use('/v1/posts/', routes.POST); +portal.use('/post', routes.NEWPOST); +web.use('/v1/communities/', routes.COMMUNITY); api.use('/v1/communities/', routes.COMMUNITY); api.use('/v1/posts/', routes.POST); -api.use('/post/', routes.POST); +api.use('/post', routes.NEWPOST); api.use('/users/', routes.USERS); portal.use('/users/', routes.USERS); diff --git a/src/services/web-api/routes/portal.js b/src/services/web-api/routes/portal.js index 6344e0f..8ee59f7 100644 --- a/src/services/web-api/routes/portal.js +++ b/src/services/web-api/routes/portal.js @@ -2,11 +2,13 @@ var express = require('express'); var router = express.Router(); const database = require('../../../database'); const fs = require('fs-extra'); +const util = require('../../../util/authentication'); const comPostGen = require('../../../util/CommunityPostGen'); const processHeaders = require('../../../util/authentication'); /* GET post titles. */ router.get('/titles/show', function (req, res) { + res.header('X-Nintendo-WhiteList','1|http,youtube.com,,2|https,youtube.com,,2|http,.youtube.com,,2|https,.youtube.com,,2|http,.ytimg.com,,2|https,.ytimg.com,,2|http,.googlevideo.com,,2|https,.googlevideo.com,,2|https,youtube.com,/embed/,6|https,youtube.com,/e/,6|https,youtube.com,/v/,6|https,www.youtube.com,/embed/,6|https,www.youtube.com,/e/,6|https,www.youtube.com,/v/,6|https,youtube.googleapis.com,/e/,6|https,youtube.googleapis.com,/v/,6|http,maps.googleapis.com,/maps/api/streetview,2|https,maps.googleapis.com,/maps/api/streetview,2|http,cbk0.google.com,/cbk,2|https,cbk0.google.com,/cbk,2|http,cbk1.google.com,/cbk,2|https,cbk1.google.com,/cbk,2|http,cbk2.google.com,/cbk,2|https,cbk2.google.com,/cbk,2|http,cbk3.google.com,/cbk,2|https,cbk3.google.com,/cbk,2|https,.cloudfront.net,,2|https,www.google-analytics.com,/,2|https,stats.g.doubleclick.net,,2|https,www.google.com,/ads/,2|https,ssl.google-analytics.com,,2') res.redirect('/communities'); }); router.get('/settings/profile', function (req, res) { @@ -45,36 +47,137 @@ router.get('/*/portal.js', function (req, res) { router.get('/*/portal.css', function (req, res) { res.sendFile("portal.css", { root: 'src/html/portal' }); }); -router.get('/posts/*', function (req, res) { - database.connect().then(async e => { +router.post('/posts/*/empathies', function (req, res, next) { + let paramPackData = util.data.decodeParamPack(req.headers["x-nintendo-parampack"]); + let pid = req.originalUrl.replace('/posts/', '').replace('/empathies','').trim(); + console.log(pid); + database.connect().then(async emp => { + let post = await database.getPostByID(parseInt(pid)); + await post.upEmpathy(); + }); + res.status(200); + res.send(); +}); +router.post('/posts/*/empathies.delete', function (req, res, next) { + let paramPackData = util.data.decodeParamPack(req.headers["x-nintendo-parampack"]); + let pid = req.originalUrl.replace('/posts/', '').replace('/empathies.delete','').trim(); + console.log(pid); + database.connect().then(async emp => { + let post = await database.getPostByID(parseInt(pid)); + await post.downEmpathy(); + }); + res.status(200); + res.send(); +}); +router.post('/posts/*/replies', async function (req, res, next) { + try + { let postID = req.originalUrl.replace('/posts', '').split('/').pop().split('?')[0]; - console.log(postID); - let post = await database.getPostByID(postID); - let community = await database.getCommunityByTitleID(post.title_id); - if(post === null) - res.sendStatus(404); + let pid = util.data.processServiceToken(req.headers["x-nintendo-servicetoken"]); + if(pid == null) + { + throw new Error('The User token was not valid'); + } else { - let file = ''; - if (req.query._pjax === "#body") - file = await fs.readFile(`src/html/portal/post_body.html`, 'utf-8'); + let usrObj; + usrObj = await util.data.processUser(pid); + 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 paintingURI = ""; + if (req.body.painting) { + paintingURI = await util.data.processPainting(painting); + } + let screenshot = ""; + if (req.body.screenshot) { + screenshot = req.body.screenshot.replace(/\0/g, "").trim(); + } + const document = { + title_id: req.body.olive_title_id, + screen_name: usrObj.user_id, + body: req.body.body, + app_data: appData, + painting: painting, + painting_uri: paintingURI, + screenshot: screenshot, + url: req.body.url, + created_at: creationDate, + feeling_id: req.body.feeling_id, + id: snowflake.nextId(), + is_autopost: req.body.is_autopost, + is_spoiler: req.body.is_spoiler, + mii: usrObj.mii, + mii_face_url: "http://mii-images.account.pretendo.cc/", + }; + const newPost = new POST(document); + newPost.save(); + res.sendStatus(200); + } + } + catch (e) + { + res.set("Content-Type", "application/xml"); + res.statusCode = 400; + response = { + result: { + has_error: 1, + version: 1, + code: 400, + error_code: 7, + message: "POSTING_FROM_NNID" + } + }; + res.send("\n" + xml(response)); + } + +}); +router.get('/posts/*', function (req, res) { + database.connect().then(async e => { + try + { + let postID = req.originalUrl.replace('/posts', '').split('/').pop().split('?')[0]; + console.log(postID); + let post = await database.getPostByID(parseInt(postID)); + let community = await database.getCommunityByTitleID(post.title_id); + if(post === null) + res.sendStatus(404); else - file = await fs.readFile(`src/html/portal/post.html`, 'utf-8'); - let newFile = file.replace(/OLV_DISPLAY_NAME/g, post.screen_name) - .replace(/OLV_TOPIC_TAG/g, post.topic_tag) - .replace(/OLV_ICON_STANDIN/g, community.browser_icon) - .replace(/OLV_COMMUNITY_ID/g, community.community_id) - .replace(/OLV_POST_TIMESTAMP/g, post.created_at) - .replace(/OLV_PAGE_TITLE_STANDIN/g, community.name); - let postContent = ''; - if(post.screenshot !== undefined && post.screenshot !== '') - postContent = '

    ' + post.body + '

    '+ - '

    '; - else if(post.painting_uri !== undefined && post.painting_uri !== '') - postContent = '

    '; - else - postContent = '

    ' + post.body + '

    '; - res.send(newFile.replace('OLV_POST_CONTENT', postContent)); + { + let file = ''; + if (req.query._pjax === "#body") + file = await fs.readFile(`src/html/portal/post_body.html`, 'utf-8'); + else + file = await fs.readFile(`src/html/portal/post.html`, 'utf-8'); + let newFile = file.replace(/OLV_DISPLAY_NAME/g, post.screen_name) + .replace(/OLV_TOPIC_TAG/g, post.topic_tag) + .replace(/OLV_ICON_STANDIN/g, community.browser_icon) + .replace(/OLV_COMMUNITY_ID/g, community.community_id) + .replace(/OLV_POST_TIMESTAMP/g, post.created_at) + .replace(/OLV_PAGE_TITLE_STANDIN/g, community.name) + .replace(/OLV_POST_ID/g, post.id); + let postContent = ''; + if(post.painting_uri !== undefined && post.painting_uri !== '') + postContent += '

    '; + else + postContent += '

    ' + post.body + '

    '; + if(post.screenshot !== undefined && post.screenshot !== '') + postContent += '

    ' + post.body + '

    '+ + '

    '; + else if(post.url !== undefined && post.url !== '') + postContent += ''; + + res.send(newFile.replace('OLV_POST_CONTENT', postContent)); + } + } + catch (e) { + res.sendStatus(404); } }); @@ -92,10 +195,10 @@ router.get('/titles/*', function (req, res) { file = await fs.readFile(`src/html/portal/community_body.html`, 'utf-8'); else file = await fs.readFile(`src/html/portal/community.html`, 'utf-8'); - let newFile = file.replace('COMMUNITY_TITLE_ID_STANDIN', communityID) - .replace('OLV_TITLEID_STANDIN', communityID) - .replace('OLV_ICON_STANDIN', community.browser_icon) - .replace('OLV_HEADER_STANDIN', community.WiiU_browser_header) + let newFile = file.replace(/COMMUNITY_TITLE_ID_STANDIN/g, communityID) + .replace(/OLV_TITLEID_STANDIN/g, community.title_id[0]) + .replace(/OLV_ICON_STANDIN/g, community.browser_icon) + .replace(/OLV_HEADER_STANDIN/g, community.WiiU_browser_header) .replace(/OLV_PAGE_TITLE_STANDIN/g, community.name) res.send(newFile); } @@ -104,9 +207,42 @@ router.get('/titles/*', function (req, res) { }); router.get('/communities', function (req, res) { - if (req.query._pjax === "#body") - res.sendFile("communities_body.html", { root: 'src/html/portal' }); - else - res.sendFile("communities.html", { root: 'src/html/portal' }); + database.connect().then(async e => { + let communities = await database.getCommunities(6); + if(communities === null) + res.sendStatus(404); + else + { + let file = ''; + if (req.query._pjax === "#body") + file = await fs.readFile(`src/html/portal/communities_body.html`, 'utf-8'); + else + file = await fs.readFile(`src/html/portal/communities.html`, 'utf-8'); + let communityBody = '
      '; + for(let i = 0; i < communities.length; i++) + { + communityBody += '
    • ' + + '' + + '' + + '
      ' + + '
      ' + + '' + communities[i].name + ''; + switch(parseInt(communities[i].platform_id)) + { + case 0: + communityBody += 'Wii U Games'; + break; + case 1: + communityBody += '3DS Games'; + break; + case 2: + communityBody += 'Both Platform Games'; + break; + } + } + res.send(file.replace('OLV_WIIU_NEW_COMMUNITIES', communityBody + '
    • ')); + } + + }); }); module.exports = router; \ No newline at end of file diff --git a/src/services/web-api/routes/post.js b/src/services/web-api/routes/post.js index 6fd8034..e4127b1 100644 --- a/src/services/web-api/routes/post.js +++ b/src/services/web-api/routes/post.js @@ -9,7 +9,17 @@ router.get('/', function (req, res) { //const paramPack = processHeaders.data.decodeParamPack(req.headers["x-nintendo-parampack"]); //"[0:1]=1407375153523200" let community = await database.getCommunityByID(req.query.community_id); - let posts = await database.getPostsByCommunity(community, parseInt(req.query.limit)); + let posts; + console.log(parseFloat(req.query.community_id)); + switch(parseFloat(req.query.type)) + { + case 0: + posts = await database.getPostsByCommunity(community, parseInt(req.query.limit)); + break; + case 1: + posts = await database.getHotPostsByCommunity(community, parseInt(req.query.limit)); + break; + } let formatType = parseInt(req.query.format); let body = ''; let platformIDTag = '';