[IMPROVE] Replace snipptedMessages publication by REST (#15678)

pull/15679/head^2
Marcos Spessatto Defendi 6 years ago committed by Guilherme Gazzo
parent 790aa94c75
commit 8cd87f3ac0
  1. 28
      app/api/server/lib/messages.js
  2. 25
      app/api/server/v1/chat.js
  3. 63
      app/message-snippet/client/tabBar/views/snippetedMessages.js
  4. 1
      app/message-snippet/server/publications/snippetedMessagesByRoom.js
  5. 10
      app/models/server/raw/Messages.js
  6. 57
      tests/end-to-end/api/05-chat.js

@ -1,5 +1,6 @@
import { canAccessRoomAsync } from '../../../authorization/server/functions/canAccessRoom';
import { Rooms, Messages, Users } from '../../../models/server/raw';
import { getValue } from '../../../settings/server/raw';
export async function findMentionedMessages({ uid, roomId, pagination: { offset, count, sort } }) {
const room = await Rooms.findOneById(roomId);
@ -56,3 +57,30 @@ export async function findStarredMessages({ uid, roomId, pagination: { offset, c
total,
};
}
export async function findSnippetedMessages({ uid, roomId, pagination: { offset, count, sort } }) {
if (!await getValue('Message_AllowSnippeting')) {
throw new Error('error-not-allowed');
}
const room = await Rooms.findOneById(roomId);
if (!await canAccessRoomAsync(room, { _id: uid })) {
throw new Error('error-not-allowed');
}
const cursor = await Messages.findSnippetedByRoom(roomId, {
sort: sort || { ts: -1 },
skip: offset,
limit: count,
});
const total = await cursor.count();
const messages = await cursor.toArray();
return {
messages,
count: messages.length,
offset,
total,
};
}

@ -9,7 +9,7 @@ import { API } from '../api';
import Rooms from '../../../models/server/models/Rooms';
import Users from '../../../models/server/models/Users';
import { settings } from '../../../settings';
import { findMentionedMessages, findStarredMessages } from '../lib/messages';
import { findMentionedMessages, findStarredMessages, findSnippetedMessages } from '../lib/messages';
API.v1.addRoute('chat.delete', { authRequired: true }, {
post() {
@ -643,3 +643,26 @@ API.v1.addRoute('chat.getStarredMessages', { authRequired: true }, {
return API.v1.success(messages);
},
});
API.v1.addRoute('chat.getSnippetedMessages', { authRequired: true }, {
get() {
const { roomId } = this.queryParams;
const { sort } = this.parseJsonQuery();
const { offset, count } = this.getPaginationItems();
if (!roomId) {
throw new Meteor.Error('error-invalid-params', 'The required "roomId" query param is missing.');
}
const messages = Promise.await(findSnippetedMessages({
uid: this.userId,
roomId,
pagination: {
offset,
count,
sort,
},
}));
return API.v1.success(messages);
},
});

@ -1,16 +1,22 @@
import _ from 'underscore';
import { ReactiveVar } from 'meteor/reactive-var';
import { Template } from 'meteor/templating';
import { Mongo } from 'meteor/mongo';
import { SnippetedMessages } from '../../lib/collections';
import { messageContext } from '../../../../ui-utils/client/lib/messageContext';
import { APIClient } from '../../../../utils/client';
import { Messages } from '../../../../models/client';
import { upsertMessageBulk } from '../../../../ui-utils/client/lib/RoomHistoryManager';
const LIMIT_DEFAULT = 50;
Template.snippetedMessages.helpers({
hasMessages() {
return Template.instance().cursor.count() > 0;
return Template.instance().messages.find().count();
},
messages() {
return Template.instance().cursor;
const instance = Template.instance();
return instance.messages.find({}, { limit: instance.limit.get(), sort: { ts: -1 } });
},
message() {
return _.extend(this, { customClass: 'snippeted', actionContext: 'snippeted' });
@ -23,15 +29,52 @@ Template.snippetedMessages.helpers({
Template.snippetedMessages.onCreated(function() {
this.rid = this.data.rid;
this.cursor = SnippetedMessages.find({ snippeted: true, rid: this.data.rid }, { sort: { ts: -1 } });
this.hasMore = new ReactiveVar(true);
this.limit = new ReactiveVar(50);
this.messages = new Mongo.Collection(null);
this.limit = new ReactiveVar(LIMIT_DEFAULT);
this.autorun(() => {
const data = Template.currentData();
this.subscribe('snippetedMessages', data.rid, this.limit.get(), function() {
if (this.cursor.count() < this.limit.get()) {
return this.hasMore.set(false);
}
const query = {
_hidden: { $ne: true },
snippeted: true,
rid: this.rid,
};
this.cursor && this.cursor.stop();
this.limit.set(LIMIT_DEFAULT);
this.cursor = Messages.find(query).observe({
added: ({ _id, ...message }) => {
this.messages.upsert({ _id }, message);
},
changed: ({ _id, ...message }) => {
this.messages.upsert({ _id }, message);
},
removed: ({ _id }) => {
this.messages.remove({ _id });
},
});
});
this.autorun(async () => {
const limit = this.limit.get();
const { messages, total } = await APIClient.v1.get(`chat.getSnippetedMessages?roomId=${ this.rid }&count=${ limit }`);
upsertMessageBulk({ msgs: messages }, this.messages);
this.hasMore.set(total > limit);
});
});
Template.mentionsFlexTab.onDestroyed(function() {
this.cursor.stop();
});
Template.snippetedMessages.events({
'scroll .js-list': _.throttle(function(e, instance) {
if (e.target.scrollTop >= e.target.scrollHeight - e.target.clientHeight && instance.hasMore.get()) {
return instance.limit.set(instance.limit.get() + 50);
}
}, 200),
});

@ -3,6 +3,7 @@ import { Meteor } from 'meteor/meteor';
import { Users, Messages } from '../../../models';
Meteor.publish('snippetedMessages', function(rid, limit = 50) {
console.warn('The publication "snippetedMessages" is deprecated and will be removed after version v3.0.0');
if (typeof this.userId === 'undefined' || this.userId === null) {
return this.ready();
}

@ -20,4 +20,14 @@ export class MessagesRaw extends BaseRaw {
return this.find(query, options);
}
findSnippetedByRoom(roomId, options) {
const query = {
_hidden: { $ne: true },
snippeted: true,
rid: roomId,
};
return this.find(query, options);
}
}

@ -2158,4 +2158,61 @@ describe('Threads', () => {
.end(done);
});
});
describe('[/chat.getSnippetedMessages]', () => {
it('should return an error when the required "roomId" parameter is not sent', (done) => {
request.get(api('chat.getSnippetedMessages'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body.errorType).to.be.equal('error-invalid-params');
})
.end(done);
});
it('should return an error when the roomId is invalid', (done) => {
request.get(api('chat.getSnippetedMessages?roomId=invalid-room'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body.error).to.be.equal('error-not-allowed');
})
.end(done);
});
it('should return an error when the snippeted messages is disabled', (done) => {
updateSetting('Message_AllowSnippeting', false).then(() => {
request.get(api('chat.getSnippetedMessages?roomId=invalid-room'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body.error).to.be.equal('error-not-allowed');
})
.end(done);
});
});
it('should return the snippeted messages', (done) => {
updateSetting('Message_AllowSnippeting', true).then(() => {
request.get(api('chat.getSnippetedMessages?roomId=GENERAL'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body.messages).to.be.an('array');
expect(res.body).to.have.property('offset');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('count');
})
.end(done);
});
});
});
});

Loading…
Cancel
Save