diff --git a/src/models/conversation.js b/src/models/conversation.js index 0b35f55..8243197 100644 --- a/src/models/conversation.js +++ b/src/models/conversation.js @@ -3,7 +3,7 @@ const moment = require("moment"); const snowflake = require('node-snowflake').Snowflake; const user = new Schema({ - pid: String, + pid: Number, official: { type: Boolean, default: false @@ -35,15 +35,14 @@ const ConversationSchema = new Schema({ }); ConversationSchema.methods.newMessage = async function(message, fromPid) { - const users = this.get('users'); - console.log(fromPid) - if(users[0].pid.toString() === fromPid.toString()) {; - users[1].read = false; + if(this.users[0].pid === fromPid) { + this.users[1].read = false; + this.markModified('users[1].read'); } else { - users[0].read = false; + this.users[0].read = false; + this.markModified('users[0].read'); } - this.set('users', users); this.set('last_updated', moment(new Date())); this.set('message_preview', message); await this.save(); @@ -51,9 +50,9 @@ ConversationSchema.methods.newMessage = async function(message, fromPid) { ConversationSchema.methods.markAsRead = async function(pid) { let users = this.get('users'); - if(users[0].pid === pid.toString()) + if(users[0].pid === pid) users[0].read = true; - else if(users[1].pid === pid.toString()) + else if(users[1].pid === pid) users[1].read = true; this.set('users', users) this.markModified('users'); diff --git a/src/models/notifications.js b/src/models/notifications.js index ccce089..0835463 100644 --- a/src/models/notifications.js +++ b/src/models/notifications.js @@ -1,37 +1,26 @@ const { Schema, model } = require('mongoose'); -const NotificationsSchema = new Schema({ +const NotificationSchema = new Schema({ pid: String, - /** - * 0 like - * 1 reply - * 2 new follower - * 3 other - */ - type: Number, - title: String, - content: String, - reference_id: String, + type: String, link: String, - created_at: { - type: Date, - default: new Date() - }, - read: { - type: Boolean, - default: false - }, - origin_pid: String, + objectID: String, + users: [{ + user: String, + timestamp: Date + }], + read: Boolean, + lastUpdated: Date }); -NotificationsSchema.methods.markRead = async function() { +NotificationSchema.methods.markRead = async function() { this.set('read', true); await this.save(); }; -const NOTIFICATION = model('NOTIFICATION', NotificationsSchema); +const NOTIFICATION = model('NOTIFICATION', NotificationSchema); module.exports = { - NotificationsSchema, + NotificationSchema, NOTIFICATION }; diff --git a/src/services/miiverse-api/routes/message.js b/src/services/miiverse-api/routes/message.js index b68147d..e28201f 100644 --- a/src/services/miiverse-api/routes/message.js +++ b/src/services/miiverse-api/routes/message.js @@ -15,8 +15,10 @@ router.post('/', upload.none(), async function (req, res) { let user = await database.getPNID(req.pid); let user2 = await database.getPNID(req.body.message_to_pid); let conversation = await database.getConversationByUsers([user.pid, user2.pid]); + let userSettings = await database.getUserSettings(req.pid), user2Settings = await database.getUserSettings(user2.pid), postID = await generatePostUID(21); + let friends = await util.data.getFriends(user2.pid); if(!conversation) { - if(!user || !user2) + if(!user || !user2 || userSettings || userSettings) return res.sendStatus(422) let document = { id: snowflake.nextId(), @@ -39,6 +41,27 @@ router.post('/', upload.none(), async function (req, res) { } if(!conversation) return res.sendStatus(404); + if(!friends || friends.indexOf(req.pid) === -1) + return res.sendStatus(422); + + if(req.body.body === '' && req.body.painting === '' && req.body.screenshot === '') { + res.status(422); + return res.redirect(`/friend_messages/${conversation.id}`); + } + let paramPackData = util.decodeParamPack(req.headers["x-nintendo-parampack"]); + let appData = "", painting = "", paintingURI = "", screenshot = null; + if (req.body.app_data) + appData = req.body.app_data.replace(/[^A-Za-z0-9+/=\s]/g, ""); + if (req.body.painting) { + painting = req.body.painting.replace(/\0/g, "").trim(); + paintingURI = await util.processPainting(painting, true); + await util.uploadCDNAsset('pn-cdn', `paintings/${req.pid}/${postID}.png`, paintingURI, 'public-read'); + } + if (req.body.screenshot) { + screenshot = req.body.screenshot.replace(/\0/g, "").trim(); + await util.uploadCDNAsset('pn-cdn', `screenshots/${req.pid}/${postID}.jpg`, Buffer.from(screenshot, 'base64'), 'public-read'); + } + let miiFace; switch (parseInt(req.body.feeling_id)) { case 1: @@ -60,22 +83,39 @@ router.post('/', upload.none(), async function (req, res) { miiFace = 'normal_face.png'; break; } - let document = { + let body = req.body.body; + if(body) + body = req.body.body.replace(/[^A-Za-z\d\s-_!@#$%^&*(){}‛¨ƒºª«»“”„¿¡←→↑↓√§¶†‡¦–—⇒⇔¤¢€£¥™©®+×÷=±∞ˇ˘˙¸˛˜′″µ°¹²³♭♪•…¬¯‰¼½¾♡♥●◆■▲▼☆★♀♂,./?;:'"\\<>]/g, ""); + if(body.length > 280) + body = body.substring(0,280); + const document = { + title_id: paramPackData.title_id, + community_id: community.olive_community_id, screen_name: user.mii.name, - body: req.body.body, - painting: req.body.raw, + body: body, + app_data: appData, + painting: painting, + screenshot: screenshot ? `/screenshots/${req.pid}/${postID}.jpg`: "", + screenshot_length: screenshot ? screenshot.length : null, + country_id: paramPackData.country_id, created_at: new Date(), - id: generatePostUID(21), - mii: user.mii.data, - mii_face_url: `https://mii.olv.pretendo.cc/mii/${PNID.pid}/${miiFace}`, - pid: user.pid, - verified: (user.access_level === 2 || user.access_level === 3), - parent: null, + feeling_id: req.body.feeling_id, + id: postID, search_key: req.body.search_key, topic_tag: req.body.topic_tag, - community_id: conversation.id, + is_autopost: req.body.is_autopost, + is_spoiler: (req.body.spoiler) ? 1 : 0, + is_app_jumpable: req.body.is_app_jumpable, + language_id: req.body.language_id, + mii: PNID.mii.data, + mii_face_url: `https://mii.olv.pretendo.cc/mii/${PNID.pid}/${miiFace}`, + pid: req.pid, + platform_id: paramPackData.platform_id, + region_id: paramPackData.region_id, + verified: (PNID.access_level === 2 || PNID.access_level === 3), message_to_pid: req.body.message_to_pid, - title_id: req.paramPackData.title_id, + parent: null, + removed: false }; const newPost = new POST(document); newPost.save(); diff --git a/src/util/util.js b/src/util/util.js index 12010cb..45e7dd4 100644 --- a/src/util/util.js +++ b/src/util/util.js @@ -314,6 +314,16 @@ let methods = { 'X-API-Key': api_key }) }) + }, + getFriendRequests: async function(pid) { + const requests = await client.getUserFriendRequestsIncoming({ + pid: pid + }, { + metadata: grpc.Metadata({ + 'X-API-Key': api_key + }) + }); + return requests.friendRequests; } }; module.exports = methods;