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

Remove sync methods #4512

Merged
merged 3 commits into from
Oct 1, 2016
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
Meteor.methods({
'permissions/get'() {
'permissions/get'(updatedAt) {
this.unblock();

return RocketChat.models.Permissions.find().fetch();
},

'permissions/sync'(updatedAt) {
this.unblock();
if (updatedAt instanceof Date) {
return RocketChat.models.Permissions.dinamicFindChangesAfter('find', updatedAt);
}

return RocketChat.models.Permissions.dinamicFindChangesAfter('find', updatedAt);
return RocketChat.models.Permissions.find().fetch();
}
});

Expand Down
80 changes: 60 additions & 20 deletions packages/rocketchat-lib/client/lib/cachedCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class CachedCollectionManager {
constructor() {
this.items = [];
this._syncEnabled = false;
this.reconnectCb = [];
this.loginCb = [];
this.logged = false;

Expand All @@ -13,6 +14,19 @@ class CachedCollectionManager {
this.clearAllCacheOnLogout();
};

let connectionWasOnline = true;
Tracker.autorun(() => {
const connected = Meteor.connection.status().connected;

if (connected === true && connectionWasOnline === false) {
for (const cb of this.reconnectCb) {
cb();
}
}

connectionWasOnline = connected;
});

Tracker.autorun(() => {
if (Meteor.userId() !== null) {
if (this.logged === false) {
Expand Down Expand Up @@ -57,6 +71,10 @@ class CachedCollectionManager {
return this._syncEnabled;
}

onReconnect(cb) {
this.reconnectCb.push(cb);
}

onLogin(cb) {
this.loginCb.push(cb);
if (this.logged) {
Expand All @@ -80,15 +98,15 @@ class CachedCollection {
useSync = true,
useCache = true,
debug = true,
version = 1,
version = 2,
maxCacheTime = 60*60*24*30
}) {
this.collection = collection || new Meteor.Collection(null);

this.ready = new ReactiveVar(false);
this.name = name;
this.methodName = methodName || `${name}/get`;
this.syncMethodName = syncMethodName || `${name}/sync`;
this.syncMethodName = syncMethodName || `${name}/get`;
this.eventName = eventName || `${name}-changed`;
this.eventType = eventType;
this.useSync = useSync;
Expand All @@ -110,6 +128,10 @@ class CachedCollection {
this.init();
});
}

if (this.useCache === false) {
return this.clearCache();
}
}

log(...args) {
Expand Down Expand Up @@ -204,27 +226,48 @@ class CachedCollection {
this.log(`syncing from ${this.updatedAt}`);

Meteor.call(this.syncMethodName, this.updatedAt, (error, data) => {
let changes = [];

if (data.update && data.update.length > 0) {
this.log(`${data.update.length} records updated in sync`);

for (const record of data.update) {
this.collection.upsert({ _id: record._id }, _.omit(record, '_id'));

if (record._updatedAt && record._updatedAt > this.updatedAt) {
this.updatedAt = record._updatedAt;
}
}
changes.push(...data.update);
}

if (data.remove && data.remove.length > 0) {
this.log(`${data.remove.length} records removed in sync`);
changes.push(...data.remove);
}

for (const record of data.remove) {
changes = changes.sort((a, b) => {
const valueA = a._updatedAt || a._deletedAt;
const valueB = b._updatedAt || b._deletedAt;

if (valueA < valueB) {
return -1;
}

if (valueA > valueB) {
return 1;
}

return 0;
});

for (const record of changes) {
delete record.$loki;

if (record._deletedAt) {
this.collection.remove({ _id: record._id });

if (record._deletedAt && record._deletedAt > this.updatedAt) {
this.updatedAt = record._deletedAt;
}
} else {
this.collection.upsert({ _id: record._id }, _.omit(record, '_id'));

if (record._updatedAt && record._updatedAt > this.updatedAt) {
this.updatedAt = record._updatedAt;
}
}
}

Expand All @@ -235,6 +278,10 @@ class CachedCollection {
}

saveCache(data) {
if (this.useCache === false) {
return;
}

this.log('saving cache');
if (!data) {
data = this.collection.find().fetch();
Expand Down Expand Up @@ -299,15 +346,8 @@ class CachedCollection {
}

if (this.useSync === true) {
let connectionWasOnline = true;
Tracker.autorun(() => {
const connected = Meteor.connection.status().connected;

if (connected === true && connectionWasOnline === false) {
this.trySync();
}

connectionWasOnline = connected;
RocketChat.CachedCollectionManager.onReconnect(() => {
this.trySync();
});
}

Expand Down
29 changes: 11 additions & 18 deletions packages/rocketchat-lib/server/publications/settings.coffee
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
Meteor.methods
'public-settings/get': ->
'public-settings/get': (updatedAt) ->
this.unblock()

return RocketChat.models.Settings.findNotHiddenPublic().fetch()

'public-settings/sync': (updatedAt) ->
this.unblock()
if updatedAt instanceof Date
result =
update: RocketChat.models.Settings.findNotHiddenPublicUpdatedAfter(updatedAt).fetch()
remove: RocketChat.models.Settings.trashFindDeletedAfter(updatedAt, {hidden: { $ne: true }, public: true}, {fields: {_id: 1, _deletedAt: 1}}).fetch()

result =
update: RocketChat.models.Settings.findNotHiddenPublicUpdatedAfter(updatedAt).fetch()
remove: RocketChat.models.Settings.trashFindDeletedAfter(updatedAt, {hidden: { $ne: true }, public: true}, {fields: {_id: 1, _deletedAt: 1}}).fetch()
return result

return result
return RocketChat.models.Settings.findNotHiddenPublic().fetch()

'private-settings/get': ->
'private-settings/get': (updatedAt) ->
unless Meteor.userId()
return []

Expand All @@ -22,15 +20,10 @@ Meteor.methods
if not RocketChat.authz.hasPermission Meteor.userId(), 'view-privileged-setting'
return []

return RocketChat.models.Settings.findNotHidden().fetch()

'private-settings/sync': (updatedAt) ->
unless Meteor.userId()
return {}
if updatedAt instanceof Date
return RocketChat.models.Settings.dinamicFindChangesAfter('findNotHidden', updatedAt);

this.unblock()

return RocketChat.models.Settings.dinamicFindChangesAfter('findNotHidden', updatedAt);
return RocketChat.models.Settings.findNotHidden().fetch()


RocketChat.models.Settings.on 'change', (type, args...) ->
Expand Down
16 changes: 4 additions & 12 deletions server/publications/subscription.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fields =


Meteor.methods
'subscriptions/get': ->
'subscriptions/get': (updatedAt) ->
unless Meteor.userId()
return []

Expand All @@ -30,18 +30,10 @@ Meteor.methods
options =
fields: fields

return RocketChat.models.Subscriptions.findByUserId(Meteor.userId(), options).fetch()

'subscriptions/sync': (updatedAt) ->
unless Meteor.userId()
return {}

this.unblock()
if updatedAt instanceof Date
return RocketChat.models.Subscriptions.dinamicFindChangesAfter('findByUserId', updatedAt, Meteor.userId(), options);

options =
fields: fields

return RocketChat.models.Subscriptions.dinamicFindChangesAfter('findByUserId', updatedAt, Meteor.userId(), options);
return RocketChat.models.Subscriptions.findByUserId(Meteor.userId(), options).fetch()


RocketChat.models.Subscriptions.on 'change', (type, args...) ->
Expand Down