Add relation between client cache and user’s token

Closes #5537
pull/5603/head
Rodrigo Nascimento 9 years ago
parent c79c04eed8
commit cf4ac3ae57
No known key found for this signature in database
GPG Key ID: 2C85B3AFE75D23F9
  1. 2
      packages/rocketchat-authorization/client/lib/ChatPermissions.coffee
  2. 21
      packages/rocketchat-lib/client/lib/cachedCollection.js
  3. 2
      packages/rocketchat-lib/client/lib/settings.coffee
  4. 4
      packages/rocketchat-ui/lib/collections.coffee

@ -1,2 +1,2 @@
RocketChat.authz.cachedCollection = new RocketChat.CachedCollection({ name: 'permissions', eventType: 'onLogged', initOnLogin: true })
RocketChat.authz.cachedCollection = new RocketChat.CachedCollection({ name: 'permissions', eventType: 'onLogged' })
@ChatPermissions = RocketChat.authz.cachedCollection.collection

@ -94,11 +94,11 @@ class CachedCollection {
syncMethodName,
eventName,
eventType = 'onUser',
initOnLogin = false,
userRelated = true,
useSync = true,
useCache = true,
debug = true,
version = 5,
version = 6,
maxCacheTime = 60*60*24*30
}) {
this.collection = collection || new Meteor.Collection(null);
@ -113,13 +113,13 @@ class CachedCollection {
this.useCache = useCache;
this.debug = debug;
this.version = version;
this.initOnLogin = initOnLogin;
this.userRelated = userRelated;
this.updatedAt = new Date(0);
this.maxCacheTime = maxCacheTime;
RocketChat.CachedCollectionManager.register(this);
if (initOnLogin === true) {
if (userRelated === true) {
RocketChat.CachedCollectionManager.onLogin(() => {
this.log('Init on login');
this.ready.set(false);
@ -151,13 +151,21 @@ class CachedCollection {
});
}
getToken() {
if (this.userRelated === false) {
return undefined;
}
return Accounts._storedLoginToken();
}
loadFromCache(callback = () => {}) {
if (this.useCache === false) {
return callback(false);
}
localforage.getItem(this.name, (error, data) => {
if (data && data.version < this.version) {
if (data && (data.version < this.version || data.token !== this.getToken())) {
this.clearCache();
callback(false);
return;
@ -291,13 +299,14 @@ class CachedCollection {
localforage.setItem(this.name, {
updatedAt: new Date,
version: this.version,
token: this.getToken(),
records: data
});
this.log('saving cache (done)');
}
clearCacheOnLogout() {
if (this.initOnLogin === true) {
if (this.userRelated === true) {
this.clearCache();
}
}

@ -3,7 +3,7 @@
# @namespace RocketChat.settings
###
RocketChat.settings.cachedCollection = new RocketChat.CachedCollection({ name: 'public-settings', eventType: 'onAll' })
RocketChat.settings.cachedCollection = new RocketChat.CachedCollection({ name: 'public-settings', eventType: 'onAll', userRelated: false })
RocketChat.settings.collection = RocketChat.settings.cachedCollection.collection
RocketChat.settings.cachedCollection.init()

@ -1,8 +1,8 @@
@ChatMessage = new Meteor.Collection null
@CachedChatRoom = new RocketChat.CachedCollection({ name: 'rooms', initOnLogin: true })
@CachedChatRoom = new RocketChat.CachedCollection({ name: 'rooms' })
@ChatRoom = CachedChatRoom.collection
@CachedChatSubscription = new RocketChat.CachedCollection({ name: 'subscriptions', initOnLogin: true })
@CachedChatSubscription = new RocketChat.CachedCollection({ name: 'subscriptions' })
@ChatSubscription = CachedChatSubscription.collection
@UserRoles = new Mongo.Collection null
@RoomRoles = new Mongo.Collection null

Loading…
Cancel
Save