Skip to content

Commit

Permalink
Merge pull request RocketChat#96 from bhardwajaditya/pwa_2
Browse files Browse the repository at this point in the history
[EVO] Switch from persistent minimongo to cached collection
  • Loading branch information
kb0304 authored Nov 26, 2019
2 parents 444344b + 3a35afd commit 15b353b
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 39 deletions.
1 change: 0 additions & 1 deletion .meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,4 @@ [email protected]
rocketchat:oauth2-server
rocketchat:i18n

frozeman:persistent-minimongo2
ground:db
1 change: 0 additions & 1 deletion .meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ [email protected]
[email protected]
[email protected]
[email protected]
frozeman:[email protected]
[email protected]
[email protected]
[email protected]
Expand Down
3 changes: 2 additions & 1 deletion app/models/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CachedChatSubscription } from './models/CachedChatSubscription';
import { CachedUserList } from './models/CachedUserList';
import { ChatRoom } from './models/ChatRoom';
import { ChatSubscription } from './models/ChatSubscription';
import { ChatMessage } from './models/ChatMessage';
import { ChatMessage, CachedChatMessage } from './models/ChatMessage';
import { RoomRoles } from './models/RoomRoles';
import { UserAndRoom } from './models/UserAndRoom';
import { UserRoles } from './models/UserRoles';
Expand Down Expand Up @@ -49,6 +49,7 @@ export {
AuthzCachedCollection,
ChatPermissions,
ChatMessage,
CachedChatMessage,
ChatSubscription,
Rooms,
CustomSounds,
Expand Down
20 changes: 5 additions & 15 deletions app/models/client/models/ChatMessage.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import s from 'underscore.string';
import { Tracker } from 'meteor/tracker';
import { PersistentMinimongo2 } from 'meteor/frozeman:persistent-minimongo2';

import { CachedCollection } from '../../../ui-cached-collection';
import { CachedChatSubscription } from './CachedChatSubscription';
import { ChatSubscription } from './ChatSubscription';
import { getConfig } from '../../../ui-utils/client/config';
import { renderMessageBody } from '../../../ui-utils/client/lib/renderMessageBody';
import { promises } from '../../../promises/client';

export const ChatMessage = new Mongo.Collection(null);
export const CachedChatMessage = new CachedCollection({ name: 'chatMessage' });

export const ChatMessage = CachedChatMessage.collection;

ChatMessage.setReactions = function(messageId, reactions) {
return this.update({ _id: messageId }, { $set: { reactions } });
Expand All @@ -20,8 +21,6 @@ ChatMessage.unsetReactions = function(messageId) {
return this.update({ _id: messageId }, { $unset: { reactions: 1 } });
};

new PersistentMinimongo2(ChatMessage, 'Message');

const normalizeThreadMessage = (message) => {
if (message.msg) {
return renderMessageBody(message).replace(/<br\s?\\?>/g, ' ');
Expand Down Expand Up @@ -100,17 +99,8 @@ Tracker.autorun(() => {
);
const limit = parseInt(getConfig('roomListLimit')) || 50;
subscriptions.forEach((subscription) => {
let ts;
const ts = undefined;
const { rid, ls } = subscription;
const lastMessage = ChatMessage.findOne(
{ rid, _hidden: { $ne: true } },
{ sort: { ts: 1 } }
);
if (lastMessage) {
({ ts } = lastMessage);
} else {
ts = undefined;
}
Meteor.call('loadHistory', rid, ts, limit, ls, (err, result) => {
if (err) {
return;
Expand Down
8 changes: 4 additions & 4 deletions app/models/client/models/Users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { PersistentMinimongo2 } from 'meteor/frozeman:persistent-minimongo2';

import { CachedCollection } from '../../../ui-cached-collection';

export const Users = {
isUserInRole(userId, roleName) {
Expand All @@ -27,10 +28,9 @@ export const Users = {
Meteor.users = new Mongo.Collection(null);
Meteor.user = () => Meteor.users.findOne({ _id: Meteor.userId() });

new PersistentMinimongo2(Meteor.users, 'Users');

// logged user data will come to this collection
const OwnUser = new Mongo.Collection('own_user');
const CachedOwnUser = new CachedCollection({ name: 'ownUser' });
const OwnUser = CachedOwnUser.collection;

// register an observer to logged user's collection and populate "original" Meteor.users with it
OwnUser.find().observe({
Expand Down
2 changes: 1 addition & 1 deletion app/settings/client/lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { settings } from '../../lib/settings';
settings.cachedCollection = new CachedCollection({
name: 'public-settings',
eventType: 'onAll',
userRelated: true,
userRelated: false,
listenChangesForLoggedUsersOnly: true,
});

Expand Down
8 changes: 2 additions & 6 deletions app/ui-master/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,14 @@ Template.main.helpers({
return iframeEnabled && iframeLogin.reactiveIframeUrl.get();
},
subsReady() {
let routerReady = FlowRouter.subsReady('userData');
const status = Meteor.status();
if (!routerReady && Meteor.user() && status && (status.status !== 'connected' || status.status !== 'connecting')) {
routerReady = true;
}
const userReady = Meteor.user();

const subscriptionsReady = CachedChatSubscription.ready.get();

const settingsReady = settings.cachedCollection.ready.get();


const ready = (routerReady && subscriptionsReady && settingsReady) || !Meteor.userId();
const ready = (userReady && subscriptionsReady && settingsReady) || !Meteor.userId();

CachedCollectionManager.syncEnabled = ready;
mainReady.set(ready);
Expand Down
15 changes: 14 additions & 1 deletion app/ui-utils/client/lib/RoomHistoryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RoomManager } from './RoomManager';
import { readMessage } from './readMessages';
import { renderMessageBody } from './renderMessageBody';
import { getConfig } from '../config';
import { ChatMessage, ChatSubscription, ChatRoom } from '../../../models';
import { ChatMessage, ChatSubscription, ChatRoom, CachedChatMessage } from '../../../models';

export const normalizeThreadMessage = (message) => {
if (message.msg) {
Expand Down Expand Up @@ -327,6 +327,19 @@ export const RoomHistoryManager = new class {
}

clear(rid) {
const query = { rid };
const options = {
sort: {
ls: -1,
},
limit: 50,
};
const retain = ChatMessage.find(query, options).fetch();
ChatMessage.remove({ rid });
retain.forEach((message) => {
ChatMessage.insert(message);
});
CachedChatMessage.save();
if (this.histories[rid]) {
this.histories[rid].hasMore.set(true);
this.histories[rid].isLoading.set(false);
Expand Down
8 changes: 0 additions & 8 deletions client/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ const createTemplateForComponent = async (
return name;
};

FlowRouter.subscriptions = function() {
Tracker.autorun(() => {
if (Meteor.userId()) {
this.register('userData', Meteor.subscribe('userData'));
}
});
};

FlowRouter.route('/', {
name: 'index',
action() {
Expand Down
1 change: 0 additions & 1 deletion client/startup/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Meteor.startup(function() {
if (!Meteor.userId() && !settings.get('Accounts_AllowAnonymousRead')) {
return;
}
Meteor.subscribe('userData');
computation.stop();
});

Expand Down
1 change: 1 addition & 0 deletions server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import './publications/activeUsers';
import './publications/channelAndPrivateAutocomplete';
import './publications/fullUserData';
import './publications/messages';
import './publications/ownUser';
import './publications/room';
import './publications/roomFiles';
import './publications/roomFilesWithSearchText';
Expand Down
6 changes: 6 additions & 0 deletions server/publications/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ Meteor.methods({
return Meteor.call('getChannelHistory', { rid, latest: latestDate, oldest: oldestDate, inclusive, count, unreads });
},
});

Meteor.methods({
'chatMessage/get'() {
return [];
},
});
26 changes: 26 additions & 0 deletions server/publications/ownUser/emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Notifications } from '../../../app/notifications';
import { Users } from '../../../app/models';
import { getDefaultUserFields } from '../../../app/utils/server/functions/getDefaultUserFields';

Users.on('change', ({ clientAction, id, data }) => {
switch (clientAction) {
case 'inserted':
data = Users.find({ _id: id }, { fields: getDefaultUserFields() });
break;

case 'updated':
// Override data cuz we do not publish all fields
data = Users.find({ _id: id }, { fields: getDefaultUserFields() });
break;

case 'removed':
}
Notifications.streamUser.__emit(data._id, clientAction, data);

Notifications.notifyUserInThisInstance(
data._id,
'ownUser-changed',
clientAction,
data
);
});
16 changes: 16 additions & 0 deletions server/publications/ownUser/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Meteor } from 'meteor/meteor';

import { Users } from '../../../app/models';
import { getDefaultUserFields } from '../../../app/utils/server/functions/getDefaultUserFields';
import './emitter';

Meteor.methods({
'ownUser/get'() {
if (!Meteor.userId()) {
return [];
}

const data = Users.find({ _id: Meteor.userId() }, { fields: getDefaultUserFields() }).fetch();
return data;
},
});

0 comments on commit 15b353b

Please sign in to comment.