Skip to content

Commit

Permalink
SK-297 added conversation field to get_participants_by_cids reque…
Browse files Browse the repository at this point in the history
…st (#157)
  • Loading branch information
Oleksandr1414 authored Feb 21, 2025
1 parent 5688ec2 commit 92997ac
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
4 changes: 2 additions & 2 deletions APIs/JSON/controllers/conversations.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ class ConversationsController extends BaseJSONController {
const { id: requestId, get_participants_by_cids: options } = data

const conversationListParticipantsOperation = ServiceLocatorContainer.use("ConversationListParticipantsOperation")
const users = await conversationListParticipantsOperation.perform(ws, options)
const { users, conversations } = await conversationListParticipantsOperation.perform(ws, options)

return new Response().addBackMessage({
response: { id: requestId, users: users },
response: { id: requestId, users, conversations },
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ class ConversationListParticipantsOperation {
const currentUserId = this.sessionService.getSessionUserId(ws)
const currentUser = await this.userService.userRepo.findById(currentUserId)

const participantIds = await this.conversationService.findConversationsParticipantIds(cids, currentUser)
const { participantIds, participantsIdsByCids } = await this.conversationService.findConversationsParticipantIds(
cids,
currentUser
)

if (!participantIds.length) {
return []
return { users: [], conversations: {} }
}

const users = await this.userService.userRepo.findAllByIds(participantIds)
Expand All @@ -23,7 +26,7 @@ class ConversationListParticipantsOperation {

const userFields = usersWithAvatars.map((user) => user.visibleParams())

return userFields
return { users: userFields, conversations: participantsIdsByCids }
}
}

Expand Down
21 changes: 18 additions & 3 deletions app/providers/repositories/conversation_participants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,24 @@ class ConversationParticipantRepository extends BaseRepository {
})
const availableConversationIds = availableConversationParticipants.map((participant) => participant.conversation_id)

const conversationsParticipants = await this.findAll({ conversation_id: { $in: availableConversationIds } })

return conversationsParticipants
const conversationsParticipants = await this.aggregate([
{ $match: { conversation_id: { $in: availableConversationIds } } },
{ $group: { _id: "$conversation_id", users: { $push: "$user_id" } } },
{
$project: {
_id: 0,
conversation_id: { $toString: "$_id" },
users: { $map: { input: "$users", as: "u", in: { $toString: "$$u" } } },
},
},
])

const conversationsParticipantsByIds = conversationsParticipants.reduce((arr, { conversation_id, users }) => {
arr[conversation_id] = users
return arr
}, {})

return conversationsParticipantsByIds
}

async findUserConversationIds(conversationIds, user_id) {
Expand Down
4 changes: 3 additions & 1 deletion app/providers/services/conversation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class ConversationService {
user.native_id
)

return conversationsParticipants.map((participant) => participant.user_id)
const participantIds = [...new Set(Object.values(conversationsParticipants).flat())]

return { participantIds, participantsIdsByCids: conversationsParticipants }
}

async validateConvIdsWhichUserHasAccess(conversationIds, userId) {
Expand Down
10 changes: 8 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,14 @@ When a user leaves the group chat, the next message will also be sent to all use
{
_id: "63480e68f4794709f802a2fa",
login: "breadpit"
}
]
},
...
],
conversations: {
635a3b4cb15254ebe43e76ff: [634ec51c0b65918393dca5bf, 63480e68f4794709f802a2fa],
63563a2ad745dc1c6ad01b5f: [63480e68f4794709f802a2fa, 507f191e810c19729de860ea, 507f191e810c19729de880ee],
...
}
}
}
```
Expand Down

0 comments on commit 92997ac

Please sign in to comment.