diff --git a/src/database.js b/src/database.js index 277751b..048b77b 100644 --- a/src/database.js +++ b/src/database.js @@ -76,7 +76,8 @@ async function getTotalPostsByCommunity(community) { verifyConnected(); return POST.find({ title_id: community.title_id, - parent: null + parent: null, + removed: false }).countDocuments(); } @@ -91,14 +92,16 @@ async function getPostsByUserID(userID) { verifyConnected(); return POST.find({ pid: userID, - parent: null + parent: null, + removed: false }); } async function getPostReplies(postID, number) { verifyConnected(); return POST.find({ - parent: postID + parent: postID, + removed: false }).limit(number); } @@ -109,7 +112,8 @@ async function getDuplicatePosts(pid, post) { body: post.body, painting: post.painting, screenshot: post.screenshot, - parent: null + parent: null, + removed: false }); } @@ -118,7 +122,8 @@ async function getUserPostRepliesAfterTimestamp(post, numberOfPosts) { return POST.find({ parent: post.pid, created_at: { $lt: post.created_at }, - message_to_pid: null + message_to_pid: null, + removed: false }).limit(numberOfPosts); } @@ -127,7 +132,8 @@ async function getNumberUserPostsByID(userID, number) { return POST.find({ pid: userID, parent: null, - message_to_pid: null + message_to_pid: null, + removed: false }).sort({ created_at: -1}).limit(number); } @@ -136,7 +142,8 @@ async function getTotalPostsByUserID(userID) { return POST.find({ pid: userID, parent: null, - message_to_pid: null + message_to_pid: null, + removed: false }).countDocuments(); } @@ -144,7 +151,8 @@ async function getHotPostsByCommunity(community, numberOfPosts) { verifyConnected(); return POST.find({ title_id: community.title_id, - parent: null + parent: null, + removed: false }).sort({empathy_count: -1}).limit(numberOfPosts); } @@ -152,7 +160,8 @@ async function getNumberNewCommunityPostsByID(community, number) { verifyConnected(); return POST.find({ title_id: community.title_id, - parent: null + parent: null, + removed: false }).sort({ created_at: -1}).limit(number); } @@ -160,7 +169,8 @@ async function getNumberPopularCommunityPostsByID(community, limit, offset) { verifyConnected(); return POST.find({ title_id: community.title_id, - parent: null + parent: null, + removed: false }).sort({ empathy_count: -1}).skip(offset).limit(limit); } @@ -169,15 +179,17 @@ async function getNumberVerifiedCommunityPostsByID(community, limit, offset) { return POST.find({ title_id: community.title_id, verified: true, - parent: null + parent: null, + removed: false }).sort({ created_at: -1}).skip(offset).limit(limit); } async function getPostsByCommunity(community, numberOfPosts) { verifyConnected(); return POST.find({ - community_id: community.id, - parent: null + title_id: community.title_id, + parent: null, + removed: false }).limit(numberOfPosts); } @@ -186,27 +198,37 @@ async function getPostsByCommunityKey(community, numberOfPosts, search_key) { return POST.find({ title_id: community.title_id, search_key: search_key, - parent: null + parent: null, + removed: false }).limit(numberOfPosts); } -async function getFriendMessages(pid, search_key, limit) { - verifyConnected(); - return POST.find({ - message_to_pid: pid, - search_key: search_key, - parent: null - }).limit(limit); -} - async function getNewPostsByCommunity(community, limit, offset) { verifyConnected(); return POST.find({ community_id: community.community_id, - parent: null + parent: null, + removed: false }).sort({ created_at: -1 }).skip(offset).limit(limit); } +async function getAllUserPosts(pid) { + verifyConnected(); + return POST.find({ + pid: pid, + message_to_pid: null + }); +} + +async function getRemovedUserPosts(pid) { + verifyConnected(); + return POST.find({ + pid: pid, + message_to_pid: null, + removed: true + }); +} + async function getUserPostsAfterTimestamp(post, numberOfPosts) { verifyConnected(); return POST.find({ @@ -214,6 +236,7 @@ async function getUserPostsAfterTimestamp(post, numberOfPosts) { created_at: { $lt: post.created_at }, parent: null, message_to_pid: null, + removed: false }).limit(numberOfPosts); } @@ -222,7 +245,8 @@ async function getUserPostsOffset(pid, limit, offset) { return POST.find({ pid: pid, parent: null, - message_to_pid: null + message_to_pid: null, + removed: false }).skip(offset).limit(limit).sort({ created_at: -1}); } @@ -231,7 +255,8 @@ async function getCommunityPostsAfterTimestamp(post, numberOfPosts) { return POST.find({ title_id: post.title_id, created_at: { $lt: post.created_at }, - parent: null + parent: null, + removed: false }).limit(numberOfPosts); } @@ -303,7 +328,8 @@ async function getNewsFeed(content, numberOfPosts) { {community_id: content.followed_communities}, ], parent: null, - message_to_pid: null + message_to_pid: null, + removed: false }).limit(numberOfPosts).sort({ created_at: -1}); } @@ -317,7 +343,8 @@ async function getNewsFeedAfterTimestamp(content, numberOfPosts, post) { ], created_at: { $lt: post.created_at }, parent: null, - message_to_pid: null + message_to_pid: null, + removed: false }).limit(numberOfPosts).sort({ created_at: -1}); } @@ -330,7 +357,8 @@ async function getNewsFeedOffset(content, limit, offset) { {community_id: content.followed_communities}, ], parent: null, - message_to_pid: null + message_to_pid: null, + removed: false }).skip(offset).limit(limit).sort({ created_at: -1}); } @@ -364,7 +392,8 @@ async function getConversationMessages(community_id, limit, offset) { verifyConnected(); return POST.find({ community_id: community_id, - parent: null + parent: null, + removed: false }).sort({created_at: 1}).skip(offset).limit(limit); } @@ -384,7 +413,8 @@ async function getLatestMessage(pid, pid2) { $or: [ {pid: pid, message_to_pid: pid2}, {pid: pid2, message_to_pid: pid} - ] + ], + removed: false }) } @@ -472,7 +502,6 @@ module.exports = { getConversationMessages, getUnreadConversationCount, getLatestMessage, - getFriendMessages, getPNID, getPNIDS, getUsersSettings, @@ -482,5 +511,7 @@ module.exports = { getNotifications, getUnreadNotificationCount, getNotification, - getLastNotification + getLastNotification, + getAllUserPosts, + getRemovedUserPosts }; diff --git a/src/models/post.js b/src/models/post.js index 635f431..1dfc895 100644 --- a/src/models/post.js +++ b/src/models/post.js @@ -63,7 +63,12 @@ const PostSchema = new Schema({ message_to_pid: { type: String, default: null - } + }, + removed: { + type: Boolean, + default: false + }, + removed_reason: String }); @@ -95,6 +100,18 @@ PostSchema.methods.downReply = async function() { await this.save(); }; +PostSchema.methods.remove = async function(reason) { + this.set('remove', true); + this.set('removed_reason', reason) + await this.save(); +}; + +PostSchema.methods.unRemove = async function(reason) { + this.set('remove', false); + this.set('removed_reason', reason) + await this.save(); +}; + const POST = model('POST', PostSchema); module.exports = { diff --git a/src/services/miiverse-api/routes/post.js b/src/services/miiverse-api/routes/post.js index fb0f32c..692dc15 100644 --- a/src/services/miiverse-api/routes/post.js +++ b/src/services/miiverse-api/routes/post.js @@ -13,30 +13,25 @@ var upload = multer(); router.post('/', upload.none(), async function (req, res, next) { try { + let PNID = await database.getPNID(req.pid), userSettings = await database.getUserSettings(req.pid), parentPost = null, postID = snowflake.nextId(); let paramPackData = util.data.decodeParamPack(req.headers["x-nintendo-parampack"]); - let user = await database.getPNID(req.pid); let community = await database.getCommunityByTitleID(paramPackData.title_id) - if(community.community_id === 'announcements') - return res.sendStatus(403) - let appData = ""; - if (req.body.app_data) { - appData = req.body.app_data.replace(/\0/g, "").replace(/\r?\n|\r/g, "").trim(); - } - let painting = ""; - if (req.body.painting) { - painting = req.body.painting.replace(/\0/g, "").replace(/\r?\n|\r/g, "").trim(); - } - let paintingURI = ""; - if (req.body.painting) { + if(userSettings.account_status !== 0 || community.community_id === 'announcements') + throw new Error('User not allowed to post') + let appData = "", painting = "", paintingURI = "", screenshot = null; + if (req.body.app_data) + appData = req.body.app_data.replace(/\0/g, "").trim(); + if (req.body.painting && req.body.painting !== 'eJztwTEBACAMA7DCNRlIQRbu4ZoEviTJTNvjZNUFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAL55fYLL3w==') { + painting = req.body.painting.replace(/\0/g, "").trim(); paintingURI = await util.data.processPainting(painting, true); + await util.data.uploadCDNAsset('pn-cdn', `paintings/${req.pid}/${postID}.png`, paintingURI, 'public-read'); } - let screenshot = ""; if (req.body.screenshot) { screenshot = req.body.screenshot.replace(/\0/g, "").trim(); + await util.data.uploadCDNAsset('pn-cdn', `screenshots/${req.pid}/${postID}.jpg`, Buffer.from(screenshot, 'base64'), 'public-read'); } let miiFace; - console.log(parseInt(req.body.feeling_id)) switch (parseInt(req.body.feeling_id)) { case 1: miiFace = 'smile_open_mouth.png'; @@ -59,31 +54,27 @@ router.post('/', upload.none(), async function (req, res, next) { } const document = { title_id: paramPackData.title_id, - screen_name: user.mii.name, + community_id: community.community_id, + screen_name: userSettings.screen_name, body: req.body.body, app_data: appData, painting: painting, - painting_uri: paintingURI, - screenshot: screenshot, - url: req.body.url, - search_key: req.body.search_key, - topic_tag: req.body.topic_tag, - community_id: community.community_id, + screenshot: screenshot ? `/screenshots/${req.pid}/${postID}.jpg`: "", country_id: paramPackData.country_id, created_at: new Date(), - feeling_id: req.body.feeling_id, - id: snowflake.nextId(), + feeling_id: req.body.emotion, + id: postID, is_autopost: req.body.is_autopost, - is_spoiler: req.body.is_spoiler, + is_spoiler: (req.body.spoiler) ? 1 : 0, is_app_jumpable: req.body.is_app_jumpable, language_id: req.body.language_id, - mii: user.mii.data, - mii_face_url: `http://mii.olv.pretendo.cc/mii/${req.pid}/${miiFace}`, + mii: PNID.mii.data, + mii_face_url: `http://mii.olv.pretendo.cc/mii/${PNID.pid}/${miiFace}`, pid: req.pid, - verified: (user.access_level === 2 || user.access_level === 3), platform_id: paramPackData.platform_id, region_id: paramPackData.region_id, - parent: null, + verified: (PNID.access_level === 2 || PNID.access_level === 3), + parent: null }; const newPost = new POST(document); newPost.save(); diff --git a/src/util/CommunityPostGen.js b/src/util/CommunityPostGen.js index 007d9e2..f28fa2c 100644 --- a/src/util/CommunityPostGen.js +++ b/src/util/CommunityPostGen.js @@ -41,8 +41,8 @@ class CommunityPostGen { .e("platform_id", "1").up() .e("region_id", "4").up() .e("reply_count", "0").up() - .e("screen_name", posts[i].screen_name).up() - .e("title_id", community.title_ids[0]).up() + .e("screen_name", "Placeholder").up() + .e("title_id", community.title_id[0]).up() .up(); } @@ -200,12 +200,12 @@ class CommunityPostGen { .e('has_shop_page', community.has_shop_page).up() .e('icon', community.icon).up() .e('title_ids'); - community.title_ids.forEach(function (title_id) { + community.title_id.forEach(function (title_id) { if(title_id !== '') xml = xml.e('title_id', title_id).up(); }) xml = xml.up() - .e('title_id', community.title_ids[0]).up() + .e('title_id', community.title_id[0]).up() .e('community_id', community.community_id).up() .e('is_recommended', community.is_recommended).up() .e('name', community.name).up()