From 2ad5c144c6be1f7404488b65140f3ed3bf8b8fb6 Mon Sep 17 00:00:00 2001 From: Chris Brame Date: Mon, 4 Jul 2022 02:41:32 -0400 Subject: [PATCH] fix(messages): account population fields --- src/controllers/api/v2/messages.js | 15 ++++++++++--- src/models/chat/conversation.js | 5 +---- src/socketio/chatSocket.js | 36 +++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/controllers/api/v2/messages.js b/src/controllers/api/v2/messages.js index 2d5637165..afda77376 100644 --- a/src/controllers/api/v2/messages.js +++ b/src/controllers/api/v2/messages.js @@ -38,6 +38,8 @@ apiMessages.getConversations = async (req, res) => { if (participant._id.toString() !== req.user._id.toString()) { convoObject.partner = participant } + + delete participant.role } recentMessage = _.first(recentMessage) @@ -70,12 +72,12 @@ apiMessages.single = async (req, res) => { if (!conversation) return apiUtils.sendApiError(res, 404, 'Conversation not found') conversation = conversation.toObject() - let isPart = false + let isParticipant = false for (const participant of conversation.participants) { - if (participant._id.toString() === req.user._id.toString()) isPart = true + if (participant._id.toString() === req.user._id.toString()) isParticipant = true } - if (!isPart) return apiUtils.sendApiError(res, 400, 'Invalid') + if (!isParticipant) return apiUtils.sendApiError(res, 400, 'Invalid') const convoMessages = await Message.getConversationWithObject({ cid: conversation._id, @@ -83,8 +85,15 @@ apiMessages.single = async (req, res) => { requestingUser: req.user }) + // Clear the role. It's not needed + for (const message of convoMessages) { + message.owner.role = undefined + } + for (const participant of conversation.participants) { if (participant._id.toString() !== req.user._id.toString()) conversation.partner = participant + + delete participant.role } conversation.requestingUserMeta = diff --git a/src/models/chat/conversation.js b/src/models/chat/conversation.js index f2fbc0b8f..8becf188c 100644 --- a/src/models/chat/conversation.js +++ b/src/models/chat/conversation.js @@ -76,10 +76,7 @@ conversationSchema.statics.getConversation = function (convoId, callback) { const query = self .model(COLLECTION) .findOne({ _id: convoId }) - .populate({ - path: 'participants', - select: '_id username fullname email title image lastOnline' - }) + .populate('participants', '_id username fullname email title image lastOnline') if (typeof callback === 'function') return query.exec(callback) diff --git a/src/socketio/chatSocket.js b/src/socketio/chatSocket.js index 862deddf5..26a778bdd 100644 --- a/src/socketio/chatSocket.js +++ b/src/socketio/chatSocket.js @@ -357,6 +357,17 @@ events.onChatMessage = function (socket) { const User = require('../models/user') + data.message.owner = { + _id: data.message.owner._id, + email: data.message.owner.email, + username: data.message.owner.username, + fullname: data.message.owner.fullname, + image: data.message.owner.image, + title: data.message.owner.title, + lastOnline: data.message.owner.lastOnline, + id: data.message.owner._id + } + async.parallel( [ function (next) { @@ -364,7 +375,17 @@ events.onChatMessage = function (socket) { if (err) return next(err) if (!toUser) return next('User Not Found!') - data.toUser = toUser + // Strip + data.toUser = { + _id: toUser._id, + email: toUser.email, + username: toUser.username, + fullname: toUser.fullname, + image: toUser.image, + title: toUser.title, + lastOnline: toUser.lastOnline, + id: toUser._id + } return next() }) @@ -374,7 +395,17 @@ events.onChatMessage = function (socket) { if (err) return next(err) if (!fromUser) return next('User Not Found') - data.fromUser = fromUser + // Strip + data.fromUser = { + _id: fromUser._id, + email: fromUser.email, + username: fromUser.username, + fullname: fromUser.fullname, + image: fromUser.image, + title: fromUser.title, + lastOnline: fromUser.lastOnline, + id: fromUser._id + } return next() }) @@ -383,7 +414,6 @@ events.onChatMessage = function (socket) { function (err) { if (err) return utils.sendToSelf(socket, socketEventConst.MESSAGES_UI_RECEIVE, { message: err }) - console.log(data) utils.sendToUser( sharedVars.sockets, sharedVars.usersOnline,