Skip to content

Commit

Permalink
fix(messages): account population fields
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Jul 4, 2022
1 parent 4b8d5cc commit 2ad5c14
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
15 changes: 12 additions & 3 deletions src/controllers/api/v2/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -70,21 +72,28 @@ 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,
userMeta: conversation.userMeta,
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 =
Expand Down
5 changes: 1 addition & 4 deletions src/models/chat/conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
36 changes: 33 additions & 3 deletions src/socketio/chatSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,35 @@ 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) {
User.getUser(to, function (err, toUser) {
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()
})
Expand All @@ -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()
})
Expand All @@ -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,
Expand Down

0 comments on commit 2ad5c14

Please sign in to comment.