diff --git a/app/api/server/v1/chat.js b/app/api/server/v1/chat.js index a4ed9765ebe4e..047c84e77a68c 100644 --- a/app/api/server/v1/chat.js +++ b/app/api/server/v1/chat.js @@ -147,7 +147,7 @@ API.v1.addRoute('chat.postMessage', { authRequired: true }, { API.v1.addRoute('chat.search', { authRequired: true }, { get() { const { roomId, searchText } = this.queryParams; - const { count } = this.getPaginationItems(); + const { offset, count } = this.getPaginationItems(); if (!roomId) { throw new Meteor.Error('error-roomId-param-not-provided', 'The required "roomId" query param is missing.'); @@ -158,7 +158,7 @@ API.v1.addRoute('chat.search', { authRequired: true }, { } let result; - Meteor.runAsUser(this.userId, () => { result = Meteor.call('messageSearch', searchText, roomId, count).message.docs; }); + Meteor.runAsUser(this.userId, () => { result = Meteor.call('messageSearch', searchText, roomId, count, offset).message.docs; }); return API.v1.success({ messages: normalizeMessagesForUser(result, this.userId), diff --git a/server/methods/messageSearch.js b/server/methods/messageSearch.js index 297a9d40b74e9..d03bf00cb8afb 100644 --- a/server/methods/messageSearch.js +++ b/server/methods/messageSearch.js @@ -8,10 +8,11 @@ import { settings } from '../../app/settings'; import { readSecondaryPreferred } from '../database/readSecondaryPreferred'; Meteor.methods({ - messageSearch(text, rid, limit) { + messageSearch(text, rid, limit, offset) { check(text, String); check(rid, Match.Maybe(String)); check(limit, Match.Optional(Number)); + check(offset, Match.Optional(Number)); // TODO: Evaluate why we are returning `users` and `channels`, as the only thing that gets set is the `messages`. const result = { @@ -46,6 +47,7 @@ Meteor.methods({ sort: { ts: -1, }, + skip: offset || 0, limit: limit || 20, }; diff --git a/tests/end-to-end/api/05-chat.js b/tests/end-to-end/api/05-chat.js index f326ddcdb9693..705889556c02d 100644 --- a/tests/end-to-end/api/05-chat.js +++ b/tests/end-to-end/api/05-chat.js @@ -1055,12 +1055,28 @@ describe('[Chat]', function() { }); describe('/chat.search', () => { + beforeEach((done) => { + const sendMessage = (text) => { + request.post(api('chat.sendMessage')) + .set(credentials) + .send({ + message: { + rid: 'GENERAL', + msg: text, + }, + }) + .end(() => {}); + }; + for (let i = 0; i < 5; i++) { sendMessage('msg1'); } + done(); + }); + it('should return a list of messages when execute successfully', (done) => { request.get(api('chat.search')) .set(credentials) .query({ roomId: 'GENERAL', - searchText: 'This message was edited via API', + searchText: 'msg1', }) .expect('Content-Type', 'application/json') .expect(200) @@ -1070,12 +1086,12 @@ describe('[Chat]', function() { }) .end(done); }); - it('should return a list of messages when is provided "count" query parameter execute successfully', (done) => { + it('should return a list of messages(length=1) when is provided "count" query parameter execute successfully', (done) => { request.get(api('chat.search')) .set(credentials) .query({ roomId: 'GENERAL', - searchText: 'This message was edited via API', + searchText: 'msg1', count: 1, }) .expect('Content-Type', 'application/json') @@ -1083,10 +1099,49 @@ describe('[Chat]', function() { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.property('messages'); + expect(res.body.messages.length).to.equal(1); + }) + .end(done); + }); + it('should return a list of messages(length=3) when is provided "count" and "offset" query parameters are executed successfully', (done) => { + request.get(api('chat.search')) + .set(credentials) + .query({ + roomId: 'GENERAL', + searchText: 'msg1', + offset: 1, + count: 3, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('messages'); + expect(res.body.messages.length).to.equal(3); + }) + .end(done); + }); + + it('should return a empty list of messages when is provided a huge offset value', (done) => { + request.get(api('chat.search')) + .set(credentials) + .query({ + roomId: 'GENERAL', + searchText: 'msg1', + offset: 9999, + count: 3, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('messages'); + expect(res.body.messages.length).to.equal(0); }) .end(done); }); }); + describe('[/chat.react]', () => { it('should return statusCode: 200 and success when try unreact a message that\'s no reacted yet', (done) => { request.post(api('chat.react'))