parent
c65f526375
commit
3b7b8062d3
@ -1 +1,4 @@ |
||||
@ChatPermissions = new Meteor.Collection 'rocketchat_permissions' |
||||
RocketChat.authz.cachedCollection = new RocketChat.CachedCollection({ name: 'permissions', methodName: 'getPermissions', eventName: 'permissions-changed', eventType: 'onAll' }) |
||||
@ChatPermissions = RocketChat.authz.cachedCollection.collection |
||||
|
||||
RocketChat.authz.cachedCollection.init() |
||||
|
@ -1,3 +1,31 @@ |
||||
Meteor.publish('permissions', function() { |
||||
return RocketChat.models.Permissions.find({}); |
||||
Meteor.methods({ |
||||
getPermissions() { |
||||
this.unblock(); |
||||
|
||||
return RocketChat.models.Permissions.find().fetch(); |
||||
} |
||||
}); |
||||
|
||||
|
||||
let subscriptionsReady = false; |
||||
RocketChat.models.Settings.findNotHidden().observe({ |
||||
added(record) { |
||||
if (subscriptionsReady) { |
||||
RocketChat.Notifications.notifyAll('permissions-changed', 'added', record); |
||||
} |
||||
}, |
||||
|
||||
changed(record) { |
||||
if (subscriptionsReady) { |
||||
RocketChat.Notifications.notifyAll('permissions-changed', 'changed', record); |
||||
} |
||||
}, |
||||
|
||||
removed(record) { |
||||
if (subscriptionsReady) { |
||||
RocketChat.Notifications.notifyAll('permissions-changed', 'removed', { _id: record._id }); |
||||
} |
||||
} |
||||
}); |
||||
|
||||
subscriptionsReady = true; |
||||
|
@ -0,0 +1,111 @@ |
||||
/* globals localforage */ |
||||
|
||||
class CachedCollection { |
||||
constructor({ |
||||
collection, |
||||
name, |
||||
methodName, |
||||
eventName, |
||||
eventType = 'onUser', |
||||
sync = true, |
||||
debug = true |
||||
}) { |
||||
this.collection = collection || new Meteor.Collection(null); |
||||
|
||||
this.ready = new ReactiveVar(false); |
||||
this.name = name; |
||||
this.methodName = methodName || name; |
||||
this.eventName = eventName || name; |
||||
this.eventType = eventType; |
||||
this.sync = sync; |
||||
this.debug = debug; |
||||
} |
||||
|
||||
log(...args) { |
||||
if (this.debug === true) { |
||||
console.log(...args); |
||||
} |
||||
} |
||||
|
||||
loadFromCache(callback = () => {}) { |
||||
localforage.getItem(this.name, (error, data) => { |
||||
if (data && data.records) { |
||||
this.log(`CachedCollection ${this.name} => ${data.records.length} records loaded from cache`); |
||||
data.records.forEach((item) => { |
||||
item.__cache__ = true; |
||||
this.collection.insert(item); |
||||
}); |
||||
|
||||
callback(true); |
||||
} else { |
||||
callback(false); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
loadFromServer(callback = () => {}) { |
||||
Meteor.call(this.methodName, (error, data) => { |
||||
this.log(`CachedCollection ${this.name} => ${data.length} records loaded from server`); |
||||
data.forEach((item) => { |
||||
const _id = item._id; |
||||
delete item._id; |
||||
this.collection.upsert({ _id: _id }, item); |
||||
item._id = _id; |
||||
}); |
||||
|
||||
callback(data); |
||||
}); |
||||
} |
||||
|
||||
loadFromServerAndPopulate(clearCache = false) { |
||||
this.loadFromServer((loadedData) => { |
||||
if (clearCache === true) { |
||||
this.collection.remove({ __cache__: true }); |
||||
} |
||||
this.ready.set(true); |
||||
this.saveCache(loadedData); |
||||
}); |
||||
} |
||||
|
||||
saveCache(data) { |
||||
localforage.setItem(this.name, { |
||||
updatedAt: new Date, |
||||
records: data |
||||
}); |
||||
} |
||||
|
||||
setupListener(eventType, eventName) { |
||||
RocketChat.Notifications[eventType || this.eventType](eventName || this.eventName, (t, record) => { |
||||
if (t === 'removed') { |
||||
this.collection.remove(record._id); |
||||
} else { |
||||
const _id = record._id; |
||||
delete record._id; |
||||
this.collection.upsert({ _id: _id }, record); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
init() { |
||||
this.loadFromCache((cacheLoaded) => { |
||||
this.ready.set(cacheLoaded); |
||||
|
||||
if (cacheLoaded === false) { |
||||
// If there is no cache load data immediately
|
||||
this.loadFromServerAndPopulate(); |
||||
} else if (this.sync === true) { |
||||
// If there is cache wait for an empty queue to load data again and sync
|
||||
const interval = Meteor.setInterval(() => { |
||||
if (Meteor.connection._outstandingMethodBlocks.length === 0) { |
||||
Meteor.clearInterval(interval); |
||||
this.loadFromServerAndPopulate(true); |
||||
} |
||||
}, 500); |
||||
} |
||||
|
||||
this.setupListener(); |
||||
}); |
||||
} |
||||
} |
||||
|
||||
RocketChat.CachedCollection = CachedCollection; |
@ -1,12 +1,42 @@ |
||||
Meteor.publish 'settings', (ids = []) -> |
||||
return RocketChat.models.Settings.findNotHiddenPublic(ids) |
||||
Meteor.methods |
||||
getPublicSettings: -> |
||||
this.unblock() |
||||
|
||||
Meteor.publish 'admin-settings', -> |
||||
unless @userId |
||||
return @ready() |
||||
return RocketChat.models.Settings.findNotHiddenPublic().fetch() |
||||
|
||||
if RocketChat.authz.hasPermission( @userId, 'view-privileged-setting') |
||||
return RocketChat.models.Settings.findNotHidden() |
||||
else |
||||
return @ready() |
||||
getPrivateSettings: -> |
||||
unless Meteor.userId() |
||||
return [] |
||||
|
||||
this.unblock() |
||||
|
||||
if not RocketChat.authz.hasPermission Meteor.userId(), 'view-privileged-setting' |
||||
return [] |
||||
|
||||
return RocketChat.models.Settings.findNotHiddenPrivate().fetch() |
||||
|
||||
|
||||
subscriptionsReady = false |
||||
RocketChat.models.Settings.findNotHidden().observe |
||||
added: (record) -> |
||||
if subscriptionsReady |
||||
e = if record.public is true then 'public-settings-changed' else 'private-settings-changed' |
||||
RocketChat.Notifications.notifyAll e, 'added', record |
||||
|
||||
changed: (record) -> |
||||
if subscriptionsReady |
||||
e = if record.public is true then 'public-settings-changed' else 'private-settings-changed' |
||||
RocketChat.Notifications.notifyAll e, 'changed', record |
||||
|
||||
removed: (record) -> |
||||
if subscriptionsReady |
||||
e = if record.public is true then 'public-settings-changed' else 'private-settings-changed' |
||||
RocketChat.Notifications.notifyAll e, 'removed', { _id: record._id } |
||||
|
||||
subscriptionsReady = true |
||||
|
||||
|
||||
RocketChat.Notifications.streamAll.allowRead 'private-settings-changed', -> |
||||
if not @userId? then return false |
||||
|
||||
return RocketChat.authz.hasPermission @userId, 'view-privileged-setting' |
||||
|
@ -1,6 +1,6 @@ |
||||
/* globals FastRender */ |
||||
|
||||
FastRender.onAllRoutes(function(/*path*/) { |
||||
this.subscribe('settings'); |
||||
// this.subscribe('settings');
|
||||
this.subscribe('meteor.loginServiceConfiguration'); |
||||
}); |
||||
|
Loading…
Reference in new issue