Skip to content

Commit 2ad5c14

Browse files
committed
fix(messages): account population fields
1 parent 4b8d5cc commit 2ad5c14

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

src/controllers/api/v2/messages.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ apiMessages.getConversations = async (req, res) => {
3838
if (participant._id.toString() !== req.user._id.toString()) {
3939
convoObject.partner = participant
4040
}
41+
42+
delete participant.role
4143
}
4244

4345
recentMessage = _.first(recentMessage)
@@ -70,21 +72,28 @@ apiMessages.single = async (req, res) => {
7072
if (!conversation) return apiUtils.sendApiError(res, 404, 'Conversation not found')
7173

7274
conversation = conversation.toObject()
73-
let isPart = false
75+
let isParticipant = false
7476
for (const participant of conversation.participants) {
75-
if (participant._id.toString() === req.user._id.toString()) isPart = true
77+
if (participant._id.toString() === req.user._id.toString()) isParticipant = true
7678
}
7779

78-
if (!isPart) return apiUtils.sendApiError(res, 400, 'Invalid')
80+
if (!isParticipant) return apiUtils.sendApiError(res, 400, 'Invalid')
7981

8082
const convoMessages = await Message.getConversationWithObject({
8183
cid: conversation._id,
8284
userMeta: conversation.userMeta,
8385
requestingUser: req.user
8486
})
8587

88+
// Clear the role. It's not needed
89+
for (const message of convoMessages) {
90+
message.owner.role = undefined
91+
}
92+
8693
for (const participant of conversation.participants) {
8794
if (participant._id.toString() !== req.user._id.toString()) conversation.partner = participant
95+
96+
delete participant.role
8897
}
8998

9099
conversation.requestingUserMeta =

src/models/chat/conversation.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ conversationSchema.statics.getConversation = function (convoId, callback) {
7676
const query = self
7777
.model(COLLECTION)
7878
.findOne({ _id: convoId })
79-
.populate({
80-
path: 'participants',
81-
select: '_id username fullname email title image lastOnline'
82-
})
79+
.populate('participants', '_id username fullname email title image lastOnline')
8380

8481
if (typeof callback === 'function') return query.exec(callback)
8582

src/socketio/chatSocket.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,35 @@ events.onChatMessage = function (socket) {
357357

358358
const User = require('../models/user')
359359

360+
data.message.owner = {
361+
_id: data.message.owner._id,
362+
email: data.message.owner.email,
363+
username: data.message.owner.username,
364+
fullname: data.message.owner.fullname,
365+
image: data.message.owner.image,
366+
title: data.message.owner.title,
367+
lastOnline: data.message.owner.lastOnline,
368+
id: data.message.owner._id
369+
}
370+
360371
async.parallel(
361372
[
362373
function (next) {
363374
User.getUser(to, function (err, toUser) {
364375
if (err) return next(err)
365376
if (!toUser) return next('User Not Found!')
366377

367-
data.toUser = toUser
378+
// Strip
379+
data.toUser = {
380+
_id: toUser._id,
381+
email: toUser.email,
382+
username: toUser.username,
383+
fullname: toUser.fullname,
384+
image: toUser.image,
385+
title: toUser.title,
386+
lastOnline: toUser.lastOnline,
387+
id: toUser._id
388+
}
368389

369390
return next()
370391
})
@@ -374,7 +395,17 @@ events.onChatMessage = function (socket) {
374395
if (err) return next(err)
375396
if (!fromUser) return next('User Not Found')
376397

377-
data.fromUser = fromUser
398+
// Strip
399+
data.fromUser = {
400+
_id: fromUser._id,
401+
email: fromUser.email,
402+
username: fromUser.username,
403+
fullname: fromUser.fullname,
404+
image: fromUser.image,
405+
title: fromUser.title,
406+
lastOnline: fromUser.lastOnline,
407+
id: fromUser._id
408+
}
378409

379410
return next()
380411
})
@@ -383,7 +414,6 @@ events.onChatMessage = function (socket) {
383414
function (err) {
384415
if (err) return utils.sendToSelf(socket, socketEventConst.MESSAGES_UI_RECEIVE, { message: err })
385416

386-
console.log(data)
387417
utils.sendToUser(
388418
sharedVars.sockets,
389419
sharedVars.usersOnline,

0 commit comments

Comments
 (0)