Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SK-272 extended conversation_list request to allow request convs by ids #142

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions APIs/JSON/validations/conversations_schema_validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const conversationsSchemaValidation = {
updated_at: Joi.object({
gt: Joi.date(),
}),
ids: Joi.array().items(Joi.alternatives().try(Joi.object(), Joi.string())),
Oleksandr1414 marked this conversation as resolved.
Show resolved Hide resolved
}).required(),
delete: Joi.object({
id: Joi.string().required(),
Expand Down
4 changes: 2 additions & 2 deletions app/providers/operations/conversation/list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class ConversationListOperation {
}

async perform(ws, options) {
const { limit, updated_at } = options
const { limit, updated_at, ids } = options
const normalizedLimit = this.#normalizeLimitParam(limit)

const currentUserId = this.sessionService.getSessionUserId(ws)
const currentUser = await this.userService.userRepo.findById(currentUserId)

const conversations = await this.conversationService.conversationsList(
currentUser,
{ updatedAt: updated_at },
{ updatedAt: updated_at, ids },
normalizedLimit
)

Expand Down
6 changes: 6 additions & 0 deletions app/providers/repositories/conversation_participants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class ConversationParticipantRepository extends BaseRepository {
return conversationsParticipants
}

async findUserConversationIds(conversationIds, user_id) {
const availableConversationParticipants = await this.findAll({ conversation_id: { $in: conversationIds }, user_id })

return availableConversationParticipants.map((participant) => participant.conversation_id)
}

async findParticipantConversations(userId, limit) {
const conversationParticipants = await this.findAll({ user_id: userId }, null, limit)

Expand Down
13 changes: 12 additions & 1 deletion app/providers/services/conversation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class ConversationService {
}

async conversationsList(user, options, limit) {
const conversationIds = await this.conversationParticipantRepo.findParticipantConversations(user.native_id, limit)
const conversationIds = await (options.ids?.length
? this.validateConvIdsWhichUserHasAccess(options.ids, user.native_id)
: this.conversationParticipantRepo.findParticipantConversations(user.native_id, limit))

const filterOptions = {}
if (options.updatedAt?.gt) {
Expand Down Expand Up @@ -82,6 +84,15 @@ class ConversationService {
return conversationsParticipants.map((participant) => participant.user_id)
}

async validateConvIdsWhichUserHasAccess(conversationIds, userId) {
const verifiedConversationIds = await this.conversationParticipantRepo.findUserConversationIds(
conversationIds,
userId
)

return verifiedConversationIds
}

async hasAccessToConversation(conversationId, userId) {
const result = { conversation: null, asParticipant: false, asOwner: false, participantIds: null }

Expand Down
4 changes: 4 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ Later, the subsequent logins can be done via `token`:
},
};

You can also add the `ids` parameter if you need to get the target chat objects.
For example: ["63480e68f4794709f802a2fa", "63480e68f4794709f802a2fy"]


{
response: {
id: "54",
Expand Down
26 changes: 26 additions & 0 deletions test/conversations.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,32 @@ describe("Conversation functions", async () => {
assert.equal(responseData.response.error, undefined)
})

it("should work with ids", async () => {
const numberOf = 2
const requestData = {
request: {
conversation_list: {
ids: ArrayOfTmpConversaionts,
},
id: "3_1",
},
}

let responseData = await packetJsonProcessor.processMessageOrError("test", JSON.stringify(requestData))

responseData = responseData.backMessages.at(0)

const conversations = responseData.response.conversations

assert.strictEqual(requestData.request.id, responseData.response.id)
assert.notEqual(responseData.response.conversations, undefined)
assert.equal(
conversations.some((el) => !ArrayOfTmpConversaionts.includes(el._id.toString())),
false
)
assert.equal(responseData.response.error, undefined)
})

it("should fail limit exceeded", async () => {
await sendLogout("test", currentUserToken)
currentUserToken = (await sendLogin("test", "user_1")).response.user.token
Expand Down