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

[IMPROVE] Replace starred messages subscription #15548

Merged
merged 1 commit into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions app/api/server/lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
24 changes: 23 additions & 1 deletion app/api/server/v1/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
},
});
3 changes: 0 additions & 3 deletions app/message-star/client/lib/StarredMessage.js

This file was deleted.

68 changes: 50 additions & 18 deletions app/message-star/client/views/starredMessages.js
Original file line number Diff line number Diff line change
@@ -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' });
Expand All @@ -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({
Expand Down
1 change: 1 addition & 0 deletions app/message-star/server/publications/starredMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions app/models/server/raw/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
41 changes: 41 additions & 0 deletions tests/end-to-end/api/05-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});