Chore: added pagination to search msg endpoint (#22632)

* added pagination to search endpoint

* readded tests
pull/22697/head^2
Leonardo Ostjen Couto 4 years ago committed by GitHub
parent 48da604646
commit df30020b9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/api/server/v1/chat.js
  2. 4
      server/methods/messageSearch.js
  3. 61
      tests/end-to-end/api/05-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),

@ -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,
};

@ -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'))

Loading…
Cancel
Save