[IMPROVE] Replace starred messages subscription (#15548)

pull/15552/head
Marcos Spessatto Defendi 6 years ago committed by Guilherme Gazzo
parent b6c00a6532
commit d0f7e199da
  1. 28
      app/api/server/lib/messages.js
  2. 24
      app/api/server/v1/chat.js
  3. 3
      app/message-star/client/lib/StarredMessage.js
  4. 68
      app/message-star/client/views/starredMessages.js
  5. 1
      app/message-star/server/publications/starredMessages.js
  6. 10
      app/models/server/raw/Messages.js
  7. 41
      tests/end-to-end/api/05-chat.js

@ -28,3 +28,31 @@ export async function findMentionedMessages({ uid, roomId, pagination: { offset,
total,
};
}
export async function findStarredMessages({ uid, roomId, pagination: { offset, count, sort } }) {
const room = await Rooms.findOneById(roomId);
if (!await canAccessRoomAsync(room, { _id: uid })) {
throw new Error('error-not-allowed');
}
const user = await Users.findOneById(uid, { fields: { username: 1 } });
if (!user) {
throw new Error('invalid-user');
}
const cursor = await Messages.findStarredByUserAtRoom(uid, 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 } from '../lib/messages';
import { findMentionedMessages, findStarredMessages } from '../lib/messages';
API.v1.addRoute('chat.delete', { authRequired: true }, {
post() {
@ -621,3 +621,25 @@ API.v1.addRoute('chat.getMentionedMessages', { authRequired: true }, {
return API.v1.success(messages);
},
});
API.v1.addRoute('chat.getStarredMessages', { 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(findStarredMessages({
uid: this.userId,
roomId,
pagination: {
offset,
count,
sort,
},
}));
return API.v1.success(messages);
},
});

@ -1,3 +0,0 @@
import { Mongo } from 'meteor/mongo';
export const StarredMessage = new Mongo.Collection('rocketchat_starred_message');

@ -1,16 +1,23 @@
import _ from 'underscore';
import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { Template } from 'meteor/templating';
import { Mongo } from 'meteor/mongo';
import { StarredMessage } from '../lib/StarredMessage';
import { messageContext } from '../../../ui-utils/client/lib/messageContext';
import { Messages } from '../../../models/client';
import { upsertMessageBulk } from '../../../ui-utils/client/lib/RoomHistoryManager';
import { APIClient } from '../../../utils/client';
const LIMIT_DEFAULT = 50;
Template.starredMessages.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, { actionContext: 'starred' });
@ -23,24 +30,49 @@ Template.starredMessages.helpers({
Template.starredMessages.onCreated(function() {
this.rid = this.data.rid;
this.cursor = StarredMessage.find({
rid: this.data.rid,
}, {
sort: {
ts: -1,
},
});
this.messages = new Mongo.Collection(null);
this.hasMore = new ReactiveVar(true);
this.limit = new ReactiveVar(50);
this.limit = new ReactiveVar(LIMIT_DEFAULT);
this.autorun(() => {
const sub = this.subscribe('starredMessages', this.data.rid, this.limit.get());
if (sub.ready()) {
if (this.cursor.count() < this.limit.get()) {
return this.hasMore.set(false);
}
}
const query = {
_hidden: { $ne: true },
'starred._id': Meteor.userId(),
rid: this.rid,
_updatedAt: {
$gt: new Date(),
},
};
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.getStarredMessages?roomId=${ this.rid }&count=${ limit }`);
upsertMessageBulk({ msgs: messages }, this.messages);
this.hasMore.set(total > limit);
});
});
Template.mentionsFlexTab.onDestroyed(function() {
this.cursor.stop();
});
Template.starredMessages.events({

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

@ -10,4 +10,14 @@ export class MessagesRaw extends BaseRaw {
return this.find(query, options);
}
findStarredByUserAtRoom(userId, roomId, options) {
const query = {
_hidden: { $ne: true },
'starred._id': userId,
rid: roomId,
};
return this.find(query, options);
}
}

@ -2117,4 +2117,45 @@ describe('Threads', () => {
.end(done);
});
});
describe('[/chat.getStarredMessages]', () => {
it('should return an error when the required "roomId" parameter is not sent', (done) => {
request.get(api('chat.getStarredMessages'))
.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.getStarredMessages?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 starred messages', (done) => {
request.get(api('chat.getStarredMessages?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